File indexing completed on 2024-09-29 03:29:23

0001 /*
0002     SPDX-FileCopyrightText: 2002-2010 Peter Hedlund <peter.hedlund@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include <QAction>
0007 #include <QApplication>
0008 #include <QCommandLineOption>
0009 #include <QCommandLineParser>
0010 #include <QDir>
0011 #include <QQmlApplicationEngine>
0012 #include <QQmlContext>
0013 #include <QQuickStyle>
0014 #include <QQuickWindow>
0015 
0016 #include <KAboutData>
0017 #include <KCrash>
0018 #include <KLocalizedString>
0019 #include <qstringliteral.h>
0020 
0021 #include "blankanswer.h"
0022 #include "exporter.h"
0023 #include "fileopener.h"
0024 #include "kwordquiz_version.h"
0025 #include "kwqcardmodel.h"
0026 #include "kwqdocumentmodel.h"
0027 #include "kwqrandomsortmodel.h"
0028 #include "languagelistmodel.h"
0029 #include "prefs.h"
0030 #include "stateprefs.h"
0031 
0032 #ifndef Q_OS_ANDROID
0033 #include <KDBusService>
0034 #endif
0035 
0036 using namespace Qt::Literals::StringLiterals;
0037 
0038 void parseArgs(QCommandLineParser &parser, QQuickWindow *view, const QString &workingDirectory)
0039 {
0040     const QStringList args = parser.positionalArguments();
0041     if (args.count() == 0) {
0042         return;
0043     }
0044 
0045     auto fileOpener = view->findChild<FileOpener *>(u"FileOpener"_s);
0046 
0047     QString goTo = parser.value(u"goto"_s);
0048     Prefs::EnumStartSession mode = Prefs::Flashcard;
0049 
0050     if (!goTo.isEmpty()) {
0051         if (goTo == u"flash"_s) {
0052             mode = Prefs::Flashcard;
0053         } else if (goTo == u"mc"_s) {
0054             mode = Prefs::MultipleChoice;
0055         } else if (goTo == u"qa"_s) {
0056             mode = Prefs::QA;
0057         } else {
0058             qWarning() << i18n("Invalid goto given: %1. Allowed: flash, mc and qa", goTo);
0059         }
0060     }
0061 
0062     auto file = QUrl::fromUserInput(args.at(args.count() - 1), workingDirectory);
0063     fileOpener->openFile(file, mode);
0064 }
0065 
0066 int main(int argc, char *argv[])
0067 {
0068 #ifdef Q_OS_ANDROID
0069     QGuiApplication app(argc, argv);
0070     QQuickStyle::setStyle(u"org.kde.breeze"_s);
0071     QIcon::setThemeName(u"tokodon"_s);
0072 #else
0073     QApplication app(argc, argv);
0074     // Default to org.kde.desktop style unless the user forces another style
0075     if (qEnvironmentVariableIsEmpty("QT_QUICK_CONTROLS_STYLE")) {
0076         QQuickStyle::setStyle(u"org.kde.desktop"_s);
0077     }
0078 #endif
0079 
0080     KCrash::initialize();
0081     KLocalizedString::setApplicationDomain(QByteArrayLiteral("kwordquiz"));
0082     QApplication::setApplicationName(u"kwordquiz"_s);
0083     QApplication::setApplicationVersion(QStringLiteral(KWORDQUIZ_VERSION_STRING));
0084     QApplication::setOrganizationDomain(u"kde.org"_s);
0085     QIcon::setFallbackThemeName(u"breeze"_s);
0086 
0087     KAboutData aboutData(u"kwordquiz"_s,
0088                          i18n("KWordQuiz"),
0089                          QStringLiteral(KWORDQUIZ_VERSION_STRING),
0090                          i18n("A powerful flashcard and vocabulary learning program"),
0091                          KAboutLicense::GPL_V2,
0092                          i18n("(c) 2003-2010, Peter Hedlund"),
0093                          QString(),
0094                          QStringLiteral("https://apps.kde.org/kwordquiz"),
0095                          QStringLiteral("submit@bugs.kde.org"));
0096     aboutData.addAuthor(i18n("Peter Hedlund"), i18nc("@info:credit", "Original author"), QStringLiteral("peter.hedlund@kdemail.net"));
0097     aboutData.addAuthor(i18nc("@info:credit", "Carl Schwan"), i18nc("@info:credit", "Port to QML"), QStringLiteral("carl@carlschwan.eu"));
0098     aboutData.addCredit(i18n("Anne-Marie Mahfouf"), i18n("KDE Edutainment Maintainer"), QStringLiteral("annma@kde.org"));
0099     aboutData.addCredit(i18n("Martin Pfeiffer"), i18n("Leitner System and several code contributions"), QStringLiteral("hubipete@gmx.net"));
0100     QGuiApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("org.kde.kwordquiz")));
0101 
0102     KAboutData::setApplicationData(aboutData);
0103 
0104 #ifndef Q_OS_ANDROID
0105     KDBusService service(KDBusService::Unique);
0106 #endif
0107 
0108     KCrash::initialize();
0109 
0110 #ifdef Q_OS_WIN
0111     QApplication::setStyle(QStringLiteral("breeze"));
0112 #endif
0113 
0114     QCommandLineParser parser;
0115     aboutData.setupCommandLine(&parser);
0116 
0117     parser.addOption(
0118         QCommandLineOption(QStringList() << QStringLiteral("g") << QStringLiteral("goto"),
0119                            i18n("Type of session to start with: \n'flash' for flashcard, \n'mc' for multiple choice, \n'qa' for question and answer"),
0120                            QStringLiteral("session")));
0121     parser.addOption(QCommandLineOption(QStringList() <<  QStringLiteral("+[File]"), i18n("File to open")));
0122     parser.process(app);
0123     aboutData.processCommandLine(&parser);
0124 
0125     QQmlApplicationEngine engine;
0126 
0127     QObject::connect(&service, &KDBusService::activateRequested, &engine, [&engine, &parser](const QStringList &arguments, const QString &workingDirectory) {
0128         parser.parse(arguments);
0129         const auto rootObjects = engine.rootObjects();
0130 
0131         for (auto obj : rootObjects) {
0132             auto view = qobject_cast<QQuickWindow *>(obj);
0133             parseArgs(parser, view, workingDirectory);
0134         }
0135     });
0136 
0137     auto prefs = Prefs::self();
0138     auto statePrefs = StatePrefs::self();
0139     qmlRegisterSingletonInstance("org.kde.kwordquiz", 1, 0, "Prefs", prefs);
0140     qmlRegisterSingletonInstance("org.kde.kwordquiz", 1, 0, "StatePrefs", statePrefs);
0141     qmlRegisterType<KWQCardModel>("org.kde.kwordquiz", 1, 0, "CardModel");
0142     qmlRegisterType<FileOpener>("org.kde.kwordquiz", 1, 0, "FileOpener");
0143     qmlRegisterType<Exporter>("org.kde.kwordquiz", 1, 0, "Exporter");
0144     qmlRegisterType<BlankAnswer>("org.kde.kwordquiz", 1, 0, "BlankAnswer");
0145     qmlRegisterType<KWQDocumentModel>("org.kde.kwordquiz", 1, 0, "DocumentModel");
0146     qmlRegisterType<KWQRandomSortModel>("org.kde.kwordquiz", 1, 0, "RandomSortModel");
0147     qmlRegisterType<KWQRandomSortModel>("org.kde.kwordquiz", 1, 0, "RandomSortModel");
0148     qmlRegisterSingletonType("org.kde.kwordquiz", 1, 0, "About", [](QQmlEngine *engine, QJSEngine *) -> QJSValue {
0149         return engine->toScriptValue(KAboutData::applicationData());
0150     });
0151     qmlRegisterSingletonType<LanguageListModel>("org.kde.kwordquiz", 1, 0, "LanguageListModel", [](QQmlEngine *engine, QJSEngine *) {
0152         return new LanguageListModel;
0153     });
0154 
0155     engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
0156     QObject::connect(&engine, &QQmlApplicationEngine::quit, &app, &QCoreApplication::quit);
0157     engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
0158 
0159     const auto rootObjects = engine.rootObjects();
0160     if (rootObjects.isEmpty()) {
0161         return -1;
0162     }
0163 
0164     for (auto obj : rootObjects) {
0165         auto view = qobject_cast<QQuickWindow *>(obj);
0166         parseArgs(parser, view, QDir::currentPath());
0167     }
0168 
0169     return app.exec();
0170 }