Use KCmdLineArgs, KApplication and KMainWindow
authorManuel Nickschas <sputnick@quassel-irc.org>
Wed, 24 Dec 2008 23:39:35 +0000 (00:39 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Fri, 26 Dec 2008 00:08:42 +0000 (01:08 +0100)
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
src/common/cliparser.h
src/common/main.cpp
src/common/quassel.cpp
src/common/quassel.h
src/core/coreapplication.cpp
src/core/coreapplication.h
src/qtui/mainwin.cpp
src/qtui/mainwin.h
src/qtui/qtuiapplication.cpp
src/qtui/qtuiapplication.h

index 56fa22a..2116577 100644 (file)
 #include "cliparser.h"
 
 #include <QDir>
+#include <QDebug>
 #include <QString>
 #include <QFileInfo>
-#include <QDebug>
 
+#ifdef HAVE_KDE
+#  include <KCmdLineArgs>
+#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
index 0cdfcaf..8515d55 100644 (file)
 #include <QStringList>
 #include <QHash>
 
-class CliParser{
+#ifdef HAVE_KDE
+#  include <KCmdLineOptions>
+#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<QString, CliParserArg> argsHash;
+
+#else
+  KCmdLineOptions _cmdLineOptions;
+#endif
 };
 
 #endif
index 3fa06b3..1f55624 100644 (file)
 
 #include <cstdlib>
 
+#ifdef HAVE_KDE
+#  include <KCmdLineArgs>
+#  include <KAboutData>
+#endif
+
 #ifdef BUILD_CORE
 #  include "coreapplication.h"
 #elif defined BUILD_QTUI
 
 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 <port>",'p', "The port quasselcore will listen at", QString("4242"));
+  cliParser->addSwitch("norestore", 'n', "Don't restore last core's state");
+  cliParser->addOption("logfile <path>", 'l', "Path to logfile");
+  cliParser->addOption("loglevel <level>", 'L', "Loglevel Debug|Info|Warning|Error", "Info");
+  cliParser->addOption("datadir <path>", 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();
index 84301f9..788fb37 100644 (file)
@@ -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;
 }
index 24459cd..bb4771e 100644 (file)
@@ -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); }
 
index 2cdc5a6..99fcfd8 100644 (file)
@@ -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);
index 07fa582..4245455 100644 (file)
 #ifndef COREAPPLICATION_H_
 #define COREAPPLICATION_H_
 
-#include <QCoreApplication>
+#ifdef HAVE_KDE
+#  include <KApplication>
+#else
+#  include <QCoreApplication>
+#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);
index 51d74d7..9eb74d4 100644 (file)
  ***************************************************************************/
 #include "mainwin.h"
 
+#ifdef HAVE_KDE
+#  include <KMenuBar>
+#  include <KStatusBar>
+#endif
+
 #include "aboutdlg.h"
 #include "action.h"
 #include "actioncollection.h"
 #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() {
index 76bd423..660cc28 100644 (file)
 #ifndef MAINWIN_H_
 #define MAINWIN_H_
 
-#include <QMainWindow>
+#ifdef HAVE_KDE
+#  include <KMainWindow>
+#else
+#  include <QMainWindow>
+#endif
+
 #include <QSystemTrayIcon>
 
 #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:
index 90e2739..6972dd0 100644 (file)
 // }
 
 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);
 }
 
index 9fb572a..8fd50d6 100644 (file)
 #ifndef QTUIAPPLICATION_H_
 #define QTUIAPPLICATION_H_
 
-#include <QApplication>
+#ifdef HAVE_KDE
+#  include <KApplication>
+#else
+#  include <QApplication>
+#endif
+
 #include <QSessionManager>
 
 #include "quassel.h"
 
 class QtUi;
 
+#ifdef HAVE_KDE
+class QtUiApplication : public KApplication, public Quassel {
+#else
 class QtUiApplication : public QApplication, public Quassel {
+#endif
+
   Q_OBJECT
 
  public: