From 9dbb07285847717a2d696b5e803ad97b1741bf8c Mon Sep 17 00:00:00 2001 From: Camila Date: Wed, 30 Aug 2023 18:34:35 +0200 Subject: [PATCH] Migrate legacy user defined exclude file. - Refactor ConfigFile::setupDefaultExcludeFilePaths. - Refactor ConfigFile::excludeFile. - Fix code style. Signed-off-by: Camila --- src/gui/folderman.cpp | 16 +++++------ src/gui/userinfo.cpp | 4 +-- src/libsync/configfile.cpp | 55 +++++++++++++++++++------------------- src/libsync/configfile.h | 2 +- 4 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/gui/folderman.cpp b/src/gui/folderman.cpp index b69c1cfd4..7000392b3 100644 --- a/src/gui/folderman.cpp +++ b/src/gui/folderman.cpp @@ -184,7 +184,7 @@ int FolderMan::setupFolders() qCInfo(lcFolderMan) << "Setup folders from settings file"; - // this is done in Application::configVersionMigration + // this is done in Application::configVersionMigration QStringList skipSettingsKeys; backwardMigrationSettingsKeys(&skipSettingsKeys, &skipSettingsKeys); const auto accounts = AccountManager::instance()->accounts(); @@ -353,10 +353,10 @@ int FolderMan::setupFoldersMigration() qCInfo(lcFolderMan) << "Setup folders from " << configPath << "(migration)"; QDir dir(configPath); - //We need to include hidden files just in case the alias starts with '.' + // We need to include hidden files just in case the alias starts with '.' dir.setFilter(QDir::Files | QDir::Hidden); - //Exclude previous backed up configs e.g. oc.cfg.backup_20230831_133749_4.0.0 - //only need the current config in use by the legacy application + // Exclude previous backed up configs e.g. oc.cfg.backup_20230831_133749_4.0.0 + // 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 @@ -565,10 +565,10 @@ void FolderMan::setupFolderFromOldConfigFile(const QString &fileNamePath, Accoun if (const auto folder = addFolderInternal(folderDefinition, accountState, std::make_unique())) { auto ok = true; if (const auto legacyBlacklist = folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, - &ok);ok && !legacyBlacklist.isEmpty()) { + &ok); ok && !legacyBlacklist.isEmpty()) { qCInfo(lcFolderMan) << "Legacy selective sync list found:" << legacyBlacklist; - for(const auto &legacyFolder : legacyBlacklist) { - folder->migrateBlackListPath(legacyFolder); + for (const auto &legacyFolder : legacyBlacklist) { + folder->migrateBlackListPath(legacyFolder); } } else { qCInfo(lcFolderMan) << "There was a problem retriving the database selective sync for " << folder; @@ -576,7 +576,7 @@ void FolderMan::setupFolderFromOldConfigFile(const QString &fileNamePath, Accoun const auto settingLegacyBlacklist = settings.value(QLatin1String("blackList")).toStringList(); if (!settingLegacyBlacklist.empty()) { - //migrate settings + // 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 diff --git a/src/gui/userinfo.cpp b/src/gui/userinfo.cpp index 15106b119..6835cc628 100644 --- a/src/gui/userinfo.cpp +++ b/src/gui/userinfo.cpp @@ -110,10 +110,10 @@ void UserInfo::slotUpdateLastInfo(const QJsonDocument &json) AccountPtr account = _accountState->account(); - if (const auto newUserId = objData.value("id").toString();!newUserId.isEmpty()) { + if (const auto newUserId = objData.value("id").toString(); !newUserId.isEmpty()) { if (QString::compare(account->davUser(), newUserId, Qt::CaseInsensitive) != 0) { // TODO: the error message should be in the UI - qInfo() << "Authed with the wrong user! Please login with the account:" << account->prettyName(); + qInfo() << "Authenticated with the wrong user! Please login with the account:" << account->prettyName(); if (const auto cred = account->credentials()) { account->credentials()->askFromUser(); } diff --git a/src/libsync/configfile.cpp b/src/libsync/configfile.cpp index 60e3912f4..60cb2cc71 100644 --- a/src/libsync/configfile.cpp +++ b/src/libsync/configfile.cpp @@ -368,32 +368,27 @@ QString ConfigFile::configPath() const return Utility::trailingSlashPath(_confDir); } -static const QLatin1String exclFile("sync-exclude.lst"); +static const QLatin1String syncExclFile("sync-exclude.lst"); +static const QLatin1String exclFile("exclude.lst"); QString ConfigFile::excludeFile(Scope scope) const { - // prefer sync-exclude.lst, but if it does not exist, check for - // exclude.lst for compatibility reasons in the user writeable - // directories. - QFileInfo fi; - - switch (scope) { - case UserScope: - fi.setFile(configPath(), exclFile); - - if (!fi.isReadable()) { - fi.setFile(configPath(), QLatin1String("exclude.lst")); - } - if (!fi.isReadable()) { - fi.setFile(configPath(), exclFile); - } - return fi.absoluteFilePath(); - case SystemScope: + if (scope == SystemScope) { return ConfigFile::excludeFileFromSystem(); } - ASSERT(false); - return QString(); + const auto excludeFilePath = scope == LegacyScope ? discoveredLegacyConfigPath() : configPath(); + + // prefer sync-exclude.lst, but if it does not exist, check for exclude.lst + QFileInfo exclFileInfo(excludeFilePath, syncExclFile); + if (!exclFileInfo.isReadable()) { + exclFileInfo.setFile(excludeFilePath, exclFile); + } + if (!exclFileInfo.isReadable()) { + exclFileInfo.setFile(excludeFilePath, syncExclFile); + } + + return exclFileInfo.absoluteFilePath(); } QString ConfigFile::excludeFileFromSystem() @@ -1162,23 +1157,29 @@ std::unique_ptr ConfigFile::settingsWithGroup(const QString &group, Q void ConfigFile::setupDefaultExcludeFilePaths(ExcludedFiles &excludedFiles) { ConfigFile cfg; - QString systemList = cfg.excludeFile(ConfigFile::SystemScope); - QString userList = cfg.excludeFile(ConfigFile::UserScope); + const auto systemList = cfg.excludeFile(ConfigFile::SystemScope); + const auto userList = cfg.excludeFile(ConfigFile::UserScope); + const auto legacyList = cfg.excludeFile(ConfigFile::LegacyScope); if (!QFile::exists(userList)) { qCInfo(lcConfigFile) << "User defined ignore list does not exist:" << userList; - if (!QFile::copy(systemList, userList)) { - qCInfo(lcConfigFile) << "Could not copy over default list to:" << userList; + + if (QFile::exists(legacyList) && QFile::copy(legacyList, userList)) { + qCInfo(lcConfigFile) << "Migrating legacy list" << legacyList << "to user list" << userList; + + } else if (QFile::copy(systemList, userList)) { + qCInfo(lcConfigFile) << "Migrating system list" << legacyList << "to user list" << userList; } } if (!QFile::exists(userList)) { qCInfo(lcConfigFile) << "Adding system ignore list to csync:" << systemList; excludedFiles.addExcludeFilePath(systemList); - } else { - qCInfo(lcConfigFile) << "Adding user defined ignore list to csync:" << userList; - excludedFiles.addExcludeFilePath(userList); + return; } + + qCInfo(lcConfigFile) << "Adding user defined ignore list to csync:" << userList; + excludedFiles.addExcludeFilePath(userList); } QString ConfigFile::discoveredLegacyConfigPath() diff --git a/src/libsync/configfile.h b/src/libsync/configfile.h index 6906c21b7..21e58412a 100644 --- a/src/libsync/configfile.h +++ b/src/libsync/configfile.h @@ -41,7 +41,7 @@ public: ConfigFile(); enum Scope { UserScope, - SystemScope }; + SystemScope, LegacyScope }; [[nodiscard]] QString configPath() const; [[nodiscard]] QString configFile() const;