common: Make the Quassel class a proper singleton, clean up
[quassel.git] / src / common / quassel.h
index b6fc378..8b10677 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2014 by the Quassel Project                        *
+ *   Copyright (C) 2005-2016 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
-#ifndef QUASSEL_H_
-#define QUASSEL_H_
+#pragma once
+
+#include <functional>
+#include <memory>
+#include <vector>
 
 #include <QCoreApplication>
 #include <QLocale>
@@ -47,12 +50,9 @@ public:
         QString baseVersion;
         QString generatedVersion;
         QString commitHash;
-        uint commitDate;
-        QString buildDate;
-        bool isSourceDirty;
-        uint protocolVersion;
-        uint clientNeedsProtocol;
-        uint coreNeedsProtocol;
+        QString commitDate;
+
+        uint protocolVersion; // deprecated
 
         QString applicationName;
         QString coreApplicationName;
@@ -73,21 +73,32 @@ public:
         SaslAuthentication = 0x0002,
         SaslExternal = 0x0004,
         HideInactiveNetworks = 0x0008,
-
-        NumFeatures = 0x0008
+        PasswordChange = 0x0010,
+        CapNegotiation = 0x0020,           /// IRCv3 capability negotiation, account tracking
+        VerifyServerSSL = 0x0040,          /// IRC server SSL validation
+        CustomRateLimits = 0x0080,         /// IRC server custom message rate limits
+        DccFileTransfer = 0x0100,          /// DCC file transfer support (forcefully disabled for now)
+        AwayFormatTimestamp = 0x0200,      /// Timestamp formatting in away (e.g. %%hh:mm%%)
+        Authenticators = 0x0400,           /// Whether or not the core supports auth backends.
+        BufferActivitySync = 0x0800,       /// Sync buffer activity status
+        CoreSideHighlights = 0x1000,       /// Core-Side highlight configuration and matching
+        SenderPrefixes = 0x2000,           /// Show prefixes for senders in backlog
+        RemoteDisconnect = 0x4000,         /// Allow this peer to be remotely disconnected
+
+        NumFeatures = 0x4000
     };
-    Q_DECLARE_FLAGS(Features, Feature);
+    Q_DECLARE_FLAGS(Features, Feature)
 
     //! The features the current version of Quassel supports (\sa Feature)
     /** \return An ORed list of all enum values in Feature
      */
     static Features features();
 
-    virtual ~Quassel();
+    static Quassel *instance();
 
-    static void setupBuildInfo(const QString &generated);
-    static inline const BuildInfo &buildInfo();
-    static inline RunMode runMode();
+    static void setupBuildInfo();
+    static const BuildInfo &buildInfo();
+    static RunMode runMode();
 
     static QString configDirPath();
 
@@ -117,14 +128,9 @@ public:
 
     static void loadTranslation(const QLocale &locale);
 
-    static inline void setCliParser(AbstractCliParser *cliParser);
-    static inline AbstractCliParser *cliParser();
-    static inline QString optionValue(const QString &option);
-    static inline bool isOptionSet(const QString &option);
-
-    static const QString &coreDumpFileName();
-
-    static bool DEBUG;
+    static void setCliParser(std::shared_ptr<AbstractCliParser> cliParser);
+    static QString optionValue(const QString &option);
+    static bool isOptionSet(const QString &option);
 
     enum LogLevel {
         DebugLevel,
@@ -133,61 +139,83 @@ public:
         ErrorLevel
     };
 
-    static inline LogLevel logLevel();
-    static inline QFile *logFile();
-    static inline bool logToSyslog();
+    static LogLevel logLevel();
+    static void setLogLevel(LogLevel logLevel);
+    static QFile *logFile();
+    static bool logToSyslog();
 
     static void logFatalMessage(const char *msg);
 
+    using ReloadHandler = std::function<bool()>;
+
+    static void registerReloadHandler(ReloadHandler handler);
+
+    using QuitHandler = std::function<void()>;
+
+    static void registerQuitHandler(QuitHandler quitHandler);
+
 protected:
-    Quassel();
-    virtual bool init();
-    virtual void quit();
+    static bool init();
+    static void destroy();
+
+    static void setRunMode(Quassel::RunMode runMode);
 
-    inline void setRunMode(RunMode mode);
-    inline void setDataDirPaths(const QStringList &paths);
-    QStringList findDataDirPaths() const;
-    inline void disableCrashhandler();
+    static void setDataDirPaths(const QStringList &paths);
+    static QStringList findDataDirPaths();
+    static void disableCrashHandler();
+
+    friend class CoreApplication;
+    friend class QtUiApplication;
+    friend class MonolithicApplication;
 
 private:
+    Quassel();
+    void setupEnvironment();
     void registerMetaTypes();
 
+    const QString &coreDumpFileName();
+
+    /**
+     * Requests a reload of relevant runtime configuration.
+     *
+     * Calls any registered reload handlers, and returns the cumulative result. If no handlers are registered,
+     * does nothing and returns true.
+     *
+     * @returns True if configuration reload successful, otherwise false
+     */
+    bool reloadConfig();
+
+    /**
+     * Requests to quit the application.
+     *
+     * Calls any registered quit handlers. If no handlers are registered, calls QCoreApplication::quit().
+     */
+    void quit();
+
+    void logBacktrace(const QString &filename);
+
     static void handleSignal(int signal);
-    static void logBacktrace(const QString &filename);
-
-    static Quassel *_instance;
-    static BuildInfo _buildInfo;
-    static AbstractCliParser *_cliParser;
-    static RunMode _runMode;
-    static bool _initialized;
-    static bool _handleCrashes;
-
-    static QString _coreDumpFileName;
-    static QString _configDirPath;
-    static QStringList _dataDirPaths;
-    static QString _translationDirPath;
-
-    static LogLevel _logLevel;
-    static QFile *_logFile;
-    static bool _logToSyslog;
-};
 
+private:
+    BuildInfo _buildInfo;
+    RunMode _runMode;
+    bool _initialized{false};
+    bool _handleCrashes{true};
 
-Q_DECLARE_OPERATORS_FOR_FLAGS(Quassel::Features);
+    QString _coreDumpFileName;
+    QString _configDirPath;
+    QStringList _dataDirPaths;
+    QString _translationDirPath;
 
-const Quassel::BuildInfo &Quassel::buildInfo() { return _buildInfo; }
-Quassel::RunMode Quassel::runMode() { return _runMode; }
-void Quassel::setRunMode(Quassel::RunMode mode) { _runMode = mode; }
-void Quassel::setDataDirPaths(const QStringList &paths) { _dataDirPaths = paths; }
-void Quassel::disableCrashhandler() { _handleCrashes = false; }
+    LogLevel _logLevel{InfoLevel};
+    bool _logToSyslog{false};
+    std::unique_ptr<QFile> _logFile;
 
-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); }
+    std::shared_ptr<AbstractCliParser> _cliParser;
+
+    std::vector<ReloadHandler> _reloadHandlers;
+    std::vector<QuitHandler> _quitHandlers;
+};
 
-Quassel::LogLevel Quassel::logLevel() { return _logLevel; }
-QFile *Quassel::logFile() { return _logFile; }
-bool Quassel::logToSyslog() { return _logToSyslog; }
 
-#endif
+Q_DECLARE_OPERATORS_FOR_FLAGS(Quassel::Features);