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

Externalize the location of the cachedb

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