#include "keygen.h"
#include "main.h"
-#include "mainwindow.h"
-#include "msinterface.h"
+#include "configdialog.h"
#include "office.h"
-#include "overview.h"
+#include "listview.h"
-#include <QApplication>
#include <QByteArray>
#include <QCheckBox>
#include <QComboBox>
#include <QCryptographicHash>
-#include <QDesktopServices>
#include <QFile>
#include <QFileDialog>
#include <QFontDatabase>
#include <QGridLayout>
#include <QHBoxLayout>
-#include <QHttp>
#include <QInputDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSettings>
#include <QSpinBox>
-#include <QStackedWidget>
+#include <QStandardItemModel>
-MMainWindow::MMainWindow()
+MConfigDialog::MConfigDialog()
{
- setWindowTitle("Magic Smoke");
+ setWindowTitle(tr("Magic Smoke Configuration"));
+
+ oldrow=-1;
+ //main layout
+ QHBoxLayout*hl;
+ QVBoxLayout*vl;
+ setLayout(hl=new QHBoxLayout);
//create Menu Bar
- QMenuBar*mb=menuBar();
- QMenu*m=mb->addMenu(tr("&File"));
+ QMenuBar*mb;
+ hl->setMenuBar(mb=new QMenuBar);
+ QMenu*m=mb->addMenu(tr("&Profile"));
m->addAction(tr("&New Profile..."),this,SLOT(newProfile()));
- m->addAction(tr("&Save Profile"),this,SLOT(saveProfile()));
- m->addSeparator();
- m->addAction(tr("&Close Window"),this,SLOT(close()));
- m=mb->addMenu(tr("&Configure"));
- m->addAction(tr("&Language..."),this,SLOT(changeLang()));
+ m->addAction(tr("&Delete Profile"),this,SLOT(deleteProfile()));
+ m->addAction(tr("&Rename Profile"),this,SLOT(renameProfile()));
+ m->addAction(tr("C&lone Profile"),this,SLOT(cloneProfile()));
+ m->addAction(tr("&Make Default Profile"),this,SLOT(defaultProfile()));
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()));
m->addSeparator();
+ m->addAction(tr("&Close Window"),this,SLOT(close()));
+ m=mb->addMenu(tr("&Settings"));
+ m->addAction(tr("&Language..."),this,SLOT(changeLang()));
m->addAction(tr("&OpenOffice.org Settings..."),this,SLOT(openOfficeCfg()));
m->addAction(tr("Set &Default Label Font..."),this,SLOT(setDefaultFont()));
mb->addMenu(MApplication::helpMenu());
//create central widget
- QWidget *loginwidget;
- setCentralWidget(loginwidget=new QWidget);
QGridLayout*gl;
- loginwidget->setLayout(gl=new QGridLayout);
+ hl->addWidget(profiles=new MListView);
+ profilemodel=new QStandardItemModel(this);
+ profiles->setModel(profilemodel);
+ profiles->setEditTriggers(QListView::NoEditTriggers);
+ connect(profiles,SIGNAL(clicked(const QModelIndex&)),this,SLOT(loadProfile()));
+ connect(profiles,SIGNAL(activated(const QModelIndex&)),this,SLOT(loadProfile()));
+ hl->addLayout(gl=new QGridLayout,10);
QLabel*lab;
int lctr=0;
- gl->addWidget(lab=new QLabel(tr("Profile:")),lctr,0);
+ gl->addWidget(lab=new QLabel(tr("Hostname:")),++lctr,0);
lab->setAlignment(Qt::AlignRight);
- gl->addWidget(profiles=new QComboBox,lctr,1);
- connect(profiles,SIGNAL(currentIndexChanged(int)),this,SLOT(loadProfile()));
- gl->addWidget(usealterhost=new QCheckBox(tr("Alternate Hostname:")),++lctr,0);
- gl->addWidget(alterhostname=new QLineEdit,lctr,1);
- connect(usealterhost,SIGNAL(toggled(bool)),alterhostname,SLOT(setEnabled(bool)));
+ gl->addWidget(hostname=new QLineEdit,lctr,1);
+ gl->addWidget(lab=new QLabel(tr("Hostkey:")),++lctr,0);
+ lab->setAlignment(Qt::AlignRight);
+ gl->addWidget(hostkey=new QLineEdit,lctr,1);
gl->addWidget(lab=new QLabel(tr("Server URL:")),++lctr,0);
lab->setAlignment(Qt::AlignRight);
- gl->addWidget(serverurl=new QLineEdit,lctr,1);
+ gl->addLayout(hl=new QHBoxLayout,lctr,1);
+ hl->addWidget(new QLabel("https://"),0);
+ hl->addWidget(serverurl=new QLineEdit,1);
gl->addWidget(useproxy=new QCheckBox(tr("Proxy:")),++lctr,0);
- QHBoxLayout*hl;
gl->addLayout(hl=new QHBoxLayout,lctr,1);
hl->addWidget(proxyname=new QLineEdit,1);
hl->addWidget(new QLabel(":"),0);
proxypass->setEchoMode(QLineEdit::Password);
connect(useproxy,SIGNAL(toggled(bool)),proxyuser,SLOT(setEnabled(bool)));
connect(useproxy,SIGNAL(toggled(bool)),proxypass,SLOT(setEnabled(bool)));
- QFrame*frm;
- gl->addWidget(frm=new QFrame,++lctr,0,1,2);
- frm->setFrameShape(QFrame::HLine);
- gl->addWidget(lab=new QLabel(tr("Username:")),++lctr,0);
+ gl->addWidget(lab=new QLabel(tr("Default Username:")),++lctr,0);
lab->setAlignment(Qt::AlignRight);
gl->addWidget(username=new QLineEdit,lctr,1);
- gl->addWidget(lab=new QLabel(tr("Password:")),++lctr,0);
- lab->setAlignment(Qt::AlignRight);
- gl->addWidget(password=new QLineEdit,lctr,1);
- password->setEchoMode(QLineEdit::Password);
- connect(password,SIGNAL(returnPressed()),this,SLOT(startLogin()));
- connect(username,SIGNAL(returnPressed()),password,SLOT(setFocus()));
gl->setRowStretch(++lctr,10);
gl->addLayout(hl=new QHBoxLayout,++lctr,0,1,2);
- QPushButton*p;
- hl->addWidget(p=new QPushButton(tr("new Profile")),0);
- connect(p,SIGNAL(clicked()),this,SLOT(newProfile()));
- hl->addWidget(p=new QPushButton(tr("save Profile")),0);
- connect(p,SIGNAL(clicked()),this,SLOT(saveProfile()));
- hl->addStretch(10);
- hl->addWidget(p=new QPushButton(tr("Login")),0);
- connect(p,SIGNAL(clicked()),this,SLOT(saveProfile()));
- connect(p,SIGNAL(clicked()),this,SLOT(startLogin()));
+
initProfiles();
loadProfile();
}
-void MMainWindow::initProfiles()
+MConfigDialog::~MConfigDialog()
+{
+ saveProfile();
+}
+
+void MConfigDialog::initProfiles(QString selIdx)
{
+ oldrow=-1;
QSettings set;
+ int defpro=set.value("defaultprofile",0).toInt();
set.beginGroup("profiles");
QStringList prf=set.childGroups();
- profiles->clear();
+ profilemodel->clear();
+ if(profilemodel->columnCount()<1)
+ profilemodel->insertColumn(0);
+ profilemodel->insertRows(0,prf.size());
+ int newrow=0;
for(int i=0;i<prf.size();i++){
- profiles->addItem(set.value(prf[i]+"/name").toString(),prf[i]);
+ QModelIndex idx=profilemodel->index(i,0);
+ profilemodel->setData(idx,set.value(prf[i]+"/name").toString());
+ profilemodel->setData(idx,prf[i],Qt::UserRole);
+ if(i==defpro){
+ QFont f=profiles->font();
+ f.setBold(true);
+ profilemodel->setData(idx,f,Qt::FontRole);
+ }
+ if(prf[i]==selIdx)newrow=i;
}
+ profiles->setCurrentIndex(profilemodel->index(newrow,0));
}
-void MMainWindow::loadProfile()
+void MConfigDialog::loadProfile()
{
- QString key=profiles->itemData(profiles->currentIndex()).toString();
+ //save old
+ saveProfile();
+ //get new
+ QModelIndex idx=profiles->currentIndex();
+ if(!idx.isValid())return;
+ oldrow=idx.row();
+ QString key=profilemodel->data(idx,Qt::UserRole).toString();
QSettings set;
set.beginGroup("profiles/"+key);
- usealterhost->setChecked(set.value("usealternatehost",false).toBool());
- alterhostname->setText(set.value("alternatehostname").toString());
- alterhostname->setEnabled(usealterhost->isChecked());
- serverurl->setText(set.value("serverurl","http://my.host.com/path/machine.php").toString());
+ hostname->setText(set.value("hostname").toString());
+ hostkey->setText(set.value("hostkey").toString());
+ serverurl->setText(set.value("serverurl","my.host.com/path/machine.php").toString());
useproxy->setChecked(set.value("useproxy",false).toBool());
proxyname->setText(set.value("proxyname","proxy").toString());
proxyport->setValue(set.value("proxyport",74).toInt());
proxyuser->setEnabled(useproxy->isChecked());
proxypass->setEnabled(useproxy->isChecked());
username->setText(set.value("username").toString());
- password->setText("");
- password->setFocus();
}
-void MMainWindow::saveProfile()
+void MConfigDialog::saveProfile()
{
- QString key=profiles->itemData(profiles->currentIndex()).toString();
+ if(oldrow<0 || oldrow>=profilemodel->rowCount())return;
+ QString key=profilemodel->data(profilemodel->index(oldrow,0),Qt::UserRole).toString();
QSettings set;
set.beginGroup("profiles/"+key);
- set.setValue("usealternatehost",usealterhost->isChecked());
- set.setValue("alternatehostname",alterhostname->text());
+ set.setValue("hostname",hostname->text());
+ set.setValue("hostkey",hostkey->text());
set.setValue("serverurl",serverurl->text());
set.setValue("useproxy",useproxy->isChecked());
set.setValue("proxyname",proxyname->text());
set.setValue("username",username->text());
}
-void MMainWindow::newProfile()
+void MConfigDialog::newProfile()
{
+ saveProfile();
//scan for existing ones...
QSettings set;
set.beginGroup("profiles");
}
//create
set.setValue(pidx+"/name",pname);
- profiles->addItem(pname,pidx);
- profiles->setCurrentIndex(profiles->count()-1);
+ set.sync();
+ initProfiles(pidx);
+ loadProfile();
}
-void MMainWindow::startLogin()
+void MConfigDialog::deleteProfile()
{
- //make it impossible for the user to interfere
- setEnabled(false);
- //create request object
- MSInterface *mw=new MSInterface(profiles->itemData(profiles->currentIndex()).toString());
- mw->setUrl(serverurl->text());
- if(useproxy->isChecked())
- mw->setProxy(proxyname->text(),proxyport->value(),proxyuser->text(),proxypass->text());
- //check server version
- if(!mw->checkServer())return;
- //start login request
- QString hn;
- if(usealterhost->isChecked())hn=alterhostname->text();
- else hn=QSettings().value("hostname").toString();
- QString hk=QSettings().value("hostkey").toString();
- if(!mw->login(username->text(),password->text(),hn,hk)){
- QMessageBox::warning(this,tr("Warning"),tr("Unable to log in."));
- mw->deleteLater();
- setEnabled(true);
- return;
+ QModelIndex idx=profiles->currentIndex();
+ if(!idx.isValid())return;
+ QString key=profilemodel->data(idx,Qt::UserRole).toString();
+ QSettings set;
+ set.beginGroup("profiles");
+ set.remove(key);
+ set.sync();
+ oldrow=-1;
+ initProfiles();
+ loadProfile();
+}
+
+void MConfigDialog::renameProfile()
+{
+ //get old name
+ QModelIndex idx=profiles->currentIndex();
+ if(!idx.isValid())return;
+ QString key=profilemodel->data(idx,Qt::UserRole).toString();
+ QString pname=profilemodel->data(idx).toString();
+ //query
+ bool ok;
+ QString npname=QInputDialog::getText(this,tr("Rename Profile"),tr("Please enter a profile name. It must be non-empty and must not be used yet:"),QLineEdit::Normal,pname,&ok);
+ if(!ok)return;
+ //check
+ if(npname==pname)return;
+ for(int i=0;i<profilemodel->rowCount();i++){
+ QString pn=profilemodel->data(profilemodel->index(i,0)).toString();
+ if(pn==npname){
+ QMessageBox::warning(this,tr("Warning"),tr("This profile name is already in use."));
+ return;
+ }
}
- //initialize
- mw->initialize();
- //open window
- MOverview *mo=new MOverview(profiles->itemData(profiles->currentIndex()).toString());
- mo->show();
-
- //make sure the application exits (only) after everything is cleaned up
- qApp->setQuitOnLastWindowClosed(false);
- connect(mw,SIGNAL(destroyed(QObject*)),qApp,SLOT(quit()));
-
- //reset main window
-// password->setText("");
-// setEnabled(true);
-// deleteLater();
- hide();
+ //set
+ QSettings().setValue("profiles/"+key+"/name",npname);
+ profilemodel->setData(idx,npname);
+}
+
+void MConfigDialog::cloneProfile()
+{
+ saveProfile();
+ //get current
+ QModelIndex idx=profiles->currentIndex();
+ if(!idx.isValid())return;
+ QString key=profilemodel->data(idx,Qt::UserRole).toString();
+ //scan for existing ones...
+ QSettings set;
+ set.beginGroup("profiles");
+ QStringList prf=set.childGroups();
+ QStringList prn;
+ for(int i=0;i<prf.size();i++){
+ prn<<set.value(prf[i]+"/name").toString();
+ }
+ //create new index
+ QString pidx;
+ for(int i=0;;i++){
+ pidx=QString::number(i);
+ if(!prf.contains(pidx))break;
+ }
+ //request name
+ QString pname;
+ while(1){
+ bool ok;
+ pname=QInputDialog::getText(this,tr("New Profile"),tr("Please enter a profile name. It must be non-empty and must not be used yet:"),QLineEdit::Normal,pname,&ok);
+ if(!ok)return;
+ if(pname.isEmpty())continue;
+ if(!prn.contains(pname))break;
+ }
+ //clone
+ set.beginGroup(key);
+ QStringList keys=set.allKeys();
+ set.endGroup();
+ for(int i=0;i<keys.size();i++)
+ set.setValue(pidx+"/"+keys[i],set.value(key+"/"+keys[i]));
+ //create
+ set.setValue(pidx+"/name",pname);
+ set.sync();
+ initProfiles(pidx);
+ loadProfile();
+}
+void MConfigDialog::defaultProfile()
+{
+ saveProfile();
+ QModelIndex idx=profiles->currentIndex();
+ if(!idx.isValid())return;
+ QString key=profilemodel->data(idx,Qt::UserRole).toString();
+ QSettings().setValue("defaultprofile",key);
+ initProfiles(key);
+ loadProfile();
}
-void MMainWindow::changeLang()
+
+void MConfigDialog::changeLang()
{
choseLanguage();
}
-void MMainWindow::exportKey()
+void MConfigDialog::exportKey()
{
QSettings st;
+ QModelIndex idx=profiles->currentIndex();
+ if(!idx.isValid())return;
+ QString pkey=profilemodel->data(idx,Qt::UserRole).toString();
+ st.beginGroup("profiles/"+pkey);
QString host=st.value("hostname").toString().trimmed();
QString key=st.value("hostkey").toString().trimmed();
saveKey(host,key);
}
-void MMainWindow::generateKey()
+void MConfigDialog::generateKey()
{
QString name;
- do{
- bool ok;
- name=QInputDialog::getText(this,tr("New Host Name"),tr("Please enter a name for the new host:"),QLineEdit::Normal,name,&ok);
- if(!ok)return;
- if(!QRegExp("[A-Za-z][A-Za-z0-9_]*").exactMatch(name)){
- QMessageBox::warning(this,tr("Warning"),tr("The host name must only consist of letters, digits and underscore. It must start with a letter."));
- continue;
- }
- }while(false);
- MKeyGen mkg;
- if(mkg.exec()==QDialog::Accepted)
- saveKey(name,mkg.getKey());
+ MKeyGen mkg(this);
+ QString k=mkg.getKey();
+ if(k=="")
+ if(mkg.exec()!=QDialog::Accepted)
+ return;
+ hostkey->setText(mkg.getKey());
}
-void MMainWindow::saveKey(QString host,QString key)
+void MConfigDialog::saveKey(QString host,QString key)
{
QStringList fn;
QFileDialog fdlg(this,tr("Export Key to File"),QString(),"Magic Smoke Host Key (*.mshk)");
fd.close();
}
-void MMainWindow::importKey()
+void MConfigDialog::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)
+ QModelIndex idx=profiles->currentIndex();
+ if(!idx.isValid())return;
+ QString profkey=profilemodel->data(idx,Qt::UserRole).toString();
+
+ if(QMessageBox::warning(this,tr("Warning"),tr("Importing a key overwrites the host key that is currently used by this profile. 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)");
return;
}
//save
- QSettings().setValue("hostkey",key);
- QSettings().setValue("hostname",hname);
+ QSettings set;
+ set.beginGroup("profiles/"+profkey);
+ set.setValue("hostkey",key);
+ set.setValue("hostname",hname);
}
-void MMainWindow::openOfficeCfg()
+void MConfigDialog::openOfficeCfg()
{
MOfficeConfig c(this);
c.exec();
}
-void MMainWindow::setDefaultFont()
+void MConfigDialog::setDefaultFont()
{
QStringList fonts=QFontDatabase().families();
QString df=QInputDialog::getItem(this,tr("Chose Default Font"),tr("Please chose a default font:"),fonts,fonts.indexOf(QSettings().value("defaultfont","").toString()),false);
--- /dev/null
+//
+// C++ Implementation: mainwindow
+//
+// Description:
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2007
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#include "main.h"
+#include "login.h"
+#include "msinterface.h"
+#include "overview.h"
+#include "configdialog.h"
+
+#include <QApplication>
+#include <QByteArray>
+#include <QComboBox>
+#include <QGridLayout>
+#include <QHBoxLayout>
+#include <QLabel>
+#include <QLineEdit>
+#include <QMenu>
+#include <QMenuBar>
+#include <QMessageBox>
+#include <QPushButton>
+#include <QSettings>
+
+MLogin::MLogin()
+{
+ setWindowTitle(tr("Magic Smoke Login"));
+ QVBoxLayout*vl;
+ setLayout(vl=new QVBoxLayout);
+ //create Menu Bar
+ QMenuBar*mb;
+ vl->setMenuBar(mb=new QMenuBar);
+ QMenu*m=mb->addMenu(tr("&File"));
+ m->addAction(tr("&Exit"),this,SLOT(close()));
+ m=mb->addMenu(tr("&Configure"));
+ m->addAction(tr("&Configuration..."),this,SLOT(configwin()));
+ mb->addMenu(MApplication::helpMenu());
+
+ //create central widget
+ QGridLayout*gl;
+ vl->addLayout(gl=new QGridLayout,10);
+ QLabel*lab;
+ int lctr=0;
+ gl->addWidget(lab=new QLabel(tr("Profile:")),lctr,0);
+ lab->setAlignment(Qt::AlignRight);
+ gl->addWidget(profiles=new QComboBox,lctr,1);
+ connect(profiles,SIGNAL(currentIndexChanged(int)),this,SLOT(loadProfile()));
+ gl->addWidget(lab=new QLabel(tr("Username:")),++lctr,0);
+ lab->setAlignment(Qt::AlignRight);
+ gl->addWidget(username=new QLineEdit,lctr,1);
+ gl->addWidget(lab=new QLabel(tr("Password:")),++lctr,0);
+ lab->setAlignment(Qt::AlignRight);
+ gl->addWidget(password=new QLineEdit,lctr,1);
+ password->setEchoMode(QLineEdit::Password);
+ connect(password,SIGNAL(returnPressed()),this,SLOT(startLogin()));
+ connect(username,SIGNAL(returnPressed()),password,SLOT(setFocus()));
+ gl->setRowStretch(++lctr,10);
+ QHBoxLayout *hl=new QHBoxLayout;
+ gl->addLayout(hl,++lctr,0,1,2);
+ QPushButton*p;
+ hl->addStretch(10);
+ hl->addWidget(p=new QPushButton(tr("Login")),0);
+ connect(p,SIGNAL(clicked()),this,SLOT(startLogin()));
+ initProfiles();
+ loadProfile();
+}
+
+void MLogin::initProfiles()
+{
+ QSettings set;
+ set.beginGroup("profiles");
+ QStringList prf=set.childGroups();
+ profiles->clear();
+ for(int i=0;i<prf.size();i++){
+ profiles->addItem(set.value(prf[i]+"/name").toString(),prf[i]);
+ }
+}
+
+void MLogin::loadProfile()
+{
+ QString key=profiles->itemData(profiles->currentIndex()).toString();
+ QSettings set;
+ set.beginGroup("profiles/"+key);
+ username->setText(set.value("username").toString());
+ password->setText("");
+ password->setFocus();
+}
+
+void MLogin::startLogin()
+{
+ //make it impossible for the user to interfere
+ setEnabled(false);
+ //create request object
+ MSInterface *mw=new MSInterface(profiles->itemData(profiles->currentIndex()).toString());
+ //check server version
+ if(!mw->checkServer()){
+ //no need for messagebox: checkServer displays one if necessary
+ mw->deleteLater();
+ return;
+ }
+ //start login request
+ if(!mw->login(username->text(),password->text())){
+ QMessageBox::warning(this,tr("Warning"),tr("Unable to log in."));
+ mw->deleteLater();
+ setEnabled(true);
+ return;
+ }
+ //initialize
+ mw->initialize();
+ //open window
+ MOverview *mo=new MOverview(profiles->itemData(profiles->currentIndex()).toString());
+ mo->show();
+
+ //make sure the application exits (only) after everything is cleaned up
+ qApp->setQuitOnLastWindowClosed(false);
+ connect(mw,SIGNAL(destroyed(QObject*)),qApp,SLOT(quit()));
+
+ hide();
+}
+
+void MLogin::configwin()
+{
+ MConfigDialog cd;
+ cd.exec();
+ initProfiles();
+ loadProfile();
+}