1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*
* Copyright 2012 Sebastian Gottfried <sebastiangottfried@web.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "application.h"
#include <QDir>
#include <QFile>
#include <QQmlEngine>
#include <QQmlContext>
#include <QStandardPaths>
#include <Kdelibs4ConfigMigrator>
#include <Kdelibs4Migration>
#include <KDeclarative/KDeclarative>
#include "bindings/utils.h"
#include "bindings/stringformatter.h"
#include "declarativeitems/griditem.h"
#include "declarativeitems/lessonpainter.h"
#include "declarativeitems/preferencesproxy.h"
#include "declarativeitems/scalebackgrounditem.h"
#include "declarativeitems/traininglinecore.h"
#include "core/keyboardlayout.h"
#include "core/key.h"
#include "core/specialkey.h"
#include "core/keychar.h"
#include "core/course.h"
#include "core/lesson.h"
#include "core/profile.h"
#include "core/trainingstats.h"
#include "core/dataindex.h"
#include "core/dataaccess.h"
#include "core/profiledataaccess.h"
#include "models/resourcemodel.h"
#include "models/lessonmodel.h"
#include "models/categorizedresourcesortfilterproxymodel.h"
#include "models/learningprogressmodel.h"
#include "models/errorsmodel.h"
Application::Application(int& argc, char** argv, int flags):
QApplication(argc, argv, flags),
m_dataIndex(new DataIndex(this))
{
registerQmlTypes();
migrateKde4Files();
DataAccess dataAccess;
dataAccess.loadDataIndex(m_dataIndex);
}
DataIndex* Application::dataIndex()
{
Application* app = qobject_cast<Application*>(QCoreApplication::instance());
return app->m_dataIndex;
}
QPointer<ResourceEditor>& Application::resourceEditorRef()
{
Application* app = qobject_cast<Application*>(QCoreApplication::instance());
return app->m_resourceEditorRef;
}
void Application::setupDeclarativeBindings(QQmlEngine* qmlEngine)
{
KDeclarative::KDeclarative kDeclarative;
kDeclarative.setDeclarativeEngine(qmlEngine);
kDeclarative.setupBindings();
Application* app = static_cast<Application*>(Application::instance());
foreach (const QString& path, app->m_qmlImportPaths)
{
qmlEngine->addImportPath(path);
}
QQmlContext* rootContext = qmlEngine->rootContext();
rootContext->setContextProperty("utils", new Utils());
rootContext->setContextProperty("strFormatter", new StringFormatter());
}
QStringList& Application::qmlImportPaths()
{
return m_qmlImportPaths;
}
void Application::registerQmlTypes()
{
qmlRegisterType<KeyboardLayout>("ktouch", 1, 0, "KeyboardLayout");
qmlRegisterType<AbstractKey>("ktouch", 1, 0, "AbstractKey");
qmlRegisterType<Key>("ktouch", 1, 0, "Key");
qmlRegisterType<SpecialKey>("ktouch", 1, 0, "SpecialKey");
qmlRegisterType<KeyChar>("ktouch", 1, 0, "KeyChar");
qmlRegisterType<Course>("ktouch", 1, 0, "Course");
qmlRegisterType<Lesson>("ktouch", 1, 0, "Lesson");
qmlRegisterType<TrainingStats>("ktouch", 1, 0, "TrainingStats");
qmlRegisterType<Profile>("ktouch", 1, 0, "Profile");
qmlRegisterType<DataIndex>("ktouch", 1, 0, "DataIndex");
qmlRegisterType<DataIndexCourse>("ktouch", 1, 0, "DataIndexCourse");
qmlRegisterType<DataIndexKeyboardLayout>("ktouch", 1, 0, "DataIndexKeyboardLayout");
qmlRegisterType<PreferencesProxy>("ktouch", 1, 0, "Preferences");
qmlRegisterType<DataAccess>("ktouch", 1, 0, "DataAccess");
qmlRegisterType<ProfileDataAccess>("ktouch", 1, 0, "ProfileDataAccess");
qmlRegisterType<ResourceModel>("ktouch", 1, 0, "ResourceModel");
qmlRegisterType<LessonModel>("ktouch", 1, 0, "LessonModel");
qmlRegisterType<CategorizedResourceSortFilterProxyModel>("ktouch", 1, 0, "CategorizedResourceSortFilterProxyModel");
qmlRegisterType<LearningProgressModel>("ktouch", 1, 0, "LearningProgressModel");
qmlRegisterType<ErrorsModel>("ktouch", 1, 0, "ErrorsModel");
qmlRegisterType<GridItem>("ktouch", 1, 0 , "Grid");
qmlRegisterType<ScaleBackgroundItem>("ktouch", 1, 0, "ScaleBackgroundItem");
qmlRegisterType<LessonPainter>("ktouch", 1, 0, "LessonPainter");
qmlRegisterType<TrainingLineCore>("ktouch", 1, 0, "TrainingLineCore");
}
void Application::migrateKde4Files()
{
QStringList configFiles;
configFiles << QLatin1String("ktouchrc");
Kdelibs4ConfigMigrator confMigrator(QLatin1String("ktouch"));
confMigrator.setConfigFiles(configFiles);
confMigrator.migrate();
Kdelibs4Migration migration;
const QDir dataDir = QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
if (!dataDir.exists())
{
dataDir.mkpath(dataDir.path());
}
const QString dbPath = dataDir.filePath("profiles.db");
const QString oldDbPath = migration.locateLocal("data", QStringLiteral("ktouch/profiles.db"));
if (!QFile(dbPath).exists() && !oldDbPath.isEmpty())
{
QFile(oldDbPath).copy(dbPath);
}
}
|