UI: Options: Fix conf reloading after import

This commit is contained in:
Nodir Temirkhodjaev 2024-10-17 20:04:05 +05:00
parent 42630d9e82
commit f0cd85d589
12 changed files with 103 additions and 96 deletions

View File

@ -8,14 +8,11 @@
#include <sqlite/sqlitedb.h>
#include <sqlite/sqlitestmt.h>
#include <appinfo/appinfocache.h>
#include <appinfo/appinfoutil.h>
#include <driver/drivermanager.h>
#include <fortmanager.h>
#include <fortsettings.h>
#include <log/logmanager.h>
#include <manager/envmanager.h>
#include <manager/serviceinfomanager.h>
#include <manager/windowmanager.h>
#include <task/taskinfo.h>
#include <task/taskmanager.h>
#include <user/iniuser.h>
@ -23,7 +20,6 @@
#include <util/conf/confutil.h>
#include <util/fileutil.h>
#include <util/ioc/ioccontainer.h>
#include <util/startuputil.h>
#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<WindowManager>()->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)

View File

@ -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);

View File

@ -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();

View File

@ -19,8 +19,6 @@ public:
bool anyEdited() const;
void initialize();
signals:
void editedChanged(bool anyEdited);

View File

@ -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);

View File

@ -59,11 +59,6 @@ bool canInstallDriver(FortSettings *settings)
return (canInstallDriver && isAdmin);
}
void showErrorMessage(const QString &errorMessage)
{
IoC<WindowManager>()->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<ConfManager>();
const auto settings = IoC<FortSettings>();
// 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<ConfManager>()->load()) {
showErrorMessage(tr("Cannot load Settings"));
}
confManager->load();
}
bool FortManager::setupDriverConf()

View File

@ -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") };

View File

@ -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:

View File

@ -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) {

View File

@ -19,7 +19,7 @@ public:
void setUp() override;
protected:
void migrateIniOnStartup() override;
void migrateIniOnLoad() override;
void migrateIniOnWrite() override;
private:

View File

@ -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();

View File

@ -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;