~verzegnassi-stefano/+junk/docviewer-tmp-001

« back to all changes in this revision

Viewing changes to src/app/main.cpp

  • Committer: Tarmac
  • Author(s): David Planella, Daniel Holbach, Fabio Colella, Stefano Verzegnassi, Arto Jalkanen
  • Date: 2014-10-29 07:39:08 UTC
  • mfrom: (31.1.22 add-plugin)
  • Revision ID: tarmac-20141029073908-8s6r18syexjo37ga
Adds the file plugin to the source tree.

Approved by Stefano Verzegnassi, Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * reminders is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
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>
 
22
 */
 
23
 
 
24
#include <QtGui/QGuiApplication>
 
25
#include <QtQuick/QQuickView>
 
26
#include <QtQml/QtQml>
 
27
#include <QLibrary>
 
28
#include <QDir>
 
29
 
 
30
#include <QDebug>
 
31
 
 
32
int main(int argc, char *argv[])
 
33
{
 
34
    QGuiApplication a(argc, argv);
 
35
    QQuickView view;
 
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
 
117
    QCoreApplication::setApplicationName("com.ubuntu.docviewer");
 
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
    qDebug() << "using main qml file from:" << qmlfile;
 
126
    view.setSource(QUrl::fromLocalFile(qmlfile));
 
127
    view.show();
 
128
 
 
129
    return a.exec();
 
130
}