| 1 | /* |
| 2 | Copyright (C) 2024 UBports Foundation. |
| 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License 3 as published by |
| 6 | the Free Software Foundation. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see http://www.gnu.org/licenses/. |
| 15 | |
| 16 | Authors: Michael Zanetti <[email protected]> |
| 17 | Riccardo Padovani <[email protected]> |
| 18 | David Planella <[email protected]> |
| 19 | */ |
| 20 | |
| 21 | #include <QtGui/QGuiApplication> |
| 22 | #include <QtQuick/QQuickView> |
| 23 | #include <QtQml/QtQml> |
| 24 | #include <QLibrary> |
| 25 | #include <QDir> |
| 26 | |
| 27 | #include <QDebug> |
| 28 | |
| 29 | #include "config.h" |
| 30 | |
| 31 | int main(int argc, char *argv[]) |
| 32 | { |
| 33 | QGuiApplication a(argc, argv); |
| 34 | QQuickView view; |
| 35 | view.setResizeMode(QQuickView::SizeRootObjectToView); |
| 36 | |
| 37 | // Set up import paths |
| 38 | QStringList importPathList = view.engine()->importPathList(); |
| 39 | |
| 40 | QStringList args = a.arguments(); |
| 41 | if (args.contains("-h") || args.contains("--help")) { |
| 42 | qDebug() << "usage: " + args.at(0) + " [-h|--help] [-I <path>] [-q <qmlfile>]"; |
| 43 | qDebug() << " -h|--help Print this help."; |
| 44 | qDebug() << " -I <path> Give a path for an additional QML import directory. May be used multiple times."; |
| 45 | qDebug() << " -q <qmlfile> Give an alternative location for the main qml file."; |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | QString qmlfile; |
| 50 | for (int i = 0; i < args.count(); i++) { |
| 51 | if (args.at(i) == "-I" && args.count() > i + 1) { |
| 52 | QString addedPath = args.at(i+1); |
| 53 | if (addedPath.startsWith('.')) { |
| 54 | addedPath = addedPath.right(addedPath.length() - 1); |
| 55 | addedPath.prepend(QDir::currentPath()); |
| 56 | } |
| 57 | importPathList.append(addedPath); |
| 58 | } else if (args.at(i) == "-q" && args.count() > i + 1) { |
| 59 | qmlfile = args.at(i+1); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_TESTABILITY")) { |
| 64 | QLibrary testLib(QLatin1String("qttestability")); |
| 65 | if (testLib.load()) { |
| 66 | typedef void (*TasInitialize)(void); |
| 67 | TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init"); |
| 68 | if (initFunction) { |
| 69 | initFunction(); |
| 70 | } else { |
| 71 | qCritical("Library qttestability resolve failed!"); |
| 72 | } |
| 73 | } else { |
| 74 | qCritical("Library qttestability load failed!"); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | view.engine()->rootContext()->setContextProperty("i18nDirectory", I18nDirectory()); |
| 79 | |
| 80 | view.engine()->setImportPathList(importPathList); |
| 81 | |
| 82 | // load the qml file |
| 83 | if (qmlfile.isEmpty()) { |
| 84 | QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DataLocation); |
| 85 | paths.prepend(QDir::currentPath()); |
| 86 | paths.prepend(QCoreApplication::applicationDirPath()); |
| 87 | paths.prepend(AssetsDir()); |
| 88 | |
| 89 | foreach (const QString &path, paths) { |
| 90 | QFileInfo fi(path + "/lomiri-calculator-app.qml"); |
| 91 | qDebug() << "Trying to load QML from:" << fi.filePath(); |
| 92 | if (fi.exists()) { |
| 93 | qmlfile = fi.filePath(); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | qDebug() << "using main qml file from:" << qmlfile; |
| 99 | view.setSource(QUrl::fromLocalFile(qmlfile)); |
| 100 | view.show(); |
| 101 | return a.exec(); |
| 102 | } |