fort/src/ui/fortmanager.cpp

108 lines
2.8 KiB
C++
Raw Normal View History

2017-09-01 15:13:17 +00:00
#include "fortmanager.h"
2017-09-01 13:13:12 +00:00
2017-09-01 15:13:17 +00:00
#include <QCoreApplication>
2017-09-01 13:13:12 +00:00
#include <QQmlApplicationEngine>
#include <QQmlContext>
2017-09-02 10:17:51 +00:00
#include "conf/addressgroup.h"
2017-09-01 13:13:12 +00:00
#include "conf/appgroup.h"
#include "conf/firewallconf.h"
#include "fortsettings.h"
2017-09-01 15:13:17 +00:00
FortManager::FortManager(QObject *parent) :
2017-09-01 13:13:12 +00:00
QObject(parent),
2017-09-01 15:13:17 +00:00
m_fortSettings(new FortSettings(qApp->arguments(), this)),
2017-09-02 10:17:51 +00:00
m_firewallConf(new FirewallConf(this)),
m_firewallConfToEdit(nullptr)
2017-09-01 13:13:12 +00:00
{
2017-09-01 15:13:17 +00:00
m_fortSettings->readConf(*m_firewallConf);
2017-09-02 10:17:51 +00:00
registerQmlTypes();
2017-09-01 13:13:12 +00:00
}
2017-09-01 15:13:17 +00:00
void FortManager::registerQmlTypes()
2017-09-01 13:13:12 +00:00
{
2017-09-02 10:17:51 +00:00
qmlRegisterUncreatableType<FortManager>("com.fortfirewall", 1, 0, "FortManager",
"Singleton");
2017-09-01 13:13:12 +00:00
qmlRegisterUncreatableType<FortSettings>("com.fortfirewall", 1, 0, "FortSettings",
"Singleton");
2017-09-02 10:17:51 +00:00
qmlRegisterType<AddressGroup>("com.fortfirewall", 1, 0, "AddressGroup");
2017-09-01 13:13:12 +00:00
qmlRegisterType<AppGroup>("com.fortfirewall", 1, 0, "AppGroup");
qmlRegisterType<FirewallConf>("com.fortfirewall", 1, 0, "FirewallConf");
}
2017-09-01 15:13:17 +00:00
void FortManager::setupContext()
2017-09-01 13:13:12 +00:00
{
QQmlContext *context = m_engine->rootContext();
2017-09-01 15:13:17 +00:00
context->setContextProperty("fortManager", this);
2017-09-01 13:13:12 +00:00
}
2017-09-01 15:13:17 +00:00
void FortManager::showWindow()
2017-09-01 13:13:12 +00:00
{
m_engine = new QQmlApplicationEngine(this);
2017-09-02 10:17:51 +00:00
connect(m_engine, &QQmlApplicationEngine::destroyed,
this, &FortManager::handleClosedWindow);
2017-09-01 13:13:12 +00:00
setupContext();
2017-09-02 14:25:47 +00:00
// New conf to edit
Q_ASSERT(!m_firewallConfToEdit);
m_firewallConfToEdit = cloneConf(*m_firewallConf);
2017-09-01 13:13:12 +00:00
m_engine->load(QUrl("qrc:/qml/main.qml"));
}
2017-09-02 14:25:47 +00:00
bool FortManager::saveConf()
2017-09-01 13:13:12 +00:00
{
2017-09-02 14:25:47 +00:00
return saveSettings(m_firewallConfToEdit);
}
2017-09-02 10:17:51 +00:00
2017-09-02 14:25:47 +00:00
bool FortManager::applyConf()
{
Q_ASSERT(m_firewallConfToEdit);
return saveSettings(cloneConf(*m_firewallConfToEdit));
2017-09-02 10:17:51 +00:00
}
2017-09-02 14:25:47 +00:00
bool FortManager::saveSettings(FirewallConf *newConf)
2017-09-02 10:17:51 +00:00
{
2017-09-02 14:25:47 +00:00
if (!m_fortSettings->writeConf(*newConf))
2017-09-02 10:17:51 +00:00
return false;
2017-09-01 15:13:17 +00:00
m_firewallConf->deleteLater();
2017-09-02 14:25:47 +00:00
m_firewallConf = newConf;
2017-09-01 15:13:17 +00:00
return true;
2017-09-01 13:13:12 +00:00
}
2017-09-02 10:17:51 +00:00
void FortManager::handleClosedWindow()
{
m_engine->deleteLater();
2017-09-02 12:52:39 +00:00
m_engine = nullptr;
2017-09-02 10:17:51 +00:00
if (m_firewallConfToEdit && m_firewallConfToEdit != m_firewallConf) {
m_firewallConfToEdit->deleteLater();
2017-09-02 12:52:39 +00:00
m_firewallConfToEdit = nullptr;
2017-09-02 10:17:51 +00:00
}
}
2017-09-02 14:25:47 +00:00
FirewallConf *FortManager::cloneConf(const FirewallConf &conf)
{
FirewallConf *newConf = new FirewallConf(this);
const QVariant data = conf.toVariant();
newConf->fromVariant(data);
// Flags
newConf->setFilterEnabled(conf.filterEnabled());
newConf->ipInclude()->setUseAll(conf.ipInclude()->useAll());
newConf->ipExclude()->setUseAll(conf.ipExclude()->useAll());
newConf->setAppBlockAll(conf.appBlockAll());
newConf->setAppAllowAll(conf.appAllowAll());
newConf->setAppGroupBits(conf.appGroupBits());
return newConf;
}