From 965183188ee726036685fcba379c2559bd183247 Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Thu, 25 Dec 2008 00:39:35 +0100 Subject: [PATCH] Use KCmdLineArgs, KApplication and KMainWindow This makes the main classes use KDE if available. Since KApplication requires all cli options to be defined before instantiation, we have to move CliParser init into main(). CliParser fully abstracts KCmdLineArgs away, such that we won't need to change anything for accessing options elsewhere. --- src/common/cliparser.cpp | 40 +++++++++++++++++++++-- src/common/cliparser.h | 17 ++++++++-- src/common/main.cpp | 61 ++++++++++++++++++++++++++++++++++-- src/common/quassel.cpp | 8 ++--- src/common/quassel.h | 5 ++- src/core/coreapplication.cpp | 17 +++++----- src/core/coreapplication.h | 13 ++++++-- src/qtui/mainwin.cpp | 11 ++++++- src/qtui/mainwin.h | 14 +++++++-- src/qtui/qtuiapplication.cpp | 9 +++--- src/qtui/qtuiapplication.h | 12 ++++++- 11 files changed, 169 insertions(+), 38 deletions(-) diff --git a/src/common/cliparser.cpp b/src/common/cliparser.cpp index 56fa22a6..2116577d 100644 --- a/src/common/cliparser.cpp +++ b/src/common/cliparser.cpp @@ -20,10 +20,44 @@ #include "cliparser.h" #include +#include #include #include -#include +#ifdef HAVE_KDE +# include +#endif + +CliParser::CliParser() { + +} + +#ifdef HAVE_KDE +void CliParser::addArgument(const QString &longName, const CliParserArg &arg) { + if(arg.shortName != 0) { + _cmdLineOptions.add(QByteArray().append(arg.shortName)); + } + _cmdLineOptions.add(longName.toUtf8(), ki18n(arg.help.toUtf8()), arg.def.toUtf8()); +} + +bool CliParser::init(const QStringList &) { + KCmdLineArgs::addCmdLineOptions(_cmdLineOptions); + return true; +} + +QString CliParser::value(const QString &longName) { + return KCmdLineArgs::parsedArgs()->getOption(longName.toUtf8()); +} + +bool CliParser::isSet(const QString &longName) { + return KCmdLineArgs::parsedArgs()->isSet(longName.toUtf8()); +} + +void CliParser::usage() { + KCmdLineArgs::usage(); +} + +#else void CliParser::addArgument(const QString &longName, const CliParserArg &arg) { if(argsHash.contains(longName)) qWarning() << "Warning: Multiple definition of argument" << longName; if(arg.shortName != 0 && !lnameOfShortArg(arg.shortName).isNull()) @@ -77,7 +111,7 @@ QString CliParser::escapedValue(const QString &value) { return escapedValue; } -bool CliParser::parse(const QStringList &args) { +bool CliParser::init(const QStringList &args) { argsRaw = args; QStringList::const_iterator currentArg; for (currentArg = argsRaw.constBegin(); currentArg != argsRaw.constEnd(); ++currentArg) { @@ -213,3 +247,5 @@ QString CliParser::lnameOfShortArg(const char arg) { } return QString(); } + +#endif diff --git a/src/common/cliparser.h b/src/common/cliparser.h index 0cdfcaf1..8515d55e 100644 --- a/src/common/cliparser.h +++ b/src/common/cliparser.h @@ -25,11 +25,16 @@ #include #include -class CliParser{ +#ifdef HAVE_KDE +# include +#endif + +class CliParser { public: - inline CliParser() {}; + CliParser(); + + bool init(const QStringList &arguments = QStringList()); - bool parse(const QStringList &arguments); QString value(const QString &longName); bool isSet(const QString &longName); inline void addSwitch(const QString &longName, const char shortName = 0, const QString &help = QString()) { @@ -64,6 +69,8 @@ private: }; void addArgument(const QString &longName, const CliParserArg &arg); + +#ifndef HAVE_KDE bool addLongArg(const CliParserArg::CliArgType type, const QString &name, const QString &value = QString()); bool addShortArg(const CliParserArg::CliArgType type, const char shortName, const QString &value = QString()); QString escapedValue(const QString &value); @@ -71,6 +78,10 @@ private: QStringList argsRaw; QHash argsHash; + +#else + KCmdLineOptions _cmdLineOptions; +#endif }; #endif diff --git a/src/common/main.cpp b/src/common/main.cpp index 3fa06b3a..1f556240 100644 --- a/src/common/main.cpp +++ b/src/common/main.cpp @@ -20,6 +20,11 @@ #include +#ifdef HAVE_KDE +# include +# include +#endif + #ifdef BUILD_CORE # include "coreapplication.h" #elif defined BUILD_QTUI @@ -35,6 +40,52 @@ int main(int argc, char **argv) { + // Setup build information and version string + # include "version.gen" + buildinfo.append(QString(",%1,%2").arg(__DATE__, __TIME__)); + Quassel::setupBuildInfo(buildinfo); + QCoreApplication::setApplicationName(Quassel::buildInfo().applicationName); + QCoreApplication::setOrganizationName(Quassel::buildInfo().organizationName); + QCoreApplication::setOrganizationDomain(Quassel::buildInfo().organizationDomain); + +#ifdef HAVE_KDE + // We need to init KCmdLineArgs first + // TODO: build an AboutData class to replace our aboutDlg strings + KAboutData aboutData(argv[0], 0, ki18n("Quassel IRC"), Quassel::buildInfo().plainVersionString.toUtf8()); + aboutData.setOrganizationDomain(Quassel::buildInfo().organizationDomain.toUtf8()); + KCmdLineArgs::init(argc, argv, &aboutData); +#endif + + // Initialize CLI arguments + // NOTE: We can't use tr() at this point, since app is not yet created + CliParser *cliParser = Quassel::cliParser(); + + // put shared client&core arguments here + cliParser->addSwitch("debug",'d', "Enable debug output"); + cliParser->addSwitch("help",'h', "Display this help and exit"); + +#ifndef BUILD_CORE + // put client-only arguments here + cliParser->addSwitch("debugbufferswitches", 0, "Enables debugging for bufferswitches"); + cliParser->addSwitch("debugmodel", 0, "Enables debugging for models"); +#endif +#ifndef BUILD_QTCLIENT + // put core-only arguments here + cliParser->addOption("port ",'p', "The port quasselcore will listen at", QString("4242")); + cliParser->addSwitch("norestore", 'n', "Don't restore last core's state"); + cliParser->addOption("logfile ", 'l', "Path to logfile"); + cliParser->addOption("loglevel ", 'L', "Loglevel Debug|Info|Warning|Error", "Info"); + cliParser->addOption("datadir ", 0, "Specify the directory holding datafiles like the Sqlite DB and the SSL Cert"); +#endif + +#ifdef HAVE_KDE + // the KDE version needs this extra call to parse argc/argv before app is instantiated + if(!cliParser->init()) { + cliParser->usage(); + return EXIT_FAILURE; + } +#endif + # if defined BUILD_CORE CoreApplication app(argc, argv); # elif defined BUILD_QTUI @@ -43,9 +94,13 @@ int main(int argc, char **argv) { MonolithicApplication app(argc, argv); # endif -# include "version.gen" - buildinfo.append(QString(",%1,%2").arg(__DATE__, __TIME__)); - app.setupBuildInfo(buildinfo); +#ifndef HAVE_KDE + // the non-KDE version parses after app has been instantiated + if(!cliParser->init(app.arguments())) { + cliParser->usage(); + return false; + } +#endif if(!app.init()) return EXIT_FAILURE; return app.exec(); diff --git a/src/common/quassel.cpp b/src/common/quassel.cpp index 84301f96..788fb372 100644 --- a/src/common/quassel.cpp +++ b/src/common/quassel.cpp @@ -58,11 +58,6 @@ Quassel::Quassel() { # endif #endif - _cliParser = new CliParser(); - - // put shared client&core arguments here - cliParser()->addSwitch("debug",'d', tr("Enable debug output")); - cliParser()->addSwitch("help",'h', tr("Display this help and exit")); } Quassel::~Quassel() { @@ -86,10 +81,11 @@ bool Quassel::init() { Network::setDefaultCodecForEncoding("UTF-8"); Network::setDefaultCodecForDecoding("ISO-8859-15"); - if(!cliParser()->parse(QCoreApplication::arguments()) || isOptionSet("help")) { + if(isOptionSet("help")) { cliParser()->usage(); return false; } + DEBUG = isOptionSet("debug"); return true; } diff --git a/src/common/quassel.h b/src/common/quassel.h index 24459cd5..bb4771e8 100644 --- a/src/common/quassel.h +++ b/src/common/quassel.h @@ -57,10 +57,9 @@ public: QString organizationDomain; }; - void setupBuildInfo(const QString &generated); - virtual ~Quassel(); + static void setupBuildInfo(const QString &generated); static inline const BuildInfo & buildInfo(); static inline RunMode runMode(); @@ -98,7 +97,7 @@ const Quassel::BuildInfo & Quassel::buildInfo() { return _buildInfo; } Quassel::RunMode Quassel::runMode() { return _runMode; } void Quassel::setRunMode(Quassel::RunMode mode) { _runMode = mode; } -CliParser *Quassel::cliParser() { return _cliParser; } +CliParser *Quassel::cliParser() { return _cliParser ? _cliParser : _cliParser = new CliParser(); } QString Quassel::optionValue(const QString &key) { return cliParser()->value(key); } bool Quassel::isOptionSet(const QString &key) { return cliParser()->isSet(key); } diff --git a/src/core/coreapplication.cpp b/src/core/coreapplication.cpp index 2cdc5a60..99fcfd80 100644 --- a/src/core/coreapplication.cpp +++ b/src/core/coreapplication.cpp @@ -27,14 +27,6 @@ CoreApplicationInternal::CoreApplicationInternal() : _coreCreated(false) { Q_INIT_RESOURCE(sql); - - // put core-only arguments here - CliParser *parser = Quassel::cliParser(); - parser->addOption("port",'p', tr("The port quasselcore will listen at"), QString("4242")); - parser->addSwitch("norestore", 'n', tr("Don't restore last core's state")); - parser->addOption("logfile", 'l', tr("Path to logfile")); - parser->addOption("loglevel", 'L', tr("Loglevel Debug|Info|Warning|Error"), "Info"); - parser->addOption("datadir", 0, tr("Specify the directory holding datafiles like the Sqlite DB and the SSL Cert")); } CoreApplicationInternal::~CoreApplicationInternal() { @@ -60,16 +52,21 @@ bool CoreApplicationInternal::init() { Core::instance(); // create and init the core _coreCreated = true; - if(!Quassel::isOptionSet("norestore")) { + // if using KDE, option is called "restore" instead of "norestore" + if(Quassel::isOptionSet("restore") || !Quassel::isOptionSet("norestore")) Core::restoreState(); - } + return true; } /*****************************************************************************/ CoreApplication::CoreApplication(int &argc, char **argv) +#ifdef HAVE_KDE + : KApplication(false), +#else : QCoreApplication(argc, argv), +#endif Quassel() { setRunMode(Quassel::CoreOnly); diff --git a/src/core/coreapplication.h b/src/core/coreapplication.h index 07fa5821..42454553 100644 --- a/src/core/coreapplication.h +++ b/src/core/coreapplication.h @@ -21,7 +21,11 @@ #ifndef COREAPPLICATION_H_ #define COREAPPLICATION_H_ -#include +#ifdef HAVE_KDE +# include +#else +# include +#endif #include "quassel.h" @@ -36,12 +40,17 @@ class CoreApplicationInternal { ~CoreApplicationInternal(); bool init(); - + private: bool _coreCreated; }; +#ifdef HAVE_KDE +class CoreApplication : public KApplication, public Quassel { +#else class CoreApplication : public QCoreApplication, public Quassel { +#endif + Q_OBJECT public: CoreApplication(int &argc, char **argv); diff --git a/src/qtui/mainwin.cpp b/src/qtui/mainwin.cpp index 51d74d78..9eb74d44 100644 --- a/src/qtui/mainwin.cpp +++ b/src/qtui/mainwin.cpp @@ -19,6 +19,11 @@ ***************************************************************************/ #include "mainwin.h" +#ifdef HAVE_KDE +# include +# include +#endif + #include "aboutdlg.h" #include "action.h" #include "actioncollection.h" @@ -76,7 +81,11 @@ #include "settingspages/notificationssettingspage.h" MainWin::MainWin(QWidget *parent) +#ifdef HAVE_KDE + : KMainWindow(parent), +#else : QMainWindow(parent), +#endif coreLagLabel(new QLabel()), sslLabel(new QLabel()), msgProcessorStatusWidget(new MsgProcessorStatusWidget()), @@ -159,7 +168,7 @@ MainWin::~MainWin() { QtUiSettings s; s.setValue("MainWinSize", size()); s.setValue("MainWinPos", pos()); - s.setValue("MainWinState", saveState()); + s.setValue("MainWinState", saveState()); qDebug() << "fini!"; } void MainWin::updateIcon() { diff --git a/src/qtui/mainwin.h b/src/qtui/mainwin.h index 76bd423c..660cc286 100644 --- a/src/qtui/mainwin.h +++ b/src/qtui/mainwin.h @@ -21,7 +21,12 @@ #ifndef MAINWIN_H_ #define MAINWIN_H_ -#include +#ifdef HAVE_KDE +# include +#else +# include +#endif + #include #include "qtui.h" @@ -41,7 +46,12 @@ class QMenu; class QLabel; //!\brief The main window of Quassel's QtUi. -class MainWin : public QMainWindow { +class MainWin +#ifdef HAVE_KDE +: public KMainWindow { +#else +: public QMainWindow { +#endif Q_OBJECT public: diff --git a/src/qtui/qtuiapplication.cpp b/src/qtui/qtuiapplication.cpp index 90e2739d..6972dd04 100644 --- a/src/qtui/qtuiapplication.cpp +++ b/src/qtui/qtuiapplication.cpp @@ -47,15 +47,14 @@ // } QtUiApplication::QtUiApplication(int &argc, char **argv) +#ifdef HAVE_KDE + : KApplication(), Quassel() +#else : QApplication(argc, argv), Quassel() +#endif { setRunMode(Quassel::ClientOnly); - // put client-only arguments here - CliParser *parser = Quassel::cliParser(); - parser->addSwitch("debugbufferswitches",0,"Enables debugging for bufferswitches"); - parser->addSwitch("debugmodel",0,"Enables debugging for models"); - qInstallMsgHandler(Client::logMessage); } diff --git a/src/qtui/qtuiapplication.h b/src/qtui/qtuiapplication.h index 9fb572ab..8fd50d6d 100644 --- a/src/qtui/qtuiapplication.h +++ b/src/qtui/qtuiapplication.h @@ -21,7 +21,12 @@ #ifndef QTUIAPPLICATION_H_ #define QTUIAPPLICATION_H_ -#include +#ifdef HAVE_KDE +# include +#else +# include +#endif + #include #include "quassel.h" @@ -29,7 +34,12 @@ class QtUi; +#ifdef HAVE_KDE +class QtUiApplication : public KApplication, public Quassel { +#else class QtUiApplication : public QApplication, public Quassel { +#endif + Q_OBJECT public: -- 2.20.1