Commit 0da7f65c authored by Boudewijn Rempt's avatar Boudewijn Rempt

Fix KisResourceLocator and test

parent d80062aa
......@@ -90,19 +90,21 @@ KisResourceLocator::LocatorError KisResourceLocator::initialize(const QString &i
}
// Check whether we're updating from an older version
if (initalizationStatus != InitalizationStatus::FirstRun && !QDir(installationResourcesLocation).entryList().contains("KRITA_RESOURCE_VERSION")) {
initalizationStatus = InitalizationStatus::FirstUpdate;
}
else {
QFile fi(installationResourcesLocation + '/' + "KRITA_RESOURCE_VERSION");
fi.open(QFile::ReadOnly);
QVersionNumber resource_version = QVersionNumber::fromString(QString::fromUtf8(fi.readAll()));
QVersionNumber krita_version = QVersionNumber::fromString(KritaVersionWrapper::versionString());
if (krita_version > resource_version) {
initalizationStatus = InitalizationStatus::Updating;
if (initalizationStatus != InitalizationStatus::FirstRun) {
QFile fi(d->resourceLocation + '/' + "KRITA_RESOURCE_VERSION");
if (!fi.exists()) {
initalizationStatus = InitalizationStatus::FirstUpdate;
}
else {
initalizationStatus = InitalizationStatus::Initialized;
fi.open(QFile::ReadOnly);
QVersionNumber resource_version = QVersionNumber::fromString(QString::fromUtf8(fi.readAll()));
QVersionNumber krita_version = QVersionNumber::fromString(KritaVersionWrapper::versionString());
if (krita_version > resource_version) {
initalizationStatus = InitalizationStatus::Updating;
}
else {
initalizationStatus = InitalizationStatus::Initialized;
}
}
}
......@@ -121,10 +123,11 @@ KisResourceLocator::LocatorError KisResourceLocator::initialize(const QString &i
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()));
}
return synchronizeDb();
return LocatorError::Ok;
}
QStringList KisResourceLocator::errorMessages() const
......@@ -139,7 +142,7 @@ KisResourceLocator::LocatorError KisResourceLocator::firstTimeInstallation(Inita
Q_FOREACH(const QString &folder, resourceTypeFolders) {
QDir dir(d->resourceLocation + '/' + folder + '/');
if (!dir.exists()) {
if (!dir.mkpath(dir.path())) {
if (!QDir().mkpath(d->resourceLocation + '/' + folder + '/')) {
d->errorMessages << i18n("3. Could not create the resource location at %1.", dir.path());
return LocatorError::CannotCreateLocation;
}
......@@ -147,9 +150,9 @@ KisResourceLocator::LocatorError KisResourceLocator::firstTimeInstallation(Inita
}
Q_FOREACH(const QString &folder, resourceTypeFolders) {
QDir dir(installationResourcesLocation + "/share/krita/" + folder + '/');
QDir dir(installationResourcesLocation + '/' + folder + '/');
if (dir.exists()) {
Q_FOREACH(const QString &entry, dir.entryList(QDir::Files | QDir::Readable | QDir::NoDotAndDotDot)) {
Q_FOREACH(const QString &entry, dir.entryList(QDir::Files | QDir::Readable)) {
QFile f(dir.canonicalPath() + '/'+ entry);
bool r = f.copy(d->resourceLocation + '/' + folder + '/' + entry);
if (!r) {
......@@ -159,7 +162,7 @@ KisResourceLocator::LocatorError KisResourceLocator::firstTimeInstallation(Inita
}
}
QFile f(installationResourcesLocation + '/' + "KRITA_RESOURCE_VERSION");
QFile f(d->resourceLocation + '/' + "KRITA_RESOURCE_VERSION");
f.open(QFile::WriteOnly);
f.write(KritaVersionWrapper::versionString().toUtf8());
f.close();
......@@ -167,7 +170,10 @@ KisResourceLocator::LocatorError KisResourceLocator::firstTimeInstallation(Inita
return LocatorError::Ok;
}
KisResourceLocator::LocatorError KisResourceLocator::synchronizeDb()
bool KisResourceLocator::synchronizeDb()
{
return LocatorError::Ok;
Q_FOREACH(const KisResourceStorageSP storage, d->storages) {
qDebug() << "Storage" << storage->location() << storage->valid();
}
return true;
}
......@@ -57,6 +57,8 @@ public:
QStringList errorMessages() const;
bool synchronizeDb();
private:
enum class InitalizationStatus {
......@@ -64,11 +66,11 @@ private:
Initialized, // Everything is ready to start synchronizing the database
FirstRun, // Krita hasn't run for this resource location yet
FirstUpdate, // Krita was installed, but it's a version from before the resource locator existed, only user-defined resources are present
Updating // Krita is updating from an older version with a resource locator
Updating // Krita is updating from an older version with resource locator
};
LocatorError firstTimeInstallation(InitalizationStatus initalizationStatus, const QString &installationResourcesLocation);
LocatorError synchronizeDb();
class Private;
......
......@@ -62,6 +62,11 @@ KisResourceStorage::~KisResourceStorage()
}
QString KisResourceStorage::location() const
{
return d->location;
}
KisResourceStorage::StorageType KisResourceStorage::type() const
{
return d->storageType;
......
......@@ -47,6 +47,7 @@ public:
KisResourceStorage(const QString &location);
~KisResourceStorage();
QString location() const;
bool valid() const;
StorageType type() const;
......
......@@ -21,6 +21,7 @@
#include <QTest>
#include <QVersionNumber>
#include <QDirIterator>
#include <kconfig.h>
#include <kconfiggroup.h>
......@@ -37,8 +38,6 @@
#error "FILES_DEST_DIR not set. A directory where data will be written to for testing installing resources"
#endif
void TestResourceLocator::initTestCase()
{
srcLocation = QString(FILES_DATA_DIR);
......@@ -52,7 +51,7 @@ void TestResourceLocator::initTestCase()
void TestResourceLocator::testLocator()
{
KisResourceLocator locator;
KisResourceLocator::LocatorError r = locator.initialize(dstLocation);
KisResourceLocator::LocatorError r = locator.initialize(srcLocation);
if (!locator.errorMessages().isEmpty()) qDebug() << locator.errorMessages();
QVERIFY(r == KisResourceLocator::LocatorError::Ok);
QVERIFY(QDir(dstLocation).exists());
......@@ -61,13 +60,17 @@ void TestResourceLocator::testLocator()
QDir srcDir(srcLocation + '/' + folder + '/');
QVERIFY(dstDir.exists());
QVERIFY(dstDir.entryList(QDir::NoDotAndDotDot) == srcDir.entryList(QDir::NoDotAndDotDot));
QVERIFY(dstDir.entryList(QDir::Files | QDir::NoDotAndDotDot) == srcDir.entryList(QDir::Files | QDir::NoDotAndDotDot));
}
QFile f(dstLocation + '/' + "KRITA_RESOURCE_VERSION");
QVERIFY(f.exists());
f.open(QFile::ReadOnly);
QVersionNumber version = QVersionNumber::fromString(QString::fromUtf8(f.readAll()));
QVERIFY(version == QVersionNumber::fromString(KritaVersionWrapper::versionString()));
locator.synchronizeDb();
}
void TestResourceLocator::cleanupTestCase()
......@@ -78,17 +81,27 @@ void TestResourceLocator::cleanupTestCase()
bool TestResourceLocator::cleanDstLocation()
{
if (QDir(dstLocation).exists()) {
Q_FOREACH(const QString &folder, KisResourceLocator::resourceTypeFolders) {
QDir dir(dstLocation + '/' + folder + '/');
if (dir.exists()) {
Q_FOREACH(const QString &entry, dir.entryList(QDir::NoDotAndDotDot)) {
QFile f(dir.canonicalPath() + '/' + entry);
f.remove();
}
{
QDirIterator iter(dstLocation, QStringList() << "*", QDir::Files, QDirIterator::Subdirectories);
while (iter.hasNext()) {
iter.next();
QFile f(iter.filePath());
bool r = f.remove();
//qDebug() << (r ? "Removed" : "Failed to remove") << iter.filePath();
}
}
{
QDirIterator iter(dstLocation, QStringList() << "*", QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (iter.hasNext()) {
iter.next();
bool r = QDir(iter.filePath()).rmpath(iter.filePath());
//qDebug() << (r ? "Removed" : "Failed to remove") << iter.filePath();
}
}
return QDir().rmpath(dstLocation);
}
return QDir().rmpath(dstLocation);
return true;
}
QTEST_MAIN(TestResourceLocator)
......
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