Migrate all legacy folders for each legacy account.
authorCamila <hello@camila.codes>
Tue, 12 Sep 2023 17:35:34 +0000 (19:35 +0200)
committerMatthieu Gallien <matthieu_gallien@yahoo.fr>
Wed, 13 Sep 2023 12:49:27 +0000 (14:49 +0200)
Also cleans up FolderMan::setupFolderFromOldConfigFile logic.

Signed-off-by: Camila <hello@camila.codes>
src/gui/folderman.cpp

index 7000392b3f5ec4e4c6295e1e764743b4bf1572b7..fee5eaef61baccdeb525aa2f880d6f5460294f2c 100644 (file)
@@ -359,11 +359,13 @@ int FolderMan::setupFoldersMigration()
     // only need the current config in use by the legacy application
     const auto dirFiles = dir.entryList({"*.cfg"});
 
-    // Normally there should be only one account when migrating. TODO: Should assume only one legacy config file
-    const auto accountState = AccountManager::instance()->accounts().value(0).data();
+    // Migrate all folders for each account found in legacy config file(s)
+    const auto legacyAccounts = AccountManager::instance()->accounts();
     for (const auto &fileName : dirFiles) {
-        const auto fullFilePath = dir.filePath(fileName);
-        setupFolderFromOldConfigFile(fullFilePath, accountState);
+        for (const auto &accountState : legacyAccounts) {
+            const auto fullFilePath = dir.filePath(fileName);
+            setupFolderFromOldConfigFile(fullFilePath, accountState.data());
+        }
     }
 
     emit folderListChanged(_folderMap);
@@ -515,94 +517,88 @@ void FolderMan::setupFolderFromOldConfigFile(const QString &fileNamePath, Accoun
     }
 
     settings.beginGroup(settingsAccountsC);
-    const auto rootChildGroups = settings.childGroups();
-    for (const auto &accountId : rootChildGroups) {
-        qCDebug(lcFolderMan) << "try to migrate accountId:" << accountId;
-        settings.beginGroup(accountId);
-        settings.beginGroup(settingsFoldersC);
+    qCDebug(lcFolderMan) << "try to migrate accountId:" << accountState->account()->id();
+    settings.beginGroup(accountState->account()->id());
+    settings.beginGroup(settingsFoldersC);
 
-        if (settings.childGroups().isEmpty()) {
-            continue;
-        }
+    if (settings.childGroups().isEmpty()) {
+        qCDebug(lcFolderMan) << "there are no legacy folders for accountId:" << accountState->account()->id();
+        return;
+    }
 
-        const auto childGroups = settings.childGroups();
-        for (const auto &alias : childGroups) {
-            settings.beginGroup(alias);
-            qCDebug(lcFolderMan) << "try to migrate folder alias:" << alias;
+    const auto childGroups = settings.childGroups();
+    for (const auto &alias : childGroups) {
+        settings.beginGroup(alias);
+        qCDebug(lcFolderMan) << "try to migrate folder alias:" << alias;
 
-            const auto path = settings.value(QLatin1String("localPath")).toString();
-            const auto targetPath = settings.value(QLatin1String("targetPath")).toString();
-            const auto journalPath = settings.value(QLatin1String("journalPath")).toString();
-            const auto paused = settings.value(QLatin1String("paused"), false).toBool();
-            const auto ignoreHiddenFiles = settings.value(QLatin1String("ignoreHiddenFiles"), false).toBool();
+        const auto path = settings.value(QLatin1String("localPath")).toString();
+        const auto targetPath = settings.value(QLatin1String("targetPath")).toString();
+        const auto journalPath = settings.value(QLatin1String("journalPath")).toString();
+        const auto paused = settings.value(QLatin1String("paused"), false).toBool();
+        const auto ignoreHiddenFiles = settings.value(QLatin1String("ignoreHiddenFiles"), false).toBool();
 
-            if (path.isEmpty()) {
-                qCDebug(lcFolderMan) << "localPath is empty";
-                settings.endGroup();
-                continue;
-            }
+        if (path.isEmpty()) {
+            qCDebug(lcFolderMan) << "localPath is empty";
+            settings.endGroup();
+            continue;
+        }
 
-            if (targetPath.isEmpty()) {
-                qCDebug(lcFolderMan) << "targetPath is empty";
-                settings.endGroup();
-                continue;
-            }
+        if (targetPath.isEmpty()) {
+            qCDebug(lcFolderMan) << "targetPath is empty";
+            settings.endGroup();
+            continue;
+        }
 
-            if (journalPath.isEmpty()) {
-                qCDebug(lcFolderMan) << "journalPath is empty";
-                settings.endGroup();
-                continue;
-            }
+        if (journalPath.isEmpty()) {
+            qCDebug(lcFolderMan) << "journalPath is empty";
+            settings.endGroup();
+            continue;
+        }
 
-            FolderDefinition folderDefinition;
-            folderDefinition.alias = alias;
-            folderDefinition.localPath = path;
-            folderDefinition.targetPath = targetPath;
-            folderDefinition.journalPath = journalPath;
-            folderDefinition.paused = paused;
-            folderDefinition.ignoreHiddenFiles = ignoreHiddenFiles;
-
-            if (const auto folder = addFolderInternal(folderDefinition, accountState, std::make_unique<VfsOff>())) {
-                auto ok = true;
-                if (const auto legacyBlacklist = folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList,
-                                                                                      &ok); ok && !legacyBlacklist.isEmpty()) {
-                    qCInfo(lcFolderMan) << "Legacy selective sync list found:" << legacyBlacklist;
-                    for (const auto &legacyFolder : legacyBlacklist) {
-                        folder->migrateBlackListPath(legacyFolder);
-                    }
-                } else {
-                    qCInfo(lcFolderMan) << "There was a problem retriving the database selective sync for " << folder;
-                }
+        FolderDefinition folderDefinition;
+        folderDefinition.alias = alias;
+        folderDefinition.localPath = path;
+        folderDefinition.targetPath = targetPath;
+        folderDefinition.journalPath = journalPath;
+        folderDefinition.paused = paused;
+        folderDefinition.ignoreHiddenFiles = ignoreHiddenFiles;
+
+        if (const auto folder = addFolderInternal(folderDefinition, accountState, std::make_unique<VfsOff>())) {
+            auto ok = true;
+            auto legacyBlacklist = folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList,
+                                                                                   &ok);
+            if (!ok) {
+                qCInfo(lcFolderMan) << "There was a problem retriving the database selective sync for " << folder;
+            }
 
-                const auto settingLegacyBlacklist = settings.value(QLatin1String("blackList")).toStringList();
-                if (!settingLegacyBlacklist.empty()) {
-                    // migrate settings
-                    folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, settingLegacyBlacklist);
-                    settings.remove(QLatin1String("blackList"));
-                    // FIXME: If you remove this codepath, you need to provide another way to do
-                    // this via theme.h or the normal FolderMan::setupFolders
+            legacyBlacklist << settings.value(QLatin1String("blackList")).toStringList();
+            if (!legacyBlacklist.isEmpty()) {
+                qCInfo(lcFolderMan) << "Legacy selective sync list found:" << legacyBlacklist;
+                for (const auto &legacyFolder : legacyBlacklist) {
+                    folder->migrateBlackListPath(legacyFolder);
                 }
+                settings.remove(QLatin1String("blackList"));
+            }
 
-                folder->saveToSettings();
+            folder->saveToSettings();
 
-                qCInfo(lcFolderMan) << "Migrated!" << folder->path();
-                settings.sync();
+            qCInfo(lcFolderMan) << "Migrated!" << folder->path();
+            settings.sync();
 
-                if (!folder) {
-                    continue;
-                }
-
-                scheduleFolder(folder);
-                emit folderSyncStateChange(folder);
+            if (!folder) {
+                continue;
             }
 
-            settings.endGroup();
+            scheduleFolder(folder);
+            emit folderSyncStateChange(folder);
         }
 
         settings.endGroup();
-        settings.endGroup();
     }
 
+    settings.endGroup();
+    settings.endGroup();
+
     return;
 }