~ubuntu-docviewer-dev/ubuntu-docviewer-app/lo-viewer

« back to all changes in this revision

Viewing changes to src/app/main.cpp

Update framework to 'ubuntu-sdk-14.10'. Fixes: https://bugs.launchpad.net/bugs/1401718.

Approved by Nekhelesh Ramananthan, Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2013-2015 Canonical Ltd.
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
 
2
 * Copyright: 2013 - 2014 Canonical, Ltd
 
3
 *
 
4
 * This file is part of docviewer
 
5
 *
 
6
 * docviewer is free software: you can redistribute it and/or modify
5
7
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; version 3.
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
7
10
 *
8
 
 * This program is distributed in the hope that it will be useful,
 
11
 * reminders is distributed in the hope that it will be useful,
9
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
14
 * GNU General Public License for more details.
12
15
 *
13
16
 * You should have received a copy of the GNU General Public License
14
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 *
 
19
 * Authors: Michael Zanetti <michael.zanetti@canonical.com>
 
20
 *          Riccardo Padovani <rpadovani@ubuntu.com>
 
21
 *          David Planella <david.planella@ubuntu.com>
15
22
 */
16
23
 
17
 
// Uncomment if you need to use QML analyzer
18
 
// #define QT_QML_DEBUG
19
 
// #include <QtQuick>
20
 
 
21
 
#include <QGuiApplication>
22
 
#include <QQuickView>
 
24
#include <QtGui/QGuiApplication>
 
25
#include <QtQuick/QQuickView>
 
26
#include <QtQml/QtQml>
23
27
#include <QLibrary>
24
 
#include <QtQml>
 
28
#include <QDir>
25
29
 
26
30
#include <QDebug>
27
31
 
28
 
#include "config.h"
29
 
 
30
 
void loadTestability() {
31
 
    QLibrary testLib(QLatin1String("qttestability"));
32
 
 
33
 
    if (testLib.load()) {
34
 
        typedef void (*TasInitialize)(void);
35
 
        TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
36
 
 
37
 
        if (initFunction) {
38
 
            initFunction();
39
 
        } else {
40
 
            qCritical("Library qttestability resolve failed!");
41
 
        }
42
 
    } else {
43
 
        qCritical("Library qttestability load failed!");
44
 
    }
45
 
}
46
 
 
47
32
int main(int argc, char *argv[])
48
33
{
49
 
    QGuiApplication app(argc, argv);
 
34
    QGuiApplication a(argc, argv);
50
35
    QQuickView view;
51
 
 
 
36
    view.setResizeMode(QQuickView::SizeRootObjectToView);
 
37
 
 
38
    // Set up import paths
 
39
    QStringList importPathList = view.engine()->importPathList();
 
40
    // Prepend the location of the plugin in the build dir,
 
41
    // so that Qt Creator finds it there, thus overriding the one installed
 
42
    // in the sistem if there is one
 
43
    importPathList.prepend(QCoreApplication::applicationDirPath() + "/../plugin/");
 
44
 
 
45
    QStringList args = a.arguments();
 
46
    if (args.contains("-h") || args.contains("--help")) {
 
47
        qDebug() << "usage: " + args.at(0) + " [-p|--phone] [-t|--tablet] [-h|--help] [-I <path>]";
 
48
        qDebug() << "    -p|--phone    If running on Desktop, start in a phone sized window.";
 
49
        qDebug() << "    -t|--tablet   If running on Desktop, start in a tablet sized window.";
 
50
        qDebug() << "    -h|--help     Print this help.";
 
51
        qDebug() << "    -I <path>     Give a path for an additional QML import directory. May be used multiple times.";
 
52
        return 0;
 
53
    }
 
54
 
 
55
    for (int i = 0; i < args.count(); i++) {
 
56
        if (args.at(i) == "-I" && args.count() > i + 1) {
 
57
            QString addedPath = args.at(i+1);
 
58
            if (addedPath.startsWith('.')) {
 
59
                addedPath = addedPath.right(addedPath.length() - 1);
 
60
                addedPath.prepend(QDir::currentPath());
 
61
            }
 
62
            importPathList.append(addedPath);
 
63
        }
 
64
    }
 
65
 
 
66
    if (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_TESTABILITY")) {
 
67
        QLibrary testLib(QLatin1String("qttestability"));
 
68
        if (testLib.load()) {
 
69
            typedef void (*TasInitialize)(void);
 
70
            TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
 
71
            if (initFunction) {
 
72
                initFunction();
 
73
            } else {
 
74
                qCritical("Library qttestability resolve failed!");
 
75
            }
 
76
        } else {
 
77
            qCritical("Library qttestability load failed!");
 
78
        }
 
79
    }
 
80
 
 
81
    view.engine()->rootContext()->setContextProperty("tablet", QVariant(false));
 
82
    view.engine()->rootContext()->setContextProperty("phone", QVariant(false));
 
83
    if (args.contains("-t") || args.contains("--tablet")) {
 
84
        qDebug() << "running in tablet mode";
 
85
        view.engine()->rootContext()->setContextProperty("tablet", QVariant(true));
 
86
    } else if (args.contains("-p") || args.contains("--phone")){
 
87
        qDebug() << "running in phone mode";
 
88
        view.engine()->rootContext()->setContextProperty("phone", QVariant(true));
 
89
    } else if (qgetenv("QT_QPA_PLATFORM") != "ubuntumirclient") {
 
90
        // Default to tablet size on X11
 
91
        view.engine()->rootContext()->setContextProperty("tablet", QVariant(true));
 
92
    }
 
93
 
 
94
    view.engine()->setImportPathList(importPathList);
 
95
 
 
96
    QString qmlfile;
 
97
    const QString filePath = QLatin1String("qml/ubuntu-docviewer-app.qml");
 
98
    QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DataLocation);
 
99
    paths.prepend(QDir::currentPath());
 
100
    paths.prepend(QCoreApplication::applicationDirPath());
 
101
    Q_FOREACH (const QString &path, paths) {
 
102
        QString myPath = path + QLatin1Char('/') + filePath;
 
103
        if (QFile::exists(myPath)) {
 
104
            qmlfile = myPath;
 
105
            break;
 
106
        }
 
107
    }
 
108
    // sanity check
 
109
    if (qmlfile.isEmpty()) {
 
110
        qFatal("File: %s does not exist at any of the standard paths!", qPrintable(filePath));
 
111
    }
 
112
 
 
113
    // Make sure our cache dir exists. It'll be used all over in this app.
 
114
    // We need to set the applicationName for that.
 
115
    // It'll be overwritten again when qml loads but we need it already now.
 
116
    // So if you want to change it, make sure to find all the places where it is set, not just here :D
52
117
    QCoreApplication::setApplicationName("com.ubuntu.docviewer");
53
 
    QCoreApplication::setOrganizationDomain("com.ubuntu.docviewer");
54
 
 
55
 
    view.rootContext()->setContextProperty("window", &view);
56
 
    QObject::connect(view.engine(), SIGNAL(quit()), &app, SLOT(quit()));
57
 
 
58
 
    if (qgetenv("QT_LOAD_TESTABILITY") == "1")
59
 
       loadTestability();
60
 
 
61
 
    if (!isRunningInstalled())
62
 
        view.engine()->addImportPath(QCoreApplication::applicationDirPath() + "/../plugin/");
63
 
 
64
 
    view.setSource(QUrl::fromLocalFile(docviewerDirectory() + "/qml/ubuntu-docviewer-app.qml"));
65
 
    view.setResizeMode(QQuickView::SizeRootObjectToView);
66
 
 
67
 
    // UITK/QML command line arguments are parsed before view's window becomes visible.
68
 
    if (view.windowState() == Qt::WindowFullScreen)
69
 
        view.showFullScreen();
70
 
    else
71
 
        view.show();
72
 
 
73
 
    return app.exec();
 
118
 
 
119
    QDir cacheDir(QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first());
 
120
    if (!cacheDir.exists()) {
 
121
        qDebug() << "creating cacheDir:" << cacheDir.absolutePath();
 
122
        cacheDir.mkpath(cacheDir.absolutePath());
 
123
    }
 
124
 
 
125
    // Expose quit() signal to QML
 
126
    QObject::connect(view.engine(), SIGNAL(quit()), &a, SLOT(quit()));
 
127
 
 
128
    qDebug() << "using main qml file from:" << qmlfile;
 
129
    view.setSource(QUrl::fromLocalFile(qmlfile));
 
130
    view.show();
 
131
 
 
132
    return a.exec();
74
133
}