Debian Codegraph @

Dekko/app/Dekko.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]>
2
3 This file is part of Dekko email client for Ubuntu devices
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License or (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, see <http://www.gnu.org/licenses/>.
17 */
18 #include "Dekko.h"
19 #include <QDebug>
20 #include <qmailnamespace.h>
21 #include <qmaillog.h>
22 #include <QUrl>
23 #include <QDirIterator>
24 #include <QtQuick/QQuickView>
25 #include <QQmlComponent>
26 #include <QQmlContext>
27 #include <QQuickView>
28 #include <QQmlEngine>
29 #include <QCommandLineOption>
30 #include <QDir>
31 #include <QtWebEngine>
32 #include <SnapStandardPaths.h>
33 #include <PluginRegistry.h>
34 #include <PlugManTypes.h>
35
36 #define SMALL_FF_WIDTH 350
37 #define MEDIUM_FF_WDTH 800
38 #define LARGE_FF_WIDTH 1100
39
40 Q_LOGGING_CATEGORY(DEKKO_MAIN, "dekko.main")
41
42 Dekko::Dekko(int &argc, char **argv) :
43 QApplication(argc, argv),
44 m_serviceRegistry(0),
45 #ifdef SERVER_AS_QTHREAD
46 m_serverThread(0),
47 #else
48 m_server(0),
49 m_worker(0),
50 #endif
51 devMode(false),
52 m_verboseLogging(false)
53 {
54 QCoreApplication::setOrganizationName(APP_ORG);
55 QCoreApplication::setApplicationName(APP_NAME);
56 QCoreApplication::setApplicationVersion(DEKKO_VERSION);
57
58 QtWebEngine::initialize();
59 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
60
61 // Uncomment to dump out the resource files
62 // Useful to be able to check a resource has been included
63 // QDirIterator it(":", QDirIterator::Subdirectories);
64 // while (it.hasNext()) {
65 // qCDebug(DEKKO_MAIN) << it.next();
66 // }
67 parser.setApplicationDescription("Dekko email client");
68 parser.addHelpOption();
69 parser.addOption({"d", "Enable dev mode"});
70 parser.addOption({"v", "Verbose logging"});
71 parser.addOption({"small", "Open in small form factor state"});
72 parser.addOption({"medium", "Open in medium form factor state"});
73 parser.addOption({"large", "Open in large form factor state"});
74 parser.addOption({"contacts", "Open the addressbook"});
75
76 QStringList cArgs = this->arguments();
77
78 for (const QString &s : cArgs) {
79 if (s.startsWith("--desktop_file_hint=")) {
80 cArgs.removeAll(s);
81 }
82 }
83
84 parser.process(cArgs);
85
86 m_verboseLogging = (parser.isSet("d") || parser.isSet("v") || QFile::exists(QStringLiteral("/tmp/dekko-debug")));
87
88 if(!m_verboseLogging)
89 QLoggingCategory::setFilterRules("dekko.*=false");
90 else
91 QLoggingCategory::setFilterRules("dekko.*=true");
92
93 }
94
95 Dekko::~Dekko(){
96 #ifndef SERVER_AS_QTHREAD
97 delete m_server;
98 m_server = 0;
99 delete m_worker;
100 m_worker = 0;
101 #endif
102 }
103
104 bool Dekko::setup()
105 {
106 QStringList arguments = this->arguments();
107 Q_UNUSED(arguments);
108
109 if (!QMail::mkLockDir()) {
110 return false;
111 }
112
113 if (qgetenv("QMF_DATA").isEmpty()) {
114 // Fall back to standard xdg cache location
115 qputenv("QMF_DATA", SnapStandardPaths::writableLocation(SnapStandardPaths::AppCacheLocation).toUtf8());
116 }
117
118 registerPlugManQmlTypes();
119
120 loadPlugins();
121 m_serviceRegistry = new ServiceRegistry(this);
122 m_serviceRegistry->setServiceKey(QStringLiteral("Dekko::Service"));
123
124 #if defined(CLICK_MODE)
125 m_serviceRegistry->startServices();
126 #else
127 if (!isServerRunning()) {
128 qCDebug(DEKKO_MAIN) << "[Dekko]" << "Message server not running attempting to start";
129 if (!startServer()) {
130 qCDebug(DEKKO_MAIN) << "[Dekko]" << "Message server failed to start";
131 return false;
132 } else {
133 qCDebug(DEKKO_MAIN) << "[Dekko]" << "Message server started successfully \\o/";
134 }
135 } else {
136 qCDebug(DEKKO_MAIN) << "[Dekko]" << "Message server already running, using that";
137 }
138 m_serviceRegistry->startServices();
139 #endif
140
141 if (!isWorkerRunning()) {
142 qCDebug(DEKKO_MAIN) << "[Dekko]" << "Message worker not running attempting to start";
143 if (!startWorker()) {
144 qCDebug(DEKKO_MAIN) << "[Dekko]" << "Message worker failed to start";
145 return false;
146 } else {
147 qCDebug(DEKKO_MAIN) << "[Dekko]" << "Message worker started successfully \\o/";
148 }
149 } else {
150 qCDebug(DEKKO_MAIN) << "[Dekko]" << "Message worker already running, using that";
151 }
152 m_engine.setNetworkAccessManagerFactory(&m_partqnam);
153
154 devMode = parser.isSet("d");
155
156 m_engine.rootContext()->setContextProperty("dekkoapp", this);
157 m_engine.rootContext()->setContextProperty("service", m_serviceRegistry);
158 m_engine.rootContext()->setContextProperty("devModeEnabled", QVariant(devMode));
159 m_engine.rootContext()->setContextProperty("verboseLogging", QVariant(m_verboseLogging));
160 // Context property to figure out if we are on unity8/mir or not
161 m_engine.rootContext()->setContextProperty(QStringLiteral("isRunningOnMir"), QVariant(qgetenv("QT_QPA_PLATFORM").contains("ubuntumirclient")));
162 // Check both APP_URIS and positionalArgs for uris we need to handle
163 QStringList uris;
164 uris << qgetenv("APP_URIS");
165 uris << parser.positionalArguments();
166 if (parser.isSet("contacts")) {
167 uris << QStringLiteral("dekko://contacts/open");
168 }
169 uris.removeDuplicates();
170 m_engine.rootContext()->setContextProperty(QStringLiteral("appUris"), uris);
171 m_engine.load(QUrl("qrc:/qml/Dekko.qml"));
172 return true;
173 }
174
175 bool Dekko::isServerRunning()
176 {
177 int lockid = QMail::fileLock(QStringLiteral("dekkod-instance.lock"));
178 if (lockid == -1)
179 return true;
180
181 QMail::fileUnlock(lockid);
182 return false;
183 }
184
185 bool Dekko::startServer()
186 {
187 #ifdef SERVER_AS_QTHREAD
188 // Use MessageServerThread
189 m_serverThread = new MessageServerThread();
190 m_serverThread->start();
191 QEventLoop loop;
192 QObject::connect(m_serverThread, &MessageServerThread::messageServerStarted, &loop, &QEventLoop::quit);
193 loop.exec();
194 return true;
195 #else
196 if (m_server) {
197 delete m_server;
198 m_server = 0;
199 }
200 m_server = new QProcess(this);
201 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
202 m_server->setProcessEnvironment(env);
203 m_server->setProcessChannelMode(QProcess::MergedChannels);
204 static const QString binary(QString("/dekkod"));
205 connect(m_server,SIGNAL(error(QProcess::ProcessError)),
206 this,SLOT(serverProcessError(QProcess::ProcessError)));
207 connect(m_server, &QProcess::readyRead, [=](){ if (m_server->canReadLine()) qCDebug(DEKKO_MAIN) << "dekkod:" << m_server->readLine(); });
208 m_server->start(QMail::messageServerPath() + binary);
209 return m_server->waitForStarted();
210 #endif
211 }
212
213 bool Dekko::isWorkerRunning()
214 {
215 int lockid = QMail::fileLock(QStringLiteral("dekko-worker.lock"));
216 if (lockid == -1)
217 return true;
218
219 QMail::fileUnlock(lockid);
220 return false;
221 }
222
223 bool Dekko::startWorker()
224 {
225 if (m_worker) {
226 delete m_worker;
227 m_worker = 0;
228 }
229 m_worker = new QProcess(this);
230 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
231 m_worker->setProcessEnvironment(env);
232 m_worker->setProcessChannelMode(QProcess::MergedChannels);
233 static const QString binary(QString("/dekko-worker"));
234 connect(m_worker,SIGNAL(error(QProcess::ProcessError)),
235 this,SLOT(workerProcessError(QProcess::ProcessError)));
236 connect(m_worker, &QProcess::readyRead, [=](){ if (m_worker->canReadLine()) qCDebug(DEKKO_MAIN) << "dekko-worker:" << m_worker->readLine(); });
237 m_worker->start(QMail::messageServerPath() + binary);
238 return m_worker->waitForStarted();
239 }
240
241 void Dekko::trimCache()
242 {
243 m_engine.trimComponentCache();
244 }
245 // TODO: show popup in mainview about server vanishing and Dekko will now close.
246 void Dekko::serverProcessError(QProcess::ProcessError error)
247 {
248 qDebug() << "[Error] " << error;
249 qWarning() << "[ERROR] Message server stopped, trying to restart";
250 delete m_server;
251 m_server = 0;
252 startServer();
253 }
254
255 void Dekko::workerProcessError(QProcess::ProcessError error)
256 {
257 qDebug() << "[Error] " << error;
258 qWarning() << "[ERROR] Message worker stopped, trying to restart";
259 delete m_worker;
260 m_worker = 0;
261 startWorker();
262 }
263
264 void Dekko::loadPlugins()
265 {
266 PluginRegistry::instance()->setPluginLocations(
267 QStringList()
268 << QStringLiteral("Dekko::Mail")
269 << QStringLiteral("Dekko::Mail::Composer")
270 << QStringLiteral("Dekko::Mail::Composer::UI")
271 << QStringLiteral("Dekko::Mail::Settings")
272 << QStringLiteral("Dekko::Mail::SetupWizard")
273 << QStringLiteral("Dekko::Stage::Settings")
274 << QStringLiteral("Dekko::Settings::AccountAction")
275 << QStringLiteral("Dekko::Settings::PluginAction")
276 << QStringLiteral("Dekko::Settings::MailAction")
277 << QStringLiteral("Dekko::Stage::Main")
278 << QStringLiteral("Dekko::Stage::Mail")
279 << QStringLiteral("Dekko::Stage::Action")
280 << QStringLiteral("Dekko::Mail::NavHeadAction")
281 << QStringLiteral("Dekko::Service")
282 << QStringLiteral("Dekko::Listener")
283 << QStringLiteral("Dekko::AddressBook::Provider")
284 << QStringLiteral("Dekko::Mail::MultiSelectAction")
285 << QStringLiteral("Dekko::Mail::FolderPicker")
286 );
287
288 PluginRegistry::instance()->loadPlugins(
289 QStringList()
290 << QStringLiteral("%1/plugins").arg(SnapStandardPaths::writableLocation(SnapStandardPaths::AppDataLocation))
291 << DEKKO_PLUGINS_DIR
292 );
293 }