From f0019b00cee4847c4dbed01e3cf777bc80d28bcb Mon Sep 17 00:00:00 2001 From: markuspg Date: Tue, 15 Sep 2020 14:10:26 +0200 Subject: [PATCH] Revise ZTree class * improve documentation * move QProcess to heap (for better clean-up through deleteLater) * reduce and reorder includes * utilize initializer list and new signal-slot syntax --- src/Lib/session.cpp | 2 ++ src/Lib/ztree.cpp | 67 ++++++++++++++++++++++++++++++--------------- src/Lib/ztree.h | 20 ++++++++------ 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/Lib/session.cpp b/src/Lib/session.cpp index 8e7d82d..feba764 100644 --- a/src/Lib/session.cpp +++ b/src/Lib/session.cpp @@ -22,6 +22,8 @@ #include "session.h" #include "settings.h" +#include + extern std::unique_ptr settings; lc::Session::Session(QVector &&argAssocClients, diff --git a/src/Lib/ztree.cpp b/src/Lib/ztree.cpp index 41b0317..c95ac5f 100644 --- a/src/Lib/ztree.cpp +++ b/src/Lib/ztree.cpp @@ -17,36 +17,59 @@ * along with Labcontrol. If not, see . */ -#include +#include "ztree.h" +#include "settings.h" -#include +#include -#include "settings.h" -#include "ztree.h" +#include extern std::unique_ptr settings; +/*! + * Setup and start a new z-Tree instance with the given parameters + * + * \param[in] argZTreeDataTargetPath The path where the new z-Tree instance + * shall write its output to + * \param[in] argZTreePort The port on which the new z-Tree instance shall + * listen + * \param[in] argZTreeVersion The version of z-Tree which shall be + * started + * \param[in] argParent The instance's parent QObject + */ lc::ZTree::ZTree(const QString &argZTreeDataTargetPath, const int &argZTreePort, - const QString &argZTreeVersionPath, QObject *argParent) - : QObject{argParent} { - QStringList arguments{QStringList{} - << "-c" - << "0" << settings->wineCmd - << QString{settings->zTreeInstDir + "/zTree_" + - argZTreeVersionPath + "/ztree.exe"} - << "/datadir" << QString{"Z:/" + argZTreeDataTargetPath} - << "/privdir" << QString{"Z:/" + argZTreeDataTargetPath} - << "/gsfdir" << QString{"Z:/" + argZTreeDataTargetPath} - << "/tempdir" << QString{"Z:/" + argZTreeDataTargetPath} - << "/leafdir" << QString{"Z:/" + argZTreeDataTargetPath} - << "/channel" << QString::number(argZTreePort - 7000)}; + const QString &argZTreeVersion, QObject *const argParent) + : QObject{argParent}, zTreeInstance{new QProcess{this}} { + const QStringList arguments{"-c", + "0", + settings->wineCmd, + QString{settings->zTreeInstDir + "/zTree_" + + argZTreeVersion + "/ztree.exe"}, + "/datadir", + QString{"Z:/" + argZTreeDataTargetPath}, + "/privdir", + QString{"Z:/" + argZTreeDataTargetPath}, + "/gsfdir", + QString{"Z:/" + argZTreeDataTargetPath}, + "/tempdir", + QString{"Z:/" + argZTreeDataTargetPath}, + "/leafdir", + QString{"Z:/" + argZTreeDataTargetPath}, + "/channel", + QString::number(argZTreePort - 7000)}; QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); - zTreeInstance.setProcessEnvironment(env); - zTreeInstance.setWorkingDirectory(QDir::homePath()); - zTreeInstance.start(settings->tasksetCmd, arguments, QIODevice::NotOpen); - connect(&zTreeInstance, SIGNAL(finished(int)), this, - SIGNAL(ZTreeClosed(int))); + zTreeInstance->setProcessEnvironment(env); + zTreeInstance->setWorkingDirectory(QDir::homePath()); + zTreeInstance->start(settings->tasksetCmd, arguments, QIODevice::NotOpen); + connect(zTreeInstance, + static_cast(&QProcess::finished), this, + &ZTree::ZTreeClosed); qDebug() << settings->tasksetCmd << arguments.join(" "); } + +/*! + * \brief Destroy the z-Tree instance (closes the running z-Tree instance) + */ +lc::ZTree::~ZTree() { zTreeInstance->deleteLater(); } diff --git a/src/Lib/ztree.h b/src/Lib/ztree.h index 39fa7be..02d4cf8 100644 --- a/src/Lib/ztree.h +++ b/src/Lib/ztree.h @@ -20,29 +20,31 @@ #ifndef ZTREE_H #define ZTREE_H -#include -#include -#include +#include + +class QProcess; namespace lc { -//! A class to contain running zTree instances. /*! - This class is element of every session and is used to handle all zTree related - data. -*/ + * A class to contain running z-Tree instances. + * + * This class is element of every Session instance and is used to handle all + * z-Tree related data as well as the execution of z-Tree itself. + */ class ZTree : public QObject { Q_OBJECT public: ZTree(const QString &argZTreeDataTargetPath, const int &argZTreePort, - const QString &argZTreeVersionPath, QObject *argParent = nullptr); + const QString &argZTreeVersion, QObject *argParent = nullptr); + ~ZTree() override; signals: void ZTreeClosed(int argExitCode); private: - QProcess zTreeInstance; + QProcess *const zTreeInstance = nullptr; }; } // namespace lc