From 7cf9847a21c62839686fd1660faeda7107fbc496 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Mon, 11 Sep 2017 08:54:19 +0500 Subject: [PATCH] UI: Exit when can't open the driver device. --- src/ui/driver/drivermanager.cpp | 5 +++++ src/ui/driver/drivermanager.h | 2 ++ src/ui/fortmanager.cpp | 18 +++++++++++------- src/ui/fortmanager.h | 3 ++- src/ui/fortsettings.cpp | 4 ++++ src/ui/main.cpp | 11 +++++++++-- src/ui/util/device.cpp | 5 +++++ src/ui/util/device.h | 2 ++ 8 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/ui/driver/drivermanager.cpp b/src/ui/driver/drivermanager.cpp index 0eebb630..e1276649 100644 --- a/src/ui/driver/drivermanager.cpp +++ b/src/ui/driver/drivermanager.cpp @@ -48,6 +48,11 @@ void DriverManager::setupWorker() m_workerThread->start(); } +bool DriverManager::isDeviceOpened() const +{ + return m_device->isOpened(); +} + bool DriverManager::openDevice() { if (!m_device->open(FortCommon::deviceName())) { diff --git a/src/ui/driver/drivermanager.h b/src/ui/driver/drivermanager.h index 1365042c..91692b2a 100644 --- a/src/ui/driver/drivermanager.h +++ b/src/ui/driver/drivermanager.h @@ -21,6 +21,8 @@ public: QString errorMessage() const { return m_errorMessage; } + bool isDeviceOpened() const; + signals: void errorMessageChanged(); diff --git a/src/ui/fortmanager.cpp b/src/ui/fortmanager.cpp index 24e63499..9a27f943 100644 --- a/src/ui/fortmanager.cpp +++ b/src/ui/fortmanager.cpp @@ -22,12 +22,13 @@ #include "util/netutil.h" #include "util/osutil.h" -FortManager::FortManager(QObject *parent) : +FortManager::FortManager(FortSettings *fortSettings, + QObject *parent) : QObject(parent), m_trayIcon(new QSystemTrayIcon(this)), m_engine(nullptr), m_appWindow(nullptr), - m_fortSettings(new FortSettings(qApp->arguments(), this)), + m_fortSettings(fortSettings), m_firewallConf(new FirewallConf(this)), m_firewallConfToEdit(nullConf()), m_driverManager(new DriverManager(this)) @@ -69,7 +70,7 @@ void FortManager::registerQmlTypes() bool FortManager::setupDriver() { if (!m_driverManager->openDevice()) { - showErrorBox(m_driverManager->errorMessage()); + showErrorBox("Setup Driver: " + m_driverManager->errorMessage()); return false; } @@ -197,7 +198,7 @@ void FortManager::setFirewallConfToEdit(FirewallConf *conf) bool FortManager::loadSettings(FirewallConf *conf) { if (!m_fortSettings->readConf(*conf)) { - showErrorBox(m_fortSettings->errorMessage()); + showErrorBox("Load Settings: " + m_fortSettings->errorMessage()); return false; } @@ -208,7 +209,7 @@ bool FortManager::saveSettings(FirewallConf *newConf, bool onlyFlags) { if (!(onlyFlags ? m_fortSettings->writeConfFlags(*newConf) : m_fortSettings->writeConf(*newConf))) { - showErrorBox(m_fortSettings->errorMessage()); + showErrorBox("Save Settings: " + m_fortSettings->errorMessage()); return false; } @@ -223,9 +224,12 @@ bool FortManager::saveSettings(FirewallConf *newConf, bool onlyFlags) bool FortManager::updateDriverConf(FirewallConf *conf) { + if (!m_driverManager->isDeviceOpened()) + return false; + // Update driver if (!m_driverManager->writeConf(*conf)) { - showErrorBox(m_driverManager->errorMessage()); + showErrorBox("Update Driver Conf: " + m_driverManager->errorMessage()); return false; } @@ -236,7 +240,7 @@ bool FortManager::updateDriverConfFlags(FirewallConf *conf) { // Update driver if (!m_driverManager->writeConfFlags(*conf)) { - showErrorBox(m_driverManager->errorMessage()); + showErrorBox("Update Driver Conf Flags: " + m_driverManager->errorMessage()); return false; } diff --git a/src/ui/fortmanager.h b/src/ui/fortmanager.h index a52e007d..5df2c0d2 100644 --- a/src/ui/fortmanager.h +++ b/src/ui/fortmanager.h @@ -21,7 +21,8 @@ class FortManager : public QObject Q_PROPERTY(DriverManager *driverManager READ driverManager CONSTANT) public: - explicit FortManager(QObject *parent = nullptr); + explicit FortManager(FortSettings *fortSettings, + QObject *parent = nullptr); FortSettings *fortSettings() const { return m_fortSettings; } diff --git a/src/ui/fortsettings.cpp b/src/ui/fortsettings.cpp index 7375b931..fb902067 100644 --- a/src/ui/fortsettings.cpp +++ b/src/ui/fortsettings.cpp @@ -97,6 +97,10 @@ bool FortSettings::readConf(FirewallConf &conf) const QString filePath = confFilePath(); const QString backupFilePath = confBackupFilePath(); + if (!(FileUtil::fileExists(filePath) + || FileUtil::fileExists(backupFilePath))) + return true; + return tryToReadConf(conf, filePath) || tryToReadConf(conf, backupFilePath); } diff --git a/src/ui/main.cpp b/src/ui/main.cpp index bc23a207..1ab06477 100644 --- a/src/ui/main.cpp +++ b/src/ui/main.cpp @@ -1,6 +1,7 @@ #include #include "../common/version.h" +#include "driver/drivermanager.h" #include "fortcommon.h" #include "fortmanager.h" #include "fortsettings.h" @@ -14,14 +15,20 @@ int main(int argc, char *argv[]) app.setApplicationVersion(APP_VERSION_STR); app.setApplicationDisplayName(APP_NAME " v" APP_VERSION_STR); - FortManager fortManager; + FortSettings fortSettings(qApp->arguments()); // Register booted provider and exit - if (fortManager.fortSettings()->boot()) { + if (fortSettings.boot()) { FortCommon::provUnregister(); return FortCommon::provRegister(true); } + FortManager fortManager(&fortSettings); + + // Error: Cannot open the driver device + if (!fortManager.driverManager()->isDeviceOpened()) + return 1; + fortManager.showTrayIcon(); return app.exec(); diff --git a/src/ui/util/device.cpp b/src/ui/util/device.cpp index 3ca0953f..ad953a17 100644 --- a/src/ui/util/device.cpp +++ b/src/ui/util/device.cpp @@ -15,6 +15,11 @@ Device::~Device() close(); } +bool Device::isOpened() const +{ + return (m_handle != INVALID_HANDLE_VALUE); +} + bool Device::open(const QString &filePath) { const DWORD access = GENERIC_READ | GENERIC_WRITE; diff --git a/src/ui/util/device.h b/src/ui/util/device.h index 0c6cb538..6b978c91 100644 --- a/src/ui/util/device.h +++ b/src/ui/util/device.h @@ -11,6 +11,8 @@ public: explicit Device(QObject *parent = nullptr); virtual ~Device(); + bool isOpened() const; + static QString lastErrorMessage(); signals: