File indexing completed on 2024-04-28 11:29:38

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2015 Gábor Péterffy <peterffy95@gmail.com>
0004 //
0005 
0006 #include <QApplication>
0007 #include <QQmlApplicationEngine>
0008 #include <QtQuick>
0009 
0010 #include "declarative/MarbleDeclarativePlugin.h"
0011 #include <MarbleGlobal.h>
0012 #include "MarbleMaps.h"
0013 #include "MarbleDirs.h"
0014 #include "TextToSpeechClient.h"
0015 
0016 using namespace Marble;
0017 
0018 static bool loadTranslator(const QString &fullPath, QApplication &app)
0019 {
0020     QTranslator* translator = new QTranslator(&app);
0021     if (!translator->load(fullPath)) {
0022         delete translator;
0023         return false;
0024     }
0025 
0026     app.installTranslator(translator);
0027 
0028     return true;
0029 }
0030 
0031 static bool loadTranslation(const QString &localeDirName, QApplication &app)
0032 {
0033     // TODO: check if any translations for Qt modules have to be loaded,
0034     // as they need to be explicitly loaded as well by the Qt-using app
0035 
0036 #ifdef Q_OS_ANDROID
0037     // load translation file from bundled packaging installation
0038     const QString fullPath = MarbleDirs::systemPath() + QLatin1String("/locale/") + localeDirName + QLatin1String("/marble_qt.qm");
0039 #else
0040     // load translation file from normal "KDE Applications" packaging installation
0041     const QString subPath = QLatin1String("locale/") + localeDirName + QLatin1String("/LC_MESSAGES/marble_qt.qm");
0042     const QString fullPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, subPath);
0043     if (fullPath.isEmpty()) {
0044         return false;
0045     }
0046 #endif
0047 
0048     return loadTranslator(fullPath, app);
0049 }
0050 
0051 // load KDE translators system based translations
0052 static void loadTranslations(QApplication &app)
0053 {
0054     QSettings settings;
0055     settings.beginGroup("localization");
0056     bool const translationsDisabled = settings.value("translationsDisabled", QVariant(false)).toBool();
0057     QString const translationFile = settings.value("translationFile").toUrl().path();
0058     settings.endGroup();
0059 
0060     if (translationsDisabled) {
0061         return;
0062     }
0063 
0064     // Quote from ecm_create_qm_loader created code:
0065     // The way Qt translation system handles plural forms makes it necessary to
0066     // have a translation file which contains only plural forms for `en`.
0067     // That's why we load the `en` translation unconditionally, then load the
0068     // translation for the current locale to overload it.
0069     const QString en(QStringLiteral("en"));
0070 
0071     loadTranslation(en, app);
0072 
0073     if (!translationFile.isEmpty() && loadTranslator(translationFile, app)) {
0074         return;
0075     }
0076 
0077     QLocale locale = QLocale::system();
0078     if (locale.name() != en) {
0079         if (!loadTranslation(locale.name(), app)) {
0080             loadTranslation(locale.bcp47Name(), app);
0081         }
0082     }
0083 }
0084 
0085 #ifdef Q_OS_ANDROID
0086 // Declare symbol of main method as exported as needed by Qt-on-Android,
0087 // where the Dalvik-native QtActivity class needs to find and invoke it
0088 // on loading the "app" module
0089 extern "C" Q_DECL_EXPORT
0090 #endif
0091 int main(int argc, char ** argv)
0092 {
0093     QApplication app(argc, argv);
0094     app.setApplicationName( "Marble Maps" );
0095     app.setOrganizationName( "KDE" );
0096     app.setOrganizationDomain( "kde.org" );
0097     app.setDesktopFileName(QStringLiteral("org.kde.marble.maps"));
0098 
0099     // Load Qt translation system catalog for libmarblewidget, the plugins and this app
0100     loadTranslations(app);
0101 
0102     MarbleDeclarativePlugin declarativePlugin;
0103     const char uri[] = "org.kde.marble";
0104     declarativePlugin.registerTypes(uri);
0105     qmlRegisterType<MarbleMaps>(uri, 0, 20, "MarbleMaps");
0106 
0107     QQmlApplicationEngine engine;
0108     TextToSpeechClient * tts = new TextToSpeechClient(&engine);
0109     engine.rootContext()->setContextProperty("textToSpeechClient", tts);
0110     engine.load(QUrl("qrc:/MainScreen.qml"));
0111     // @todo Ship translations and only fall back to english if no translations for the system locale are installed
0112     tts->setLocale("en");
0113 
0114     return app.exec();
0115 }