Commit 34ebe4af authored by Boudewijn Rempt's avatar Boudewijn Rempt

Externalize the location of the cachedb

parent a27c392d
...@@ -23,11 +23,8 @@ ...@@ -23,11 +23,8 @@
#include <QDir> #include <QDir>
#include <QDirIterator> #include <QDirIterator>
#include <kconfiggroup.h>
#include <ksharedconfig.h>
const QString dbDriver = "QSQLITE"; const QString dbDriver = "QSQLITE";
const QString dbLocationKey = "cachedblocation";
class KisResourceCacheDb::Private class KisResourceCacheDb::Private
{ {
...@@ -35,13 +32,13 @@ public: ...@@ -35,13 +32,13 @@ public:
bool valid {false}; bool valid {false};
QSqlError initDb(); QSqlError initDb(const QString &location);
}; };
KisResourceCacheDb::KisResourceCacheDb() KisResourceCacheDb::KisResourceCacheDb(const QString &location)
: d(new Private()) : d(new Private())
{ {
QSqlError err = d->initDb(); QSqlError err = d->initDb(location);
if (err.isValid()) { if (err.isValid()) {
qWarning() << "Could not initialize the database:" << err; qWarning() << "Could not initialize the database:" << err;
} }
...@@ -57,19 +54,16 @@ bool KisResourceCacheDb::isValid() const ...@@ -57,19 +54,16 @@ bool KisResourceCacheDb::isValid() const
return d->valid; return d->valid;
} }
QSqlError KisResourceCacheDb::Private::initDb() QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
{ {
QSqlDatabase db = QSqlDatabase::addDatabase(dbDriver); QSqlDatabase db = QSqlDatabase::addDatabase(dbDriver);
const KConfigGroup group(KSharedConfig::openConfig(), "ResourceManagement"); QDir dbLocation(location);
QDir dbLocation(group.readEntry<QString>("cachedb", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)));
if (!dbLocation.exists()) { if (!dbLocation.exists()) {
dbLocation.mkpath(dbLocation.path()); dbLocation.mkpath(dbLocation.path());
} }
QString dbFile(group.readEntry<QString>("cachedb", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/" + ResourceCacheDbFilename)); db.setDatabaseName(location + "/" + ResourceCacheDbFilename);
db.setDatabaseName(dbFile);
if (!db.open()) { if (!db.open()) {
return db.lastError(); return db.lastError();
......
...@@ -35,17 +35,16 @@ class KRITARESOURCES_EXPORT KisResourceCacheDb ...@@ -35,17 +35,16 @@ class KRITARESOURCES_EXPORT KisResourceCacheDb
{ {
public: public:
enum class ResourceCacheDbStatus { /**
OK, * @brief KisResourceCacheDb create a resource cache database.
NoDatabase, * @param location the location of the database
}; */
explicit KisResourceCacheDb(const QString &location);
KisResourceCacheDb();
~KisResourceCacheDb(); ~KisResourceCacheDb();
/** /**
* @brief isValid * @brief isValid
* @return true if the database has been correctly created * @return true if the database has been correctly created, false if the database cannot be used
*/ */
bool isValid() const; bool isValid() const;
......
...@@ -23,15 +23,11 @@ ...@@ -23,15 +23,11 @@
#include <QStandardPaths> #include <QStandardPaths>
#include <QDir> #include <QDir>
#include <kconfiggroup.h>
#include <ksharedconfig.h>
#include <KisResourceCacheDb.h> #include <KisResourceCacheDb.h>
void TestResourceCacheDb::initTestCase() void TestResourceCacheDb::initTestCase()
{ {
const KConfigGroup group(KSharedConfig::openConfig(), "ResourceManagement"); QDir dbLocation(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
QDir dbLocation(group.readEntry<QString>("cachedb", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)));
if (dbLocation.exists()) { if (dbLocation.exists()) {
QFile(dbLocation.path() + "/" + ResourceCacheDbFilename).remove(); QFile(dbLocation.path() + "/" + ResourceCacheDbFilename).remove();
dbLocation.rmpath(dbLocation.path()); dbLocation.rmpath(dbLocation.path());
...@@ -40,7 +36,7 @@ void TestResourceCacheDb::initTestCase() ...@@ -40,7 +36,7 @@ void TestResourceCacheDb::initTestCase()
void TestResourceCacheDb::testCreateDatabase() void TestResourceCacheDb::testCreateDatabase()
{ {
KisResourceCacheDb cacheDb; KisResourceCacheDb cacheDb(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
Q_UNUSED(cacheDb); Q_UNUSED(cacheDb);
QVERIFY(cacheDb.isValid()); QVERIFY(cacheDb.isValid());
...@@ -70,8 +66,7 @@ void TestResourceCacheDb::testLookupTables() ...@@ -70,8 +66,7 @@ void TestResourceCacheDb::testLookupTables()
void TestResourceCacheDb::cleanupTestCase() void TestResourceCacheDb::cleanupTestCase()
{ {
const KConfigGroup group(KSharedConfig::openConfig(), "ResourceManagement"); QDir dbLocation(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
QDir dbLocation(group.readEntry<QString>("cachedb", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)));
bool res = QFile(dbLocation.path() + "/" + ResourceCacheDbFilename).remove(); bool res = QFile(dbLocation.path() + "/" + ResourceCacheDbFilename).remove();
Q_ASSERT(res); Q_ASSERT(res);
res = dbLocation.rmpath(dbLocation.path()); res = dbLocation.rmpath(dbLocation.path());
......
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