Add support for Qt5's native command line parser
[quassel.git] / src / common / main.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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
23 #ifdef BUILD_CORE
24 #  include "coreapplication.h"
25 #elif defined BUILD_QTUI
26 #  include "qtuiapplication.h"
27 #elif defined BUILD_MONO
28 #  include "monoapplication.h"
29
30 #else
31 #error "Something is wrong - you need to #define a build mode!"
32 #endif
33
34 // We don't want quasselcore to depend on KDE
35 #if defined HAVE_KDE4 && defined BUILD_CORE
36 #  undef HAVE_KDE4
37 #endif
38
39 #ifdef HAVE_KDE4
40 #  include <KAboutData>
41 #  include "kcmdlinewrapper.h"
42 #elif defined HAVE_QT5
43 #  include "qt5cliparser.h"
44 #else
45 #  include "cliparser.h"
46 #endif
47
48 #if !defined(BUILD_CORE) && defined(STATIC)
49 #include <QtPlugin>
50 Q_IMPORT_PLUGIN(qjpeg)
51 Q_IMPORT_PLUGIN(qgif)
52 #endif
53
54 #include "quassel.h"
55
56 int main(int argc, char **argv)
57 {
58     Quassel::setupBuildInfo();
59     QCoreApplication::setApplicationName(Quassel::buildInfo().applicationName);
60     QCoreApplication::setApplicationVersion(Quassel::buildInfo().plainVersionString);
61     QCoreApplication::setOrganizationName(Quassel::buildInfo().organizationName);
62     QCoreApplication::setOrganizationDomain(Quassel::buildInfo().organizationDomain);
63
64     // on OSX with Qt4, raster seems to fix performance issues
65 #if QT_VERSION < 0x050000 && defined Q_OS_MAC && !defined BUILD_CORE
66     QApplication::setGraphicsSystem("raster");
67 #endif
68
69     // We need to explicitly initialize the required resources when linking statically
70 #ifndef BUILD_QTUI
71     Q_INIT_RESOURCE(sql);
72 #endif
73 #ifndef BUILD_CORE
74     Q_INIT_RESOURCE(pics);
75     Q_INIT_RESOURCE(hicolor);
76 #endif
77
78 #ifdef EMBED_DATA
79     Q_INIT_RESOURCE(i18n);
80 # ifndef BUILD_CORE
81     Q_INIT_RESOURCE(data);
82 #   ifdef WITH_OXYGEN
83     Q_INIT_RESOURCE(oxygen);
84 #   endif
85 # endif
86 #endif
87
88     AbstractCliParser *cliParser;
89
90 #ifdef HAVE_KDE4
91     // We need to init KCmdLineArgs first
92     // TODO: build an AboutData compat class to replace our aboutDlg strings
93     KAboutData aboutData("quassel", "kdelibs4", ki18n("Quassel IRC"), Quassel::buildInfo().plainVersionString.toUtf8(),
94         ki18n("A modern, distributed IRC client"));
95     aboutData.addLicense(KAboutData::License_GPL_V2);
96     aboutData.addLicense(KAboutData::License_GPL_V3);
97     aboutData.setBugAddress("http://bugs.quassel-irc.org/projects/quassel-irc/issues/new");
98     aboutData.setOrganizationDomain(Quassel::buildInfo().organizationDomain.toUtf8());
99     KCmdLineArgs::init(argc, argv, &aboutData);
100
101     cliParser = new KCmdLineWrapper();
102 #elif defined HAVE_QT5
103     cliParser = new Qt5CliParser();
104 #else
105     cliParser = new CliParser();
106 #endif
107     Quassel::setCliParser(cliParser);
108
109     // Initialize CLI arguments
110     // NOTE: We can't use tr() at this point, since app is not yet created
111
112     // put shared client&core arguments here
113     cliParser->addSwitch("debug", 'd', "Enable debug output");
114     cliParser->addSwitch("help", 'h', "Display this help and exit");
115     cliParser->addSwitch("version", 'v', "Display version information");
116 #ifdef BUILD_QTUI
117     cliParser->addOption("configdir", 'c', "Specify the directory holding the client configuration", "path");
118 #else
119     cliParser->addOption("configdir", 'c', "Specify the directory holding configuration files, the SQlite database and the SSL certificate", "path");
120 #endif
121     cliParser->addOption("datadir", 0, "DEPRECATED - Use --configdir instead", "path");
122
123 #ifndef BUILD_CORE
124     // put client-only arguments here
125     cliParser->addOption("icontheme", 0, "Override the system icon theme ('oxygen' is recommended)", "theme");
126     cliParser->addOption("qss", 0, "Load a custom application stylesheet", "file.qss");
127     cliParser->addSwitch("debugbufferswitches", 0, "Enables debugging for bufferswitches");
128     cliParser->addSwitch("debugmodel", 0, "Enables debugging for models");
129     cliParser->addSwitch("hidewindow", 0, "Start the client minimized to the system tray");
130 #endif
131 #ifndef BUILD_QTUI
132     // put core-only arguments here
133     cliParser->addOption("listen", 0, "The address(es) quasselcore will listen on", "<address>[,<address>[,...]]", "::,0.0.0.0");
134     cliParser->addOption("port", 'p', "The port quasselcore will listen at", "port", "4242");
135     cliParser->addSwitch("norestore", 'n', "Don't restore last core's state");
136     cliParser->addOption("loglevel", 'L', "Loglevel Debug|Info|Warning|Error", "level", "Info");
137 #ifdef HAVE_SYSLOG
138     cliParser->addSwitch("syslog", 0, "Log to syslog");
139 #endif
140     cliParser->addOption("logfile", 'l', "Log to a file", "path");
141     cliParser->addOption("select-backend", 0, "Switch storage backend (migrating data if possible)", "backendidentifier");
142     cliParser->addSwitch("add-user", 0, "Starts an interactive session to add a new core user");
143     cliParser->addOption("change-userpass", 0, "Starts an interactive session to change the password of the user identified by <username>", "username");
144     cliParser->addSwitch("oidentd", 0, "Enable oidentd integration");
145     cliParser->addOption("oidentd-conffile", 0, "Set path to oidentd configuration file", "file");
146 #ifdef HAVE_SSL
147     cliParser->addSwitch("require-ssl", 0, "Require SSL for client connections");
148 #endif
149     cliParser->addSwitch("enable-experimental-dcc", 0, "Enable highly experimental and unfinished support for CTCP DCC (DANGEROUS)");
150 #endif
151
152 #ifdef HAVE_KDE4
153     // the KDE version needs this extra call to parse argc/argv before app is instantiated
154     if (!cliParser->init()) {
155         cliParser->usage();
156         return EXIT_FAILURE;
157     }
158 #endif
159
160 #  if defined BUILD_CORE
161     CoreApplication app(argc, argv);
162 #  elif defined BUILD_QTUI
163     QtUiApplication app(argc, argv);
164 #  elif defined BUILD_MONO
165     MonolithicApplication app(argc, argv);
166 #  endif
167
168 #ifndef HAVE_KDE4
169     // the non-KDE version parses after app has been instantiated
170     if (!cliParser->init(app.arguments())) {
171         cliParser->usage();
172         return false;
173     }
174 #endif
175
176     if (!app.init()) return EXIT_FAILURE;
177     return app.exec();
178 }