fort/src/ui/fortsettings.cpp

197 lines
5.4 KiB
C++
Raw Normal View History

2017-08-30 16:20:31 +00:00
#include "fortsettings.h"
#include <QCommandLineParser>
#include <QJsonDocument>
#include <QSettings>
#include <QStandardPaths>
#include "conf/firewallconf.h"
#include "util/fileutil.h"
FortSettings::FortSettings(const QStringList &args,
QObject *parent) :
QObject(parent)
{
processArguments(args);
setupIni();
}
void FortSettings::processArguments(const QStringList &args)
{
const QCommandLineOption profileOption("profile", "Directory to store settings.");
QCommandLineParser parser;
parser.addOption(profileOption);
parser.process(args);
m_profilePath = parser.value(profileOption);
if (m_profilePath.isEmpty()) {
m_profilePath = QStandardPaths::writableLocation(
QStandardPaths::AppConfigLocation);
}
m_profilePath = FileUtil::absolutePath(m_profilePath);
const QLatin1Char slash('/');
if (!m_profilePath.endsWith(slash))
m_profilePath += slash;
}
void FortSettings::setupIni()
{
const QString qrcIniPath(":/FortFirewall.ini");
const QString iniPath(m_profilePath + "FortFirewall.ini");
FileUtil::makePath(m_profilePath);
// Copy default .ini into writable location
if (!FileUtil::fileExists(iniPath)) {
const QString text = FileUtil::readFile(qrcIniPath);
if (!FileUtil::writeFile(iniPath, text)) {
FileUtil::removeFile(iniPath);
}
}
m_ini = new QSettings(iniPath, QSettings::IniFormat, this);
}
QString FortSettings::confFilePath() const
{
return m_profilePath + QLatin1String("FortFirewall.conf");
}
QString FortSettings::confBackupFilePath() const
{
return confFilePath() + QLatin1String(".backup");
}
bool FortSettings::readConf(FirewallConf &conf) const
{
const QString filePath = confFilePath();
const QString backupFilePath = confBackupFilePath();
return tryToReadConf(conf, filePath)
|| tryToReadConf(conf, backupFilePath);
}
bool FortSettings::tryToReadConf(FirewallConf &conf, const QString &filePath) const
{
const QString text = FileUtil::readFile(filePath);
QJsonParseError jsonParseError;
const QJsonDocument jsonDoc = QJsonDocument::fromJson(
text.toUtf8(), &jsonParseError);
if (jsonParseError.error != QJsonParseError::NoError)
return false;
conf.fromVariant(jsonDoc.toVariant());
return readConfFlags(conf);
}
bool FortSettings::writeConf(const FirewallConf &conf)
{
const QString filePath = confFilePath();
const QString backupFilePath = confBackupFilePath();
if (FileUtil::fileExists(backupFilePath)
&& !FileUtil::renameFile(backupFilePath, filePath))
return false;
return tryToWriteConf(conf, backupFilePath)
&& FileUtil::renameFile(backupFilePath, filePath);
}
bool FortSettings::tryToWriteConf(const FirewallConf &conf, const QString &filePath)
{
const QJsonDocument jsonDoc = QJsonDocument::fromVariant(
conf.toVariant());
const QByteArray data = jsonDoc.toJson(QJsonDocument::Indented);
2017-09-01 13:13:33 +00:00
if (!FileUtil::writeFileData(filePath, data))
2017-08-30 16:20:31 +00:00
return false;
return writeConfFlags(conf);
}
bool FortSettings::readConfFlags(FirewallConf &conf) const
{
2017-08-30 16:31:52 +00:00
m_ini->beginGroup("confFlags");
conf.setFilterEnabled(iniBool("filterEnabled", true));
2017-08-30 16:20:31 +00:00
conf.setIpIncludeAll(iniBool("ipIncludeAll"));
conf.setIpExcludeAll(iniBool("ipExcludeAll"));
conf.setAppLogBlocked(iniBool("appLogBlocked", true));
conf.setAppBlockAll(iniBool("appBlockAll", true));
conf.setAppAllowAll(iniBool("appAllowAll"));
conf.setAppGroupBits(iniUInt("appGroupBits", 0xFFFF));
2017-08-30 16:31:52 +00:00
m_ini->endGroup();
2017-08-30 16:20:31 +00:00
return true;
}
bool FortSettings::writeConfFlags(const FirewallConf &conf)
{
2017-08-30 16:31:52 +00:00
m_ini->beginGroup("confFlags");
setIniValue("filterEnabled", conf.filterEnabled());
2017-08-30 16:20:31 +00:00
setIniValue("ipIncludeAll", conf.ipIncludeAll());
setIniValue("ipExcludeAll", conf.ipExcludeAll());
setIniValue("appLogBlocked", conf.appLogBlocked());
setIniValue("appBlockAll", conf.appBlockAll());
setIniValue("appAllowAll", conf.appAllowAll());
setIniValue("appGroupBits", conf.appGroupBits());
2017-08-30 16:31:52 +00:00
m_ini->endGroup();
2017-08-30 16:20:31 +00:00
m_ini->sync();
2017-09-01 13:13:33 +00:00
return m_ini->status() == QSettings::NoError;
2017-08-30 16:20:31 +00:00
}
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();
}
int FortSettings::iniUInt(const QString &key, int defaultValue) const
{
return iniValue(key, defaultValue).toUInt();
}
int 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();
}
QVariant FortSettings::iniValue(const QString &key,
const QVariant &defaultValue) const
{
if (key.isEmpty())
return QVariant();
return m_ini->value(key, defaultValue);
}
2017-08-30 16:31:52 +00:00
void FortSettings::setIniValue(const QString &key, const QVariant &value,
const QVariant &defaultValue)
2017-08-30 16:20:31 +00:00
{
2017-08-30 16:31:52 +00:00
if (m_ini->value(key, defaultValue) == value)
return;
2017-08-30 16:20:31 +00:00
m_ini->setValue(key, value);
2017-08-30 16:31:52 +00:00
emit iniChanged();
2017-08-30 16:20:31 +00:00
}