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
4142namespace {
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
4571QString 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
0 commit comments