UI: Separate Data/FortFirewall.ini from <User>/FortFirewall.user.ini

This commit is contained in:
Nodir Temirkhodjaev 2021-05-16 17:24:32 +03:00
parent b8f5b846b2
commit 03c635eaa1
56 changed files with 824 additions and 565 deletions

View File

@ -3,7 +3,7 @@
;Fort Firewall global configuration
;ATTENTION: Use slashes as path separator (C:/path/to)!
;ATTENTION: Use slashes as path separator (C:/path/to/directory/)!!
[global]
@ -22,7 +22,7 @@
;defaultLanguage=en
;Directory to store settings.
;Default is "%APPDATA%/Fort Firewall".
;Default is "%LocalAppData%/Fort Firewall", but "%ProgramData%/Fort Firewall" for a Service.
;profileDir=%FORTHOME%/Data
;Directory to store statistics.
@ -37,3 +37,6 @@
;Default is "<profileDir>/cache".
;cacheDir=%FORTHOME%/Data/cache
;Directory to store user settings.
;Default is "%LocalAppData%/Fort Firewall".
;userDir=%FORTHOME%/Data

View File

@ -105,6 +105,8 @@ SOURCES += \
task/taskworker.cpp \
task/taskzonedownloader.cpp \
translationmanager.cpp \
user/iniuser.cpp \
user/usersettings.cpp \
util/conf/addressrange.cpp \
util/conf/confutil.cpp \
util/dateutil.cpp \
@ -114,6 +116,8 @@ SOURCES += \
util/guiutil.cpp \
util/hotkeymanager.cpp \
util/iconcache.cpp \
util/ini/mapsettings.cpp \
util/ini/settings.cpp \
util/json/jsonutil.cpp \
util/json/mapwrapper.cpp \
util/logger.cpp \
@ -242,6 +246,8 @@ HEADERS += \
task/taskworker.h \
task/taskzonedownloader.h \
translationmanager.h \
user/iniuser.h \
user/usersettings.h \
util/classhelpers.h \
util/conf/addressrange.h \
util/conf/confappswalker.h \
@ -253,6 +259,8 @@ HEADERS += \
util/guiutil.h \
util/hotkeymanager.h \
util/iconcache.h \
util/ini/mapsettings.h \
util/ini/settings.h \
util/json/jsonutil.h \
util/json/mapwrapper.h \
util/logger.h \

View File

@ -11,6 +11,7 @@
#include "../fortsettings.h"
#include "../task/taskinfo.h"
#include "../task/taskmanager.h"
#include "../user/iniuser.h"
#include "../util/conf/confutil.h"
#include "../util/dateutil.h"
#include "../util/fileutil.h"
@ -363,6 +364,11 @@ TaskManager *ConfManager::taskManager() const
return fortManager()->taskManager();
}
IniUser *ConfManager::iniUser() const
{
return fortManager()->iniUser();
}
void ConfManager::showErrorMessage(const QString &errorMessage)
{
logWarning() << "Error:" << errorMessage;
@ -432,8 +438,7 @@ void ConfManager::setConf(FirewallConf *newConf)
FirewallConf *ConfManager::createConf()
{
FirewallConf *conf = new FirewallConf(this);
conf->ini().setSettings(settings());
FirewallConf *conf = new FirewallConf(settings(), this);
return conf;
}
@ -496,7 +501,7 @@ bool ConfManager::saveConf(FirewallConf &conf)
void ConfManager::applySavedConf(FirewallConf *newConf)
{
if (!newConf->anyEdited() || newConf->iniStateEdited())
if (!newConf->anyEdited())
return;
const bool onlyFlags = !newConf->optEdited();
@ -540,11 +545,10 @@ void ConfManager::saveIni()
conf()->resetEdited();
}
void ConfManager::saveIniState()
void ConfManager::saveIniUser()
{
conf()->setIniStateEdited();
saveIni();
iniUser()->save();
iniUser()->clear();
}
bool ConfManager::saveVariant(const QVariant &confVar)

View File

@ -13,6 +13,7 @@ class FirewallConf;
class FortManager;
class FortSettings;
class IniOptions;
class IniUser;
class SqliteDb;
class SqliteStmt;
class TaskInfo;
@ -39,6 +40,8 @@ public:
FirewallConf *conf() const { return m_conf; }
FirewallConf *confToEdit() const { return m_confToEdit; }
IniUser *iniUser() const;
bool initialize();
void initConfToEdit();
@ -53,7 +56,7 @@ public:
bool saveFlags();
void saveIni();
void saveIniState();
void saveIniUser();
bool saveVariant(const QVariant &confVar);

View File

@ -1,11 +1,12 @@
#include "firewallconf.h"
#include "../fortsettings.h"
#include "../util/fileutil.h"
#include "../util/net/netutil.h"
#include "addressgroup.h"
#include "appgroup.h"
FirewallConf::FirewallConf(QObject *parent) :
FirewallConf::FirewallConf(FortSettings *settings, QObject *parent) :
QObject(parent),
m_editedFlags(AllEdited), // update all on load()!
m_provBoot(false),
@ -21,7 +22,8 @@ FirewallConf::FirewallConf(QObject *parent) :
m_logBlockedIp(false),
m_appBlockAll(true),
m_appAllowAll(false),
m_activePeriodEnabled(false)
m_activePeriodEnabled(false),
m_ini(settings)
{
setupAddressGroups();
}
@ -423,9 +425,9 @@ QVariant FirewallConf::toVariant(bool onlyFlags) const
}
if ((flags & IniEdited) != 0) {
const auto map = ini().map();
if (!map.isEmpty()) {
map["ini"] = map;
const QVariantMap iniMap = ini().map();
if (!iniMap.isEmpty()) {
map["ini"] = iniMap;
}
}

View File

@ -8,6 +8,7 @@
class AddressGroup;
class AppGroup;
class FortSettings;
class FirewallConf : public QObject
{
@ -19,11 +20,10 @@ public:
OptEdited = 0x1,
FlagsEdited = 0x2,
IniEdited = 0x4,
IniStateEdited = 0x8,
AllEdited = (OptEdited | FlagsEdited | IniEdited)
};
explicit FirewallConf(QObject *parent = nullptr);
explicit FirewallConf(FortSettings *settings = nullptr, QObject *parent = nullptr);
uint editedFlags() const { return m_editedFlags; }
@ -36,9 +36,6 @@ public:
bool iniEdited() const { return (m_editedFlags & IniEdited) != 0; }
void setIniEdited() { m_editedFlags |= IniEdited; }
bool iniStateEdited() const { return (m_editedFlags & IniStateEdited) != 0; }
void setIniStateEdited() { m_editedFlags |= IniStateEdited; }
bool anyEdited() const { return m_editedFlags != NoneEdited; }
void resetEdited(bool v = false);
@ -106,8 +103,8 @@ public:
const QVector<qint64> &removedAppGroupIdList() const { return m_removedAppGroupIdList; }
void clearRemovedAppGroupIdList() const;
IniOptions &ini() { return m_iniOptions; }
const IniOptions &ini() const { return m_iniOptions; }
IniOptions &ini() { return m_ini; }
const IniOptions &ini() const { return m_ini; }
void copyFlags(const FirewallConf &o);
void copy(const FirewallConf &o);
@ -183,7 +180,7 @@ private:
QList<AppGroup *> m_appGroups;
mutable QVector<qint64> m_removedAppGroupIdList;
IniOptions m_iniOptions;
IniOptions m_ini;
};
#endif // FIREWALLCONF_H

View File

@ -1,44 +1,3 @@
#include "inioptions.h"
#include "../fortsettings.h"
IniOptions::IniOptions(const IniOptions &o) : MapWrapper(o.map()) { }
QString IniOptions::defaultLanguage() const
{
return settings() ? settings()->defaultLanguage() : "en";
}
QVariant IniOptions::value(const QString &key, const QVariant &defaultValue) const
{
const QVariant v = MapWrapper::value(key);
if (!v.isNull())
return v;
return settings() ? settings()->iniValue(key, defaultValue) : defaultValue;
}
void IniOptions::setCache(const QString &key, const QVariant &v) const
{
Q_ASSERT(settings());
settings()->setCacheValue(key, v);
}
bool IniOptions::isTransientKey(const QString &key)
{
return key.endsWith('_');
}
void IniOptions::save(FortSettings *settings) const
{
auto it = map().constBegin();
const auto end = map().constEnd();
for (; it != end; ++it) {
const QString &key = it.key();
if (!isTransientKey(key)) {
settings->setIniValue(key, it.value());
}
}
}
IniOptions::IniOptions(Settings *settings) : MapSettings(settings) { }

View File

@ -1,11 +1,7 @@
#ifndef INIOPTIONS_H
#define INIOPTIONS_H
#include <QRect>
#include "../util/json/mapwrapper.h"
class FortSettings;
#include "../util/ini/mapsettings.h"
#define DEFAULT_APP_GROUP_BITS 0xFFFF
#define DEFAULT_MONTH_START 1
@ -14,14 +10,10 @@ class FortSettings;
#define DEFAULT_TRAF_MONTH_KEEP_MONTHS 36 // ~3 years
#define DEFAULT_LOG_IP_KEEP_COUNT 10000
class IniOptions : public MapWrapper
class IniOptions : public MapSettings
{
public:
explicit IniOptions() = default;
explicit IniOptions(const IniOptions &o);
FortSettings *settings() const { return m_settings; }
void setSettings(FortSettings *v) { m_settings = v; }
explicit IniOptions(Settings *settings = nullptr);
bool logDebug() const { return valueBool("base/debug"); }
void setLogDebug(bool v) { setValue("base/debug", v); }
@ -29,9 +21,6 @@ public:
bool logConsole() const { return valueBool("base/console"); }
void setLogConsole(bool v) { setValue("base/console", v); }
bool hotKeyEnabled() const { return valueBool("hotKey/enabled"); }
void setHotKeyEnabled(bool v) { setValue("hotKey/enabled", v); }
bool hasPassword() const { return valueBool("base/hasPassword_"); }
void setHasPassword(bool v) { setValue("base/hasPassword_", v); }
@ -39,7 +28,7 @@ public:
void setPassword(const QString &v) { setValue("base/password_", v); }
bool explorerIntegratedSet() const { return contains("ext/explorerIntegrated_"); }
void cacheExplorerIntegrated(bool v) { setCache("ext/explorerIntegrated_", v); }
void cacheExplorerIntegrated(bool v) { setCacheValue("ext/explorerIntegrated_", v); }
bool explorerIntegrated() const { return valueBool("ext/explorerIntegrated_"); }
void setExplorerIntegrated(bool v) { setValue("ext/explorerIntegrated_", v); }
@ -49,26 +38,6 @@ public:
QVariant taskInfoList() const { return value("task/infoList_"); }
void setTaskInfoList(const QVariant &v) { setValue("task/infoList_", v); }
QString defaultLanguage() const;
QString language() const { return valueText("base/language", defaultLanguage()); }
void setLanguage(const QString &v) { setValue("base/language", v); }
QString hotKeyPrograms() const { return valueText("hotKey/programs"); }
QString hotKeyOptions() const { return valueText("hotKey/options"); }
QString hotKeyZones() const { return valueText("hotKey/zones"); }
QString hotKeyGraph() const { return valueText("hotKey/graph"); }
QString hotKeyConnections() const { return valueText("hotKey/connections"); }
QString hotKeyFilter() const { return valueText("hotKey/filter", "Ctrl+Alt+Shift+F"); }
QString hotKeyStopTraffic() const { return valueText("hotKey/stopTraffic"); }
QString hotKeyStopInetTraffic() const { return valueText("hotKey/stopInetTraffic"); }
QString hotKeyAllowAllNew() const { return valueText("hotKey/allowAllNew"); }
QString hotKeyAppGroupModifiers() const
{
return valueText("hotKey/appGroupModifiers", "Ctrl+Alt+Shift");
}
QString hotKeyQuit() const { return valueText("hotKey/quit"); }
qint32 quotaDayAlerted() const { return valueInt("quota/dayAlerted"); }
void setQuotaDayAlerted(qint32 v) { setValue("quota/dayAlerted", v); }
@ -117,60 +86,6 @@ public:
}
void setBlockedIpKeepCount(int v) { setValue("stat/blockedIpKeepCount", v); }
QRect progWindowGeometry() const { return value("progWindow/geometry").toRect(); }
void setProgWindowGeometry(const QRect &v) { setValue("progWindow/geometry", v); }
bool progWindowMaximized() const { return valueBool("progWindow/maximized"); }
void setProgWindowMaximized(bool on) { setValue("progWindow/maximized", on); }
bool progAppsSortDesc() const { return valueBool("progWindow/appsSortDesc"); }
void setProgSortDesc(bool v) { setValue("progWindow/appsSortDesc", v); }
int progAppsSortColumn() const { return valueInt("progWindow/appsSortColumn"); }
void setProgSortColumn(int v) { setValue("progWindow/appsSortColumn", v); }
int progAppsHeaderVersion() const { return valueInt("progWindow/appsHeaderVersion"); }
void setProgAppsHeaderVersion(int v) { setValue("progWindow/appsHeaderVersion", v); }
QByteArray progAppsHeader() const { return valueByteArray("progWindow/appsHeader"); }
void setProgAppsHeader(const QByteArray &v) { setValue("progWindow/appsHeader", v); }
QRect optWindowGeometry() const { return value("optWindow/geometry").toRect(); }
void setOptWindowGeometry(const QRect &v) { setValue("optWindow/geometry", v); }
bool optWindowMaximized() const { return valueBool("optWindow/maximized"); }
void setOptWindowMaximized(bool on) { setValue("optWindow/maximized", on); }
QByteArray optWindowAddrSplit() const { return valueByteArray("optWindow/addrSplit"); }
void setOptWindowAddrSplit(const QByteArray &v) { setValue("optWindow/addrSplit", v); }
QByteArray optWindowAppsSplit() const { return valueByteArray("optWindow/appsSplit"); }
void setOptWindowAppsSplit(const QByteArray &v) { setValue("optWindow/appsSplit", v); }
QByteArray optWindowStatSplit() const { return valueByteArray("optWindow/statSplit"); }
void setOptWindowStatSplit(const QByteArray &v) { setValue("optWindow/statSplit", v); }
QRect zoneWindowGeometry() const { return value("zoneWindow/geometry").toRect(); }
void setZoneWindowGeometry(const QRect &v) { setValue("zoneWindow/geometry", v); }
bool zoneWindowMaximized() const { return valueBool("zoneWindow/maximized"); }
void setZoneWindowMaximized(bool on) { setValue("zoneWindow/maximized", on); }
int zonesHeaderVersion() const { return valueInt("zoneWindow/zonesHeaderVersion"); }
void setZonesHeaderVersion(int v) { setValue("zoneWindow/zonesHeaderVersion", v); }
QByteArray zonesHeader() const { return valueByteArray("zoneWindow/zonesHeader"); }
void setZonesHeader(const QByteArray &v) { setValue("zoneWindow/zonesHeader", v); }
bool graphWindowVisible() const { return valueBool("graphWindow/visible"); }
void setGraphWindowVisible(bool on) { setValue("graphWindow/visible", on); }
QRect graphWindowGeometry() const { return value("graphWindow/geometry").toRect(); }
void setGraphWindowGeometry(const QRect &v) { setValue("graphWindow/geometry", v); }
bool graphWindowMaximized() const { return valueBool("graphWindow/maximized"); }
void setGraphWindowMaximized(bool on) { setValue("graphWindow/maximized", on); }
bool graphWindowAlwaysOnTop() const { return valueBool("graphWindow/alwaysOnTop", true); }
void setGraphWindowAlwaysOnTop(bool on) { setValue("graphWindow/alwaysOnTop", on); }
@ -236,36 +151,6 @@ public:
return valueColor("graphWindow/gridColor", QColor(200, 200, 200));
}
void setGraphWindowGridColor(const QColor &v) { setColor("graphWindow/gridColor", v); }
QRect connWindowGeometry() const { return value("connWindow/geometry").toRect(); }
void setConnWindowGeometry(const QRect &v) { setValue("connWindow/geometry", v); }
bool connWindowMaximized() const { return valueBool("connWindow/maximized"); }
void setConnWindowMaximized(bool on) { setValue("connWindow/maximized", on); }
int connListHeaderVersion() const { return valueInt("connWindow/connListHeaderVersion"); }
void setConnListHeaderVersion(int v) { setValue("connWindow/connListHeaderVersion", v); }
QByteArray connListHeader() const { return valueByteArray("connWindow/connListHeader"); }
void setConnListHeader(const QByteArray &v) { setValue("connWindow/connListHeader", v); }
bool connAutoScroll() const { return valueBool("connWindow/autoScroll"); }
void setConnAutoScroll(bool on) { setValue("connWindow/autoScroll", on); }
bool connShowHostNames() const { return valueBool("connWindow/showHostNames"); }
void setConnShowHostNames(bool on) { setValue("connWindow/showHostNames", on); }
public slots:
void save(FortSettings *settings) const;
protected:
QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const override;
void setCache(const QString &key, const QVariant &v) const;
static bool isTransientKey(const QString &key);
private:
FortSettings *m_settings = nullptr;
};
#endif // INIOPTIONS_H

View File

@ -32,6 +32,11 @@ IniOptions *ConnectionsController::ini() const
return &conf()->ini();
}
IniUser *ConnectionsController::iniUser() const
{
return confManager()->iniUser();
}
ConnListModel *ConnectionsController::connListModel() const
{
return fortManager()->connListModel();

View File

@ -9,6 +9,7 @@ class FirewallConf;
class FortManager;
class FortSettings;
class IniOptions;
class IniUser;
class TranslationManager;
class ConnectionsController : public QObject
@ -23,6 +24,7 @@ public:
ConfManager *confManager() const;
FirewallConf *conf() const;
IniOptions *ini() const;
IniUser *iniUser() const;
ConnListModel *connListModel() const;
TranslationManager *translationManager() const;

View File

@ -12,6 +12,7 @@
#include "../../fortmanager.h"
#include "../../fortsettings.h"
#include "../../model/connlistmodel.h"
#include "../../user/iniuser.h"
#include "../../util/guiutil.h"
#include "../../util/iconcache.h"
#include "../../util/window/widgetwindowstatewatcher.h"
@ -64,6 +65,11 @@ IniOptions *ConnectionsWindow::ini() const
return ctrl()->ini();
}
IniUser *ConnectionsWindow::iniUser() const
{
return ctrl()->iniUser();
}
ConnListModel *ConnectionsWindow::connListModel() const
{
return fortManager()->connListModel();
@ -76,24 +82,24 @@ AppInfoCache *ConnectionsWindow::appInfoCache() const
void ConnectionsWindow::saveWindowState()
{
ini()->setConnWindowGeometry(m_stateWatcher->geometry());
ini()->setConnWindowMaximized(m_stateWatcher->maximized());
iniUser()->setConnWindowGeometry(m_stateWatcher->geometry());
iniUser()->setConnWindowMaximized(m_stateWatcher->maximized());
auto header = m_connListView->horizontalHeader();
ini()->setConnListHeader(header->saveState());
ini()->setConnListHeaderVersion(CONN_LIST_HEADER_VERSION);
iniUser()->setConnListHeader(header->saveState());
iniUser()->setConnListHeaderVersion(CONN_LIST_HEADER_VERSION);
confManager()->saveIniState();
confManager()->saveIniUser();
}
void ConnectionsWindow::restoreWindowState()
{
m_stateWatcher->restore(
this, QSize(1024, 768), ini()->connWindowGeometry(), ini()->connWindowMaximized());
m_stateWatcher->restore(this, QSize(1024, 768), iniUser()->connWindowGeometry(),
iniUser()->connWindowMaximized());
if (ini()->connListHeaderVersion() == CONN_LIST_HEADER_VERSION) {
if (iniUser()->connListHeaderVersion() == CONN_LIST_HEADER_VERSION) {
auto header = m_connListView->horizontalHeader();
header->restoreState(ini()->connListHeader());
header->restoreState(iniUser()->connListHeader());
}
}
@ -262,12 +268,12 @@ void ConnectionsWindow::setupLogBlockedIp()
void ConnectionsWindow::setupAutoScroll()
{
m_cbAutoScroll = ControlUtil::createCheckBox(ini()->connAutoScroll(), [&](bool checked) {
if (ini()->connAutoScroll() == checked)
m_cbAutoScroll = ControlUtil::createCheckBox(iniUser()->connAutoScroll(), [&](bool checked) {
if (iniUser()->connAutoScroll() == checked)
return;
ini()->setConnAutoScroll(checked);
confManager()->saveIni();
iniUser()->setConnAutoScroll(checked);
confManager()->saveIniUser();
syncAutoScroll();
});
@ -275,15 +281,16 @@ void ConnectionsWindow::setupAutoScroll()
void ConnectionsWindow::setupShowHostNames()
{
m_cbShowHostNames = ControlUtil::createCheckBox(ini()->connShowHostNames(), [&](bool checked) {
if (ini()->connShowHostNames() == checked)
return;
m_cbShowHostNames =
ControlUtil::createCheckBox(iniUser()->connShowHostNames(), [&](bool checked) {
if (iniUser()->connShowHostNames() == checked)
return;
ini()->setConnShowHostNames(checked);
confManager()->saveIni();
iniUser()->setConnShowHostNames(checked);
confManager()->saveIniUser();
syncShowHostNames();
});
syncShowHostNames();
});
}
void ConnectionsWindow::setupTableConnList()
@ -357,7 +364,7 @@ void ConnectionsWindow::setupTableConnsChanged()
void ConnectionsWindow::syncAutoScroll()
{
if (ini()->connAutoScroll()) {
if (iniUser()->connAutoScroll()) {
connect(connListModel(), &QAbstractItemModel::rowsInserted, m_connListView,
&QAbstractItemView::scrollToBottom);
@ -370,7 +377,7 @@ void ConnectionsWindow::syncAutoScroll()
void ConnectionsWindow::syncShowHostNames()
{
connListModel()->setResolveAddress(ini()->connShowHostNames());
connListModel()->setResolveAddress(iniUser()->connShowHostNames());
}
void ConnectionsWindow::deleteConn(int row)

View File

@ -15,6 +15,7 @@ class FirewallConf;
class FortManager;
class FortSettings;
class IniOptions;
class IniUser;
class TableView;
class WidgetWindowStateWatcher;
@ -31,6 +32,7 @@ public:
ConfManager *confManager() const;
FirewallConf *conf() const;
IniOptions *ini() const;
IniUser *iniUser() const;
ConnListModel *connListModel() const;
AppInfoCache *appInfoCache() const;

View File

@ -7,6 +7,7 @@
#include "../../conf/confmanager.h"
#include "../../conf/firewallconf.h"
#include "../../fortcompat.h"
#include "../../user/iniuser.h"
#include "../../util/dateutil.h"
#include "../../util/net/netutil.h"
#include "../../util/window/widgetwindowstatewatcher.h"
@ -41,20 +42,25 @@ IniOptions *GraphWindow::ini() const
return &conf()->ini();
}
IniUser *GraphWindow::iniUser() const
{
return confManager()->iniUser();
}
void GraphWindow::saveWindowState(bool wasVisible)
{
ini()->setGraphWindowGeometry(m_stateWatcher->geometry());
ini()->setGraphWindowMaximized(m_stateWatcher->maximized());
iniUser()->setGraphWindowGeometry(m_stateWatcher->geometry());
iniUser()->setGraphWindowMaximized(m_stateWatcher->maximized());
ini()->setGraphWindowVisible(wasVisible);
iniUser()->setGraphWindowVisible(wasVisible);
confManager()->saveIniState();
confManager()->saveIniUser();
}
void GraphWindow::restoreWindowState()
{
m_stateWatcher->restore(
this, QSize(400, 300), ini()->graphWindowGeometry(), ini()->graphWindowMaximized());
m_stateWatcher->restore(this, QSize(400, 300), iniUser()->graphWindowGeometry(),
iniUser()->graphWindowMaximized());
}
void GraphWindow::setupStateWatcher()

View File

@ -8,6 +8,7 @@
class ConfManager;
class FirewallConf;
class IniOptions;
class IniUser;
class GraphPlot;
class QCPBars;
class WidgetWindowStateWatcher;
@ -22,6 +23,7 @@ public:
ConfManager *confManager() const;
FirewallConf *conf() const;
IniOptions *ini() const;
IniUser *iniUser() const;
void saveWindowState(bool wasVisible);
void restoreWindowState();

View File

@ -40,6 +40,11 @@ IniOptions *OptionsController::ini() const
return &conf()->ini();
}
IniUser *OptionsController::iniUser() const
{
return confManager()->iniUser();
}
TaskManager *OptionsController::taskManager() const
{
return fortManager()->taskManager();
@ -98,7 +103,7 @@ void OptionsController::resetEdited()
void OptionsController::initialize()
{
// Settings/configuration was migrated?
if (settings()->confMigrated()) {
if (settings()->wasMigrated()) {
setOptEdited();
}
}

View File

@ -9,6 +9,7 @@ class FirewallConf;
class FortManager;
class FortSettings;
class IniOptions;
class IniUser;
class TaskManager;
class TranslationManager;
class ZoneListModel;
@ -26,6 +27,7 @@ public:
ConfManager *confManager() const;
FirewallConf *conf() const;
IniOptions *ini() const;
IniUser *iniUser() const;
TaskManager *taskManager() const;
DriverManager *driverManager() const;
TranslationManager *translationManager() const;
@ -41,8 +43,8 @@ signals:
void cancelChanges(IniOptions *oldIni);
void afterSaveWindowState(IniOptions *ini);
void afterRestoreWindowState(IniOptions *ini);
void afterSaveWindowState(IniUser *ini);
void afterRestoreWindowState(IniUser *ini);
void retranslateUi();

View File

@ -5,6 +5,7 @@
#include "../../conf/confmanager.h"
#include "../../conf/firewallconf.h"
#include "../../user/iniuser.h"
#include "../../util/guiutil.h"
#include "../../util/window/widgetwindowstatewatcher.h"
#include "optionscontroller.h"
@ -35,6 +36,11 @@ IniOptions *OptionsWindow::ini() const
return &conf()->ini();
}
IniUser *OptionsWindow::iniUser() const
{
return ctrl()->iniUser();
}
void OptionsWindow::cancelChanges()
{
if (ctrl()->conf() && ctrl()->conf()->anyEdited()) {
@ -44,20 +50,20 @@ void OptionsWindow::cancelChanges()
void OptionsWindow::saveWindowState()
{
ini()->setOptWindowGeometry(m_stateWatcher->geometry());
ini()->setOptWindowMaximized(m_stateWatcher->maximized());
iniUser()->setOptWindowGeometry(m_stateWatcher->geometry());
iniUser()->setOptWindowMaximized(m_stateWatcher->maximized());
emit ctrl()->afterSaveWindowState(ini());
emit ctrl()->afterSaveWindowState(iniUser());
confManager()->saveIniState();
confManager()->saveIniUser();
}
void OptionsWindow::restoreWindowState()
{
m_stateWatcher->restore(
this, QSize(1024, 768), ini()->optWindowGeometry(), ini()->optWindowMaximized());
m_stateWatcher->restore(this, QSize(1024, 768), iniUser()->optWindowGeometry(),
iniUser()->optWindowMaximized());
emit ctrl()->afterRestoreWindowState(ini());
emit ctrl()->afterRestoreWindowState(iniUser());
}
void OptionsWindow::setupController()

View File

@ -7,6 +7,7 @@ class ConfManager;
class FirewallConf;
class FortManager;
class IniOptions;
class IniUser;
class MainPage;
class OptionsController;
class WidgetWindowStateWatcher;
@ -22,6 +23,7 @@ public:
ConfManager *confManager() const;
FirewallConf *conf() const;
IniOptions *ini() const;
IniUser *iniUser() const;
void cancelChanges();

View File

@ -17,6 +17,7 @@
#include "../../../fortmanager.h"
#include "../../../fortsettings.h"
#include "../../../model/zonelistmodel.h"
#include "../../../user/iniuser.h"
#include "../../../util/iconcache.h"
#include "../../../util/net/netutil.h"
#include "../../../util/textareautil.h"
@ -48,12 +49,12 @@ void AddressesPage::setAddressGroupIndex(int v)
}
}
void AddressesPage::onSaveWindowState(IniOptions *ini)
void AddressesPage::onSaveWindowState(IniUser *ini)
{
ini->setOptWindowAddrSplit(m_splitter->saveState());
}
void AddressesPage::onRestoreWindowState(IniOptions *ini)
void AddressesPage::onRestoreWindowState(IniUser *ini)
{
m_splitter->restoreState(ini->optWindowAddrSplit());
}

View File

@ -23,8 +23,8 @@ signals:
void addressGroupChanged();
protected slots:
void onSaveWindowState(IniOptions *ini) override;
void onRestoreWindowState(IniOptions *ini) override;
void onSaveWindowState(IniUser *ini) override;
void onRestoreWindowState(IniUser *ini) override;
void onRetranslateUi() override;

View File

@ -14,6 +14,7 @@
#include "../../../conf/appgroup.h"
#include "../../../conf/firewallconf.h"
#include "../../../fortsettings.h"
#include "../../../user/iniuser.h"
#include "../../../util/iconcache.h"
#include "../../../util/net/netutil.h"
#include "../../../util/textareautil.h"
@ -56,12 +57,12 @@ void ApplicationsPage::setAppGroupIndex(int v)
}
}
void ApplicationsPage::onSaveWindowState(IniOptions *ini)
void ApplicationsPage::onSaveWindowState(IniUser *ini)
{
ini->setOptWindowAppsSplit(m_splitter->saveState());
}
void ApplicationsPage::onRestoreWindowState(IniOptions *ini)
void ApplicationsPage::onRestoreWindowState(IniUser *ini)
{
m_splitter->restoreState(ini->optWindowAppsSplit());
}

View File

@ -26,8 +26,8 @@ signals:
void appGroupChanged();
protected slots:
void onSaveWindowState(IniOptions *ini) override;
void onRestoreWindowState(IniOptions *ini) override;
void onSaveWindowState(IniUser *ini) override;
void onRestoreWindowState(IniUser *ini) override;
void onRetranslateUi() override;

View File

@ -4,6 +4,7 @@
#include "../../../conf/firewallconf.h"
#include "../../../fortmanager.h"
#include "../../../user/iniuser.h"
#include "../../../util/osutil.h"
#include "../optionscontroller.h"
@ -37,6 +38,11 @@ IniOptions *BasePage::ini() const
return &conf()->ini();
}
IniUser *BasePage::iniUser() const
{
return ctrl()->iniUser();
}
DriverManager *BasePage::driverManager() const
{
return ctrl()->driverManager();

View File

@ -20,6 +20,7 @@ class FirewallConf;
class FortManager;
class FortSettings;
class IniOptions;
class IniUser;
class OptionsController;
class TaskManager;
class TranslationManager;
@ -39,6 +40,7 @@ protected:
ConfManager *confManager() const;
FirewallConf *conf() const;
IniOptions *ini() const;
IniUser *iniUser() const;
DriverManager *driverManager() const;
TranslationManager *translationManager() const;
TaskManager *taskManager() const;
@ -50,8 +52,8 @@ protected slots:
virtual void onCancelChanges(IniOptions * /*oldIni*/) { }
virtual void onSaveWindowState(IniOptions * /*ini*/) { }
virtual void onRestoreWindowState(IniOptions * /*ini*/) { }
virtual void onSaveWindowState(IniUser * /*ini*/) { }
virtual void onRestoreWindowState(IniUser * /*ini*/) { }
virtual void onRetranslateUi() { }

View File

@ -12,6 +12,7 @@
#include <QStandardItemModel>
#include <QVBoxLayout>
#include "../../../conf/confmanager.h"
#include "../../../conf/firewallconf.h"
#include "../../../driver/drivermanager.h"
#include "../../../fortmanager.h"
@ -19,6 +20,7 @@
#include "../../../task/taskinfoupdatechecker.h"
#include "../../../task/taskmanager.h"
#include "../../../translationmanager.h"
#include "../../../user/iniuser.h"
#include "../../../util/iconcache.h"
#include "../../../util/osutil.h"
#include "../../../util/startuputil.h"
@ -123,16 +125,6 @@ void OptionsPage::saveService(bool isService)
fortManager(), &FortManager::processRestartRequired, Qt::QueuedConnection);
}
void OptionsPage::onCancelChanges(IniOptions *oldIni)
{
if (!conf()->iniEdited())
return;
if (ini()->language() != oldIni->language()) {
translationManager()->switchLanguageByName(oldIni->language());
}
}
void OptionsPage::onEditResetted()
{
retranslateEditPassword();
@ -366,9 +358,9 @@ void OptionsPage::setupGlobalBox()
});
m_cbExplorerMenu->setEnabled(settings()->hasService() || OsUtil::isUserAdmin());
m_cbHotKeys = ControlUtil::createCheckBox(ini()->hotKeyEnabled(), [&](bool checked) {
ini()->setHotKeyEnabled(checked);
ctrl()->setIniEdited();
m_cbHotKeys = ControlUtil::createCheckBox(iniUser()->hotKeyEnabled(), [&](bool checked) {
iniUser()->setHotKeyEnabled(checked);
confManager()->saveIniUser();
});
// Password Row
@ -458,8 +450,8 @@ void OptionsPage::setupComboLanguage()
m_comboLanguage =
ControlUtil::createComboBox(translationManager()->naturalLabels(), [&](int index) {
if (translationManager()->switchLanguage(index)) {
ini()->setLanguage(translationManager()->localeName());
ctrl()->setIniEdited();
iniUser()->setLanguage(translationManager()->localeName());
confManager()->saveIniUser();
}
});
m_comboLanguage->setFixedWidth(200);

View File

@ -14,8 +14,6 @@ protected slots:
void onAboutToSave() override;
void onEditResetted() override;
void onCancelChanges(IniOptions *oldIni) override;
void onRetranslateUi() override;
private:

View File

@ -5,9 +5,9 @@ RulesPage::RulesPage(OptionsController *ctrl, QWidget *parent) : BasePage(ctrl,
setupUi();
}
void RulesPage::onSaveWindowState(IniOptions * /*ini*/) { }
void RulesPage::onSaveWindowState(IniUser * /*ini*/) { }
void RulesPage::onRestoreWindowState(IniOptions * /*ini*/) { }
void RulesPage::onRestoreWindowState(IniUser * /*ini*/) { }
void RulesPage::onRetranslateUi() { }

View File

@ -11,8 +11,8 @@ public:
explicit RulesPage(OptionsController *ctrl = nullptr, QWidget *parent = nullptr);
protected slots:
void onSaveWindowState(IniOptions *ini) override;
void onRestoreWindowState(IniOptions *ini) override;
void onSaveWindowState(IniUser *ini) override;
void onRestoreWindowState(IniUser *ini) override;
void onRetranslateUi() override;

View File

@ -22,6 +22,7 @@
#include "../../../fortsettings.h"
#include "../../../model/appstatmodel.h"
#include "../../../model/traflistmodel.h"
#include "../../../user/iniuser.h"
#include "../../../util/iconcache.h"
#include "../../../util/net/netutil.h"
#include "../../controls/appinforow.h"
@ -70,12 +71,12 @@ void StatisticsPage::setIniEdited()
}
}
void StatisticsPage::onSaveWindowState(IniOptions *ini)
void StatisticsPage::onSaveWindowState(IniUser *ini)
{
ini->setOptWindowStatSplit(m_splitter->saveState());
}
void StatisticsPage::onRestoreWindowState(IniOptions *ini)
void StatisticsPage::onRestoreWindowState(IniUser *ini)
{
m_splitter->restoreState(ini->optWindowStatSplit());
}

View File

@ -30,8 +30,8 @@ public:
void setIniEdited();
protected slots:
void onSaveWindowState(IniOptions *ini) override;
void onRestoreWindowState(IniOptions *ini) override;
void onSaveWindowState(IniUser *ini) override;
void onRestoreWindowState(IniUser *ini) override;
void onRetranslateUi() override;

View File

@ -32,6 +32,11 @@ IniOptions *ProgramsController::ini() const
return &conf()->ini();
}
IniUser *ProgramsController::iniUser() const
{
return confManager()->iniUser();
}
AppListModel *ProgramsController::appListModel() const
{
return fortManager()->appListModel();

View File

@ -9,6 +9,7 @@ class FirewallConf;
class FortManager;
class FortSettings;
class IniOptions;
class IniUser;
class TranslationManager;
class ProgramsController : public QObject
@ -23,6 +24,7 @@ public:
ConfManager *confManager() const;
FirewallConf *conf() const;
IniOptions *ini() const;
IniUser *iniUser() const;
AppListModel *appListModel() const;
TranslationManager *translationManager() const;

View File

@ -12,6 +12,7 @@
#include "../../fortmanager.h"
#include "../../fortsettings.h"
#include "../../model/applistmodel.h"
#include "../../user/iniuser.h"
#include "../../util/guiutil.h"
#include "../../util/iconcache.h"
#include "../../util/window/widgetwindowstatewatcher.h"
@ -63,6 +64,11 @@ IniOptions *ProgramsWindow::ini() const
return ctrl()->ini();
}
IniUser *ProgramsWindow::iniUser() const
{
return ctrl()->iniUser();
}
AppListModel *ProgramsWindow::appListModel() const
{
return fortManager()->appListModel();
@ -75,24 +81,24 @@ AppInfoCache *ProgramsWindow::appInfoCache() const
void ProgramsWindow::saveWindowState()
{
ini()->setProgWindowGeometry(m_stateWatcher->geometry());
ini()->setProgWindowMaximized(m_stateWatcher->maximized());
iniUser()->setProgWindowGeometry(m_stateWatcher->geometry());
iniUser()->setProgWindowMaximized(m_stateWatcher->maximized());
auto header = m_appListView->horizontalHeader();
ini()->setProgAppsHeader(header->saveState());
ini()->setProgAppsHeaderVersion(APPS_HEADER_VERSION);
iniUser()->setProgAppsHeader(header->saveState());
iniUser()->setProgAppsHeaderVersion(APPS_HEADER_VERSION);
confManager()->saveIniState();
confManager()->saveIniUser();
}
void ProgramsWindow::restoreWindowState()
{
m_stateWatcher->restore(
this, QSize(1024, 768), ini()->progWindowGeometry(), ini()->progWindowMaximized());
m_stateWatcher->restore(this, QSize(1024, 768), iniUser()->progWindowGeometry(),
iniUser()->progWindowMaximized());
if (ini()->progAppsHeaderVersion() == APPS_HEADER_VERSION) {
if (iniUser()->progAppsHeaderVersion() == APPS_HEADER_VERSION) {
auto header = m_appListView->horizontalHeader();
header->restoreState(ini()->progAppsHeader());
header->restoreState(iniUser()->progAppsHeader());
}
}

View File

@ -14,6 +14,7 @@ class FirewallConf;
class FortManager;
class FortSettings;
class IniOptions;
class IniUser;
class ProgramEditDialog;
class ProgramsController;
class TableView;
@ -34,6 +35,7 @@ public:
ConfManager *confManager() const;
FirewallConf *conf() const;
IniOptions *ini() const;
IniUser *iniUser() const;
AppListModel *appListModel() const;
AppInfoCache *appInfoCache() const;

View File

@ -32,6 +32,11 @@ IniOptions *TrayController::ini() const
return &conf()->ini();
}
IniUser *TrayController::iniUser() const
{
return confManager()->iniUser();
}
HotKeyManager *TrayController::hotKeyManager() const
{
return fortManager()->hotKeyManager();

View File

@ -9,6 +9,7 @@ class FortManager;
class FortSettings;
class HotKeyManager;
class IniOptions;
class IniUser;
class TranslationManager;
class TrayController : public QObject
@ -23,6 +24,7 @@ public:
ConfManager *confManager() const;
FirewallConf *conf() const;
IniOptions *ini() const;
IniUser *iniUser() const;
HotKeyManager *hotKeyManager() const;
TranslationManager *translationManager() const;

View File

@ -12,6 +12,7 @@
#include "../../fortcompat.h"
#include "../../fortmanager.h"
#include "../../fortsettings.h"
#include "../../user/iniuser.h"
#include "../../util/guiutil.h"
#include "../../util/hotkeymanager.h"
#include "../../util/iconcache.h"
@ -83,6 +84,11 @@ IniOptions *TrayIcon::ini() const
return ctrl()->ini();
}
IniUser *TrayIcon::iniUser() const
{
return ctrl()->iniUser();
}
HotKeyManager *TrayIcon::hotKeyManager() const
{
return ctrl()->hotKeyManager();
@ -190,38 +196,38 @@ void TrayIcon::setupTrayMenu()
m_programsAction = addAction(menu, IconCache::icon(":/icons/window.png"), QString(),
fortManager(), SLOT(showProgramsWindow()));
addHotKey(m_programsAction, ini()->hotKeyPrograms());
addHotKey(m_programsAction, iniUser()->hotKeyPrograms());
m_optionsAction = addAction(menu, IconCache::icon(":/icons/cog.png"), QString(), fortManager(),
SLOT(showOptionsWindow()));
addHotKey(m_optionsAction, ini()->hotKeyOptions());
addHotKey(m_optionsAction, iniUser()->hotKeyOptions());
m_zonesAction = addAction(menu, IconCache::icon(":/icons/map-map-marker.png"), QString(),
fortManager(), SLOT(showZonesWindow()));
addHotKey(m_zonesAction, ini()->hotKeyZones());
addHotKey(m_zonesAction, iniUser()->hotKeyZones());
m_graphWindowAction = addAction(menu, IconCache::icon(":/icons/line-graph.png"), QString(),
fortManager(), SLOT(switchGraphWindow()), true, !!fortManager()->graphWindow());
addHotKey(m_graphWindowAction, ini()->hotKeyGraph());
addHotKey(m_graphWindowAction, iniUser()->hotKeyGraph());
m_connectionsAction = addAction(menu, IconCache::icon(":/icons/connect.png"), QString(),
fortManager(), SLOT(showConnectionsWindow()));
addHotKey(m_connectionsAction, ini()->hotKeyConnections());
addHotKey(m_connectionsAction, iniUser()->hotKeyConnections());
menu->addSeparator();
m_filterEnabledAction = addAction(menu, QIcon(), QString(), this, SLOT(saveTrayFlags()), true);
addHotKey(m_filterEnabledAction, ini()->hotKeyFilter());
addHotKey(m_filterEnabledAction, iniUser()->hotKeyFilter());
m_stopTrafficAction = addAction(menu, QIcon(), QString(), this, SLOT(saveTrayFlags()), true);
addHotKey(m_stopTrafficAction, ini()->hotKeyStopTraffic());
addHotKey(m_stopTrafficAction, iniUser()->hotKeyStopTraffic());
m_stopInetTrafficAction =
addAction(menu, QIcon(), QString(), this, SLOT(saveTrayFlags()), true);
addHotKey(m_stopInetTrafficAction, ini()->hotKeyStopInetTraffic());
addHotKey(m_stopInetTrafficAction, iniUser()->hotKeyStopInetTraffic());
m_allowAllNewAction = addAction(menu, QIcon(), QString(), this, SLOT(saveTrayFlags()), true);
addHotKey(m_allowAllNewAction, ini()->hotKeyAllowAllNew());
addHotKey(m_allowAllNewAction, iniUser()->hotKeyAllowAllNew());
menu->addSeparator();
@ -230,7 +236,7 @@ void TrayIcon::setupTrayMenu()
if (i < 12) {
const QString shortcutText =
ini()->hotKeyAppGroupModifiers() + "+F" + QString::number(i + 1);
iniUser()->hotKeyAppGroupModifiers() + "+F" + QString::number(i + 1);
addHotKey(a, shortcutText);
}
@ -240,7 +246,7 @@ void TrayIcon::setupTrayMenu()
menu->addSeparator();
m_quitAction = addAction(menu, QIcon(), tr("Quit"), fortManager(), SLOT(quitByCheckPassword()));
addHotKey(m_quitAction, ini()->hotKeyQuit());
addHotKey(m_quitAction, iniUser()->hotKeyQuit());
this->setContextMenu(menu);
}
@ -293,7 +299,7 @@ void TrayIcon::addHotKey(QAction *action, const QString &shortcutText)
void TrayIcon::updateHotKeys()
{
hotKeyManager()->setEnabled(conf()->ini().hotKeyEnabled());
hotKeyManager()->setEnabled(iniUser()->hotKeyEnabled());
}
void TrayIcon::removeHotKeys()

View File

@ -11,6 +11,7 @@ class FirewallConf;
class FortManager;
class FortSettings;
class IniOptions;
class IniUser;
class HotKeyManager;
class TrayController;
@ -27,6 +28,7 @@ public:
ConfManager *confManager() const;
FirewallConf *conf() const;
IniOptions *ini() const;
IniUser *iniUser() const;
HotKeyManager *hotKeyManager() const;
public slots:

View File

@ -32,6 +32,11 @@ IniOptions *ZonesController::ini() const
return &conf()->ini();
}
IniUser *ZonesController::iniUser() const
{
return confManager()->iniUser();
}
ZoneListModel *ZonesController::zoneListModel() const
{
return fortManager()->zoneListModel();

View File

@ -8,6 +8,7 @@ class FirewallConf;
class FortManager;
class FortSettings;
class IniOptions;
class IniUser;
class TranslationManager;
class ZoneListModel;
@ -23,6 +24,7 @@ public:
ConfManager *confManager() const;
FirewallConf *conf() const;
IniOptions *ini() const;
IniUser *iniUser() const;
ZoneListModel *zoneListModel() const;
TranslationManager *translationManager() const;

View File

@ -18,6 +18,7 @@
#include "../../model/zonesourcewrapper.h"
#include "../../task/taskinfozonedownloader.h"
#include "../../task/taskmanager.h"
#include "../../user/iniuser.h"
#include "../../util/conf/confutil.h"
#include "../../util/guiutil.h"
#include "../../util/iconcache.h"
@ -57,6 +58,11 @@ IniOptions *ZonesWindow::ini() const
return ctrl()->ini();
}
IniUser *ZonesWindow::iniUser() const
{
return ctrl()->iniUser();
}
TaskManager *ZonesWindow::taskManager() const
{
return fortManager()->taskManager();
@ -69,24 +75,24 @@ ZoneListModel *ZonesWindow::zoneListModel() const
void ZonesWindow::saveWindowState()
{
ini()->setZoneWindowGeometry(m_stateWatcher->geometry());
ini()->setZoneWindowMaximized(m_stateWatcher->maximized());
iniUser()->setZoneWindowGeometry(m_stateWatcher->geometry());
iniUser()->setZoneWindowMaximized(m_stateWatcher->maximized());
auto header = m_zoneListView->horizontalHeader();
ini()->setZonesHeader(header->saveState());
ini()->setZonesHeaderVersion(ZONES_HEADER_VERSION);
iniUser()->setZonesHeader(header->saveState());
iniUser()->setZonesHeaderVersion(ZONES_HEADER_VERSION);
confManager()->saveIniState();
confManager()->saveIniUser();
}
void ZonesWindow::restoreWindowState()
{
m_stateWatcher->restore(
this, QSize(1024, 768), ini()->zoneWindowGeometry(), ini()->zoneWindowMaximized());
m_stateWatcher->restore(this, QSize(1024, 768), iniUser()->zoneWindowGeometry(),
iniUser()->zoneWindowMaximized());
if (ini()->zonesHeaderVersion() == ZONES_HEADER_VERSION) {
if (iniUser()->zonesHeaderVersion() == ZONES_HEADER_VERSION) {
auto header = m_zoneListView->horizontalHeader();
header->restoreState(ini()->zonesHeader());
header->restoreState(iniUser()->zonesHeader());
}
}

View File

@ -12,6 +12,7 @@ QT_FORWARD_DECLARE_CLASS(QPushButton)
class ConfManager;
class FortManager;
class IniOptions;
class IniUser;
class TableView;
class TaskManager;
class WidgetWindowStateWatcher;
@ -29,6 +30,7 @@ public:
FortManager *fortManager() const;
ConfManager *confManager() const;
IniOptions *ini() const;
IniUser *iniUser() const;
TaskManager *taskManager() const;
ZoneListModel *zoneListModel() const;

View File

@ -37,6 +37,7 @@
#include "rpc/taskmanagerrpc.h"
#include "task/taskinfozonedownloader.h"
#include "translationmanager.h"
#include "user/usersettings.h"
#include "util/dateutil.h"
#include "util/envmanager.h"
#include "util/fileutil.h"
@ -69,6 +70,11 @@ FortManager::~FortManager()
OsUtil::closeMutex(m_instanceMutex);
}
IniUser *FortManager::iniUser() const
{
return &userSettings()->ini();
}
FirewallConf *FortManager::conf() const
{
return confManager()->conf();
@ -325,9 +331,15 @@ void FortManager::setupTaskManager()
taskManager()->initialize();
}
void FortManager::setupUserSettings()
{
m_userSettings = new UserSettings(this);
m_userSettings->initialize(settings());
}
void FortManager::setupTranslationManager()
{
TranslationManager::instance()->switchLanguageByName(settings()->language());
TranslationManager::instance()->switchLanguageByName(iniUser()->language());
}
void FortManager::setupMainWindow()
@ -433,7 +445,7 @@ void FortManager::show()
{
showTrayIcon();
if (conf()->ini().graphWindowVisible()) {
if (iniUser()->graphWindowVisible()) {
showGraphWindow();
}
}
@ -441,6 +453,7 @@ void FortManager::show()
void FortManager::showTrayIcon()
{
if (!m_trayIcon) {
setupUserSettings();
setupTranslationManager();
setupMainWindow();
setupHotKeyManager();
@ -709,7 +722,7 @@ bool FortManager::showYesNoBox(
void FortManager::loadConf()
{
QString viaVersion;
if (!settings()->confCanMigrate(viaVersion)) {
if (!settings()->canMigrate(viaVersion)) {
showInfoBox(tr("Please first install Fort Firewall v%1 and save Options from it.")
.arg(viaVersion));
abort(); // Abort the program

View File

@ -20,6 +20,7 @@ class FortSettings;
class GraphWindow;
class HostInfoCache;
class HotKeyManager;
class IniUser;
class LogManager;
class MainWindow;
class NativeEventFilter;
@ -30,6 +31,7 @@ class RpcManager;
class StatManager;
class TaskManager;
class TrayIcon;
class UserSettings;
class ZoneListModel;
class ZonesWindow;
@ -56,6 +58,8 @@ public:
ConnectionsWindow *connWindow() const { return m_connWindow; }
FortSettings *settings() const { return m_settings; }
UserSettings *userSettings() const { return m_userSettings; }
IniUser *iniUser() const;
EnvManager *envManager() const { return m_envManager; }
ControlManager *controlManager() const { return m_controlManager; }
RpcManager *rpcManager() const { return m_rpcManager; }
@ -160,6 +164,7 @@ private:
void setupTaskManager();
void setupUserSettings();
void setupTranslationManager();
void setupMainWindow();
@ -211,6 +216,7 @@ private:
ConnectionsWindow *m_connWindow = nullptr;
FortSettings *m_settings = nullptr;
UserSettings *m_userSettings = nullptr;
EnvManager *m_envManager = nullptr;
ControlManager *m_controlManager = nullptr;
RpcManager *m_rpcManager = nullptr;

View File

@ -2,11 +2,9 @@
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QSettings>
#include <fort_version.h>
#include "conf/addressgroup.h"
#include "conf/firewallconf.h"
#include "util/dateutil.h"
#include "util/envmanager.h"
@ -30,8 +28,7 @@ QString expandPath(const QString &path, EnvManager *envManager = nullptr)
}
FortSettings::FortSettings(QObject *parent) :
QObject(parent),
m_iniExists(false),
Settings(parent),
m_isDefaultProfilePath(false),
m_noCache(false),
m_isService(false),
@ -42,6 +39,59 @@ FortSettings::FortSettings(QObject *parent) :
{
}
QString FortSettings::confFilePath() const
{
return profilePath() + APP_BASE + ".config";
}
QString FortSettings::statFilePath() const
{
return statPath() + APP_BASE + ".stat";
}
QString FortSettings::cacheFilePath() const
{
return noCache() ? ":memory:" : cachePath() + "appinfo.db";
}
void FortSettings::setPassword(const QString &password)
{
setPasswordHash(StringUtil::cryptoHash(password));
if (!hasPassword()) {
resetCheckedPassword();
}
}
bool FortSettings::checkPassword(const QString &password) const
{
return StringUtil::cryptoHash(password) == passwordHash();
}
bool FortSettings::isPasswordRequired()
{
return hasPassword() && !(m_passwordUnlockType != 0 && m_passwordChecked);
}
void FortSettings::setPasswordChecked(bool checked, int unlockType)
{
if (m_passwordChecked == checked && m_passwordUnlockType == unlockType)
return;
m_passwordChecked = checked;
m_passwordUnlockType = checked ? unlockType : 0;
emit passwordCheckedChanged();
}
void FortSettings::resetCheckedPassword(int unlockType)
{
if (unlockType != 0 && unlockType != m_passwordUnlockType)
return;
setPasswordChecked(false);
}
void FortSettings::setupGlobal()
{
// Use global settings from program's binary directory
@ -55,17 +105,20 @@ void FortSettings::setupGlobal()
m_noCache = settings.value("global/noCache").toBool();
m_defaultLanguage = settings.value("global/defaultLanguage").toString();
m_profilePath = settings.value("global/profileDir").toString();
m_statPath = settings.value("global/statDir").toString();
m_logsPath = settings.value("global/logsDir").toString();
m_cachePath = settings.value("global/cacheDir").toString();
m_userPath = settings.value("global/userDir").toString();
}
void FortSettings::initialize(const QStringList &args, EnvManager *envManager)
{
processArguments(args);
setupPaths(envManager);
setupIni();
setupIni(profilePath() + APP_BASE + ".ini");
if (isService()) {
qputenv("FORT_SILENT", "1"); // For batch scripts
@ -192,6 +245,22 @@ void FortSettings::setupPaths(EnvManager *envManager)
m_cachePath = expandPath(m_cachePath, envManager);
}
// User Settings Path
if (m_userPath.isEmpty()) {
m_userPath = defaultConfigPath();
} else {
m_userPath = expandPath(m_userPath, envManager);
}
// Create directories
FileUtil::makePath(profilePath());
FileUtil::makePath(statPath());
FileUtil::makePath(logsPath());
if (!noCache()) {
FileUtil::makePath(cachePath());
}
FileUtil::makePath(userPath());
// Remove old cache file
// TODO: COMPAT: Remove after v4.1.0 (via v4.0.0)
FileUtil::removeFile(cachePath() + "appinfocache.db");
@ -210,82 +279,17 @@ QString FortSettings::defaultProfilePath(bool hasService, EnvManager *envManager
if (hasService)
return expandPath(QLatin1String("%ProgramData%\\") + APP_NAME, envManager);
return defaultConfigPath();
}
QString FortSettings::defaultConfigPath()
{
return pathSlash(FileUtil::appConfigLocation());
}
void FortSettings::setupIni()
{
const QString iniPath(profilePath() + (APP_BASE ".ini"));
FileUtil::makePath(profilePath());
FileUtil::makePath(statPath());
FileUtil::makePath(logsPath());
if (!noCache()) {
FileUtil::makePath(cachePath());
}
m_iniExists = FileUtil::fileExists(iniPath);
m_ini = new QSettings(iniPath, QSettings::IniFormat, this);
migrateIniOnStartup();
}
QString FortSettings::confFilePath() const
{
return profilePath() + (APP_BASE ".config");
}
QString FortSettings::statFilePath() const
{
return statPath() + (APP_BASE ".stat");
}
QString FortSettings::cacheFilePath() const
{
return noCache() ? ":memory:" : cachePath() + "appinfo.db";
}
void FortSettings::setPassword(const QString &password)
{
setPasswordHash(StringUtil::cryptoHash(password));
if (!hasPassword()) {
resetCheckedPassword();
}
}
bool FortSettings::checkPassword(const QString &password) const
{
return StringUtil::cryptoHash(password) == passwordHash();
}
bool FortSettings::isPasswordRequired()
{
return hasPassword() && !(m_passwordUnlockType != 0 && m_passwordChecked);
}
void FortSettings::setPasswordChecked(bool checked, int unlockType)
{
if (m_passwordChecked == checked && m_passwordUnlockType == unlockType)
return;
m_passwordChecked = checked;
m_passwordUnlockType = checked ? unlockType : 0;
emit passwordCheckedChanged();
}
void FortSettings::resetCheckedPassword(int unlockType)
{
if (unlockType != 0 && unlockType != m_passwordUnlockType)
return;
setPasswordChecked(false);
}
void FortSettings::readConfIni(FirewallConf &conf) const
{
m_ini->beginGroup("confFlags");
ini()->beginGroup("confFlags");
conf.setProvBoot(iniBool("provBoot"));
conf.setFilterEnabled(iniBool("filterEnabled", true));
conf.setFilterLocals(iniBool("filterLocals"));
@ -300,13 +304,13 @@ void FortSettings::readConfIni(FirewallConf &conf) const
conf.setAppBlockAll(iniBool("appBlockAll", true));
conf.setAppAllowAll(iniBool("appAllowAll"));
conf.setAppGroupBits(iniUInt("appGroupBits", DEFAULT_APP_GROUP_BITS));
m_ini->endGroup();
ini()->endGroup();
m_ini->beginGroup("stat");
ini()->beginGroup("stat");
conf.setActivePeriodEnabled(iniBool("activePeriodEnabled"));
conf.setActivePeriodFrom(DateUtil::reformatTime(iniText("activePeriodFrom")));
conf.setActivePeriodTo(DateUtil::reformatTime(iniText("activePeriodTo")));
m_ini->endGroup();
ini()->endGroup();
}
void FortSettings::writeConfIni(const FirewallConf &conf)
@ -314,7 +318,7 @@ void FortSettings::writeConfIni(const FirewallConf &conf)
bool changed = false;
if (conf.flagsEdited()) {
m_ini->beginGroup("confFlags");
ini()->beginGroup("confFlags");
setIniValue("provBoot", conf.provBoot());
setIniValue("filterEnabled", conf.filterEnabled());
setIniValue("filterLocals", conf.filterLocals());
@ -329,20 +333,22 @@ void FortSettings::writeConfIni(const FirewallConf &conf)
setIniValue("appBlockAll", conf.appBlockAll());
setIniValue("appAllowAll", conf.appAllowAll());
setIniValue("appGroupBits", conf.appGroupBits(), DEFAULT_APP_GROUP_BITS);
m_ini->endGroup();
ini()->endGroup();
m_ini->beginGroup("stat");
ini()->beginGroup("stat");
setIniValue("activePeriodEnabled", conf.activePeriodEnabled());
setIniValue("activePeriodFrom", conf.activePeriodFrom());
setIniValue("activePeriodTo", conf.activePeriodTo());
m_ini->endGroup();
ini()->endGroup();
changed = true;
}
if (conf.iniEdited()) {
const IniOptions &ini = conf.ini();
ini.save(this);
// Save changed keys
ini.save();
// Password
if (ini.hasPassword() != hasPassword() || !ini.password().isEmpty()) {
@ -367,8 +373,8 @@ void FortSettings::migrateIniOnStartup()
#if 0
// COMPAT: v3.0.0: Options Window
if (version < 0x030000) {
setCacheValue("optWindow/geometry", m_ini->value("window/geometry"));
setCacheValue("optWindow/maximized", m_ini->value("window/maximized"));
setCacheValue("optWindow/geometry", ini()->value("window/geometry"));
setCacheValue("optWindow/maximized", ini()->value("window/maximized"));
// Abandon "window/addrSplit" & "window/appsSplit"
}
#endif
@ -380,19 +386,19 @@ void FortSettings::migrateIniOnWrite()
if (version == appVersion())
return;
setIniVersion(appVersion());
Settings::migrateIniOnWrite();
#if 0
// COMPAT: v3.0.0: Options Window
if (version < 0x030000) {
removeIniKey("window");
m_ini->setValue("optWindow/geometry", cacheValue("optWindow/geometry"));
m_ini->setValue("optWindow/maximized", cacheValue("optWindow/maximized"));
ini()->setValue("optWindow/geometry", cacheValue("optWindow/geometry"));
ini()->setValue("optWindow/maximized", cacheValue("optWindow/maximized"));
}
#endif
}
bool FortSettings::confMigrated() const
bool FortSettings::wasMigrated() const
{
const int version = iniVersion();
if (version == appVersion())
@ -407,7 +413,7 @@ bool FortSettings::confMigrated() const
return false;
}
bool FortSettings::confCanMigrate(QString &viaVersion) const
bool FortSettings::canMigrate(QString &viaVersion) const
{
const int version = iniVersion();
if (version == appVersion())
@ -421,132 +427,3 @@ bool FortSettings::confCanMigrate(QString &viaVersion) const
return true;
}
bool FortSettings::hasError() const
{
return m_ini->status() != QSettings::NoError;
}
QString FortSettings::errorMessage() const
{
switch (m_ini->status()) {
case QSettings::AccessError:
return "Access Error";
case QSettings::FormatError:
return "Format Error";
default:
return "Unknown";
}
}
bool FortSettings::iniBool(const QString &key, bool defaultValue) const
{
return iniValue(key, defaultValue).toBool();
}
int FortSettings::iniInt(const QString &key, int defaultValue) const
{
return iniValue(key, defaultValue).toInt();
}
uint FortSettings::iniUInt(const QString &key, int defaultValue) const
{
return iniValue(key, defaultValue).toUInt();
}
qreal FortSettings::iniReal(const QString &key, qreal defaultValue) const
{
return iniValue(key, defaultValue).toReal();
}
QString FortSettings::iniText(const QString &key, const QString &defaultValue) const
{
return iniValue(key, defaultValue).toString();
}
QStringList FortSettings::iniList(const QString &key) const
{
return iniValue(key).toStringList();
}
QVariantMap FortSettings::iniMap(const QString &key) const
{
return iniValue(key).toMap();
}
QByteArray FortSettings::iniByteArray(const QString &key) const
{
return iniValue(key).toByteArray();
}
QVariant FortSettings::iniValue(const QString &key, const QVariant &defaultValue) const
{
if (key.isEmpty())
return QVariant();
// Try to load from cache
const auto cachedValue = cacheValue(key);
if (!cachedValue.isNull())
return cachedValue;
// Load from .ini
const auto value = m_ini->value(key, defaultValue);
// Save to cache
setCacheValue(key, value);
return value;
}
void FortSettings::setIniValue(
const QString &key, const QVariant &value, const QVariant &defaultValue)
{
const QVariant oldValue = iniValue(key, defaultValue);
if (oldValue == value)
return;
// Save to .ini
m_ini->setValue(key, value);
// Save to cache
setCacheValue(key, value);
}
QVariant FortSettings::cacheValue(const QString &key) const
{
return m_cache.value(key);
}
void FortSettings::setCacheValue(const QString &key, const QVariant &value) const
{
m_cache.insert(key, value);
}
void FortSettings::clearCache()
{
m_cache.clear();
iniSync();
}
void FortSettings::removeIniKey(const QString &key)
{
m_ini->remove(key);
}
QStringList FortSettings::iniChildKeys(const QString &prefix) const
{
m_ini->beginGroup(prefix);
const QStringList list = m_ini->childKeys();
m_ini->endGroup();
return list;
}
void FortSettings::iniSync()
{
m_ini->sync();
}
int FortSettings::appVersion()
{
return APP_VERSION;
}

View File

@ -1,27 +1,18 @@
#ifndef FORTSETTINGS_H
#define FORTSETTINGS_H
#include <QColor>
#include <QHash>
#include <QSettings>
#include "util/ini/settings.h"
class EnvManager;
class FirewallConf;
class FortSettings : public QObject
class FortSettings : public Settings
{
Q_OBJECT
friend class IniOptions;
public:
explicit FortSettings(QObject *parent = nullptr);
int iniVersion() const { return iniInt("base/version", appVersion()); }
void setIniVersion(int v) { setIniValue("base/version", v); }
QString language() const { return iniText("base/language", defaultLanguage()); }
QString passwordHash() const { return iniText("base/passwordHash"); }
void setPasswordHash(const QString &v) { setIniValue("base/passwordHash", v); }
@ -47,6 +38,8 @@ public:
QString cachePath() const { return m_cachePath; }
QString cacheFilePath() const;
QString userPath() const { return m_userPath; }
bool isWindowControl() const { return m_isWindowControl; }
QString controlCommand() const { return m_controlCommand; }
@ -65,61 +58,31 @@ public:
void setPasswordChecked(bool checked, int unlockType = 0);
void resetCheckedPassword(int unlockType = 0);
bool confMigrated() const;
bool confCanMigrate(QString &viaVersion) const;
void setupGlobal();
void initialize(const QStringList &args, EnvManager *envManager = nullptr);
bool hasError() const;
QString errorMessage() const;
bool wasMigrated() const;
bool canMigrate(QString &viaVersion) const;
static QString defaultProfilePath(bool hasService, EnvManager *envManager = nullptr);
static QString defaultConfigPath();
signals:
void passwordCheckedChanged();
public slots:
void setupGlobal();
void initialize(const QStringList &args, EnvManager *envManager = nullptr);
void readConfIni(FirewallConf &conf) const;
void writeConfIni(const FirewallConf &conf);
void clearCache();
protected:
void migrateIniOnStartup() override;
void migrateIniOnWrite() override;
private:
void processArguments(const QStringList &args);
void setupPaths(EnvManager *envManager);
void setupIni();
void migrateIniOnStartup();
void migrateIniOnWrite();
bool iniBool(const QString &key, bool defaultValue = false) const;
int iniInt(const QString &key, int defaultValue = 0) const;
uint iniUInt(const QString &key, int defaultValue = 0) const;
qreal iniReal(const QString &key, qreal defaultValue = 0) const;
QString iniText(const QString &key, const QString &defaultValue = QString()) const;
QStringList iniList(const QString &key) const;
QVariantMap iniMap(const QString &key) const;
QByteArray iniByteArray(const QString &key) const;
QVariant iniValue(const QString &key, const QVariant &defaultValue = QVariant()) const;
void setIniValue(
const QString &key, const QVariant &value, const QVariant &defaultValue = QVariant());
QVariant cacheValue(const QString &key) const;
void setCacheValue(const QString &key, const QVariant &value) const;
void removeIniKey(const QString &key);
QStringList iniChildKeys(const QString &prefix) const;
void iniSync();
static int appVersion();
private:
uint m_iniExists : 1;
uint m_isDefaultProfilePath : 1;
uint m_noCache : 1;
uint m_isService : 1;
@ -134,14 +97,11 @@ private:
QString m_statPath;
QString m_logsPath;
QString m_cachePath;
QString m_userPath;
QString m_controlCommand;
QStringList m_args;
QStringList m_appArguments;
QSettings *m_ini = nullptr;
mutable QHash<QString, QVariant> m_cache;
};
#endif // FORTSETTINGS_H

3
src/ui/user/iniuser.cpp Normal file
View File

@ -0,0 +1,3 @@
#include "iniuser.h"
IniUser::IniUser(Settings *settings) : MapSettings(settings) { }

111
src/ui/user/iniuser.h Normal file
View File

@ -0,0 +1,111 @@
#ifndef INIUSER_H
#define INIUSER_H
#include "../util/ini/mapsettings.h"
class IniUser : public MapSettings
{
public:
explicit IniUser(Settings *settings = nullptr);
QString defaultLanguage() const { return m_defaultLanguage; }
void setDefaultLanguage(const QString &v) { m_defaultLanguage = v; }
QString language() const { return valueText("base/language", defaultLanguage()); }
void setLanguage(const QString &v) { setValue("base/language", v); }
bool hotKeyEnabled() const { return valueBool("hotKey/enabled"); }
void setHotKeyEnabled(bool v) { setValue("hotKey/enabled", v); }
QString hotKeyPrograms() const { return valueText("hotKey/programs"); }
QString hotKeyOptions() const { return valueText("hotKey/options"); }
QString hotKeyZones() const { return valueText("hotKey/zones"); }
QString hotKeyGraph() const { return valueText("hotKey/graph"); }
QString hotKeyConnections() const { return valueText("hotKey/connections"); }
QString hotKeyFilter() const { return valueText("hotKey/filter", "Ctrl+Alt+Shift+F"); }
QString hotKeyStopTraffic() const { return valueText("hotKey/stopTraffic"); }
QString hotKeyStopInetTraffic() const { return valueText("hotKey/stopInetTraffic"); }
QString hotKeyAllowAllNew() const { return valueText("hotKey/allowAllNew"); }
QString hotKeyAppGroupModifiers() const
{
return valueText("hotKey/appGroupModifiers", "Ctrl+Alt+Shift");
}
QString hotKeyQuit() const { return valueText("hotKey/quit"); }
QRect progWindowGeometry() const { return value("progWindow/geometry").toRect(); }
void setProgWindowGeometry(const QRect &v) { setValue("progWindow/geometry", v); }
bool progWindowMaximized() const { return valueBool("progWindow/maximized"); }
void setProgWindowMaximized(bool on) { setValue("progWindow/maximized", on); }
bool progAppsSortDesc() const { return valueBool("progWindow/appsSortDesc"); }
void setProgSortDesc(bool v) { setValue("progWindow/appsSortDesc", v); }
int progAppsSortColumn() const { return valueInt("progWindow/appsSortColumn"); }
void setProgSortColumn(int v) { setValue("progWindow/appsSortColumn", v); }
int progAppsHeaderVersion() const { return valueInt("progWindow/appsHeaderVersion"); }
void setProgAppsHeaderVersion(int v) { setValue("progWindow/appsHeaderVersion", v); }
QByteArray progAppsHeader() const { return valueByteArray("progWindow/appsHeader"); }
void setProgAppsHeader(const QByteArray &v) { setValue("progWindow/appsHeader", v); }
QRect optWindowGeometry() const { return value("optWindow/geometry").toRect(); }
void setOptWindowGeometry(const QRect &v) { setValue("optWindow/geometry", v); }
bool optWindowMaximized() const { return valueBool("optWindow/maximized"); }
void setOptWindowMaximized(bool on) { setValue("optWindow/maximized", on); }
QByteArray optWindowAddrSplit() const { return valueByteArray("optWindow/addrSplit"); }
void setOptWindowAddrSplit(const QByteArray &v) { setValue("optWindow/addrSplit", v); }
QByteArray optWindowAppsSplit() const { return valueByteArray("optWindow/appsSplit"); }
void setOptWindowAppsSplit(const QByteArray &v) { setValue("optWindow/appsSplit", v); }
QByteArray optWindowStatSplit() const { return valueByteArray("optWindow/statSplit"); }
void setOptWindowStatSplit(const QByteArray &v) { setValue("optWindow/statSplit", v); }
QRect zoneWindowGeometry() const { return value("zoneWindow/geometry").toRect(); }
void setZoneWindowGeometry(const QRect &v) { setValue("zoneWindow/geometry", v); }
bool zoneWindowMaximized() const { return valueBool("zoneWindow/maximized"); }
void setZoneWindowMaximized(bool on) { setValue("zoneWindow/maximized", on); }
int zonesHeaderVersion() const { return valueInt("zoneWindow/zonesHeaderVersion"); }
void setZonesHeaderVersion(int v) { setValue("zoneWindow/zonesHeaderVersion", v); }
QByteArray zonesHeader() const { return valueByteArray("zoneWindow/zonesHeader"); }
void setZonesHeader(const QByteArray &v) { setValue("zoneWindow/zonesHeader", v); }
bool graphWindowVisible() const { return valueBool("graphWindow/visible"); }
void setGraphWindowVisible(bool on) { setValue("graphWindow/visible", on); }
QRect graphWindowGeometry() const { return value("graphWindow/geometry").toRect(); }
void setGraphWindowGeometry(const QRect &v) { setValue("graphWindow/geometry", v); }
bool graphWindowMaximized() const { return valueBool("graphWindow/maximized"); }
void setGraphWindowMaximized(bool on) { setValue("graphWindow/maximized", on); }
QRect connWindowGeometry() const { return value("connWindow/geometry").toRect(); }
void setConnWindowGeometry(const QRect &v) { setValue("connWindow/geometry", v); }
bool connWindowMaximized() const { return valueBool("connWindow/maximized"); }
void setConnWindowMaximized(bool on) { setValue("connWindow/maximized", on); }
int connListHeaderVersion() const { return valueInt("connWindow/connListHeaderVersion"); }
void setConnListHeaderVersion(int v) { setValue("connWindow/connListHeaderVersion", v); }
QByteArray connListHeader() const { return valueByteArray("connWindow/connListHeader"); }
void setConnListHeader(const QByteArray &v) { setValue("connWindow/connListHeader", v); }
bool connAutoScroll() const { return valueBool("connWindow/autoScroll"); }
void setConnAutoScroll(bool on) { setValue("connWindow/autoScroll", on); }
bool connShowHostNames() const { return valueBool("connWindow/showHostNames"); }
void setConnShowHostNames(bool on) { setValue("connWindow/showHostNames", on); }
private:
QString m_defaultLanguage;
};
#endif // INIUSER_H

View File

@ -0,0 +1,14 @@
#include "usersettings.h"
#include <fort_version.h>
#include "../fortsettings.h"
UserSettings::UserSettings(QObject *parent) : Settings(parent), m_ini(this) { }
void UserSettings::initialize(FortSettings *settings)
{
setupIni(settings->userPath() + APP_BASE + ".user.ini");
ini().setDefaultLanguage(settings->defaultLanguage());
}

View File

@ -0,0 +1,25 @@
#ifndef USERSETTINGS_H
#define USERSETTINGS_H
#include "../util/ini/settings.h"
#include "iniuser.h"
class FortSettings;
class UserSettings : public Settings
{
Q_OBJECT
public:
explicit UserSettings(QObject *parent = nullptr);
IniUser &ini() { return m_ini; }
const IniUser &ini() const { return m_ini; }
void initialize(FortSettings *settings);
private:
IniUser m_ini;
};
#endif // USERSETTINGS_H

View File

@ -0,0 +1,52 @@
#include "mapsettings.h"
#include "settings.h"
MapSettings::MapSettings(Settings *settings) : m_settings(settings) { }
MapSettings::MapSettings(const MapSettings &o) : MapWrapper(o.map()), m_settings(o.settings()) { }
void MapSettings::save() const
{
Q_ASSERT(settings());
auto it = map().constBegin();
const auto end = map().constEnd();
for (; it != end; ++it) {
const QString &key = it.key();
if (!isTransientKey(key)) {
settings()->setIniValue(key, it.value());
}
}
}
QVariant MapSettings::value(const QString &key, const QVariant &defaultValue) const
{
const QVariant v = MapWrapper::value(key);
if (!v.isNull())
return v;
return settings() ? settings()->iniValue(key, defaultValue) : defaultValue;
}
void MapSettings::setValue(const QString &key, const QVariant &v, const QVariant &defaultValue)
{
const QVariant oldValue = value(key, defaultValue);
if (oldValue == v)
return;
MapWrapper::setValue(key, v, defaultValue);
}
void MapSettings::setCacheValue(const QString &key, const QVariant &v) const
{
Q_ASSERT(settings());
settings()->setCacheValue(key, v);
}
bool MapSettings::isTransientKey(const QString &key)
{
return key.endsWith('_');
}

View File

@ -0,0 +1,35 @@
#ifndef MAPSETTINGS_H
#define MAPSETTINGS_H
#include <QRect>
#include "../json/mapwrapper.h"
class Settings;
class MapSettings : public MapWrapper
{
public:
explicit MapSettings(Settings *settings = nullptr);
explicit MapSettings(const MapSettings &o);
Settings *settings() const { return m_settings; }
void setSettings(Settings *v) { m_settings = v; }
public slots:
void save() const;
protected:
QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const override;
void setValue(const QString &key, const QVariant &v,
const QVariant &defaultValue = QVariant()) override;
void setCacheValue(const QString &key, const QVariant &v) const;
static bool isTransientKey(const QString &key);
private:
Settings *m_settings = nullptr;
};
#endif // MAPSETTINGS_H

View File

@ -0,0 +1,150 @@
#include "settings.h"
#include <fort_version.h>
#include "../fileutil.h"
Settings::Settings(QObject *parent) : QObject(parent) { }
void Settings::setupIni(const QString &filePath)
{
const QString iniPath(filePath);
m_iniExists = FileUtil::fileExists(iniPath);
m_ini = new QSettings(iniPath, QSettings::IniFormat, this);
migrateIniOnStartup();
}
void Settings::migrateIniOnWrite()
{
setIniVersion(appVersion());
}
bool Settings::hasError() const
{
return m_ini->status() != QSettings::NoError;
}
QString Settings::errorMessage() const
{
switch (m_ini->status()) {
case QSettings::AccessError:
return "Access Error";
case QSettings::FormatError:
return "Format Error";
default:
return "Unknown";
}
}
bool Settings::iniBool(const QString &key, bool defaultValue) const
{
return iniValue(key, defaultValue).toBool();
}
int Settings::iniInt(const QString &key, int defaultValue) const
{
return iniValue(key, defaultValue).toInt();
}
uint Settings::iniUInt(const QString &key, int defaultValue) const
{
return iniValue(key, defaultValue).toUInt();
}
qreal Settings::iniReal(const QString &key, qreal defaultValue) const
{
return iniValue(key, defaultValue).toReal();
}
QString Settings::iniText(const QString &key, const QString &defaultValue) const
{
return iniValue(key, defaultValue).toString();
}
QStringList Settings::iniList(const QString &key) const
{
return iniValue(key).toStringList();
}
QVariantMap Settings::iniMap(const QString &key) const
{
return iniValue(key).toMap();
}
QByteArray Settings::iniByteArray(const QString &key) const
{
return iniValue(key).toByteArray();
}
QVariant Settings::iniValue(const QString &key, const QVariant &defaultValue) const
{
if (key.isEmpty())
return QVariant();
// Try to load from cache
const auto cachedValue = cacheValue(key);
if (!cachedValue.isNull())
return cachedValue;
// Load from .ini
const auto value = m_ini->value(key, defaultValue);
// Save to cache
setCacheValue(key, value);
return value;
}
void Settings::setIniValue(const QString &key, const QVariant &value, const QVariant &defaultValue)
{
const QVariant oldValue = iniValue(key, defaultValue);
if (oldValue == value)
return;
// Save to .ini
m_ini->setValue(key, value);
// Save to cache
setCacheValue(key, value);
}
QVariant Settings::cacheValue(const QString &key) const
{
return m_cache.value(key);
}
void Settings::setCacheValue(const QString &key, const QVariant &value) const
{
m_cache.insert(key, value);
}
void Settings::clearCache()
{
m_cache.clear();
iniSync();
}
void Settings::removeIniKey(const QString &key)
{
m_ini->remove(key);
}
QStringList Settings::iniChildKeys(const QString &prefix) const
{
m_ini->beginGroup(prefix);
const QStringList list = m_ini->childKeys();
m_ini->endGroup();
return list;
}
void Settings::iniSync()
{
m_ini->sync();
}
int Settings::appVersion()
{
return APP_VERSION;
}

View File

@ -0,0 +1,66 @@
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QHash>
#include <QSettings>
class Settings : public QObject
{
Q_OBJECT
friend class MapSettings;
public:
explicit Settings(QObject *parent = nullptr);
bool hasError() const;
QString errorMessage() const;
void clearCache();
protected:
int iniVersion() const { return iniInt("base/version", appVersion()); }
void setIniVersion(int v) { setIniValue("base/version", v); }
bool iniExists() const { return m_iniExists; }
QSettings *ini() const { return m_ini; }
void setupIni(const QString &filePath);
virtual void migrateIniOnStartup() { }
virtual void migrateIniOnWrite();
bool iniBool(const QString &key, bool defaultValue = false) const;
int iniInt(const QString &key, int defaultValue = 0) const;
uint iniUInt(const QString &key, int defaultValue = 0) const;
qreal iniReal(const QString &key, qreal defaultValue = 0) const;
QString iniText(const QString &key, const QString &defaultValue = QString()) const;
QStringList iniList(const QString &key) const;
QVariantMap iniMap(const QString &key) const;
QByteArray iniByteArray(const QString &key) const;
QVariant iniValue(const QString &key, const QVariant &defaultValue = QVariant()) const;
void setIniValue(
const QString &key, const QVariant &value, const QVariant &defaultValue = QVariant());
QVariant cacheValue(const QString &key) const;
void setCacheValue(const QString &key, const QVariant &value) const;
void removeIniKey(const QString &key);
QStringList iniChildKeys(const QString &prefix) const;
void iniSync();
static int appVersion();
private:
uint m_iniExists : 1;
QSettings *m_ini = nullptr;
mutable QHash<QString, QVariant> m_cache;
};
#endif // SETTINGS_H

View File

@ -71,7 +71,7 @@ QVariant MapWrapper::value(const QString &key, const QVariant &defaultValue) con
return map().value(key, defaultValue);
}
void MapWrapper::setValue(const QString &key, const QVariant &v)
void MapWrapper::setValue(const QString &key, const QVariant &v, const QVariant & /*defaultValue*/)
{
m_map[key] = v;
}

View File

@ -30,7 +30,8 @@ protected:
void setColor(const QString &key, const QColor &v);
virtual QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
void setValue(const QString &key, const QVariant &v);
virtual void setValue(
const QString &key, const QVariant &v, const QVariant &defaultValue = QVariant());
bool contains(const QString &key) const;