From 5dd8320fca7e7f8937153ff6ff6a80d3713c6f85 Mon Sep 17 00:00:00 2001 From: Konrad Rosenbaum Date: Sun, 10 Jul 2016 21:43:47 +0200 Subject: [PATCH] better logging, better handling of datadir arguments; first draft of client selection --- commonlib/mapplication.cpp | 22 ++++++--- commonlib/mapplication.h | 7 ++- commonlib/misc/debug.cpp | 11 +++- sessman/login.cpp | 28 ++++++++++- sessman/login.h | 10 +++- sessman/sman.cpp | 113 +++++++++++++++++++++++++++++++++++-------- sessman/sman.h | 13 +++++- src/main.cpp | 5 ++ 8 files changed, 170 insertions(+), 39 deletions(-) diff --git a/commonlib/mapplication.cpp b/commonlib/mapplication.cpp index e82efc5..7a47193 100644 --- a/commonlib/mapplication.cpp +++ b/commonlib/mapplication.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -38,7 +39,6 @@ #include "hmac.h" #include "keygen.h" #include "mapplication.h" -// #include "login.h" #include "msinterface.h" #include #include "misc.h" @@ -277,6 +277,9 @@ void MApplication::help(int idx) QDesktopServices::openUrl(u); } +#define ENV_DATADIR "MAGICSMOKE_DATADIR" +#define ENV_CONFIGDIR "MAGICSMOKE_CONFIGDIR" + MApplication::MApplication(int&ac,char**av) :QApplication(ac,av) { @@ -296,6 +299,12 @@ MApplication::MApplication(int&ac,char**av) return new MProgressWrapperGui(label,button); }); + //check environment + if(qEnvironmentVariableIsSet(ENV_DATADIR)) + setDataDir(QString::fromLocal8Bit(qgetenv(ENV_DATADIR))); + if(qEnvironmentVariableIsSet(ENV_CONFIGDIR)) + setConfigDir(QString::fromLocal8Bit(qgetenv(ENV_CONFIGDIR))); + //check parameters // qDebug()<<"arguments"<initialize(); } void MApplication::initUpdater() diff --git a/commonlib/mapplication.h b/commonlib/mapplication.h index eb17647..81ee0f9 100644 --- a/commonlib/mapplication.h +++ b/commonlib/mapplication.h @@ -63,6 +63,10 @@ class MAGICSMOKE_COMMON_EXPORT MApplication:public QApplication void versionDlg(); ///start a browser with the help page void help(); + + ///initialize the updater + void initUpdater(); + private slots: ///jump to a specific help page void help(int); @@ -70,9 +74,6 @@ class MAGICSMOKE_COMMON_EXPORT MApplication:public QApplication ///initialize the help menu from config files void initHelpUrl(); - ///initialize the updater - void initUpdater(); - ///updater is ready for download void updateReadyDownload(const QString&); ///updater is ready for install diff --git a/commonlib/misc/debug.cpp b/commonlib/misc/debug.cpp index 986074f..854ee3c 100644 --- a/commonlib/misc/debug.cpp +++ b/commonlib/misc/debug.cpp @@ -21,11 +21,14 @@ static QFile*mylogFile=0; + static void mymsghandler(QtMsgType,const QMessageLogContext&, const QString&msg) { if(mylogFile){ - mylogFile->write(msg.toLatin1()); - mylogFile->write("\n",1); + QByteArray data= + QDateTime::currentDateTime().toString("yyyy-MM-dd hh.mm.ss.zzz").toLatin1() +" "+ + msg.toLatin1()+"\n"; + mylogFile->write(data); mylogFile->flush(); } #ifdef Q_OS_UNIX @@ -39,8 +42,9 @@ void initDebug(const QString&dataDir) qDebug()<<"-nolog argument was given, will not create a log file"; } //create new log file + QFileInfo appfi(qApp->applicationFilePath()); QDir(dataDir).mkpath("debuglog"); - mylogFile=new QFile(dataDir+"/debuglog/log-"+QDateTime::currentDateTime().toString("yyyy-MM-dd_hh.mm.ss.zzz")+".txt"); + mylogFile=new QFile(dataDir+"/debuglog/"+appfi.baseName()+"-"+QDateTime::currentDateTime().toString("yyyy-MM-dd_hh.mm.ss.zzz")+".log"); //...open it if(mylogFile->open(QIODevice::WriteOnly|QIODevice::Append|QIODevice::Text)){ //install as default log @@ -52,6 +56,7 @@ void initDebug(const QString&dataDir) mylogFile=0; qDebug("Failed to install debuglog."); } + qDebug()<<"Log file opened by"< #include @@ -30,7 +31,7 @@ #include #include -MLogin::MLogin() +MLogin::MLogin(MSessionManager*sm) { setWindowTitle(tr("Magic Smoke Login")); setWindowFlags(windowFlags()|Qt::Window|Qt::Dialog); @@ -54,6 +55,9 @@ MLogin::MLogin() 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("Client:")),++lctr,0); + lab->setAlignment(Qt::AlignRight); + gl->addWidget(clients=new QComboBox,lctr,1); gl->addWidget(lab=new QLabel(tr("Username:")),++lctr,0); lab->setAlignment(Qt::AlignRight); gl->addWidget(username=new QLineEdit,lctr,1); @@ -73,6 +77,7 @@ MLogin::MLogin() resizer=new QSizeGrip(this); connect(p,SIGNAL(clicked()),this,SLOT(startLogin())); initProfiles(); + initClients(sm); loadProfile(); //react to Escape button @@ -107,6 +112,27 @@ void MLogin::loadProfile() password->setFocus(); } +#define LASTCLIENTSETTING "SessionManager/LastClient" +void MLogin::initClients(MSessionManager*sm) +{ + QString last=QSettings().value(LASTCLIENTSETTING).toString().trimmed(); + int pos=-1; + for(auto entry:sm->menuItems()){ + if(!entry.first.startsWith('@'))continue; + if(last==entry.first)pos=clients->count(); + clients->addItem(entry.second.replace("&",""),entry.first); + } + if(pos>=0) + clients->setCurrentIndex(pos); + connect(clients,static_cast(&QComboBox::currentIndexChanged), + this, [this]{QSettings().setValue(LASTCLIENTSETTING,clients->currentData());} ); +} + +QString MLogin::getCurrentClient() const +{ + return clients->currentData().toString(); +} + void MLogin::startLogin() { qDebug()<<"Logging in..."; diff --git a/sessman/login.h b/sessman/login.h index 2bb39c4..9aa6b8b 100644 --- a/sessman/login.h +++ b/sessman/login.h @@ -19,17 +19,20 @@ class QComboBox; class QLineEdit; class QSizeGrip; +class MSessionManager; + /**login and profile configuration window*/ class MLogin:public QWidget { Q_OBJECT public: - MLogin(); + explicit MLogin(MSessionManager*); + QString getCurrentClient()const; private: QString sessionid; QLineEdit*username,*password; - QComboBox*profiles; + QComboBox*profiles,*clients; QSizeGrip*resizer; protected: @@ -39,9 +42,10 @@ class MLogin:public QWidget void initProfiles(); void loadProfile(); void startLogin(); - void configwin(); + void initClients(MSessionManager*); public slots: + void configwin(); void relogin(); signals: diff --git a/sessman/sman.cpp b/sessman/sman.cpp index 83e37c9..18a6829 100644 --- a/sessman/sman.cpp +++ b/sessman/sman.cpp @@ -9,13 +9,17 @@ // +#include #include #include #include #include +#include #include -#include #include +#include +#include +#include #include "sman.h" #include "login.h" @@ -24,8 +28,11 @@ #include "../sesscli/scrand.h" #include +#include #include +static QPointer loginwindow; + MSessionManager::MSessionManager ( QObject* parent ) : QObject ( parent ) { // create socket @@ -66,6 +73,15 @@ MSessionManager::MSessionManager ( QObject* parent ) : QObject ( parent ) //create systray icon micon=new QSystemTrayIcon(QIcon(":/icon.png"),this); micon->setToolTip(tr("MagicSmoke Session Manager, waiting for login...")); + QMenu*m=new QMenu; + QSignalMapper*sm=new QSignalMapper(m); + for(auto entry:menuItems()){ + QAction*a=m->addAction(entry.second); + sm->setMapping(a,entry.first); + connect(a,SIGNAL(triggered()),sm,SLOT(map())); + } + connect(sm,SIGNAL(mapped(QString)),this,SLOT(execCmd(QString))); + micon->setContextMenu(m); micon->show(); } @@ -179,7 +195,7 @@ void MSessionManager::readyRead() }else if(cmd=="getmenu") sendMenu(s); else if(cmd=="exec") - execCmd(par); + execCmd(par.trimmed()); else qDebug()<<"Warning: unknown client command"<write("newmenu\n"); - //TODO: send "menu command entry-text" for each entry + //send "menu command entry-text" for each entry + for(auto entry:menuItems()) + s->write(QString("menu %1 %2\n").arg(entry.first).arg(entry.second).toUtf8()); s->write("endmenu\n"); } -void MSessionManager::execCmd ( QString cmd) +void MSessionManager::execCmd (const QString &cmd) { - qDebug()<<"I wish I knew how to execute"<write("quit\n"); + c->flush(); + } + QTimer::singleShot(1000,qApp,SLOT(quit())); + }else if(cmd.startsWith("@")){ + QString bin=(cmd=="@@main")?QString():"-"+cmd.mid(1); + startBinary(bin); + }else + qDebug()<<"I wish I knew how to execute"<setProcessChannelMode(QProcess::ForwardedChannels); - p->start(qApp->applicationDirPath()+"/magicsmoke",QStringList()<<"-sms:"+mkey); + connect(p,SIGNAL(finished(int,QProcess::ExitStatus)), this,SLOT(processLost(int,QProcess::ExitStatus))); + QString binname=qApp->applicationDirPath()+"/magicsmoke"+bin; + p->start(binname,QStringList()<<"-sms:"+mkey); + qDebug()<<"starting MagicSmoke client binary"<waitForStarted(5000)){ + qDebug()<<"Error: binary does not start!"<errorString(); + delete p; + return; + } + qDebug()<<"Started"<processId(); +} + +void MSessionManager::processLost ( int exitCode, QProcess::ExitStatus exitStatus) +{ + QProcess *p=qobject_cast(sender()); + qDebug()<<"Process exited with status"<program(); + qDebug()<<" ...Last Error reads"<errorString(); + p->deleteLater(); + } } + void MSessionManager::loginSucceeded() { micon->setToolTip(tr("MagicSmoke Session Manager, logged in as %1 at %2.").arg(username()).arg(profileName())); if(mconnections.count()>0) sendNewSessionId(); else - execCmd("msmoke"); + execCmd(loginwindow->getCurrentClient()); } void MSessionManager::sendNewSessionId() @@ -256,27 +317,37 @@ void MSessionManager::loginLost() s->write("closed\n"); } - -int main(int ac,char**av) +QList> MSessionManager::menuItems() const { - QApplication app(ac,av); - //locate settings - app.setOrganizationName("MagicSmoke"); - app.setApplicationName("MagicSmoke2"); - - //set icon - app.setWindowIcon(QIcon(":/icon.png")); + static QList> items; + if(items.isEmpty()){ + auto _ = [&](QString c,QString t){items.append(QPair(c,t));}; + _("config",tr("&Configuration...")); + _("@@main",tr("Start &Expert Client")); + _("@wizard",tr("Start &Wizard")); + _("@statistic",tr("Start &Statistics Client")); + _("@print",tr("Start &Print@Home Daemon")); + _("quit",tr("&Quit Session")); + } + return items; +} - MSInterface::setAppDataDir("$BASE/.magicSmoke2"); - WTransaction::setLogPrefix("SM-T"); - MBoxWrapper::setWarning([](QString title,QString text){QMessageBox::warning(nullptr,title,text);}); +int main(int ac,char**av) +{ + MApplication app(ac,av); + WTransaction::setLogPrefix("SM-T"); + app.initialize(); + //init updater + app.initUpdater(); MSessionManager *sm=new MSessionManager(&app); - MLogin lw; + MLogin lw(sm); + loginwindow=&lw; lw.connect(&lw,SIGNAL(loginSucceeded()),sm,SLOT(loginSucceeded())); lw.connect(&lw,SIGNAL(lostSession()),sm,SLOT(loginLost())); + lw.connect(sm,SIGNAL(openConfig()),&lw,SLOT(configwin())); lw.show(); return app.exec(); diff --git a/sessman/sman.h b/sessman/sman.h index a0cafab..3837c54 100644 --- a/sessman/sman.h +++ b/sessman/sman.h @@ -16,6 +16,8 @@ #include #include +#include +#include class QLocalServer; class QLocalSocket; @@ -35,15 +37,24 @@ public: virtual QString profile()const; virtual QString profileName()const; + virtual QList> menuItems()const; + private slots: void newConnection(); void readyRead(); void socketClosed(); void socketLost(QObject*); + void execCmd(const QString&); + void loginSucceeded(); void sendNewSessionId(); void loginLost(); + void processLost(int,QProcess::ExitStatus); + +signals: + void openConfig(); + private: QLocalServer*mserver=nullptr; QListmconnections; @@ -51,8 +62,8 @@ private: QSystemTrayIcon*micon; void sendMenu(QLocalSocket*); - void execCmd(QString); void sendSessionInfo(QLocalSocket*); + void startBinary(QString); }; diff --git a/src/main.cpp b/src/main.cpp index f99f2da..082ec33 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include "main.h" #include +#include "barcode-plugin.h" int MagicSmokeMain::realmain(int argc,char**argv) @@ -28,6 +29,10 @@ int MagicSmokeMain::realmain(int argc,char**argv) //init app.initialize(); + //init plugins + MBarcodeHub::instance()->initialize(); + + //get session MSessionClient sc; sc.connect(&sc,SIGNAL(sessionLost()),&app,SLOT(quit())); sc.connect(&sc,SIGNAL(managerLost()),&app,SLOT(quit())); -- 1.7.2.5