common: Make the Quassel class a proper singleton, clean up
[quassel.git] / src / common / main.cpp
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 #include <cstdlib>
22 #include <memory>
23
24 #include <QTextCodec>
25
26 #ifdef BUILD_CORE
27 #  include "coreapplication.h"
28 #elif defined BUILD_QTUI
29 #  include "aboutdata.h"
30 #  include "qtuiapplication.h"
31 #elif defined BUILD_MONO
32 #  include "aboutdata.h"
33 #  include "monoapplication.h"
34
35 #else
36 #error "Something is wrong - you need to #define a build mode!"
37 #endif
38
39 // We don't want quasselcore to depend on KDE
40 #if defined HAVE_KDE4 && defined BUILD_CORE
41 #  undef HAVE_KDE4
42 #endif
43 // We don't want quasselcore to depend on KDE
44 #if defined HAVE_KF5 && defined BUILD_CORE
45 #  undef HAVE_KF5
46 #endif
47
48 #ifdef HAVE_KDE4
49 #  include <KAboutData>
50 #  include "kcmdlinewrapper.h"
51 #elif defined HAVE_KF5
52 #  include <KCoreAddons/KAboutData>
53 #  include <KCoreAddons/Kdelibs4ConfigMigrator>
54 #  include "qt5cliparser.h"
55 #elif defined HAVE_QT5
56 #  include "qt5cliparser.h"
57 #else
58 #  include "cliparser.h"
59 #endif
60
61 #if !defined(BUILD_CORE) && defined(STATIC)
62 #include <QtPlugin>
63 Q_IMPORT_PLUGIN(qjpeg)
64 Q_IMPORT_PLUGIN(qgif)
65 #endif
66
67 #include "quassel.h"
68
69 int main(int argc, char **argv)
70 {
71 #if QT_VERSION < 0x050000
72     // All our source files are in UTF-8, and Qt5 even requires that
73     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
74     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
75 #endif
76
77     Quassel::setupBuildInfo();
78     QCoreApplication::setApplicationName(Quassel::buildInfo().applicationName);
79     QCoreApplication::setApplicationVersion(Quassel::buildInfo().plainVersionString);
80     QCoreApplication::setOrganizationName(Quassel::buildInfo().organizationName);
81     QCoreApplication::setOrganizationDomain(Quassel::buildInfo().organizationDomain);
82
83     // on OSX with Qt4, raster seems to fix performance issues
84 #if QT_VERSION < 0x050000 && defined Q_OS_MAC && !defined BUILD_CORE
85     QApplication::setGraphicsSystem("raster");
86 #endif
87
88     // We need to explicitly initialize the required resources when linking statically
89 #ifndef BUILD_QTUI
90     Q_INIT_RESOURCE(sql);
91 #endif
92 #ifndef BUILD_CORE
93     Q_INIT_RESOURCE(pics);
94     Q_INIT_RESOURCE(hicolor);
95 #endif
96
97 #ifdef EMBED_DATA
98     Q_INIT_RESOURCE(i18n);
99 # ifndef BUILD_CORE
100     Q_INIT_RESOURCE(data);
101 #   ifdef WITH_OXYGEN
102     Q_INIT_RESOURCE(oxygen);
103 #   endif
104 #   ifdef WITH_BREEZE
105     Q_INIT_RESOURCE(breeze);
106 #   endif
107 #   ifdef WITH_BREEZE_DARK
108     Q_INIT_RESOURCE(breezedark);
109 #   endif
110 # endif
111 #endif
112
113     std::shared_ptr<AbstractCliParser> cliParser;
114
115 #ifdef HAVE_KDE4
116     // We need to init KCmdLineArgs first
117     KAboutData aboutData("quassel", "kdelibs4", ki18n("Quassel IRC"), Quassel::buildInfo().plainVersionString.toUtf8(),
118         ki18n("A modern, distributed IRC client"));
119     aboutData.addLicense(KAboutData::License_GPL_V2);
120     aboutData.addLicense(KAboutData::License_GPL_V3);
121     aboutData.setBugAddress("http://bugs.quassel-irc.org/projects/quassel-irc/issues/new");
122     aboutData.setOrganizationDomain(Quassel::buildInfo().organizationDomain.toUtf8());
123     KCmdLineArgs::init(argc, argv, &aboutData);
124
125     cliParser = std::make_shared<KCmdLineWrapper>();
126 #elif defined HAVE_QT5
127     cliParser = std::make_shared<Qt5CliParser>();
128 #else
129     cliParser = std::make_shared<CliParser>();
130 #endif
131     Quassel::setCliParser(cliParser);
132
133     // Initialize CLI arguments
134     // NOTE: We can't use tr() at this point, since app is not yet created
135     // TODO: Change this once we get rid of KDE4 and can initialize the parser after creating the app
136
137     // put shared client&core arguments here
138     cliParser->addSwitch("debug", 'd', "Enable debug output");
139     cliParser->addSwitch("help", 'h', "Display this help and exit");
140     cliParser->addSwitch("version", 'v', "Display version information");
141 #ifdef BUILD_QTUI
142     cliParser->addOption("configdir", 'c', "Specify the directory holding the client configuration", "path");
143 #else
144     cliParser->addOption("configdir", 'c', "Specify the directory holding configuration files, the SQlite database and the SSL certificate", "path");
145 #endif
146     cliParser->addOption("datadir", 0, "DEPRECATED - Use --configdir instead", "path");
147
148 #ifndef BUILD_CORE
149     // put client-only arguments here
150     cliParser->addOption("icontheme", 0, "Override the system icon theme ('breeze' is recommended)", "theme");
151     cliParser->addOption("qss", 0, "Load a custom application stylesheet", "file.qss");
152     cliParser->addSwitch("debugbufferswitches", 0, "Enables debugging for bufferswitches");
153     cliParser->addSwitch("debugmodel", 0, "Enables debugging for models");
154     cliParser->addSwitch("hidewindow", 0, "Start the client minimized to the system tray");
155 #endif
156 #ifndef BUILD_QTUI
157     // put core-only arguments here
158     cliParser->addOption("listen", 0, "The address(es) quasselcore will listen on", "<address>[,<address>[,...]]", "::,0.0.0.0");
159     cliParser->addOption("port", 'p', "The port quasselcore will listen at", "port", "4242");
160     cliParser->addSwitch("norestore", 'n', "Don't restore last core's state");
161     cliParser->addOption("loglevel", 'L', "Loglevel Debug|Info|Warning|Error", "level", "Info");
162 #ifdef HAVE_SYSLOG
163     cliParser->addSwitch("syslog", 0, "Log to syslog");
164 #endif
165     cliParser->addOption("logfile", 'l', "Log to a file", "path");
166     cliParser->addOption("select-backend", 0, "Switch storage backend (migrating data if possible)", "backendidentifier");
167     cliParser->addOption("select-authenticator", 0, "Select authentication backend", "authidentifier");
168     cliParser->addSwitch("add-user", 0, "Starts an interactive session to add a new core user");
169     cliParser->addOption("change-userpass", 0, "Starts an interactive session to change the password of the user identified by <username>", "username");
170     cliParser->addSwitch("oidentd", 0, "Enable oidentd integration");
171     cliParser->addOption("oidentd-conffile", 0, "Set path to oidentd configuration file", "file");
172 #ifdef HAVE_SSL
173     cliParser->addSwitch("require-ssl", 0, "Require SSL for remote (non-loopback) client connections");
174     cliParser->addOption("ssl-cert", 0, "Specify the path to the SSL Certificate", "path", "configdir/quasselCert.pem");
175     cliParser->addOption("ssl-key", 0, "Specify the path to the SSL key", "path", "ssl-cert-path");
176 #endif
177     cliParser->addSwitch("enable-experimental-dcc", 0, "Enable highly experimental and unfinished support for CTCP DCC (DANGEROUS)");
178 #endif
179
180 #ifdef HAVE_KDE4
181     // the KDE version needs this extra call to parse argc/argv before app is instantiated
182     if (!cliParser->init()) {
183         cliParser->usage();
184         return EXIT_FAILURE;
185     }
186 #endif
187
188 #if defined BUILD_CORE
189     CoreApplication app(argc, argv);
190 #elif defined BUILD_QTUI
191 # if QT_VERSION >= 0x050600 && defined(Q_OS_WIN)
192     QtUiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
193 #endif
194 # if QT_VERSION >= 0x050700
195     QtUiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
196 # endif
197     QtUiApplication app(argc, argv);
198 #elif defined BUILD_MONO
199 # if QT_VERSION >= 0x050600 && defined(Q_OS_WIN)
200     MonolithicApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
201 #endif
202 # if QT_VERSION >= 0x050700
203     MonolithicApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
204 # endif
205     MonolithicApplication app(argc, argv);
206 #endif
207
208 #ifndef HAVE_KDE4
209     // the non-KDE version parses after app has been instantiated
210     if (!cliParser->init(app.arguments())) {
211         cliParser->usage();
212         return EXIT_FAILURE;
213     }
214 #endif
215
216 // Migrate settings from KDE4 to KF5 if appropriate
217 #ifdef HAVE_KF5
218     Kdelibs4ConfigMigrator migrator(QCoreApplication::applicationName());
219     migrator.setConfigFiles(QStringList() << "quasselrc" << "quassel.notifyrc");
220     migrator.migrate();
221 #endif
222
223 #ifdef HAVE_KF5
224     // FIXME: This should be done after loading the translation catalogue, but still in main()
225     AboutData aboutData;
226     AboutData::setQuasselPersons(&aboutData);
227     KAboutData::setApplicationData(aboutData.kAboutData());
228 #endif
229
230     if (!app.init())
231         return EXIT_FAILURE;
232
233     return app.exec();
234 }