Merge "persistent color scheme selection"
[trojita.git] / src / Gui / main.cpp
blob851db4f1fb0cd13a6a49a67b55660e4afe89d460
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 <QCommandLineOption>
25 #include <QCommandLineParser>
26 #include <QFile>
27 #include <QLibraryInfo>
28 #include <QObject>
29 #include <QSettings>
30 #include <QTextStream>
31 #include <QTranslator>
33 #include "AppVersion/SetCoreApplication.h"
34 #include "Common/Application.h"
35 #include "Common/MetaTypes.h"
36 #include "Common/SettingsNames.h"
37 #include "Gui/Util.h"
38 #include "Gui/Window.h"
39 #include "IPC/IPC.h"
40 #include "UiUtils/IconLoader.h"
42 #include "static_plugins.h"
44 int main(int argc, char **argv)
46 Common::registerMetaTypes();
48 QApplication app(argc, argv);
50 Q_INIT_RESOURCE(icons);
51 Q_INIT_RESOURCE(license);
53 QTranslator qtTranslator;
54 qtTranslator.load(QLatin1String("qt_") + QLocale::system().name(),
55 QLibraryInfo::location(QLibraryInfo::TranslationsPath));
56 app.installTranslator(&qtTranslator);
58 QLatin1String localeSuffix("/locale");
59 QString localeName(QLatin1String("trojita_common_") +
60 (qgetenv("KDE_LANG") == "x-test" ? QStringLiteral("x_test") : QLocale::system().name()));
62 // The "installed to system" localization
63 QTranslator appSystemTranslator;
64 if (!Gui::Util::pkgDataDir().isEmpty()) {
65 appSystemTranslator.load(localeName, Gui::Util::pkgDataDir() + localeSuffix);
66 app.installTranslator(&appSystemTranslator);
69 // The "in the directory with the binary" localization
70 QTranslator appDirectoryTranslator;
71 appDirectoryTranslator.load(localeName, app.applicationDirPath() + localeSuffix);
72 app.installTranslator(&appDirectoryTranslator);
74 AppVersion::setGitVersion();
75 AppVersion::setCoreApplicationData();
77 app.setAttribute(Qt::AA_UseHighDpiPixmaps);
78 #if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
79 app.setDesktopFileName(QStringLiteral("org.kde.trojita"));
80 #endif
81 app.setWindowIcon(UiUtils::loadIcon(QStringLiteral("trojita")));
83 QCommandLineParser parser;
84 parser.setApplicationDescription(QObject::tr("Trojitá %1 - fast Qt IMAP e-mail client").arg(Common::Application::version));
85 parser.addHelpOption();
86 parser.addVersionOption();
88 QCommandLineOption addressbookOption(QStringList() << QLatin1String("a") << QLatin1String("addressbook"),
89 QObject::tr("Show addressbook window."));
90 parser.addOption(addressbookOption);
92 QCommandLineOption composeOption(QStringList() << QLatin1String("c") << QLatin1String("compose"),
93 QObject::tr("Compose new e-mail (default when url is provided)."));
94 parser.addOption(composeOption);
96 QCommandLineOption logtodiskOption(QStringList() << QLatin1String("l") << QLatin1String("log-to-disk"),
97 QObject::tr("Activate debug traffic logging to disk by default."));
98 parser.addOption(logtodiskOption);
100 QCommandLineOption mainwindowOption(QStringList() << QLatin1String("m") << QLatin1String("mainwindow"),
101 QObject::tr("Show main window (default when no option is provided)."));
102 parser.addOption(mainwindowOption);
104 QCommandLineOption profileOption(QStringList() << QLatin1String("p") << QLatin1String("profile"),
105 QObject::tr("Set profile."), QObject::tr("profile"));
106 parser.addOption(profileOption);
108 parser.addPositionalArgument(QLatin1String("url"), QObject::tr("Mailto: URL address for composing new e-mail."),
109 QObject::tr("[url]"));
111 parser.process(app);
113 QTextStream qOut(stdout, QIODevice::WriteOnly);
114 QTextStream qErr(stderr, QIODevice::WriteOnly);
116 bool error = false;
117 bool showMainWindow = false;
118 bool showComposeWindow = false;
119 bool showAddressbookWindow = false;
120 QString profileName;
121 QString url;
123 showAddressbookWindow = parser.isSet(addressbookOption);
124 showComposeWindow = parser.isSet(composeOption);
125 showMainWindow = parser.isSet(mainwindowOption);
126 if (parser.isSet(profileOption))
127 profileName = parser.value(profileOption);
128 if (parser.positionalArguments().size() > 0) {
129 if (parser.positionalArguments().size() == 1) {
130 url = parser.positionalArguments().at(0);
131 if (!url.startsWith(QLatin1String("mailto:"))) {
132 qErr << QObject::tr("Unexpected argument '%1'.").arg(url) << endl;
133 error = true;
134 } else
135 showComposeWindow = true;
136 } else {
137 qErr << QObject::tr("Unexpected arguments.") << endl;
138 error = true;
142 if (!showMainWindow && !showComposeWindow && !showAddressbookWindow)
143 showMainWindow = true;
145 if (error) {
146 qOut << endl << parser.helpText();
147 return error ? 1 : 0;
150 // Hack: support multiple "profiles"
151 if (!profileName.isEmpty()) {
152 // We are abusing the env vars here. Yes, it's a hidden global. Yes, it's ugly.
153 // Take it or leave it, this is a time-limited hack.
154 // The env var is also in UTF-8. I like UTF-8.
155 qputenv("TROJITA_PROFILE", profileName.toUtf8());
156 } else {
157 #ifndef Q_OS_WIN32
158 unsetenv("TROJITA_PROFILE");
159 #else
160 putenv("TROJITA_PROFILE=");
161 #endif
164 if (IPC::Instance::isRunning()) {
165 if (showMainWindow)
166 IPC::Instance::showMainWindow();
167 if (showAddressbookWindow)
168 IPC::Instance::showAddressbookWindow();
169 if (showComposeWindow)
170 IPC::Instance::composeMail(url);
171 return 0;
174 QSettings settings(Common::Application::organization,
175 profileName.isEmpty() ? Common::Application::name : Common::Application::name + QLatin1Char('-') + profileName);
176 Gui::MainWindow win(&settings);
178 QString errmsg;
179 if (!IPC::registerInstance(&win, errmsg))
180 qErr << QObject::tr("Error: Registering IPC instance failed: %1").arg(errmsg) << endl;
182 if ( settings.value(Common::SettingsNames::guiStartMinimized, QVariant(false)).toBool() ) {
183 if ( !settings.value(Common::SettingsNames::guiShowSystray, QVariant(true)).toBool() ) {
184 win.show();
185 win.setWindowState(Qt::WindowMinimized);
187 } else {
188 win.show();
191 if (showAddressbookWindow)
192 win.invokeContactEditor();
194 if (showComposeWindow) {
195 if (url.isEmpty())
196 win.slotComposeMail();
197 else
198 win.slotComposeMailUrl(QUrl::fromEncoded(url.toUtf8()));
201 if (parser.isSet(logtodiskOption)) {
202 win.enableLoggingToDisk();
205 return app.exec();