Skip to content

Commit f2f34b5

Browse files
committed
Store the current dir in a syncable settings file for Kraft 2
Do not use a link any more to be more sync friendly. Other options, that should be synced between machines, can use that file as well. Fixes #268
1 parent 9dd78fc commit f2f34b5

File tree

2 files changed

+70
-26
lines changed

2 files changed

+70
-26
lines changed

src/defaultprovider.cpp

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <QSqlQuery>
1818
#include <QSqlTableModel>
1919
#include <QSqlRecord>
20+
#include <QSettings>
2021

2122
#include <QFile>
2223
#include <QTextStream>
@@ -40,29 +41,69 @@ QString DefaultProvider::_v2BaseDir{};
4041

4142
namespace {
4243

44+
// constants for the syncable Kraft settings for 2.0
45+
const QString SyncedSettingsFileName{ "Kraft2.ini"};
46+
const QString SyncedSettingsCurrDirEntry{"Global/currentDir"};
47+
const QString SyncedSettingsPrevDirEntry{"Global/previousDir"};
48+
49+
bool writeSyncedSettingsCurrDir(const QString basePath, const QString& currKraftDir)
50+
{
51+
bool re;
52+
const QFileInfo fi(basePath, SyncedSettingsFileName);
53+
54+
QSettings settings(fi.absoluteFilePath(), QSettings::NativeFormat);
55+
re = settings.isWritable();
56+
if (re) {
57+
// remember the previous entry
58+
const QString prevDir = settings.value(SyncedSettingsCurrDirEntry, "").toString();
59+
if (!prevDir.isEmpty()) {
60+
settings.setValue(SyncedSettingsPrevDirEntry, prevDir);
61+
}
62+
settings.setValue(SyncedSettingsCurrDirEntry, currKraftDir);
63+
settings.sync();
64+
}
65+
return re;
66+
}
67+
68+
4369
// get the v2BaseDir from the settings file and check if the current
4470
// link is pointing to a valid directory
4571
QString polishedBaseDir()
4672
{
73+
4774
QString re;
4875
const QString base = KraftSettings::self()->kraftV2BaseDir();
4976
if (base.isEmpty()) {
5077
qDebug() << "No v2 base dir in config file";
5178
return QString();
5279
}
53-
QDir dir(base);
54-
if (dir.cd("current")) {
55-
qDebug() << "Found Base Path in config file (with current)" << dir.path();
5680

57-
QFileInfo fi(dir.path());
81+
QFileInfo fiLink(base, "current");
5882

59-
if (fi.isSymLink()) {
60-
re = fi.symLinkTarget();
61-
} else {
62-
qDebug() << "ERROR: No proper current link in v2 base dir";
83+
if (fiLink.isSymLink()) { // Migrate oder pre-Kraft 2.0 installations
84+
QFileInfo fiTarget(fiLink.symLinkTarget()); // full path
85+
86+
// write the settings file
87+
const QString currKraftDir = fiTarget.fileName();
88+
89+
// remove the file "current"
90+
const QString currFileName = fiLink.absoluteFilePath();
91+
QFile::remove(currFileName); // remove the link
92+
93+
writeSyncedSettingsCurrDir(base, currKraftDir);
94+
}
95+
96+
QFileInfo fi(base, SyncedSettingsFileName);
97+
98+
if (fi.exists() ) {
99+
QSettings settings(fi.absoluteFilePath(), QSettings::NativeFormat);
100+
101+
QString relCurr = settings.value(SyncedSettingsCurrDirEntry, QString()).toString();
102+
if (!relCurr.isEmpty()) {
103+
fi.setFile(QDir(base), relCurr);
104+
re = fi.absoluteFilePath();
105+
qDebug() << "Current Kraft 2.0 base dir:" << re;
63106
}
64-
} else {
65-
qDebug() << "No current link in v2 base dir";
66107
}
67108
return re;
68109
}
@@ -410,23 +451,23 @@ bool DefaultProvider::switchToV2BaseDir(const QString& dirStr)
410451
{
411452
bool ok{false};
412453

413-
// snip off the md5 fragment
414-
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
415-
const QString fragment = dirStr.split("/", QString::SkipEmptyParts).last();
416-
#else
417-
const QString fragment = dirStr.split("/", Qt::SkipEmptyParts).last();
418-
#endif
419454
QDir dir(dirStr);
420-
if (dir.cdUp()) {
421-
const QString linkFile = dir.absoluteFilePath("current");
422-
QFile f(linkFile);
423-
if (f.exists()) {
424-
f.remove();
455+
const QString frag{dir.dirName()};
456+
457+
if (dir.exists() && !frag.isEmpty()) { // the new directory exists
458+
// remove the old link if existing
459+
if (dir.cdUp()) {
460+
const QString linkFile = dir.absoluteFilePath("current");
461+
QFile f(linkFile);
462+
if (f.exists()) {
463+
f.remove();
464+
}
465+
ok = writeSyncedSettingsCurrDir(dir.absolutePath(), frag);
425466
}
426-
ok = QFile::link(fragment, linkFile);
467+
427468
} else {
428469
// the fragment dir could not be created, try again, but only to a limit amount
429-
qDebug() << fragment << "could not be created in" << dir.absolutePath();
470+
qDebug() << frag << "could not be created in" << dir.absolutePath();
430471
ok = false;
431472
}
432473

tests/t_defaultprovider.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <QSqlDatabase>
55
#include <QSqlQuery>
66
#include <QStandardPaths>
7+
#include <QSettings>
78

89
#include "defaultprovider.h"
910

@@ -183,14 +184,16 @@ private Q_SLOTS:
183184

184185
const QString newDir = DefaultProvider::self()->createV2BaseDir(p);
185186
QVERIFY(newDir.startsWith(p));
186-
187+
qDebug() << "New directory:" << newDir;
187188
bool ok = DefaultProvider::self()->switchToV2BaseDir(newDir);
188189
QVERIFY(ok);
189190

190-
QFileInfo fi{p + "/current"};
191+
QSettings settings(p + "/Kraft2.ini", QSettings::NativeFormat);
192+
const QString frag = settings.value("Global/currentDir", "NOT_FOUND").toString();
193+
QVERIFY(frag != "NOT_FOUND");
194+
QFileInfo fi{p, frag};
191195
QVERIFY(fi.exists());
192196
QVERIFY(fi.isDir());
193-
194197
}
195198
};
196199

0 commit comments

Comments
 (0)