From 256f10712e605c4883a265df9953d0ba39701f21 Mon Sep 17 00:00:00 2001 From: markuspg Date: Sat, 27 Oct 2018 20:55:42 +0200 Subject: [PATCH] Rename state_t to EState and move it into Client --- Labcontrol.pro | 1 - src/Lib/client.cpp | 34 +++++++++++++++--------------- src/Lib/client.h | 29 +++++++++++++++++++++----- src/Lib/clientpinger.cpp | 10 ++++----- src/Lib/clientpinger.h | 6 +++--- src/Lib/global.h | 44 --------------------------------------- src/Lib/lablib.h | 1 - src/Lib/receiptsprinter.h | 2 -- src/Lib/ztree.h | 2 -- src/main.cpp | 2 +- src/mainwindow.cpp | 17 ++++++++------- 11 files changed, 59 insertions(+), 89 deletions(-) delete mode 100644 src/Lib/global.h diff --git a/Labcontrol.pro b/Labcontrol.pro index 0fb2715..49d2c5f 100644 --- a/Labcontrol.pro +++ b/Labcontrol.pro @@ -26,7 +26,6 @@ HEADERS += src/localzleafstarter.h \ src/Lib/client.h \ src/Lib/clienthelpnotificationserver.h \ src/Lib/clientpinger.h \ - src/Lib/global.h \ src/Lib/lablib.h \ src/Lib/netstatagent.h \ src/Lib/receipts_handler.h \ diff --git a/src/Lib/client.cpp b/src/Lib/client.cpp index 814b8b0..dadfcf2 100644 --- a/src/Lib/client.cpp +++ b/src/Lib/client.cpp @@ -73,7 +73,7 @@ void lc::Client::BeamFile(const QString &argFileToBeam, const QString *const argPublickeyPathUser, const QString *const argUserNameOnClients) { - if (state < state_t::RESPONDING) { + if (state < EState::RESPONDING) { return; } @@ -104,17 +104,17 @@ void lc::Client::Boot() pingTimer->start(3000); protectedCycles = 7; - GotStatusChanged(state_t::BOOTING); + GotStatusChanged(EState::BOOTING); } -void lc::Client::GotStatusChanged(state_t argState) +void lc::Client::GotStatusChanged(EState argState) { - if ((protectedCycles > 0) && (state == state_t::BOOTING) - && (argState != state_t::RESPONDING)) { + if ((protectedCycles > 0) && (state == EState::BOOTING) + && (argState != EState::RESPONDING)) { return; } - if ((protectedCycles > 0) && (state == state_t::SHUTTING_DOWN) - && argState != state_t::NOT_RESPONDING) { + if ((protectedCycles > 0) && (state == EState::SHUTTING_DOWN) + && argState != EState::NOT_RESPONDING) { return; } state = argState; @@ -142,7 +142,7 @@ void lc::Client::KillZLeaf() void lc::Client::OpenFilesystem(const QString *const argUserToBeUsed) { - if (state < state_t::RESPONDING) { + if (state < EState::RESPONDING) { return; } const QStringList arguments{QString{ "sftp://" + *argUserToBeUsed + "@" + ip }}; @@ -157,7 +157,7 @@ void lc::Client::OpenTerminal(const QString &argCommand, const bool argOpenAsRoot) { if (!settings->termEmulCmd.isEmpty()) { - if (state < state_t::RESPONDING) { + if (state < EState::RESPONDING) { return; } @@ -196,11 +196,11 @@ void lc::Client::SetStateToZLEAF_RUNNING(const QString &argClientIP) if ( argClientIP != ip ) { return; } - if (state != state_t::ZLEAF_RUNNING) { + if (state != EState::ZLEAF_RUNNING) { pingTimer->stop(); // Inform the ClientPinger instance, that zLeaf is now running pinger->setStateToZLEAF_RUNNING(); - this->GotStatusChanged(state_t::ZLEAF_RUNNING); + this->GotStatusChanged(EState::ZLEAF_RUNNING); qDebug() << "Client" << name << "got 'ZLEAF_RUNNING' signal."; } } @@ -231,8 +231,8 @@ void lc::Client::ShowDesktopFullControl() void lc::Client::Shutdown() { - if (state == state_t::NOT_RESPONDING || state == state_t::BOOTING - || state == state_t::SHUTTING_DOWN) { + if (state == EState::NOT_RESPONDING || state == EState::BOOTING + || state == EState::SHUTTING_DOWN) { return; } const QStringList arguments{"-i", settings->pkeyPathUser, @@ -251,12 +251,12 @@ void lc::Client::Shutdown() pingTimer->start(3000); protectedCycles = 3; - GotStatusChanged(state_t::SHUTTING_DOWN); + GotStatusChanged(EState::SHUTTING_DOWN); } void lc::Client::StartZLeaf(const QString *argFakeName, QString cmd) { - if (state < state_t::RESPONDING + if (state < EState::RESPONDING || zLeafVersion.isEmpty() || GetSessionPort() < 7000) { return; @@ -264,7 +264,7 @@ void lc::Client::StartZLeaf(const QString *argFakeName, QString cmd) // Create a QMessageBox for user interaction if there is already a zLeaf running std::unique_ptr messageBoxRunningZLeafFound; - if (state == state_t::ZLEAF_RUNNING) { + if (state == EState::ZLEAF_RUNNING) { messageBoxRunningZLeafFound.reset(new QMessageBox{QMessageBox::Warning, "Running zLeaf found", QString{"There is already a zLeaf running on " + name + "."}, QMessageBox::No | QMessageBox::Yes}); @@ -277,7 +277,7 @@ void lc::Client::StartZLeaf(const QString *argFakeName, QString cmd) if ((messageBoxRunningZLeafFound.get() != nullptr && messageBoxRunningZLeafFound->clickedButton() == messageBoxRunningZLeafFound->button(QMessageBox::Yes)) - || state != state_t::ZLEAF_RUNNING) { + || state != EState::ZLEAF_RUNNING) { QStringList arguments; if (argFakeName == nullptr) { arguments << "-i" << settings->pkeyPathUser diff --git a/src/Lib/client.h b/src/Lib/client.h index 2cc9228..635fb5a 100644 --- a/src/Lib/client.h +++ b/src/Lib/client.h @@ -20,8 +20,7 @@ #ifndef CLIENT_H #define CLIENT_H -#include "global.h" - +#include #include class QTimer; @@ -39,6 +38,26 @@ class Client : public QObject { Q_OBJECT +public: + enum class EState : unsigned short { + //! The client's state is not yet defined (should only occur directly after client creation) + UNINITIALIZED = 1 << 0, + + //! The client is booting but not yet responding + BOOTING = 1 << 1, + //! An error occurred determining the client's state + ERROR = 1 << 2, + //! The client is not responding to pings + NOT_RESPONDING = 1 << 3, + //! The client is shutting down but not yet stopped responding + SHUTTING_DOWN = 1 << 4, + //! The client is responding to pings + RESPONDING = 1 << 5, + //! The client is running a zLeaf + ZLEAF_RUNNING = 1 << 6, + }; + Q_ENUM(EState) + public slots: //! Sets the STATE of the client to 'ZLEAF_RUNNING' void SetStateToZLEAF_RUNNING(const QString &argClientIP); @@ -85,7 +104,7 @@ public: /*! @return The current state of the client */ - state_t GetClientState() const + EState GetClientState() const { return state; } @@ -151,14 +170,14 @@ private: ClientPinger *pinger = nullptr; QThread pingerThread; const Settings *const settings = nullptr; - state_t state = state_t::UNINITIALIZED; + EState state = EState::UNINITIALIZED; //! QTimer used to trigger pings by pinger's ClientPinger instance QTimer *pingTimer = nullptr; int sessionPort = 0; QString zLeafVersion; private slots: - void GotStatusChanged(const state_t argState); + void GotStatusChanged(const EState argState); void RequestAPing(); signals: diff --git a/src/Lib/clientpinger.cpp b/src/Lib/clientpinger.cpp index 8f4a9d6..3120a21 100644 --- a/src/Lib/clientpinger.cpp +++ b/src/Lib/clientpinger.cpp @@ -35,18 +35,18 @@ lc::ClientPinger::ClientPinger(const QString &argIP, void lc::ClientPinger::doPing() { // Initialize the new state to be queried - state_t newState = state_t::UNINITIALIZED; + Client::EState newState = Client::EState::UNINITIALIZED; // Query the current state of the client pingProcess->start(pingCommand, pingArguments); if (!pingProcess->waitForFinished(2500) || pingProcess->exitStatus() != QProcess::NormalExit) - newState = state_t::ERROR; + newState = Client::EState::ERROR; else { if (pingProcess->exitCode() == 0) { - newState = state_t::RESPONDING; + newState = Client::EState::RESPONDING; } else { - newState = state_t::NOT_RESPONDING; + newState = Client::EState::NOT_RESPONDING; } } @@ -58,5 +58,5 @@ void lc::ClientPinger::doPing() void lc::ClientPinger::setStateToZLEAF_RUNNING() { - state = state_t::ZLEAF_RUNNING; + state = Client::EState::ZLEAF_RUNNING; } diff --git a/src/Lib/clientpinger.h b/src/Lib/clientpinger.h index 30b14eb..d0bc7b1 100644 --- a/src/Lib/clientpinger.h +++ b/src/Lib/clientpinger.h @@ -20,7 +20,7 @@ #ifndef CLIENTPINGER_H #define CLIENTPINGER_H -#include "global.h" +#include "client.h" #include #include @@ -63,12 +63,12 @@ private: //! The 'ping' process which will be executed on every call of 'do_ping()' std::unique_ptr pingProcess; //! Stores the current state of the client - state_t state = state_t::UNINITIALIZED; + Client::EState state = Client::EState::UNINITIALIZED; signals: //! This signal was just implemented for testing purposes //! This signal is emitted if the ping finished and the state of the client changed - void PingFinished(state_t state); + void PingFinished(Client::EState state); }; } // namespace lc diff --git a/src/Lib/global.h b/src/Lib/global.h deleted file mode 100644 index 7f834bd..0000000 --- a/src/Lib/global.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2014-2018 Markus Prasser, Tobias Weiss - * - * This file is part of Labcontrol. - * - * Labcontrol is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Labcontrol is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Labcontrol. If not, see . - */ - -#ifndef GLOBAL_H -#define GLOBAL_H - -#include - -//! Opens a terminal for the client -enum class state_t : unsigned short int { - //! The client is booting but not yet responding - BOOTING = 1 << 0, - //! An error occurred determining the client's state - ERROR = 1 << 1, - //! The client is not responding to pings - NOT_RESPONDING = 1 << 2, - //! The client is shutting down but not yet stopped responding - SHUTTING_DOWN = 1 << 3, - //! The client's state is not yet defined (should only occur directly after client creation) - UNINITIALIZED = 1 << 4, - //! The client is responding to pings - RESPONDING = 1 << 5, - //! The client is running a zLeaf - ZLEAF_RUNNING = 1 << 6, -}; -Q_DECLARE_METATYPE(state_t) - -#endif // GLOBAL_H diff --git a/src/Lib/lablib.h b/src/Lib/lablib.h index 1f6efe2..82ee60b 100644 --- a/src/Lib/lablib.h +++ b/src/Lib/lablib.h @@ -38,7 +38,6 @@ #include "client.h" #include "clienthelpnotificationserver.h" -#include "global.h" #include "netstatagent.h" #include "session.h" #include "sessionsmodel.h" diff --git a/src/Lib/receiptsprinter.h b/src/Lib/receiptsprinter.h index 5433d08..5c9a526 100644 --- a/src/Lib/receiptsprinter.h +++ b/src/Lib/receiptsprinter.h @@ -24,8 +24,6 @@ #include #include -#include "global.h" - namespace lc { class Settings; diff --git a/src/Lib/ztree.h b/src/Lib/ztree.h index 9c00c56..056c326 100644 --- a/src/Lib/ztree.h +++ b/src/Lib/ztree.h @@ -20,8 +20,6 @@ #ifndef ZTREE_H #define ZTREE_H -#include "global.h" - #include namespace lc { diff --git a/src/main.cpp b/src/main.cpp index 9be497f..dffffff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,7 +25,7 @@ int main(int argc, char *argv[]) { - qRegisterMetaType(); + qRegisterMetaType(); QApplication a{argc, argv}; lc::Settings settings{QSettings{"Labcontrol", "Labcontrol"}}; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f51fde3..d594bfa 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -17,6 +17,7 @@ * along with Labcontrol. If not, see . */ +#include #include #include #include @@ -471,31 +472,31 @@ void lc::MainWindow::StartReceiptsHandler(QString argzTreeDataTargetPath, void lc::MainWindow::UpdateClientsTableView() { for ( auto s : *valid_items ) { - state_t state = static_cast< Client * >( s->data( + lc::Client::EState state = static_cast< Client * >( s->data( Qt::UserRole ).value() )->GetClientState(); switch ( state ) { - case state_t::RESPONDING: + case Client::EState::RESPONDING: s->setBackground( QBrush( QColor( 128, 255, 128, 255 ) ) ); s->setIcon( icons[ ( int )icons_t::ON ] ); break; - case state_t::NOT_RESPONDING: + case lc::Client::EState::NOT_RESPONDING: s->setBackground( QBrush( QColor( 255, 255, 128, 255 ) ) ); s->setIcon( icons[ ( int )icons_t::OFF ] ); break; - case state_t::BOOTING: + case Client::EState::BOOTING: s->setBackground( QBrush( QColor( 128, 128, 255, 255 ) ) ); s->setIcon( icons[ ( int )icons_t::BOOT ] ); break; - case state_t::SHUTTING_DOWN: + case Client::EState::SHUTTING_DOWN: s->setBackground( QBrush( QColor( 128, 128, 255, 255 ) ) ); s->setIcon( icons[ ( int )icons_t::DOWN ] ); break; - case state_t::ZLEAF_RUNNING: + case Client::EState::ZLEAF_RUNNING: s->setBackground( QBrush( QColor( 0, 255, 0, 255 ) ) ); s->setIcon( icons[ ( int )icons_t::ZLEAF ] ); break; - case state_t::UNINITIALIZED: - case state_t::ERROR: + case Client::EState::UNINITIALIZED: + case Client::EState::ERROR: s->setBackground( QBrush( QColor( 255, 128, 128, 255 ) ) ); break; }