diff --git a/src/ui/conf/confmanager.cpp b/src/ui/conf/confmanager.cpp index dfecdef2..12b13bf9 100644 --- a/src/ui/conf/confmanager.cpp +++ b/src/ui/conf/confmanager.cpp @@ -8,14 +8,11 @@ #include #include -#include -#include #include -#include #include -#include #include #include +#include #include #include #include @@ -23,7 +20,6 @@ #include #include #include -#include #include "addressgroup.h" #include "appgroup.h" @@ -454,6 +450,11 @@ bool importFile(const QString &filePath, const QString &path) return true; } +void showErrorMessage(const QString &errorMessage) +{ + IoC()->showErrorBox(errorMessage); +} + } ConfManager::ConfManager(const QString &filePath, QObject *parent, quint32 openFlags) : @@ -584,6 +585,17 @@ void ConfManager::setupDefault(FirewallConf &conf) const conf.addDefaultAppGroup(); } +bool ConfManager::checkCanMigrate(Settings *settings) const +{ + QString viaVersion; + if (!settings->canMigrate(viaVersion)) { + showErrorMessage(tr("Please first install Fort Firewall v%1 and save Options from it.") + .arg(viaVersion)); + return false; + } + return true; +} + bool ConfManager::loadConf(FirewallConf &conf) { if (conf.optEdited()) { @@ -603,16 +615,22 @@ bool ConfManager::loadConf(FirewallConf &conf) return true; } -bool ConfManager::load() +void ConfManager::load() { Q_ASSERT(conf()); - if (!loadConf(*conf())) - return false; + if (!loadConf(*conf())) { + showErrorMessage(tr("Cannot load Settings")); + return; + } applySavedConf(conf()); +} - return true; +void ConfManager::reload() +{ + setConf(createConf()); + load(); } bool ConfManager::saveConf(FirewallConf &conf) @@ -771,18 +789,28 @@ bool ConfManager::exportBackup(const QString &path) // Export User Ini { - if (!exportFile(iniUser().settings()->filePath(), outPath)) + Settings *settings = iniUser().settings(); + + if (!exportFile(settings->filePath(), outPath)) return false; } - return exportMasterBackup(outPath); + // Export DB + if (!exportMasterBackup(outPath)) { + qCWarning(LC) << "Export error:" << path; + return false; + } + + return true; } bool ConfManager::exportMasterBackup(const QString &path) { // Export Ini { - if (!exportFile(conf()->ini().settings()->filePath(), path)) + Settings *settings = conf()->ini().settings(); + + if (!exportFile(settings->filePath(), path)) return false; } @@ -794,7 +822,7 @@ bool ConfManager::exportMasterBackup(const QString &path) FileUtil::removeFile(destFilePath); if (!sqliteDb()->vacuumInto(destFilePath)) { - qCWarning(LC) << "Export Db error:" << sqliteDb()->errorMessage() << "to:" << path; + qCWarning(LC) << "Export Db error:" << sqliteDb()->errorMessage(); return false; } } @@ -810,49 +838,51 @@ bool ConfManager::importBackup(const QString &path) { Settings *settings = iniUser().settings(); + if (!checkCanMigrate(settings)) + return false; + if (!importFile(settings->filePath(), inPath)) return false; - settings->clearCache(); + settings->reload(); emit iniUserChanged(iniUser(), /*onlyFlags=*/false); } // Import DB - return importMasterBackup(inPath); + if (!importMasterBackup(inPath)) { + qCWarning(LC) << "Import error:" << path; + return false; + } + + return true; } bool ConfManager::importMasterBackup(const QString &path) { - bool ok; - // Import Ini { Settings *settings = conf()->ini().settings(); - ok = importFile(settings->filePath(), path); + if (!importFile(settings->filePath(), path)) + return false; - settings->clearCache(); + settings->reload(); } // Import Db - if (ok) { - SqliteDb::MigrateOptions opt = migrateOptions(); + SqliteDb::MigrateOptions opt = migrateOptions(); - opt.backupFilePath = path + FileUtil::fileName(sqliteDb()->filePath()); + opt.backupFilePath = path + FileUtil::fileName(sqliteDb()->filePath()); - ok = sqliteDb()->import(opt); - } + if (!sqliteDb()->import(opt)) + return false; - if (ok) { - emit imported(); + emit imported(); - load(); // Reload conf - } else { - qCWarning(LC) << "Import error:" << path; - } + reload(); // Reload conf - return ok; + return true; } bool ConfManager::checkPassword(const QString &password) diff --git a/src/ui/conf/confmanager.h b/src/ui/conf/confmanager.h index d083a5f9..c3f2bf81 100644 --- a/src/ui/conf/confmanager.h +++ b/src/ui/conf/confmanager.h @@ -13,6 +13,7 @@ class FirewallConf; class IniOptions; class IniUser; +class Settings; class TaskInfo; class ConfManager : public QObject, public IocService @@ -39,8 +40,11 @@ public: void initIniUserToEdit(); void setIniUserToEdit(IniUser *iniUser); + bool checkCanMigrate(Settings *settings) const; + bool loadConf(FirewallConf &conf); - bool load(); + void load(); + void reload(); virtual bool saveConf(FirewallConf &conf); void applySavedConf(FirewallConf *newConf); diff --git a/src/ui/form/opt/optionscontroller.cpp b/src/ui/form/opt/optionscontroller.cpp index 260c6906..fa446e5b 100644 --- a/src/ui/form/opt/optionscontroller.cpp +++ b/src/ui/form/opt/optionscontroller.cpp @@ -108,14 +108,6 @@ void OptionsController::resetEdited() emit editResetted(); } -void OptionsController::initialize() -{ - // Settings/configuration was migrated? - if (settings()->wasMigrated()) { - setOptEdited(); - } -} - void OptionsController::save(bool closeOnSuccess) { emit aboutToSave(); diff --git a/src/ui/form/opt/optionscontroller.h b/src/ui/form/opt/optionscontroller.h index 9911755d..77962f11 100644 --- a/src/ui/form/opt/optionscontroller.h +++ b/src/ui/form/opt/optionscontroller.h @@ -19,8 +19,6 @@ public: bool anyEdited() const; - void initialize(); - signals: void editedChanged(bool anyEdited); diff --git a/src/ui/form/opt/optionswindow.cpp b/src/ui/form/opt/optionswindow.cpp index bf2ab41b..7ba57273 100644 --- a/src/ui/form/opt/optionswindow.cpp +++ b/src/ui/form/opt/optionswindow.cpp @@ -68,8 +68,6 @@ void OptionsWindow::restoreWindowState() void OptionsWindow::setupController() { - ctrl()->initialize(); - connect(ctrl(), &OptionsController::editedChanged, this, &QWidget::setWindowModified); connect(ctrl(), &OptionsController::retranslateUi, this, &OptionsWindow::retranslateUi); diff --git a/src/ui/fortmanager.cpp b/src/ui/fortmanager.cpp index 249773ca..d2ef767a 100644 --- a/src/ui/fortmanager.cpp +++ b/src/ui/fortmanager.cpp @@ -59,11 +59,6 @@ bool canInstallDriver(FortSettings *settings) return (canInstallDriver && isAdmin); } -void showErrorMessage(const QString &errorMessage) -{ - IoC()->showErrorBox(errorMessage); -} - inline void setupMasterServices(IocContainer *ioc, const FortSettings *settings) { ioc->setService(new ConfManager(settings->confFilePath())); @@ -488,13 +483,11 @@ void FortManager::processRestartRequired(const QString &info) void FortManager::loadConf() { + auto confManager = IoC(); const auto settings = IoC(); // Validate migration - QString viaVersion; - if (!settings->canMigrate(viaVersion)) { - showErrorMessage(tr("Please first install Fort Firewall v%1 and save Options from it.") - .arg(viaVersion)); + if (!confManager->checkCanMigrate(settings)) { exit(-1); // Exit the program } @@ -503,9 +496,7 @@ void FortManager::loadConf() : settings->hasService() ? "Client" : "Program"); - if (!IoC()->load()) { - showErrorMessage(tr("Cannot load Settings")); - } + confManager->load(); } bool FortManager::setupDriverConf() diff --git a/src/ui/fortsettings.cpp b/src/ui/fortsettings.cpp index 7d5aaf06..0a80627f 100644 --- a/src/ui/fortsettings.cpp +++ b/src/ui/fortsettings.cpp @@ -499,7 +499,7 @@ void FortSettings::writeConfIniOptions(const IniOptions &ini) } } -void FortSettings::migrateIniOnStartup() +void FortSettings::migrateIniOnLoad() { if (!iniExists()) { iniFlush(); @@ -576,36 +576,6 @@ void FortSettings::migrateIniOnWrite() } } -bool FortSettings::wasMigrated() const -{ - int version; - if (checkIniVersion(version)) - return false; - -#if 0 - // COMPAT: v3.0.0 - if (version < 0x030000 && appVersion() >= 0x030000) - return true; -#endif - - return false; -} - -bool FortSettings::canMigrate(QString &viaVersion) const -{ - int version; - if (checkIniVersion(version)) - return true; - - // COMPAT: v3.0.0 - if (version < 0x030000 && appVersion() > 0x030000) { - viaVersion = "3.0.0"; - return false; - } - - return true; -} - QStringList FortSettings::unlockTypeStrings() { return { tr("Window closed"), tr("Session lockout"), tr("Program exit") }; diff --git a/src/ui/fortsettings.h b/src/ui/fortsettings.h index 7cb10b12..74b7da31 100644 --- a/src/ui/fortsettings.h +++ b/src/ui/fortsettings.h @@ -87,9 +87,6 @@ public: void setupGlobal(); void initialize(const QStringList &args, EnvManager *envManager); - bool wasMigrated() const; - bool canMigrate(QString &viaVersion) const; - static bool isPortable(); static QString defaultProfilePath(bool isService); @@ -106,7 +103,7 @@ public slots: void writeConfIniOptions(const IniOptions &ini); protected: - void migrateIniOnStartup() override; + void migrateIniOnLoad() override; void migrateIniOnWrite() override; private: diff --git a/src/ui/user/usersettings.cpp b/src/ui/user/usersettings.cpp index 95c651fd..957fe8de 100644 --- a/src/ui/user/usersettings.cpp +++ b/src/ui/user/usersettings.cpp @@ -16,7 +16,7 @@ void UserSettings::setUp() setupIni(settings->userPath() + APP_BASE + ".user.ini"); } -void UserSettings::migrateIniOnStartup() +void UserSettings::migrateIniOnLoad() { if (!iniExists()) { iniUser().saveDefaultIni(); @@ -27,7 +27,7 @@ void UserSettings::migrateIniOnStartup() if (checkIniVersion(version)) return; - Settings::migrateIniOnStartup(); + Settings::migrateIniOnLoad(); // COMPAT: v3.4.0: .ini ~> .user.ini if (version < 0x030400) { diff --git a/src/ui/user/usersettings.h b/src/ui/user/usersettings.h index c45b7fa5..0dd503a6 100644 --- a/src/ui/user/usersettings.h +++ b/src/ui/user/usersettings.h @@ -19,7 +19,7 @@ public: void setUp() override; protected: - void migrateIniOnStartup() override; + void migrateIniOnLoad() override; void migrateIniOnWrite() override; private: diff --git a/src/ui/util/ini/settings.cpp b/src/ui/util/ini/settings.cpp index c433b73f..6f38b7cf 100644 --- a/src/ui/util/ini/settings.cpp +++ b/src/ui/util/ini/settings.cpp @@ -14,6 +14,21 @@ const QLoggingCategory LC("settings"); Settings::Settings(QObject *parent) : QObject(parent) { } +bool Settings::canMigrate(QString &viaVersion) const +{ + int version; + if (checkIniVersion(version)) + return true; + + // COMPAT: v3.0.0 + if (version < 0x030000 && appVersion() > 0x030000) { + viaVersion = "3.0.0"; + return false; + } + + return true; +} + bool Settings::checkIniVersion(int &oldVersion) const { if (!iniExists()) @@ -46,7 +61,7 @@ void Settings::setupIni(const QString &filePath) m_iniExists = FileUtil::fileExists(iniPath); m_ini = new QSettings(iniPath, QSettings::IniFormat, this); - migrateIniOnStartup(); + migrateIniOnLoad(); } void Settings::migrateIniOnWrite() @@ -158,6 +173,15 @@ void Settings::setCacheValue(const QString &key, const QVariant &value) const m_cache.insert(key, value); } +void Settings::reload() +{ + m_iniExists = true; + + clearCache(); + + migrateIniOnLoad(); +} + void Settings::clearCache() { m_cache.clear(); diff --git a/src/ui/util/ini/settings.h b/src/ui/util/ini/settings.h index f491bf51..2eb241e5 100644 --- a/src/ui/util/ini/settings.h +++ b/src/ui/util/ini/settings.h @@ -18,8 +18,11 @@ public: QString filePath() const { return ini()->fileName(); } + void reload(); void clearCache(); + bool canMigrate(QString &viaVersion) const; + protected: bool iniVersionSet() const { return ini()->contains("base/version"); } @@ -35,7 +38,7 @@ protected: void setupIni(const QString &filePath); - virtual void migrateIniOnStartup() { } + virtual void migrateIniOnLoad() { } virtual void migrateIniOnWrite(); bool iniBool(const QString &key, bool defaultValue = false) const;