8b106771af7f16938ed9bed255dffaaaae7a5fa4
[quassel.git] / src / common / quassel.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include <functional>
24 #include <memory>
25 #include <vector>
26
27 #include <QCoreApplication>
28 #include <QLocale>
29 #include <QString>
30
31 #include "abstractcliparser.h"
32
33 class QFile;
34
35 class Quassel
36 {
37     Q_DECLARE_TR_FUNCTIONS(Quassel)
38
39 public:
40     enum RunMode {
41         Monolithic,
42         ClientOnly,
43         CoreOnly
44     };
45
46     struct BuildInfo {
47         QString fancyVersionString; // clickable rev
48         QString plainVersionString; // no <a> tag
49
50         QString baseVersion;
51         QString generatedVersion;
52         QString commitHash;
53         QString commitDate;
54
55         uint protocolVersion; // deprecated
56
57         QString applicationName;
58         QString coreApplicationName;
59         QString clientApplicationName;
60         QString organizationName;
61         QString organizationDomain;
62     };
63
64     //! A list of features that are optional in core and/or client, but need runtime checking
65     /** Some features require an uptodate counterpart, but don't justify a protocol break.
66      *  This is what we use this enum for. Add such features to it and check at runtime on the other
67      *  side for their existence.
68      *
69      *  This list should be cleaned up after every protocol break, as we can assume them to be present then.
70      */
71     enum Feature {
72         SynchronizedMarkerLine = 0x0001,
73         SaslAuthentication = 0x0002,
74         SaslExternal = 0x0004,
75         HideInactiveNetworks = 0x0008,
76         PasswordChange = 0x0010,
77         CapNegotiation = 0x0020,           /// IRCv3 capability negotiation, account tracking
78         VerifyServerSSL = 0x0040,          /// IRC server SSL validation
79         CustomRateLimits = 0x0080,         /// IRC server custom message rate limits
80         DccFileTransfer = 0x0100,          /// DCC file transfer support (forcefully disabled for now)
81         AwayFormatTimestamp = 0x0200,      /// Timestamp formatting in away (e.g. %%hh:mm%%)
82         Authenticators = 0x0400,           /// Whether or not the core supports auth backends.
83         BufferActivitySync = 0x0800,       /// Sync buffer activity status
84         CoreSideHighlights = 0x1000,       /// Core-Side highlight configuration and matching
85         SenderPrefixes = 0x2000,           /// Show prefixes for senders in backlog
86         RemoteDisconnect = 0x4000,         /// Allow this peer to be remotely disconnected
87
88         NumFeatures = 0x4000
89     };
90     Q_DECLARE_FLAGS(Features, Feature)
91
92     //! The features the current version of Quassel supports (\sa Feature)
93     /** \return An ORed list of all enum values in Feature
94      */
95     static Features features();
96
97     static Quassel *instance();
98
99     static void setupBuildInfo();
100     static const BuildInfo &buildInfo();
101     static RunMode runMode();
102
103     static QString configDirPath();
104
105     //! Returns a list of data directory paths
106     /** There are several locations for applications to install their data files in. On Unix,
107     *  a common location is /usr/share; others include $PREFIX/share and additional directories
108     *  specified in the env variable XDG_DATA_DIRS.
109     *  \return A list of directory paths to look for data files in
110     */
111     static QStringList dataDirPaths();
112
113     //! Searches for a data file in the possible data directories
114     /** Data files can reside in $DATA_DIR/apps/quassel, where $DATA_DIR is one of the directories
115     *  returned by \sa dataDirPaths().
116     *  \Note With KDE integration enabled, files are searched (only) in KDE's appdata dirs.
117     *  \return The full path to the data file if found; a null QString else
118     */
119     static QString findDataFilePath(const QString &filename);
120
121     static QString translationDirPath();
122
123     //! Returns a list of directories we look for scripts in
124     /** We look for a subdirectory named "scripts" in the configdir and in all datadir paths.
125     *   \return A list of directory paths containing executable scripts for /exec
126     */
127     static QStringList scriptDirPaths();
128
129     static void loadTranslation(const QLocale &locale);
130
131     static void setCliParser(std::shared_ptr<AbstractCliParser> cliParser);
132     static QString optionValue(const QString &option);
133     static bool isOptionSet(const QString &option);
134
135     enum LogLevel {
136         DebugLevel,
137         InfoLevel,
138         WarningLevel,
139         ErrorLevel
140     };
141
142     static LogLevel logLevel();
143     static void setLogLevel(LogLevel logLevel);
144     static QFile *logFile();
145     static bool logToSyslog();
146
147     static void logFatalMessage(const char *msg);
148
149     using ReloadHandler = std::function<bool()>;
150
151     static void registerReloadHandler(ReloadHandler handler);
152
153     using QuitHandler = std::function<void()>;
154
155     static void registerQuitHandler(QuitHandler quitHandler);
156
157 protected:
158     static bool init();
159     static void destroy();
160
161     static void setRunMode(Quassel::RunMode runMode);
162
163     static void setDataDirPaths(const QStringList &paths);
164     static QStringList findDataDirPaths();
165     static void disableCrashHandler();
166
167     friend class CoreApplication;
168     friend class QtUiApplication;
169     friend class MonolithicApplication;
170
171 private:
172     Quassel();
173     void setupEnvironment();
174     void registerMetaTypes();
175
176     const QString &coreDumpFileName();
177
178     /**
179      * Requests a reload of relevant runtime configuration.
180      *
181      * Calls any registered reload handlers, and returns the cumulative result. If no handlers are registered,
182      * does nothing and returns true.
183      *
184      * @returns True if configuration reload successful, otherwise false
185      */
186     bool reloadConfig();
187
188     /**
189      * Requests to quit the application.
190      *
191      * Calls any registered quit handlers. If no handlers are registered, calls QCoreApplication::quit().
192      */
193     void quit();
194
195     void logBacktrace(const QString &filename);
196
197     static void handleSignal(int signal);
198
199 private:
200     BuildInfo _buildInfo;
201     RunMode _runMode;
202     bool _initialized{false};
203     bool _handleCrashes{true};
204
205     QString _coreDumpFileName;
206     QString _configDirPath;
207     QStringList _dataDirPaths;
208     QString _translationDirPath;
209
210     LogLevel _logLevel{InfoLevel};
211     bool _logToSyslog{false};
212     std::unique_ptr<QFile> _logFile;
213
214     std::shared_ptr<AbstractCliParser> _cliParser;
215
216     std::vector<ReloadHandler> _reloadHandlers;
217     std::vector<QuitHandler> _quitHandlers;
218 };
219
220
221 Q_DECLARE_OPERATORS_FOR_FLAGS(Quassel::Features);