File indexing completed on 2022-09-30 10:18:08

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Sebastian Gottfried <sebastiangottfried@web.de>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "application.h"
0008 
0009 #include <QDir>
0010 #include <QFile>
0011 #include <QQmlContext>
0012 #include <QQmlEngine>
0013 #include <QQuickStyle>
0014 #include <QStandardPaths>
0015 
0016 #include <KLocalizedContext>
0017 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0018 #include <Kdelibs4ConfigMigrator>
0019 #include <Kdelibs4Migration>
0020 #endif
0021 
0022 #include "bindings/utils.h"
0023 #include "bindings/stringformatter.h"
0024 #include "declarativeitems/griditem.h"
0025 #include "declarativeitems/kcolorschemeproxy.h"
0026 #include "declarativeitems/lessonpainter.h"
0027 #include "declarativeitems/lessontexthighlighteritem.h"
0028 #include "declarativeitems/preferencesproxy.h"
0029 #include "declarativeitems/scalebackgrounditem.h"
0030 #include "declarativeitems/traininglinecore.h"
0031 #include "core/keyboardlayout.h"
0032 #include "core/key.h"
0033 #include "core/specialkey.h"
0034 #include "core/keychar.h"
0035 #include "core/course.h"
0036 #include "core/lesson.h"
0037 #include "core/profile.h"
0038 #include "core/trainingstats.h"
0039 #include "core/dataindex.h"
0040 #include "core/dataaccess.h"
0041 #include "core/profiledataaccess.h"
0042 #include "models/resourcemodel.h"
0043 #include "models/lessonmodel.h"
0044 #include "models/categorizedresourcesortfilterproxymodel.h"
0045 #include "models/learningprogressmodel.h"
0046 #include "models/errorsmodel.h"
0047 
0048 
0049 Application::Application(int& argc, char** argv, int flags):
0050     QApplication(argc, argv, flags),
0051     m_dataIndex(new DataIndex(this))
0052 {
0053     registerQmlTypes();
0054 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0055     migrateKde4Files();
0056 #endif
0057 
0058     QQuickStyle::setStyle("Default");
0059 
0060     DataAccess dataAccess;
0061     dataAccess.loadDataIndex(m_dataIndex);
0062 }
0063 
0064 DataIndex* Application::dataIndex()
0065 {
0066     Application* app = qobject_cast<Application*>(QCoreApplication::instance());
0067 
0068     return app->m_dataIndex;
0069 }
0070 
0071 QPointer<ResourceEditor>& Application::resourceEditorRef()
0072 {
0073     Application* app = qobject_cast<Application*>(QCoreApplication::instance());
0074 
0075     return app->m_resourceEditorRef;
0076 }
0077 
0078 void Application::setupDeclarativeBindings(QQmlEngine* qmlEngine)
0079 {
0080     // prepare i18n
0081     auto context = new KLocalizedContext(qmlEngine);
0082     qmlEngine->rootContext()->setContextObject(context);
0083 
0084     Application* app = static_cast<Application*>(Application::instance());
0085     foreach (const QString& path, app->m_qmlImportPaths)
0086     {
0087         qmlEngine->addImportPath(path);
0088     }
0089 
0090     QQmlContext* rootContext = qmlEngine->rootContext();
0091 
0092     rootContext->setContextProperty(QStringLiteral("utils"), new Utils());
0093     rootContext->setContextProperty(QStringLiteral("strFormatter"), new StringFormatter());
0094 }
0095 
0096 QStringList& Application::qmlImportPaths()
0097 {
0098     return m_qmlImportPaths;
0099 }
0100 
0101 void Application::registerQmlTypes()
0102 {
0103     qmlRegisterType<KeyboardLayout>("ktouch", 1, 0, "KeyboardLayout");
0104     qmlRegisterType<AbstractKey>("ktouch", 1, 0, "AbstractKey");
0105     qmlRegisterType<Key>("ktouch", 1, 0, "Key");
0106     qmlRegisterType<SpecialKey>("ktouch", 1, 0, "SpecialKey");
0107     qmlRegisterType<KeyChar>("ktouch", 1, 0, "KeyChar");
0108     qmlRegisterType<Course>("ktouch", 1, 0, "Course");
0109     qmlRegisterType<Lesson>("ktouch", 1, 0, "Lesson");
0110     qmlRegisterType<TrainingStats>("ktouch", 1, 0, "TrainingStats");
0111     qmlRegisterType<Profile>("ktouch", 1, 0, "Profile");
0112     qmlRegisterType<DataIndex>("ktouch", 1, 0, "DataIndex");
0113     qmlRegisterType<DataIndexCourse>("ktouch", 1, 0, "DataIndexCourse");
0114     qmlRegisterType<DataIndexKeyboardLayout>("ktouch", 1, 0, "DataIndexKeyboardLayout");
0115     qmlRegisterType<PreferencesProxy>("ktouch", 1, 0, "Preferences");
0116     qmlRegisterType<DataAccess>("ktouch", 1, 0, "DataAccess");
0117     qmlRegisterType<ProfileDataAccess>("ktouch", 1, 0, "ProfileDataAccess");
0118 
0119     qmlRegisterType<ResourceModel>("ktouch", 1, 0, "ResourceModel");
0120     qmlRegisterType<LessonModel>("ktouch", 1, 0, "LessonModel");
0121     qmlRegisterType<CategorizedResourceSortFilterProxyModel>("ktouch", 1, 0, "CategorizedResourceSortFilterProxyModel");
0122     qmlRegisterType<LearningProgressModel>("ktouch", 1, 0, "LearningProgressModel");
0123     qmlRegisterType<ErrorsModel>("ktouch", 1, 0, "ErrorsModel");
0124 
0125     qmlRegisterType<GridItem>("ktouch", 1, 0 , "LineGrid");
0126     qmlRegisterType<ScaleBackgroundItem>("ktouch", 1, 0, "ScaleBackgroundItem");
0127     qmlRegisterType<LessonPainter>("ktouch", 1, 0, "LessonPainter");
0128     qmlRegisterType<LessonTextHighlighterItem>("ktouch", 1, 0, "LessonTextHighlighter");
0129     qmlRegisterType<TrainingLineCore>("ktouch", 1, 0, "TrainingLineCore");
0130     qmlRegisterType<KColorSchemeProxy>("ktouch", 1, 0, "KColorScheme");
0131 }
0132 
0133 void Application::migrateKde4Files()
0134 {
0135 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0136     QStringList configFiles;
0137     configFiles << QStringLiteral("ktouchrc");
0138     Kdelibs4ConfigMigrator confMigrator(QStringLiteral("ktouch"));
0139     confMigrator.setConfigFiles(configFiles);
0140     confMigrator.migrate();
0141 
0142     Kdelibs4Migration migration;
0143     const QDir dataDir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
0144     if (!dataDir.exists())
0145     {
0146         dataDir.mkpath(dataDir.path());
0147     }
0148     const QString dbPath = dataDir.filePath(QStringLiteral("profiles.db"));
0149     const QString oldDbPath = migration.locateLocal("data", QStringLiteral("ktouch/profiles.db"));
0150     if (!QFile(dbPath).exists() && !oldDbPath.isEmpty())
0151     {
0152         QFile(oldDbPath).copy(dbPath);
0153     }
0154 #endif
0155 }