diff --git a/src/ui/control/control.h b/src/ui/control/control.h index 8ae8ebfb..03610451 100644 --- a/src/ui/control/control.h +++ b/src/ui/control/control.h @@ -13,7 +13,7 @@ enum Command : qint8 { Rpc_AppInfoManager_lookupAppInfo, Rpc_AppInfoManager_checkLookupFinished, Rpc_ConfManager_, - Rpc_DriverManager_, + Rpc_DriverManager_updateState, Rpc_QuotaManager_alert, Rpc_StatManager_, Rpc_TaskManager_, diff --git a/src/ui/driver/drivermanager.cpp b/src/ui/driver/drivermanager.cpp index e75bf549..0f0e1ec1 100644 --- a/src/ui/driver/drivermanager.cpp +++ b/src/ui/driver/drivermanager.cpp @@ -25,52 +25,61 @@ DriverManager::~DriverManager() QString DriverManager::errorMessage() const { - return (m_errorCode == 0) ? QString() : OsUtil::errorMessage(m_errorCode); + return (errorCode() == 0) ? QString() : OsUtil::errorMessage(errorCode()); } -void DriverManager::updateError(bool success) +void DriverManager::setErrorCode(quint32 v) { - m_errorCode = success ? 0 : OsUtil::lastErrorCode(); - emit errorMessageChanged(); + if (m_errorCode != v) { + m_errorCode = v; + emit errorCodeChanged(); + } +} + +void DriverManager::updateErrorCode(bool success) +{ + setErrorCode(success ? 0 : OsUtil::lastErrorCode()); } bool DriverManager::isDeviceError() const { - return m_errorCode != 0 && m_errorCode != DriverCommon::userErrorCode(); + return errorCode() != 0 && errorCode() != DriverCommon::userErrorCode(); } void DriverManager::setupWorker() { - QThreadPool::globalInstance()->start(m_driverWorker); + QThreadPool::globalInstance()->start(driverWorker()); } void DriverManager::abortWorker() { - m_driverWorker->abort(); + driverWorker()->abort(); } bool DriverManager::isDeviceOpened() const { - return m_device->isOpened(); + return device()->isOpened(); } bool DriverManager::openDevice() { - const bool res = m_device->open(DriverCommon::deviceName()); - - updateError(res); + const bool res = device()->open(DriverCommon::deviceName()); emit isDeviceOpenedChanged(); + updateErrorCode(res); + return res; } bool DriverManager::closeDevice() { - const bool res = m_device->close(); + const bool res = device()->close(); emit isDeviceOpenedChanged(); + updateErrorCode(true); + return res; } @@ -101,11 +110,11 @@ bool DriverManager::writeData(quint32 code, QByteArray &buf, int size) if (!isDeviceOpened()) return true; - m_driverWorker->cancelAsyncIo(); + driverWorker()->cancelAsyncIo(); - const bool res = m_device->ioctl(code, buf.data(), size); + const bool res = device()->ioctl(code, buf.data(), size); - updateError(res); + updateErrorCode(res); return res; } diff --git a/src/ui/driver/drivermanager.h b/src/ui/driver/drivermanager.h index 4187b6e8..f031a09a 100644 --- a/src/ui/driver/drivermanager.h +++ b/src/ui/driver/drivermanager.h @@ -20,8 +20,10 @@ public: ~DriverManager() override; CLASS_DELETE_COPY_MOVE(DriverManager) + Device *device() const { return m_device; } DriverWorker *driverWorker() const { return m_driverWorker; } + quint32 errorCode() const { return m_errorCode; } QString errorMessage() const; bool isDeviceError() const; @@ -31,7 +33,7 @@ public: virtual void uninstallDriver(); signals: - void errorMessageChanged(); + void errorCodeChanged(); void isDeviceOpenedChanged(); public slots: @@ -44,8 +46,11 @@ public slots: bool writeApp(QByteArray &buf, int size, bool remove = false); bool writeZones(QByteArray &buf, int size, bool onlyFlags = false); +protected: + void setErrorCode(quint32 v); + private: - void updateError(bool success); + void updateErrorCode(bool success); void setupWorker(); void abortWorker(); diff --git a/src/ui/form/opt/pages/optionspage.cpp b/src/ui/form/opt/pages/optionspage.cpp index 64070e4d..d32d537b 100644 --- a/src/ui/form/opt/pages/optionspage.cpp +++ b/src/ui/form/opt/pages/optionspage.cpp @@ -495,7 +495,7 @@ void OptionsPage::setupDriverIcon() refreshDriverIcon(); connect(driverManager(), &DriverManager::isDeviceOpenedChanged, this, refreshDriverIcon); - connect(driverManager(), &DriverManager::errorMessageChanged, this, + connect(driverManager(), &DriverManager::errorCodeChanged, this, &OptionsPage::retranslateDriverMessage); } diff --git a/src/ui/rpc/drivermanagerrpc.cpp b/src/ui/rpc/drivermanagerrpc.cpp index ba29bf8b..87e1271d 100644 --- a/src/ui/rpc/drivermanagerrpc.cpp +++ b/src/ui/rpc/drivermanagerrpc.cpp @@ -8,9 +8,12 @@ DriverManagerRpc::DriverManagerRpc(FortManager *fortManager, QObject *parent) : { } -bool DriverManagerRpc::isDeviceOpened() const +void DriverManagerRpc::setIsDeviceOpened(bool v) { - return m_isDeviceOpened; + if (m_isDeviceOpened != v) { + m_isDeviceOpened = v; + emit isDeviceOpenedChanged(); + } } RpcManager *DriverManagerRpc::rpcManager() const @@ -22,6 +25,12 @@ void DriverManagerRpc::reinstallDriver() { } void DriverManagerRpc::uninstallDriver() { } +void DriverManagerRpc::updateState(quint32 errorCode, bool isDeviceOpened) +{ + setIsDeviceOpened(isDeviceOpened); + setErrorCode(errorCode); +} + bool DriverManagerRpc::openDevice() { return false; diff --git a/src/ui/rpc/drivermanagerrpc.h b/src/ui/rpc/drivermanagerrpc.h index 4d3aa049..0680bbcc 100644 --- a/src/ui/rpc/drivermanagerrpc.h +++ b/src/ui/rpc/drivermanagerrpc.h @@ -13,7 +13,8 @@ class DriverManagerRpc : public DriverManager public: explicit DriverManagerRpc(FortManager *fortManager, QObject *parent = nullptr); - bool isDeviceOpened() const override; + bool isDeviceOpened() const override { return m_isDeviceOpened; } + void setIsDeviceOpened(bool v); FortManager *fortManager() const { return m_fortManager; } RpcManager *rpcManager() const; @@ -21,6 +22,8 @@ public: void reinstallDriver() override; void uninstallDriver() override; + void updateState(quint32 errorCode, bool isDeviceOpened); + public slots: bool openDevice() override; bool closeDevice() override; diff --git a/src/ui/rpc/rpcmanager.cpp b/src/ui/rpc/rpcmanager.cpp index 1b77f638..6378f589 100644 --- a/src/ui/rpc/rpcmanager.cpp +++ b/src/ui/rpc/rpcmanager.cpp @@ -68,6 +68,7 @@ void RpcManager::initialize() void RpcManager::setupServerSignals() { setupAppInfoManagerSignals(); + setupDriverManagerSignals(); setupQuotaManagerSignals(); } @@ -79,6 +80,16 @@ void RpcManager::setupAppInfoManagerSignals() }); } +void RpcManager::setupDriverManagerSignals() +{ + const auto updateClientStates = [&] { + invokeOnClients(Control::Rpc_DriverManager_updateState, + { driverManager()->errorCode(), driverManager()->isDeviceOpened() }); + }; + connect(driverManager(), &DriverManager::errorCodeChanged, this, updateClientStates); + connect(driverManager(), &DriverManager::isDeviceOpenedChanged, this, updateClientStates); +} + void RpcManager::setupQuotaManagerSignals() { connect(quotaManager(), &QuotaManager::alert, this, [&](qint8 alertType) { @@ -124,9 +135,13 @@ bool RpcManager::processCommandRpc( case Control::Rpc_ConfManager_: confManager(); return true; - case Control::Rpc_DriverManager_: - driverManager(); - return true; + case Control::Rpc_DriverManager_updateState: + if (settings()->isServiceClient()) { + auto dm = qobject_cast(driverManager()); + dm->updateState(args.value(0).toUInt(), args.value(1).toBool()); + return true; + } + break; case Control::Rpc_QuotaManager_alert: emit quotaManager()->alert(args.value(0).toInt()); return true; @@ -138,6 +153,7 @@ bool RpcManager::processCommandRpc( return true; default: errorMessage = "Unknown command"; - return false; } + + return false; } diff --git a/src/ui/rpc/rpcmanager.h b/src/ui/rpc/rpcmanager.h index a803d1ce..1707124c 100644 --- a/src/ui/rpc/rpcmanager.h +++ b/src/ui/rpc/rpcmanager.h @@ -45,6 +45,7 @@ public: private: void setupServerSignals(); void setupAppInfoManagerSignals(); + void setupDriverManagerSignals(); void setupQuotaManagerSignals(); void setupClient();