Commit 5ba2ede2 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Initialize the database and the resource locator from KisApplication

parent 2284ea83
......@@ -24,9 +24,9 @@ target_link_libraries(kritaresources
Qt5::Core
Qt5::Widgets
PRIVATE
Qt5::Sql
kritaversion
kritaglobal
Qt5::Sql
KF5::ConfigCore
KF5::CoreAddons
KF5::I18n
......
......@@ -51,43 +51,15 @@ const QString KisResourceCacheDb::dbLocationKey {"ResourceCacheDbDirectory"};
const QString KisResourceCacheDb::resourceCacheDbFilename {"resourcecache.sqlite"};
const QString KisResourceCacheDb::databaseVersion {"0.0.1"};
bool KisResourceCacheDb::s_valid {false};
class KisResourceCacheDb::Private
{
public:
bool valid {false};
QSqlError initDb(const QString &location);
};
KisResourceCacheDb::KisResourceCacheDb()
: d(new Private())
{
}
KisResourceCacheDb::~KisResourceCacheDb()
{
}
bool KisResourceCacheDb::isValid() const
bool KisResourceCacheDb::isValid()
{
return d->valid;
return s_valid;
}
bool KisResourceCacheDb::initialize(const QString &location) const
{
QSqlError err = d->initDb(location);
if (err.isValid()) {
qWarning() << "Could not initialize the database:" << err;
}
d->valid = !err.isValid();
return d->valid;
}
QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
QSqlError initDb(const QString &location)
{
if (!QSqlDatabase::connectionNames().isEmpty()) {
infoResources << "Already connected to resource cache database";
......@@ -100,7 +72,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
}
QSqlDatabase db = QSqlDatabase::addDatabase(dbDriver);
db.setDatabaseName(location + "/" + resourceCacheDbFilename);
db.setDatabaseName(location + "/" + KisResourceCacheDb::resourceCacheDbFilename);
if (!db.open()) {
infoResources << "Could not connect to resource cache database";
......@@ -145,7 +117,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
<< "Krita version that created the database" << kritaVersion
<< "At" << creationDate;
if (schemaVersion != databaseVersion) {
if (schemaVersion != KisResourceCacheDb::databaseVersion) {
// XXX: Implement migration
warnResources << "Database schema is outdated, migration is needed";
schemaIsOutDated = true;
......@@ -189,7 +161,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
QFile f(":/fill_origin_types.sql");
if (f.open(QFile::ReadOnly)) {
QString sql = f.readAll();
Q_FOREACH(const QString &originType, storageTypes) {
Q_FOREACH(const QString &originType, KisResourceCacheDb::storageTypes) {
QSqlQuery query(sql);
query.addBindValue(originType);
if (!query.exec()) {
......@@ -214,7 +186,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
QFile f(":/fill_resource_types.sql");
if (f.open(QFile::ReadOnly)) {
QString sql = f.readAll();
Q_FOREACH(const QString &resourceType, resourceTypes) {
Q_FOREACH(const QString &resourceType, KisResourceCacheDb::resourceTypes) {
QSqlQuery query(sql);
query.addBindValue(resourceType);
if (!query.exec()) {
......@@ -235,7 +207,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
QString sql = f.readAll();
QSqlQuery query;
query.prepare(sql);
query.addBindValue(databaseVersion);
query.addBindValue(KisResourceCacheDb::databaseVersion);
query.addBindValue(KritaVersionWrapper::versionString());
query.addBindValue(QDateTime::currentDateTimeUtc().toString());
if (!query.exec()) {
......@@ -251,3 +223,14 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
return QSqlError();
}
bool KisResourceCacheDb::initialize(const QString &location)
{
QSqlError err = initDb(location);
if (err.isValid()) {
qWarning() << "Could not initialize the database:" << err;
}
s_valid = !err.isValid();
return s_valid;
}
......@@ -21,7 +21,6 @@
#define KISRESOURCECACHEDB_H
#include <QObject>
#include <QScopedPointer>
#include <kritaresources_export.h>
......@@ -42,31 +41,25 @@ public:
static const QStringList resourceTypes; ///< all types of resources
static const QStringList storageTypes; ///< kinds of places where resources can be stored
/**
* @brief KisResourceCacheDb create a resource cache database.
*/
explicit KisResourceCacheDb();
~KisResourceCacheDb();
/**
* @brief isValid
* @return true if the database has been correctly created, false if the database cannot be used
*/
bool isValid() const;
static bool isValid();
/**
* @brief initialize
* @param location the location of the database
* @return true if the database has been initialized correctly
*/
bool initialize(const QString &location) const;
static bool initialize(const QString &location);
private:
explicit KisResourceCacheDb();
~KisResourceCacheDb();
class Private;
QScopedPointer<Private> d;
static bool s_valid;
};
#endif // KISRESOURCECACHEDB_H
......@@ -129,16 +129,7 @@ KisResourceLocator::LocatorError KisResourceLocator::initialize(const QString &i
initalizationStatus = InitalizationStatus::Initialized;
}
// Add the folder
d->storages.append(QSharedPointer<KisResourceStorage>::create(d->resourceLocation));
// And add bundles and adobe libraries
QStringList filters = QStringList() << "*.bundle" << "*.abr" << "*.asl";
QDirIterator iter(d->resourceLocation, filters, QDir::Files, QDirIterator::Subdirectories);
while (iter.hasNext()) {
iter.next();
d->storages.append(QSharedPointer<KisResourceStorage>::create(iter.filePath()));
}
findStorages();
return LocatorError::Ok;
}
......@@ -183,10 +174,26 @@ KisResourceLocator::LocatorError KisResourceLocator::firstTimeInstallation(Inita
return LocatorError::Ok;
}
void KisResourceLocator::findStorages()
{
// Add the folder
d->storages.append(QSharedPointer<KisResourceStorage>::create(d->resourceLocation));
// And add bundles and adobe libraries
QStringList filters = QStringList() << "*.bundle" << "*.abr" << "*.asl";
QDirIterator iter(d->resourceLocation, filters, QDir::Files, QDirIterator::Subdirectories);
while (iter.hasNext()) {
iter.next();
d->storages.append(QSharedPointer<KisResourceStorage>::create(iter.filePath()));
}
}
bool KisResourceLocator::synchronizeDb()
{
Q_FOREACH(const KisResourceStorageSP storage, d->storages) {
qDebug() << "Storage" << storage->location() << storage->valid();
}
return true;
}
......@@ -77,7 +77,7 @@ private:
};
LocatorError firstTimeInstallation(InitalizationStatus initalizationStatus, const QString &installationResourcesLocation);
void findStorages();
class Private;
......
......@@ -36,10 +36,9 @@ void TestResourceCacheDb::initTestCase()
void TestResourceCacheDb::testCreateDatabase()
{
KisResourceCacheDb cacheDb;
bool res = cacheDb.initialize(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
bool res = KisResourceCacheDb::initialize(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
QVERIFY(res);
QVERIFY(cacheDb.isValid());
QVERIFY(KisResourceCacheDb::isValid());
QSqlDatabase sqlDb = QSqlDatabase::database();
......@@ -58,9 +57,9 @@ void TestResourceCacheDb::testCreateDatabase()
QVERIFY2(dbTables.contains(table), table.toLatin1());
}
res = cacheDb.initialize(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
res = KisResourceCacheDb::initialize(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
QVERIFY(res);
QVERIFY(cacheDb.isValid());
QVERIFY(KisResourceCacheDb::isValid());
}
void TestResourceCacheDb::testLookupTables()
......
......@@ -96,6 +96,7 @@
#include <KritaVersionWrapper.h>
#include <dialogs/KisSessionManagerDialog.h>
#include <KisResourceCacheDb.h>
#include <KisResourceLocator.h>
#include <KisResourceLoader.h>
#include <KisResourceLoaderRegistry.h>
......@@ -349,7 +350,19 @@ void KisApplication::loadResources()
reg->add(new KisResourceLoader<KisSessionResource>("windowlayouts", "sessions", QStringList() << "application/x-krita-windowlayout"));
reg->add(new KisResourceLoader<KisSessionResource>("sessions", "sessions", QStringList() << "application/x-krita-session"));
qDebug() << "loadResources()" << reg->keys();
KisResourceLocator::LocatorError r = KisResourceLocator::instance()->initialize(KoResourcePaths::getApplicationRoot() + "/share/krita");
if (r != KisResourceLocator::LocatorError::Ok ) {
QMessageBox::critical(0, i18nc("@title:window", "Krita: Fatal error"), KisResourceLocator::instance()->errorMessages().join('\n'));
qApp->quit();
}
KisResourceCacheDb::initialize(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
if (!KisResourceLocator::instance()->synchronizeDb()) {
QMessageBox::critical(0, i18nc("@title:window", "Krita: Fatal error"), KisResourceLocator::instance()->errorMessages().join('\n'));
qApp->quit();
}
setSplashScreenLoadingText(i18n("Loading Resources..."));
processEvents();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment