fort/src/ui/fortmanager.cpp

89 lines
2.2 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);
createConfToEdit();
2017-09-01 13:13:12 +00:00
setupContext();
m_engine->load(QUrl("qrc:/qml/main.qml"));
}
2017-09-02 10:17:51 +00:00
void FortManager::createConfToEdit()
2017-09-01 13:13:12 +00:00
{
2017-09-02 10:17:51 +00:00
m_firewallConfToEdit = new FirewallConf(this);
2017-09-01 15:13:17 +00:00
2017-09-02 10:17:51 +00:00
// Clone from current one
{
const QVariant data = m_firewallConf->toVariant();
m_firewallConfToEdit->fromVariant(data);
m_fortSettings->readConfFlags(*m_firewallConfToEdit);
2017-09-01 15:13:17 +00:00
}
2017-09-02 10:17:51 +00:00
}
bool FortManager::saveConf()
{
if (!m_fortSettings->writeConf(*m_firewallConfToEdit))
return false;
2017-09-01 15:13:17 +00:00
m_firewallConf->deleteLater();
2017-09-02 10:17:51 +00:00
m_firewallConf = m_firewallConfToEdit;
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();
m_engine = 0;
if (m_firewallConfToEdit && m_firewallConfToEdit != m_firewallConf) {
m_firewallConfToEdit->deleteLater();
m_firewallConfToEdit = 0;
}
}