From af5ee11e78a6c98f755d0cc5e3bef77028b227dd Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Sat, 10 Jan 2009 21:38:47 +0100 Subject: [PATCH 1/1] Reworking CliParser to allow building quasselcore always without KDE deps Rather than hacking KDE support into CliParser using #ifdef (which forces all quassel binaries to be built even with or without KDE support), I have now created an abstract base class and specializations for the two cases. quasselcore now always builds without KDE deps. --- src/CMakeLists.txt | 2 +- src/common/CMakeLists.txt | 5 +-- src/common/abstractcliparser.h | 69 +++++++++++++++++++++++++++++++ src/common/cliparser.cpp | 34 +-------------- src/common/cliparser.h | 43 ++----------------- src/common/main.cpp | 24 ++++++++--- src/common/quassel.cpp | 2 +- src/common/quassel.h | 10 +++-- src/core/coreapplication.cpp | 14 +------ src/core/coreapplication.h | 4 -- src/uisupport/CMakeLists.txt | 5 +++ src/uisupport/kcmdlinewrapper.cpp | 53 ++++++++++++++++++++++++ src/uisupport/kcmdlinewrapper.h | 45 ++++++++++++++++++++ 13 files changed, 204 insertions(+), 106 deletions(-) create mode 100644 src/common/abstractcliparser.h create mode 100644 src/uisupport/kcmdlinewrapper.cpp create mode 100644 src/uisupport/kcmdlinewrapper.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 11e40819..9af8c50a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,7 +43,7 @@ if(WANT_CORE) COMPILE_FLAGS "-DQT_NETWORK_LIB -DQT_SCRIPT_LIB -DQT_SQL_LIB -DBUILD_CORE" OUTPUT_NAME ../quasselcore) target_link_libraries(quasselcore mod_core mod_common - ${QUASSEL_QT_LIBRARIES} ${QUASSEL_KDE_LIBRARIES} ${QUASSEL_SSL_LIBRARIES}) + ${QUASSEL_QT_LIBRARIES} ${QUASSEL_SSL_LIBRARIES}) install(TARGETS quasselcore RUNTIME DESTINATION ${BIN_INSTALL_DIR}) endif(WANT_CORE) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index fb4c3e8c..acded6de 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -31,8 +31,6 @@ if(CMAKE_HOST_UNIX) set(SOURCES ${SOURCES} logbacktrace_unix.cpp) endif(CMAKE_HOST_UNIX) - - set(MOC_HDRS aliasmanager.h backlogmanager.h @@ -50,6 +48,7 @@ set(MOC_HDRS syncableobject.h) set(HEADERS ${MOC_HDRS} + abstractcliparser.h bufferinfo.h cliparser.h logger.h @@ -59,11 +58,9 @@ set(HEADERS ${MOC_HDRS} qt4_wrap_cpp(MOC ${MOC_HDRS}) - include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}) # for version.inc and version.gen set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES version.gen) add_library(mod_common STATIC ${SOURCES} ${MOC} ${CMAKE_BINARY_DIR}/i18n/qrc_i18n.cxx) set_source_files_properties(${CMAKE_BINARY_DIR}/i18n/qrc_i18n.cxx PROPERTIES GENERATED true) add_dependencies(mod_common i18n) - diff --git a/src/common/abstractcliparser.h b/src/common/abstractcliparser.h new file mode 100644 index 00000000..680d86cc --- /dev/null +++ b/src/common/abstractcliparser.h @@ -0,0 +1,69 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel IRC Team * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef ABSTRACTCLIPARSER_H +#define ABSTRACTCLIPARSER_H + +#include + +class AbstractCliParser { +public: + virtual bool init(const QStringList &arguments = QStringList()) = 0; + + virtual QString value(const QString &longName) = 0; + virtual bool isSet(const QString &longName) = 0; + inline void addSwitch(const QString &longName, const char shortName = 0, const QString &help = QString()) { + addArgument(longName, CliParserArg(CliParserArg::CliArgSwitch, shortName, help)); + } + inline void addOption(const QString &longName, const char shortName = 0, const QString &help = QString(), const QString &def = QString()) { + addArgument(longName, CliParserArg(CliParserArg::CliArgOption, shortName, help, def)); + } + virtual void usage() = 0; + + virtual ~AbstractCliParser() {}; + +protected: + struct CliParserArg { + enum CliArgType { + CliArgInvalid, + CliArgSwitch, + CliArgOption + }; + + CliParserArg(const CliArgType _type = CliArgInvalid, const char _shortName = 0, const QString _help = QString(), const QString _def = QString()) + : type(_type), + shortName(_shortName), + help(_help), + def(_def), + value(QString()), + boolValue(false) {}; + + CliArgType type; + char shortName; + QString help; + QString def; + QString value; + bool boolValue; + }; + + virtual void addArgument(const QString &longName, const CliParserArg &arg) = 0; +}; + +#endif diff --git a/src/common/cliparser.cpp b/src/common/cliparser.cpp index a5b72234..625f64ab 100644 --- a/src/common/cliparser.cpp +++ b/src/common/cliparser.cpp @@ -24,40 +24,10 @@ #include #include -#ifdef HAVE_KDE -# include -#endif - -CliParser::CliParser() { +CliParser::CliParser() : AbstractCliParser() { } -#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) { QString longName = longName_; longName.remove(QRegExp("\\s*<.*>\\s*")); // KCmdLineArgs takes args of the form "arg " @@ -249,5 +219,3 @@ QString CliParser::lnameOfShortArg(const char arg) { } return QString(); } - -#endif diff --git a/src/common/cliparser.h b/src/common/cliparser.h index 8515d55e..3c096a2e 100644 --- a/src/common/cliparser.h +++ b/src/common/cliparser.h @@ -21,15 +21,12 @@ #ifndef CLIPARSER_H #define CLIPARSER_H -#include -#include #include -#ifdef HAVE_KDE -# include -#endif +#include "abstractcliparser.h" -class CliParser { +//! Quassel's own parser for command line arguments +class CliParser : public AbstractCliParser { public: CliParser(); @@ -37,40 +34,10 @@ public: QString value(const QString &longName); bool isSet(const QString &longName); - inline void addSwitch(const QString &longName, const char shortName = 0, const QString &help = QString()) { - addArgument(longName, CliParserArg(CliParserArg::CliArgSwitch, shortName, help)); - } - inline void addOption(const QString &longName, const char shortName = 0, const QString &help = QString(), const QString &def = QString()) { - addArgument(longName, CliParserArg(CliParserArg::CliArgOption, shortName, help, def)); - } void usage(); private: - struct CliParserArg { - enum CliArgType { - CliArgInvalid, - CliArgSwitch, - CliArgOption - }; - CliParserArg(const CliArgType _type = CliArgInvalid, const char _shortName = 0, const QString _help = QString(), const QString _def = QString()) - : type(_type), - shortName(_shortName), - help(_help), - def(_def), - value(QString()), - boolValue(false) {}; - - CliArgType type; - char shortName; - QString help; - QString def; - QString value; - bool boolValue; - }; - 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); @@ -78,10 +45,6 @@ private: QStringList argsRaw; QHash argsHash; - -#else - KCmdLineOptions _cmdLineOptions; -#endif }; #endif diff --git a/src/common/main.cpp b/src/common/main.cpp index e62c24da..9c468fda 100644 --- a/src/common/main.cpp +++ b/src/common/main.cpp @@ -20,11 +20,6 @@ #include -#ifdef HAVE_KDE -# include -# include -#endif - #ifdef BUILD_CORE # include "coreapplication.h" #elif defined BUILD_QTUI @@ -36,6 +31,17 @@ #error "Something is wrong - you need to #define a build mode!" #endif +// We don't want quasselcore to depend on KDE +#if defined HAVE_KDE && defined BUILD_CORE +# undef HAVE_KDE +#endif + +#ifdef HAVE_KDE +# include +# include "kcmdlinewrapper.h" +#endif + +#include "cliparser.h" #include "quassel.h" int main(int argc, char **argv) { @@ -49,6 +55,8 @@ int main(int argc, char **argv) { QCoreApplication::setOrganizationName(Quassel::buildInfo().organizationName); QCoreApplication::setOrganizationDomain(Quassel::buildInfo().organizationDomain); + AbstractCliParser *cliParser; + #ifdef HAVE_KDE // We need to init KCmdLineArgs first // TODO: build an AboutData compat class to replace our aboutDlg strings @@ -58,11 +66,15 @@ int main(int argc, char **argv) { aboutData.addLicense(KAboutData::License_GPL_V3); aboutData.setOrganizationDomain(Quassel::buildInfo().organizationDomain.toUtf8()); KCmdLineArgs::init(argc, argv, &aboutData); + + cliParser = new KCmdLineWrapper(); +#else + cliParser = new CliParser(); #endif + Quassel::setCliParser(cliParser); // 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"); diff --git a/src/common/quassel.cpp b/src/common/quassel.cpp index 1e6e1ed8..57d60333 100644 --- a/src/common/quassel.cpp +++ b/src/common/quassel.cpp @@ -35,7 +35,7 @@ #include "syncableobject.h" Quassel::BuildInfo Quassel::_buildInfo; -CliParser *Quassel::_cliParser = 0; +AbstractCliParser *Quassel::_cliParser = 0; Quassel::RunMode Quassel::_runMode; bool Quassel::_initialized = false; bool Quassel::DEBUG = false; diff --git a/src/common/quassel.h b/src/common/quassel.h index bb4771e8..b490075c 100644 --- a/src/common/quassel.h +++ b/src/common/quassel.h @@ -24,7 +24,7 @@ #include #include -#include "cliparser.h" +#include "abstractcliparser.h" class Quassel { Q_DECLARE_TR_FUNCTIONS(Quassel) @@ -63,7 +63,8 @@ public: static inline const BuildInfo & buildInfo(); static inline RunMode runMode(); - static inline CliParser *cliParser(); + static inline void setCliParser(AbstractCliParser *cliParser); + static inline AbstractCliParser *cliParser(); static inline QString optionValue(const QString &option); static inline bool isOptionSet(const QString &option); @@ -86,7 +87,7 @@ private: static void logBacktrace(const QString &filename); static BuildInfo _buildInfo; - static CliParser *_cliParser; + static AbstractCliParser *_cliParser; static RunMode _runMode; static bool _initialized; @@ -97,7 +98,8 @@ 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 : _cliParser = new CliParser(); } +void Quassel::setCliParser(AbstractCliParser *parser) { _cliParser = parser; } +AbstractCliParser *Quassel::cliParser() { return _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 ca0f5273..5d373c96 100644 --- a/src/core/coreapplication.cpp +++ b/src/core/coreapplication.cpp @@ -52,12 +52,7 @@ bool CoreApplicationInternal::init() { Core::instance(); // create and init the core _coreCreated = true; -#ifdef HAVE_KDE - // if using KDE, option is called "restore" instead of "norestore" - if(Quassel::isOptionSet("restore")) -#else if(!Quassel::isOptionSet("norestore")) -#endif Core::restoreState(); return true; @@ -65,14 +60,7 @@ bool CoreApplicationInternal::init() { /*****************************************************************************/ -CoreApplication::CoreApplication(int &argc, char **argv) -#ifdef HAVE_KDE -: KApplication(false), Quassel() { - Q_UNUSED(argc); Q_UNUSED(argv); -#else -: QCoreApplication(argc, argv), Quassel() { -#endif - +CoreApplication::CoreApplication(int &argc, char **argv) : QCoreApplication(argc, argv), Quassel() { setRunMode(Quassel::CoreOnly); _internal = new CoreApplicationInternal(); } diff --git a/src/core/coreapplication.h b/src/core/coreapplication.h index 42454553..ecf4f6d5 100644 --- a/src/core/coreapplication.h +++ b/src/core/coreapplication.h @@ -45,11 +45,7 @@ class CoreApplicationInternal { bool _coreCreated; }; -#ifdef HAVE_KDE -class CoreApplication : public KApplication, public Quassel { -#else class CoreApplication : public QCoreApplication, public Quassel { -#endif Q_OBJECT public: diff --git a/src/uisupport/CMakeLists.txt b/src/uisupport/CMakeLists.txt index e8518d51..1df6325f 100644 --- a/src/uisupport/CMakeLists.txt +++ b/src/uisupport/CMakeLists.txt @@ -50,6 +50,11 @@ set(HEADERS uisettings.h uistyle.h) +if(HAVE_KDE) + set(SOURCES ${SOURCES} kcmdlinewrapper.cpp) + set(HEADERS ${HEADERS} kcmdlinewrapper.h) +endif(HAVE_KDE) + qt4_wrap_cpp(MOC ${MOC_HDRS}) include_directories(${CMAKE_SOURCE_DIR}/src/common diff --git a/src/uisupport/kcmdlinewrapper.cpp b/src/uisupport/kcmdlinewrapper.cpp new file mode 100644 index 00000000..1f1704e0 --- /dev/null +++ b/src/uisupport/kcmdlinewrapper.cpp @@ -0,0 +1,53 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel IRC Team * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#include "kcmdlinewrapper.h" + +#include + +KCmdLineWrapper::KCmdLineWrapper() { + +} + +void KCmdLineWrapper::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 KCmdLineWrapper::init(const QStringList &) { + KCmdLineArgs::addCmdLineOptions(_cmdLineOptions); + return true; +} + +QString KCmdLineWrapper::value(const QString &longName) { + return KCmdLineArgs::parsedArgs()->getOption(longName.toUtf8()); +} + +bool KCmdLineWrapper::isSet(const QString &longName) { + // KCmdLineArgs handles --nooption like NOT --option + if(longName.startsWith("no")) + return !KCmdLineArgs::parsedArgs()->isSet(longName.mid(2).toUtf8()); + return KCmdLineArgs::parsedArgs()->isSet(longName.toUtf8()); +} + +void KCmdLineWrapper::usage() { + KCmdLineArgs::usage(); +} diff --git a/src/uisupport/kcmdlinewrapper.h b/src/uisupport/kcmdlinewrapper.h new file mode 100644 index 00000000..72d8e2a7 --- /dev/null +++ b/src/uisupport/kcmdlinewrapper.h @@ -0,0 +1,45 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel IRC Team * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef KCMDLINEWRAPPER_H +#define KCMDLINEWRAPPER_H + +#include "abstractcliparser.h" + +#include + +//! Wrapper for KCmdLineOptions +class KCmdLineWrapper : public AbstractCliParser { +public: + KCmdLineWrapper(); + + bool init(const QStringList &arguments = QStringList()); + + QString value(const QString &longName); + bool isSet(const QString &longName); + void usage(); + +private: + void addArgument(const QString &longName, const CliParserArg &arg); + + KCmdLineOptions _cmdLineOptions; +}; + +#endif -- 2.20.1