~zeller-benjamin/dialer-app/applauncherd

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
/*
 * Copyright (C) 2012 Canonical, Ltd.
 *
 * This file is part of dialer-app.
 *
 * dialer-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.
 *
 * dialer-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 "dialerapplication.h"

#include <QDir>
#include <QUrl>
#include <QUrlQuery>
#include <QDebug>
#include <QStringList>
#include <QQuickItem>
#include <QQmlComponent>
#include <QQmlContext>
#include <QQuickView>
#include <QDBusInterface>
#include <QDBusReply>
#include <QDBusConnectionInterface>
#include <QLibrary>
#include "config.h"
#include <QQmlEngine>
#include <QDir>
#include <QGuiApplication>

#ifdef MAPPLAUNCHERD_ENABLED
#include <mdeclarativecache5/MDeclarativeCache>
#endif

static void printUsage(const QStringList& arguments)
{
    qDebug() << "usage:"
             << arguments.at(0).toUtf8().constData()
             << "[tel:[///]PHONE_NUMBER]"
             << "[tel:[///]voicemail]"
             << "[dialer:[///]?view=<view name>]"
             << "[--fullscreen]"
             << "[--help]"
             << "[-testability]";
}

//this is necessary to work on desktop
//On desktop use: export DIALER_APP_ICON_THEME=ubuntu-mobile
static void installIconPath()
{
    qDebug() << __PRETTY_FUNCTION__;
    QByteArray iconTheme = qgetenv("DIALER_APP_ICON_THEME");
    if (!iconTheme.isEmpty()) {
        QIcon::setThemeName(iconTheme);
    }
}


DialerApplication::DialerApplication()
    : QObject(0), m_view(0), m_applicationIsReady(false), m_fullScreen(false)
{
    qApp->setApplicationName("DialerApp");
    qApp->setOrganizationName("com.ubuntu.dialer-app");
}

bool DialerApplication::setup()
{
    installIconPath();

    if (mValidSchemes.isEmpty()) {
        mValidSchemes << "tel" << "dialer";
    }

    QStringList arguments = qApp->arguments();

    if (arguments.contains("--help")) {
        printUsage(arguments);
        return false;
    }

    if (arguments.contains("--fullscreen")) {
        arguments.removeAll("--fullscreen");
        m_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/dialer-app --desktop_file_hint=/usr/share/applications/dialer-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 (mValidSchemes.contains(uri.scheme())) {
            m_arg = arguments.at(1);
        }
    }
#ifdef MAPPLAUNCHERD_ENABLED
    m_view = MDeclarativeCache::qQuickView();
#else
    m_view = new QQuickView();
#endif
    QObject::connect(m_view, SIGNAL(statusChanged(QQuickView::Status)), this, SLOT(onViewStatusChanged(QQuickView::Status)));
    QObject::connect(m_view->engine(), SIGNAL(quit()), qApp, SLOT(quit()));
    m_view->setResizeMode(QQuickView::SizeRootObjectToView);
    m_view->setTitle("Dialer");
    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(dialerAppDirectory()));

    // 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);
    }

    // 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("dialer-app.qml"));
    if (m_fullScreen) {
        m_view->showFullScreen();
    } else {
        m_view->show();
    }

    return true;
}

bool DialerApplication::fullScreen() const
{
    return m_fullScreen;
}

void DialerApplication::setFullScreen(bool value)
{
    m_fullScreen = value;
    m_view->setWindowState(m_fullScreen ? Qt::WindowFullScreen : Qt::WindowNoState);
    Q_EMIT fullScreenChanged();
}

DialerApplication::~DialerApplication()
{
    if (m_view) {
        delete m_view;
    }
}

void DialerApplication::onViewStatusChanged(QQuickView::Status status)
{
    if (status != QQuickView::Ready) {
        return;
    }

    onApplicationReady();
}

void DialerApplication::onApplicationReady()
{
    m_applicationIsReady = true;
    parseArgument(m_arg);
    m_arg.clear();
}

void DialerApplication::parseArgument(const QString &arg)
{
    if (arg.isEmpty()) {
        return;
    }

    QUrl url(arg);
    QString scheme = url.scheme();
    // we can't fill value with url.path() as it might contain the # character and QUrl will drop it.
    QString value;
    if (!mValidSchemes.contains(url.scheme())) {
        qWarning() << "Url scheme not valid for dialer-app";
        return;
    } else if (url.scheme() == "tel") {
        // remove the initial tel:, it doesn't matter if it contains /// or //, as we
        // now use libphonenumber and it will remove these invalid characters when in the beginning
        // of the number
        value = arg;
        value = QUrl::fromPercentEncoding(value.remove("tel:").toLatin1());
    }

    QQuickItem *mainView = m_view->rootObject();
    if (!mainView) {
        return;
    }

    if (scheme == "tel") {
        if (value == "voicemail") {
            // FIXME: check if we should call the voicemail directly or just populate it
            QMetaObject::invokeMethod(mainView, "callVoicemail");
        } else {
            // do not call the number directly, instead only populate the dialpad view
            QMetaObject::invokeMethod(mainView, "populateDialpad", Q_ARG(QVariant, value), Q_ARG(QVariant, QString()));
        }
    } else if (scheme == "dialer") {
        QUrlQuery query(url);
        QString viewName = query.queryItemValue("view");
        if (viewName == "liveCall") {
            QMetaObject::invokeMethod(mainView, "switchToLiveCall", Q_ARG(QVariant, QVariant()), Q_ARG(QVariant, QVariant()));
        }

    }
}

void DialerApplication::activateWindow()
{
    if (m_view) {
        m_view->raise();
        m_view->requestActivate();
    }
}

QStringList DialerApplication::mmiPluginList()
{
    QStringList plugins;
    QString mmiDirectory = dialerAppDirectory() + "/MMI";
    QString mmiCustomDirectory = "/custom" + mmiDirectory;
    QDir directory(mmiDirectory);
    QDir customDirectory(mmiCustomDirectory);
    Q_FOREACH(const QString &file, directory.entryList(QStringList() << "*.qml")) {
        plugins << directory.filePath(file);
    }
    Q_FOREACH(const QString &file, customDirectory.entryList(QStringList() << "*.qml")) {
        plugins << customDirectory.filePath(file);
    }
    return plugins;
}