Add DriverManager class.

This commit is contained in:
Nodir Temirkhodjaev 2017-09-04 00:41:03 +05:00
parent 0e4c740859
commit 1dda1b2260
8 changed files with 198 additions and 15 deletions

View File

@ -7,11 +7,12 @@ TEMPLATE = app
SOURCES += \
main.cpp \
activityLog/logbuffer.cpp \
activityLog/logentry.cpp \
conf/addressgroup.cpp \
conf/appgroup.cpp \
conf/firewallconf.cpp \
activityLog/logbuffer.cpp \
activityLog/logentry.cpp \
drivermanager.cpp \
fortcommon.cpp \
fortmanager.cpp \
fortsettings.cpp \
@ -23,11 +24,12 @@ SOURCES += \
util/processinfo.cpp
HEADERS += \
activityLog/logbuffer.h \
activityLog/logentry.h \
conf/addressgroup.h \
conf/appgroup.h \
conf/firewallconf.h \
activityLog/logbuffer.h \
activityLog/logentry.h \
drivermanager.h \
fortcommon.h \
fortmanager.h \
fortsettings.h \

70
src/ui/drivermanager.cpp Normal file
View File

@ -0,0 +1,70 @@
#include "drivermanager.h"
#include "fortcommon.h"
#include "conf/firewallconf.h"
#include "util/confutil.h"
#include "util/device.h"
DriverManager::DriverManager(QObject *parent) :
QObject(parent),
m_device(new Device(this))
{
}
void DriverManager::setErrorMessage(const QString &errorMessage)
{
if (m_errorMessage != errorMessage) {
m_errorMessage = errorMessage;
emit errorMessageChanged();
}
}
bool DriverManager::openDevice()
{
if (!m_device->open(FortCommon::deviceName())) {
setErrorMessage(m_device->getLastErrorMessage());
return false;
}
return true;
}
bool DriverManager::writeConf(const FirewallConf &conf)
{
ConfUtil confUtil;
QByteArray buf;
const int confSize = confUtil.write(conf, buf);
if (!confSize) {
setErrorMessage(confUtil.errorMessage());
return false;
}
return writeData(FortCommon::ioctlSetConf(),
buf, confSize);
}
bool DriverManager::writeConfFlags(const FirewallConf &conf)
{
ConfUtil confUtil;
QByteArray buf;
const int flagsSize = confUtil.writeFlags(conf, buf);
if (!flagsSize) {
setErrorMessage(confUtil.errorMessage());
return false;
}
return writeData(FortCommon::ioctlSetFlags(),
buf, flagsSize);
}
bool DriverManager::writeData(int code, QByteArray &buf, int size)
{
if (!m_device->ioctl(code, buf.data(), size)) {
setErrorMessage(m_device->getLastErrorMessage());
return false;
}
return true;
}

39
src/ui/drivermanager.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef DRIVERMANAGER_H
#define DRIVERMANAGER_H
#include <QObject>
class Device;
class FirewallConf;
class DriverManager : public QObject
{
Q_OBJECT
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
public:
explicit DriverManager(QObject *parent = nullptr);
QString errorMessage() const { return m_errorMessage; }
signals:
void errorMessageChanged();
public slots:
bool openDevice();
bool writeConf(const FirewallConf &conf);
bool writeConfFlags(const FirewallConf &conf);
private:
void setErrorMessage(const QString &errorMessage);
bool writeData(int code, QByteArray &buf, int size);
private:
Device *m_device;
QString m_errorMessage;
};
#endif // DRIVERMANAGER_H

View File

@ -2,6 +2,7 @@
#include <QApplication>
#include <QMenu>
#include <QMessageBox>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QSystemTrayIcon>
@ -10,6 +11,7 @@
#include "conf/addressgroup.h"
#include "conf/appgroup.h"
#include "conf/firewallconf.h"
#include "drivermanager.h"
#include "fortsettings.h"
FortManager::FortManager(QObject *parent) :
@ -18,7 +20,8 @@ FortManager::FortManager(QObject *parent) :
m_engine(new QQmlApplicationEngine(this)),
m_fortSettings(new FortSettings(qApp->arguments(), this)),
m_firewallConf(new FirewallConf(this)),
m_firewallConfToEdit(nullConf())
m_firewallConfToEdit(nullConf()),
m_driverManager(new DriverManager(this))
{
m_fortSettings->readConf(*m_firewallConf);
@ -65,6 +68,16 @@ void FortManager::setupEngine()
Q_ASSERT(m_appWindow);
}
bool FortManager::setupDriver()
{
if (!m_driverManager->openDevice()) {
showErrorBox(m_driverManager->errorMessage());
return false;
}
return true;
}
void FortManager::showTrayIcon()
{
m_trayIcon->show();
@ -88,6 +101,11 @@ void FortManager::closeWindow()
setFirewallConfToEdit(nullConf());
}
void FortManager::showErrorBox(const QString &text)
{
QMessageBox::critical(&m_window, QString(), text);
}
bool FortManager::saveConf()
{
return saveSettings(m_firewallConfToEdit);
@ -112,14 +130,22 @@ void FortManager::setFirewallConfToEdit(FirewallConf *conf)
bool FortManager::saveSettings(FirewallConf *newConf)
{
if (!m_fortSettings->writeConf(*newConf))
if (!m_fortSettings->writeConf(*newConf)) {
showErrorBox(m_fortSettings->errorMessage());
return false;
}
m_firewallConf->deleteLater();
m_firewallConf = newConf;
updateTrayMenu();
// Update driver
if (!m_driverManager->writeConf(*m_firewallConf)) {
showErrorBox(m_driverManager->errorMessage());
return false;
}
return true;
}
@ -140,6 +166,11 @@ void FortManager::saveTrayFlags()
}
m_fortSettings->writeConfFlags(*m_firewallConf);
// Update driver
if (!m_driverManager->writeConfFlags(*m_firewallConf)) {
showErrorBox(m_driverManager->errorMessage());
}
}
FirewallConf *FortManager::cloneConf(const FirewallConf &conf)

View File

@ -7,6 +7,7 @@
class QQmlApplicationEngine;
class QSystemTrayIcon;
class DriverManager;
class FortSettings;
class FirewallConf;
@ -29,11 +30,15 @@ signals:
void firewallConfToEditChanged();
public slots:
bool setupDriver();
void showTrayIcon();
void showWindow();
void closeWindow();
void showErrorBox(const QString &text);
bool saveConf();
bool applyConf();
@ -80,6 +85,8 @@ private:
QAction *m_appBlockAllAction;
QAction *m_appAllowAllAction;
QMenu *m_appGroupsMenu;
DriverManager *m_driverManager;
};
#endif // FORTMANAGER_H

View File

@ -83,6 +83,14 @@ void FortSettings::setupIni()
m_ini = new QSettings(iniPath, QSettings::IniFormat, this);
}
void FortSettings::setErrorMessage(const QString &errorMessage)
{
if (m_errorMessage != errorMessage) {
m_errorMessage = errorMessage;
emit errorMessageChanged();
}
}
QString FortSettings::confFilePath() const
{
return m_profilePath + QLatin1String("FortFirewall.conf");
@ -93,7 +101,7 @@ QString FortSettings::confBackupFilePath() const
return confFilePath() + QLatin1String(".backup");
}
bool FortSettings::readConf(FirewallConf &conf) const
bool FortSettings::readConf(FirewallConf &conf)
{
const QString filePath = confFilePath();
const QString backupFilePath = confBackupFilePath();
@ -102,15 +110,17 @@ bool FortSettings::readConf(FirewallConf &conf) const
|| tryToReadConf(conf, backupFilePath);
}
bool FortSettings::tryToReadConf(FirewallConf &conf, const QString &filePath) const
bool FortSettings::tryToReadConf(FirewallConf &conf, const QString &filePath)
{
const QByteArray data = FileUtil::readFileData(filePath);
QJsonParseError jsonParseError;
const QJsonDocument jsonDoc = QJsonDocument::fromJson(
data, &jsonParseError);
if (jsonParseError.error != QJsonParseError::NoError)
if (jsonParseError.error != QJsonParseError::NoError) {
setErrorMessage(jsonParseError.errorString());
return false;
}
conf.fromVariant(jsonDoc.toVariant());
@ -123,11 +133,20 @@ bool FortSettings::writeConf(const FirewallConf &conf)
const QString backupFilePath = confBackupFilePath();
if (FileUtil::fileExists(backupFilePath)
&& !FileUtil::renameFile(backupFilePath, filePath))
&& !FileUtil::renameFile(backupFilePath, filePath)) {
setErrorMessage(tr("Can't rename old backup conf. file"));
return false;
}
if (!tryToWriteConf(conf, backupFilePath))
return false;
return tryToWriteConf(conf, backupFilePath)
&& FileUtil::renameFile(backupFilePath, filePath);
if (!FileUtil::renameFile(backupFilePath, filePath)) {
setErrorMessage(tr("Can't rename backup conf. file"));
return false;
}
return true;
}
bool FortSettings::tryToWriteConf(const FirewallConf &conf, const QString &filePath)
@ -137,8 +156,10 @@ bool FortSettings::tryToWriteConf(const FirewallConf &conf, const QString &fileP
const QByteArray data = jsonDoc.toJson(QJsonDocument::Indented);
if (!FileUtil::writeFileData(filePath, data))
if (!FileUtil::writeFileData(filePath, data)) {
setErrorMessage(tr("Can't write conf. file"));
return false;
}
return writeConfFlags(conf);
}

View File

@ -13,6 +13,7 @@ class FortSettings : public QObject
Q_PROPERTY(QString language READ language WRITE setLanguage NOTIFY iniChanged)
Q_PROPERTY(QString updatesUrl READ updatesUrl WRITE setUpdatesUrl NOTIFY iniChanged)
Q_PROPERTY(bool startWithWindows READ startWithWindows WRITE setStartWithWindows NOTIFY startWithWindowsChanged)
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
public:
explicit FortSettings(const QStringList &args,
@ -32,15 +33,18 @@ public:
bool startWithWindows() const;
void setStartWithWindows(bool start);
QString errorMessage() const { return m_errorMessage; }
signals:
void iniChanged();
void startWithWindowsChanged();
void errorMessageChanged();
public slots:
QString confFilePath() const;
QString confBackupFilePath() const;
bool readConf(FirewallConf &conf) const;
bool readConf(FirewallConf &conf);
bool writeConf(const FirewallConf &conf);
bool readConfFlags(FirewallConf &conf) const;
@ -50,7 +54,9 @@ private:
void processArguments(const QStringList &args);
void setupIni();
bool tryToReadConf(FirewallConf &conf, const QString &filePath) const;
void setErrorMessage(const QString &errorMessage);
bool tryToReadConf(FirewallConf &conf, const QString &filePath);
bool tryToWriteConf(const FirewallConf &conf, const QString &filePath);
bool iniBool(const QString &key, bool defaultValue = false) const;
@ -72,6 +78,8 @@ private:
QString m_profilePath;
QString m_errorMessage;
QSettings *m_ini;
};

View File

@ -22,6 +22,11 @@ int main(int argc, char *argv[])
return FortCommon::provRegister(true);
}
// Open the driver device
if (!fortManager.setupDriver()) {
return 1;
}
fortManager.showTrayIcon();
return app.exec();