~nicolas-doffay/unity8/search-history-persist

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
/*
 * Copyright (C) 2012 Canonical, Ltd.
 *
 * Authors:
 *  Gerry Boland <gerry.boland@canonical.com>
 *
 * This program 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.
 *
 * This program 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/>.
 */

// Qt
#include <QtQuick/QQuickView>
#include <QtGui/QIcon>
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlEngine>
#include <QtQml/QQmlContext>
#include <qpa/qplatformnativeinterface.h>
#include <QLibrary>
#include <QDebug>
#include <libintl.h>
#include <dlfcn.h>

// local
#include "paths.h"
#include "MouseTouchAdaptor.h"
#include "ApplicationArguments.h"

#include <unity-mir/qmirserver.h>

namespace {

void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
{
    QStringList importPathList = engine->importPathList();
    for (int i = paths.count() -1; i >= 0; i--) {
        // don't duplicate
        const QString& path = paths[i];
        QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
        if (iter == importPathList.end()) {
            engine->addImportPath(path);
        }
    }
}

/* When you append and import path to the list of import paths it will be the *last*
   place where Qt will search for QML modules.
   The usual QQmlEngine::addImportPath() actually prepends the given path.*/
void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
{
    QStringList importPathList = engine->importPathList();
    Q_FOREACH(const QString& path, paths) {
        // don't duplicate
        QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
        if (iter == importPathList.end()) {
            importPathList.append(path);
        }
    }
    engine->setImportPathList(importPathList);
}

void resolveIconTheme() {
    const char *ubuntuIconTheme = getenv("UBUNTU_ICON_THEME");
    if (ubuntuIconTheme == NULL) {
        ubuntuIconTheme = "ubuntu-mobile";
    }
    QIcon::setThemeName(ubuntuIconTheme);
}
} // namespace {

int startShell(int argc, const char** argv, void* server)
{
    const bool isUbuntuMirServer = qgetenv("QT_QPA_PLATFORM") == "ubuntumirserver";

    QGuiApplication::setApplicationName("Unity 8");
    QGuiApplication *application;
    if (isUbuntuMirServer) {
        QLibrary unityMir("unity-mir", 1);
        unityMir.load();

        typedef QGuiApplication* (*createServerApplication_t)(int&, const char **, void*);
        createServerApplication_t createQMirServerApplication = (createServerApplication_t) unityMir.resolve("createQMirServerApplication");
        if (!createQMirServerApplication) {
            qDebug() << "unable to resolve symbol: createQMirServerApplication";
            return 4;
        }

        application = createQMirServerApplication(argc, argv, server);
    } else {
        application = new QGuiApplication(argc, (char**)argv);
    }

    resolveIconTheme();

    QStringList args = application->arguments();
    ApplicationArguments qmlArgs(args);

    // 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 (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_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!");
        }
    }

    bindtextdomain("unity8", translationDirectory().toUtf8().data());

    QQuickView* view = new QQuickView();
    view->setResizeMode(QQuickView::SizeRootObjectToView);
    view->setTitle("Qml Phone Shell");
    view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
    view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
    if (args.contains(QLatin1String("-frameless"))) {
        view->setFlags(Qt::FramelessWindowHint);
    }

    // You will need this if you want to interact with touch-only components using a mouse
    // Needed only when manually testing on a desktop.
    MouseTouchAdaptor *mouseTouchAdaptor = 0;
    if (args.contains(QLatin1String("-mousetouch"))) {
        mouseTouchAdaptor = new MouseTouchAdaptor;
        application->installNativeEventFilter(mouseTouchAdaptor);
    }

    QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
    /* Shell is declared as a system session so that it always receives all
       input events.
       FIXME: use the enum value corresponding to SYSTEM_SESSION_TYPE (= 1)
       when it becomes available.
    */
    nativeInterface->setProperty("ubuntuSessionType", 1);
    view->setProperty("role", 2); // INDICATOR_ACTOR_ROLE

    QUrl source("Shell.qml");
    prependImportPaths(view->engine(), ::overrideImportPaths());
    appendImportPaths(view->engine(), ::fallbackImportPaths());

    if (isUbuntuMirServer) {
        QStringList importPaths = view->engine()->importPathList();
        importPaths.replaceInStrings(QRegExp("qt5/imports$"), "qt5/imports/Unity-Mir");
        view->engine()->setImportPathList(importPaths);
    }

    view->setSource(source);
    view->setColor("transparent");

    if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || isUbuntuMirServer || args.contains(QLatin1String("-fullscreen"))) {
        view->showFullScreen();
    } else {
        view->show();
    }

    int result = application->exec();

    delete view;
    delete mouseTouchAdaptor;
    delete application;

    return result;
}

int main(int argc, const char *argv[])
{
    /* Workaround Qt platform integration plugin not advertising itself
       as having the following capabilities:
        - QPlatformIntegration::ThreadedOpenGL
        - QPlatformIntegration::BufferQueueingOpenGL
    */
    setenv("QML_FORCE_THREADED_RENDERER", "1", 1);
    setenv("QML_FIXED_ANIMATION_STEP", "1", 1);

    // For ubuntumirserver/ubuntumirclient
    if (qgetenv("QT_QPA_PLATFORM").startsWith("ubuntumir")) {
        setenv("QT_QPA_PLATFORM", "ubuntumirserver", 1);

        // If we use unity-mir directly, we automatically link to the Mir-server
        // platform-api bindings, which result in unexpected behaviour when
        // running the non-Mir scenario.
        QLibrary unityMir("unity-mir", 1);
        unityMir.load();
        if (!unityMir.isLoaded()) {
            qDebug() << "Library unity-mir not found/loaded";
            return 1;
        }

        typedef QMirServer* (*createServer_t)(int, const char **);
        createServer_t createQMirServer = (createServer_t) unityMir.resolve("createQMirServer");
        if (!createQMirServer) {
            qDebug() << "unable to resolve symbol: createQMirServer";
            return 2;
        }

        QMirServer* mirServer = createQMirServer(argc, argv);

        typedef int (*runWithClient_t)(QMirServer*, std::function<int(int, const char**, void*)>);
        runWithClient_t runWithClient = (runWithClient_t) unityMir.resolve("runQMirServerWithClient");
        if (!runWithClient) {
            qDebug() << "unable to resolve symbol: runWithClient";
            return 3;
        }

        return runWithClient(mirServer, startShell);
    } else {
        return startShell(argc, argv, nullptr);
    }
}