1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/*
* Copyright (C) 2012-2016 Canonical, Ltd.
*
* This file is part of messaging-app.
*
* messaging-app is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* messaging-app is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "messagingapplication.h"
#include "audiorecorder.h"
#include "fileoperations.h"
#include "stickers-history-model.h"
#include <libnotify/notify.h>
#include <QDir>
#include <QUrl>
#include <QUrlQuery>
#include <QDebug>
#include <QDir>
#include <QStringList>
#include <QQuickItem>
#include <QQmlComponent>
#include <QQmlContext>
#include <QQuickView>
#include <QDBusInterface>
#include <QDBusReply>
#include <QDBusConnectionInterface>
#include <QLibrary>
#include "config.h"
#include <QQmlEngine>
#include <QMimeDatabase>
#include <QStandardPaths>
#include <QVersitReader>
#define MESSAGING_FULLSCREEN_FLAG 0x00800000
using namespace QtVersit;
#define Pair QPair<QString,QString>
namespace C {
#include <libintl.h>
}
static void printUsage(const QStringList& arguments)
{
qDebug() << "usage:"
<< arguments.at(0).toUtf8().constData()
<< "[message:///PHONE_NUMBER]"
<< "[--fullscreen]"
<< "[--help]"
<< "[-testability]";
}
//this is necessary to work on desktop
//On desktop use: export MESSAGING_APP_ICON_THEME=ubuntu-mobile
static void installIconPath()
{
QByteArray iconTheme = qgetenv("MESSAGING_APP_ICON_THEME");
if (!iconTheme.isEmpty()) {
QIcon::setThemeName(iconTheme);
}
}
static QObject* FileOperations_singleton_factory(QQmlEngine* engine, QJSEngine* scriptEngine)
{
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
return new FileOperations();
}
static QObject* StickersHistoryModel_singleton_factory(QQmlEngine* engine, QJSEngine* scriptEngine)
{
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
return new StickersHistoryModel();
}
MessagingApplication::MessagingApplication(int &argc, char **argv)
: QGuiApplication(argc, argv), m_view(0), m_applicationIsReady(false)
{
setApplicationName("MessagingApp");
}
bool MessagingApplication::fullscreen() const
{
// FIXME: Correct flag to be used will be defined in future releases
return m_view->flags() & static_cast <Qt::WindowFlags> (MESSAGING_FULLSCREEN_FLAG);
}
bool MessagingApplication::setup()
{
installIconPath();
static QList<QString> validSchemes;
bool fullScreen = false;
if (validSchemes.isEmpty()) {
validSchemes << "message";
}
QStringList arguments = this->arguments();
if (arguments.contains("--help")) {
printUsage(arguments);
return false;
}
if (arguments.contains("--fullscreen")) {
arguments.removeAll("--fullscreen");
fullScreen = true;
}
// The testability driver is only loaded by QApplication but not by QGuiApplication.
// However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
if (arguments.contains("-testability") || qgetenv("QT_LOAD_TESTABILITY") == "1") {
arguments.removeAll("-testability");
QLibrary testLib(QLatin1String("qttestability"));
if (testLib.load()) {
typedef void (*TasInitialize)(void);
TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
if (initFunction) {
initFunction();
} else {
qCritical("Library qttestability resolve failed!");
}
} else {
qCritical("Library qttestability load failed!");
}
}
/* Ubuntu APP Manager gathers info on the list of running applications from the .desktop
file specified on the command line with the desktop_file_hint switch, and will also pass a stage hint
So app will be launched like this:
/usr/bin/messaging-app --desktop_file_hint=/usr/share/applications/messaging-app.desktop
--stage_hint=main_stage
So remove whatever --arg still there before continue parsing
*/
for (int i = arguments.count() - 1; i >=0; --i) {
if (arguments[i].startsWith("--")) {
arguments.removeAt(i);
}
}
if (arguments.size() == 2) {
QUrl uri(arguments.at(1));
if (validSchemes.contains(uri.scheme())) {
m_arg = arguments.at(1);
}
}
m_view = new QQuickView();
QObject::connect(m_view, SIGNAL(statusChanged(QQuickView::Status)), this, SLOT(onViewStatusChanged(QQuickView::Status)));
QObject::connect(m_view->engine(), SIGNAL(quit()), SLOT(quit()));
m_view->setResizeMode(QQuickView::SizeRootObjectToView);
m_view->setTitle("Messaging");
m_view->rootContext()->setContextProperty("application", this);
m_view->rootContext()->setContextProperty("i18nDirectory", I18N_DIRECTORY);
m_view->rootContext()->setContextProperty("view", m_view);
m_view->engine()->setBaseUrl(QUrl::fromLocalFile(messagingAppDirectory()));
m_view->engine()->addImportPath(UNITY8_QML_PATH);
// check if there is a contacts backend override
QString contactsBackend = qgetenv("QTCONTACTS_MANAGER_OVERRIDE");
if (!contactsBackend.isEmpty()) {
qDebug() << "Overriding the contacts backend, using:" << contactsBackend;
m_view->rootContext()->setContextProperty("QTCONTACTS_MANAGER_OVERRIDE", contactsBackend);
}
QDir dataLocation(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
m_view->rootContext()->setContextProperty("dataLocation", dataLocation.absolutePath());
dataLocation.mkpath("stickers");
const char* uri = "messagingapp.private";
qmlRegisterType<AudioRecorder>(uri, 0, 1, "AudioRecorder");
qmlRegisterSingletonType<FileOperations>(uri, 0, 1, "FileOperations", FileOperations_singleton_factory);
qmlRegisterSingletonType<StickersHistoryModel>(uri, 0, 1, "StickersHistoryModel", StickersHistoryModel_singleton_factory);
// used by autopilot tests to load vcards during tests
QByteArray testData = qgetenv("QTCONTACTS_PRELOAD_VCARD");
m_view->rootContext()->setContextProperty("QTCONTACTS_PRELOAD_VCARD", testData);
QString pluginPath = ubuntuPhonePluginPath();
if (!pluginPath.isNull()) {
m_view->engine()->addImportPath(pluginPath);
}
m_view->setSource(QUrl::fromLocalFile("messaging-app.qml"));
if (fullScreen) {
m_view->showFullScreen();
} else {
m_view->show();
}
notify_init(C::gettext("Messaging application"));
return true;
}
MessagingApplication::~MessagingApplication()
{
if (m_view) {
delete m_view;
}
}
void MessagingApplication::setFullscreen(bool fullscreen)
{
// FIXME: Correct flag to be used will be defined in future releases
if (fullscreen) {
m_view->setFlags(m_view->flags() | static_cast <Qt::WindowFlags> (MESSAGING_FULLSCREEN_FLAG));
} else {
m_view->setFlags(m_view->flags() & !static_cast <Qt::WindowFlags> (MESSAGING_FULLSCREEN_FLAG));
}
Q_EMIT fullscreenChanged();
}
void MessagingApplication::onViewStatusChanged(QQuickView::Status status)
{
if (status != QQuickView::Ready) {
return;
}
onApplicationReady();
}
void MessagingApplication::onApplicationReady()
{
m_applicationIsReady = true;
parseArgument(m_arg);
m_arg.clear();
}
void MessagingApplication::parseArgument(const QString &arg)
{
if (arg.isEmpty()) {
return;
}
QVariantMap properties;
QUrl url(arg);
QString scheme = url.scheme();
QString value = url.path();
// Remove the first "/" if needed. We have two possible scenarios:
// message:///phonenumber and message:phonenumber
if (value.startsWith("/")) {
value = value.right(value.length()-1);
if (!value.isEmpty()) {
QStringList participantIds = value.split(";");
properties["participantIds"] = participantIds;
}
}
QUrlQuery query(url);
Q_FOREACH(const Pair &item, query.queryItems(QUrl::FullyDecoded)) {
if (item.first == "text") {
properties["text"] = item.second;
}
if (item.first == "accountId") {
properties["accountId"] = item.second;
}
if (item.first == "chatType") {
properties["chatType"] = item.second.toInt();
}
if (item.first == "threadId") {
properties["threadId"] = item.second;
}
}
QQuickItem *mainView = m_view->rootObject();
if (!mainView) {
return;
}
if (scheme == "message") {
if (value.isEmpty() && properties.isEmpty()) {
QMetaObject::invokeMethod(mainView, "startNewMessage");
} else {
QMetaObject::invokeMethod(mainView, "startChat", Q_ARG(QVariant, properties));
}
}
}
void MessagingApplication::activateWindow()
{
if (m_view) {
m_view->raise();
m_view->requestActivate();
}
}
QString MessagingApplication::readTextFile(const QString &fileName) {
QString text;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
return QString();
}
text = QString(file.readAll());
file.close();
return text;
}
QString MessagingApplication::fileMimeType(const QString &fileName) {
QMimeDatabase db;
QMimeType type = db.mimeTypeForFile(fileName);
return type.name();
}
void MessagingApplication::showNotificationMessage(const QString &message, const QString &icon)
{
NotifyNotification *notification = notify_notification_new(message.toStdString().c_str(),
NULL,
icon.toStdString().c_str());
notify_notification_set_urgency(notification, NOTIFY_URGENCY_LOW);
GError *error = NULL;
if (!notify_notification_show(notification, &error)) {
qWarning() << "Failed to show notification:" << error->message;
g_error_free (error);
}
g_object_unref(G_OBJECT(notification));
}
// find QQuickItem childs
inline QObject *findRecursiveChild(QQuickItem *object, const QString &objectName, const QString &property = QString::null, const QVariant &value = QVariant())
{
// check the object
if (!object) {
return NULL;
}
// check the direct children first
Q_FOREACH(QQuickItem *child, object->childItems()) {
if (child->objectName() == objectName) {
if (property.isEmpty()) {
return child;
} else if (child->property(property.toLatin1().data()) == value) {
return child;
}
}
}
// now check the grand-children
Q_FOREACH(QQuickItem *child, object->childItems()) {
QObject *result = findRecursiveChild(child, objectName, property, value);
if (result) {
return result;
}
}
return NULL;
}
QObject *MessagingApplication::findMessagingChild(const QString &objectName)
{
return findRecursiveChild(m_view->rootObject(), objectName);
}
QObject *MessagingApplication::findMessagingChild(const QString &objectName, const QString &property, const QVariant &value)
{
return findRecursiveChild(m_view->rootObject(), objectName, property, value);
}
|