Debian Codegraph @

Dekko/app/Dekko.cpp

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