clazy: fix QString(QLatin1String(...))
[trojita.git] / src / Gui / main.cpp
blob03f565a4cf9293109bf903588e363e4c3de82b67
1 /* Copyright (C) 2006 - 2015 Jan Kundrát <jkt@flaska.net>
2 Copyright (C) 2013 - 2014 Pali Rohár <pali.rohar@gmail.com>
4 This file is part of the Trojita Qt IMAP e-mail client,
5 http://trojita.flaska.net/
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of
10 the License or (at your option) version 3 or any later version
11 accepted by the membership of KDE e.V. (or its successor approved
12 by the membership of KDE e.V.), which shall act as a proxy
13 defined in Section 14 of version 3 of the license.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <QApplication>
24 #include <QFile>
25 #include <QLibraryInfo>
26 #include <QObject>
27 #include <QSettings>
28 #include <QTextStream>
29 #include <QTranslator>
31 #include "AppVersion/SetCoreApplication.h"
32 #include "Common/Application.h"
33 #include "Common/MetaTypes.h"
34 #include "Common/SettingsNames.h"
35 #include "Gui/Util.h"
36 #include "Gui/Window.h"
37 #include "IPC/IPC.h"
39 #include "static_plugins.h"
41 int main(int argc, char **argv)
43 Common::registerMetaTypes();
45 QApplication app(argc, argv);
47 Q_INIT_RESOURCE(icons);
48 Q_INIT_RESOURCE(license);
50 QTranslator qtTranslator;
51 qtTranslator.load(QLatin1String("qt_") + QLocale::system().name(),
52 QLibraryInfo::location(QLibraryInfo::TranslationsPath));
53 app.installTranslator(&qtTranslator);
55 QLatin1String localeSuffix("/locale");
56 QString localeName(QLatin1String("trojita_common_") +
57 (qgetenv("KDE_LANG") == "x-test" ? QStringLiteral("x_test") : QLocale::system().name()));
59 // The "installed to system" localization
60 QTranslator appSystemTranslator;
61 if (!Gui::Util::pkgDataDir().isEmpty()) {
62 appSystemTranslator.load(localeName, Gui::Util::pkgDataDir() + localeSuffix);
63 app.installTranslator(&appSystemTranslator);
66 // The "in the directory with the binary" localization
67 QTranslator appDirectoryTranslator;
68 appDirectoryTranslator.load(localeName, app.applicationDirPath() + localeSuffix);
69 app.installTranslator(&appDirectoryTranslator);
71 AppVersion::setGitVersion();
72 AppVersion::setCoreApplicationData();
74 app.setWindowIcon(QIcon(QStringLiteral(":/icons/trojita.png")));
76 QTextStream qOut(stdout, QIODevice::WriteOnly);
77 QTextStream qErr(stderr, QIODevice::WriteOnly);
79 const QStringList &arguments = app.arguments();
81 bool error = false;
82 bool showHelp = false;
83 bool showMainWindow = false;
84 bool showComposeWindow = false;
85 bool showAddressbookWindow = false;
86 bool logToDisk = false;
88 QString profileName;
90 QString url;
92 for (int i = 1; i < arguments.size(); ++i) {
93 const QString &arg = arguments.at(i);
95 if (arg.startsWith(QLatin1Char('-'))) {
96 if (arg == QLatin1String("-m") || arg == QLatin1String("--mainwindow")) {
97 showMainWindow = true;
98 } else if (arg == QLatin1String("-a") || arg == QLatin1String("--addressbook")) {
99 showAddressbookWindow = true;
100 } else if (arg == QLatin1String("-c") || arg == QLatin1String("--compose")) {
101 showComposeWindow = true;
102 } else if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
103 showHelp = true;
104 } else if (arg == QLatin1String("-p") || arg == QLatin1String("--profile")) {
105 if (i+1 == arguments.size() || arguments.at(i+1).startsWith(QLatin1Char('-'))) {
106 qErr << QObject::tr("Error: Profile was not specified") << endl;
107 error = true;
108 break;
109 } else if (!profileName.isEmpty()) {
110 qErr << QObject::tr("Error: Duplicate profile option '%1'").arg(arg) << endl;
111 error = true;
112 break;
113 } else {
114 profileName = arguments.at(i+1);
115 ++i;
117 } else if (arg == QLatin1String("--log-to-disk")) {
118 logToDisk = true;
119 } else {
120 qErr << QObject::tr("Warning: Unknown option '%1'").arg(arg) << endl;
122 } else {
123 if (!url.isEmpty() || !arg.startsWith(QLatin1String("mailto:"))) {
124 qErr << QObject::tr("Warning: Unexpected argument '%1'").arg(arg) << endl;
125 } else {
126 url = arg;
127 showComposeWindow = true;
132 if (!showMainWindow && !showComposeWindow && !showAddressbookWindow)
133 showMainWindow = true;
135 if (error)
136 showHelp = true;
138 if (showHelp) {
139 qOut << endl << QObject::trUtf8(
140 "Usage: %1 [options] [url]\n"
141 "\n"
142 "Trojitá %2 - fast Qt IMAP e-mail client\n"
143 "\n"
144 "Options:\n"
145 " -h, --help Show this help\n"
146 " -m, --mainwindow Show main window (default when no option is provided)\n"
147 " -a, --addressbook Show addressbook window\n"
148 " -c, --compose Compose new email (default when url is provided)\n"
149 " -p, --profile <profile> Set profile (cannot start with char '-')\n"
150 " --log-to-disk Activate debug traffic logging to disk by default\n"
151 "\n"
152 "Arguments:\n"
153 " url Mailto: url address for composing new email\n"
154 ).arg(arguments.at(0), Common::Application::version) << endl;
155 return error ? 1 : 0;
158 // Hack: support multiple "profiles"
159 if (!profileName.isEmpty()) {
160 // We are abusing the env vars here. Yes, it's a hidden global. Yes, it's ugly.
161 // Take it or leave it, this is a time-limited hack.
162 // The env var is also in UTF-8. I like UTF-8.
163 qputenv("TROJITA_PROFILE", profileName.toUtf8());
164 } else {
165 #ifndef Q_OS_WIN32
166 unsetenv("TROJITA_PROFILE");
167 #else
168 putenv("TROJITA_PROFILE=");
169 #endif
172 if (IPC::Instance::isRunning()) {
173 if (showMainWindow)
174 IPC::Instance::showMainWindow();
175 if (showAddressbookWindow)
176 IPC::Instance::showAddressbookWindow();
177 if (showComposeWindow)
178 IPC::Instance::composeMail(url);
179 return 0;
182 QSettings settings(Common::Application::organization,
183 profileName.isEmpty() ? Common::Application::name : Common::Application::name + QLatin1Char('-') + profileName);
184 Gui::MainWindow win(&settings);
186 QString errmsg;
187 if (!IPC::registerInstance(&win, errmsg))
188 qErr << QObject::tr("Error: Registering IPC instance failed: %1").arg(errmsg) << endl;
190 if ( settings.value(Common::SettingsNames::guiStartMinimized, QVariant(false)).toBool() ) {
191 if ( !settings.value(Common::SettingsNames::guiShowSystray, QVariant(true)).toBool() ) {
192 win.show();
193 win.setWindowState(Qt::WindowMinimized);
195 } else {
196 win.show();
199 if (showAddressbookWindow)
200 win.invokeContactEditor();
202 if (showComposeWindow) {
203 if (url.isEmpty())
204 win.slotComposeMail();
205 else
206 win.slotComposeMailUrl(QUrl::fromEncoded(url.toUtf8()));
209 if (logToDisk) {
210 win.enableLoggingToDisk();
213 return app.exec();