#include <QSettings>
#include <QInputDialog>
#include <QMessageBox>
+#include <QFileDialog>
+#include <QCryptographicHash>
MMainWindow::MMainWindow()
{
m->addAction(tr("&Close Window"),this,SLOT(close()));
m=mb->addMenu(tr("&Configure"));
m->addAction(tr("&Language..."),this,SLOT(changeLang()));
+ m->addSeparator();
+ m->addAction(tr("&Export Host Key..."),this,SLOT(exportKey()));
+ m->addAction(tr("&Import Host Key..."),this,SLOT(importKey()));
+ m->addAction(tr("&Generate Host Key..."),this,SLOT(generateKey()));
//create central widget
QWidget *loginwidget;
{
choseLanguage();
}
+
+void MMainWindow::exportKey()
+{
+ QString key=QSettings().value("hostkey").toString().trimmed();
+ saveKey(key);
+}
+
+void MMainWindow::generateKey()
+{
+ MKeyGen mkg;
+ if(mkg.exec()==QDialog::Accepted)
+ saveKey(mkg.getKey());
+}
+
+void MMainWindow::saveKey(QString key)
+{
+ QStringList fn;
+ QFileDialog fdlg(this,tr("Export Key to File"),QString(),"Magic Smoke Host Key (*.mshk)");
+ fdlg.setDefaultSuffix("mshk");
+ fdlg.setAcceptMode(QFileDialog::AcceptSave);
+ fdlg.setFileMode(QFileDialog::AnyFile);
+ if(!fdlg.exec())return;
+ fn=fdlg.selectedFiles();
+ if(fn.size()!=1)return;
+ QFile fd(fn[0]);
+ if(!fd.open(QIODevice::WriteOnly|QIODevice::Truncate)){
+ QMessageBox::warning(this,tr("Warning"),tr("Unable to open file %1 for writing: %2").arg(fn[0]).arg(fd.errorString()));
+ return;
+ }
+ QString chk=QCryptographicHash::hash(key.toAscii(),QCryptographicHash::Md5).toHex();
+ QString out="MagicSmokeHostKey\n"+key+"\n"+chk;
+ fd.write(out.toAscii());
+ fd.close();
+}
+
+void MMainWindow::importKey()
+{
+ if(QMessageBox::warning(this,tr("Warning"),tr("Importing a key overwrites the host key that is currently used by this program. This may disable your accounts. Do you still want to continue?"),QMessageBox::Yes|QMessageBox::Abort,QMessageBox::Abort)!=QMessageBox::Yes)
+ return;
+ QStringList fn;
+ QFileDialog fdlg(this,tr("Import Key from File"),QString(),"Magic Smoke Host Key (*.mshk)");
+ fdlg.setDefaultSuffix("mshk");
+ fdlg.setAcceptMode(QFileDialog::AcceptOpen);
+ fdlg.setFileMode(QFileDialog::ExistingFile);
+ if(!fdlg.exec())return;
+ fn=fdlg.selectedFiles();
+ if(fn.size()!=1)return;
+ QFile fd(fn[0]);
+ if(!fd.open(QIODevice::ReadOnly)){
+ QMessageBox::warning(this,tr("Warning"),tr("Unable to open file %1 for reading: %2").arg(fn[0]).arg(fd.errorString()));
+ return;
+ }
+ //read content (max: 10k to limit potential damage)
+ QStringList fc=QString::fromAscii(fd.read(10240)).split("\n",QString::SkipEmptyParts);
+ fd.close();
+ //check content
+ if(fc.size()<3){
+ QMessageBox::warning(this,tr("Warning"),tr("This is not a host key file."));
+ return;
+ }
+ if(fc[0].trimmed()!="MagicSmokeHostKey"){
+ QMessageBox::warning(this,tr("Warning"),tr("This is not a host key file."));
+ return;
+ }
+ QString key=fc[1].trimmed();
+ if(!QRegExp("[0-9a-fA-F]+").exactMatch(key) || key.size()<40){
+ QMessageBox::warning(this,tr("Warning"),tr("This host key file does not contain a valid key."));
+ return;
+ }
+ QString chk=QCryptographicHash::hash(key.toAscii(),QCryptographicHash::Md5).toHex();
+ if(chk!=fc[2].trimmed()){
+ QMessageBox::warning(this,tr("Warning"),tr("The key check sum did not match. Please get a clean copy of the host key file."));
+ return;
+ }
+ //save
+ QSettings().setValue("hostkey",key);
+}