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