From 6a589b4fb3ad6b658367132d1be33d115b5b948c Mon Sep 17 00:00:00 2001 From: sieren Date: Thu, 13 Oct 2016 12:07:25 +0200 Subject: [PATCH 1/3] Cleanup: Remove unused code --- sources/qst/startuptab.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/sources/qst/startuptab.cpp b/sources/qst/startuptab.cpp index d155484..034020e 100644 --- a/sources/qst/startuptab.cpp +++ b/sources/qst/startuptab.cpp @@ -59,7 +59,6 @@ void StartupTab::initGUI() QGridLayout *filePathLayout = new QGridLayout; mpFilePathLine = new QLineEdit(mCurrentSyncthingPath.c_str()); - // mpFilePathLine->setFixedWidth(maximumWidth / devicePixelRatio()); mpFilePathBrowse = new QPushButton(tr("Browse")); mpAppSpawnedLabel = new QLabel(tr("Not started")); @@ -95,7 +94,6 @@ void StartupTab::initGUI() QGridLayout *iNotifyLayout = new QGridLayout; mpINotifyFilePath = new QLineEdit(mCurrentINotifyPath.c_str()); - // mpFilePathLine->setFixedWidth(maximumWidth / devicePixelRatio()); mpINotifyBrowse = new QPushButton(tr("Browse")); iNotifyLayout->addWidget(mpINotifyFilePath,2, 0, 1, 4); From e6c7a2d0e243c70de9139fb824787604cbc56cfe Mon Sep 17 00:00:00 2001 From: sieren Date: Thu, 13 Oct 2016 12:09:39 +0200 Subject: [PATCH 2/3] Make Process Information dynamic Extend process information notifications so that they can be used for any process. --- includes/qst/startuptab.hpp | 3 ++- includes/qst/syncconnector.h | 8 ++++++-- sources/qst/startuptab.cpp | 30 +++++++++++++++++++++--------- sources/qst/syncconnector.cpp | 10 +++++----- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/includes/qst/startuptab.hpp b/includes/qst/startuptab.hpp index b5132e8..5e82406 100644 --- a/includes/qst/startuptab.hpp +++ b/includes/qst/startuptab.hpp @@ -78,9 +78,10 @@ private slots: void pathEnterPressed(); void showFileBrowser(); void showINotifyFileBrowser(); - void processSpawnedChanged(kSyncthingProcessState state); + void processSpawnedChanged(const ProcessStateInfo& info); private: + void updateLabelWithState(QLabel* label, const ProcessState &state); void loadSettings(); void initGUI(); template diff --git a/includes/qst/syncconnector.h b/includes/qst/syncconnector.h index 85380ce..9d1a782 100644 --- a/includes/qst/syncconnector.h +++ b/includes/qst/syncconnector.h @@ -58,9 +58,13 @@ typedef enum processState NOT_RUNNING, ALREADY_RUNNING, PAUSED -} kSyncthingProcessState; +} ProcessState; using ConnectionStateCallback = std::function; +using ProcessStateInfo = std::map; + +static const std::string kSyncthingIdentifier{"syncthing"}; +static const std::string kNotifyIdentifier{"inotify"}; namespace qst { @@ -89,7 +93,7 @@ namespace connector signals: void onConnectionHealthChanged(ConnectionHealthStatus healthState); - void onProcessSpawned(kSyncthingProcessState procState); + void onProcessSpawned(ProcessStateInfo procState); void onNetworkActivityChanged(bool act); private slots: diff --git a/sources/qst/startuptab.cpp b/sources/qst/startuptab.cpp index 034020e..e0d46be 100644 --- a/sources/qst/startuptab.cpp +++ b/sources/qst/startuptab.cpp @@ -123,20 +123,32 @@ void StartupTab::initGUI() //------------------------------------------------------------------------------------// -void StartupTab::processSpawnedChanged(kSyncthingProcessState state) +void StartupTab::processSpawnedChanged(const ProcessStateInfo& info) +{ + auto syncProcess = info.find(kSyncthingIdentifier); + if (syncProcess != info.end()) + { + updateLabelWithState(mpAppSpawnedLabel, syncProcess->second); + } +} + + +//------------------------------------------------------------------------------------// + +void StartupTab::updateLabelWithState(QLabel* label, const ProcessState &state) { switch (state) { - case kSyncthingProcessState::SPAWNED: - mpAppSpawnedLabel->setText(tr("Status: Launched")); + case ProcessState::SPAWNED: + label->setText(tr("Status: Launched")); break; - case kSyncthingProcessState::NOT_RUNNING: - mpAppSpawnedLabel->setText(tr("Status: Not started")); + case ProcessState::NOT_RUNNING: + label->setText(tr("Status: Not started")); break; - case kSyncthingProcessState::ALREADY_RUNNING: - mpAppSpawnedLabel->setText(tr("Already Running")); + case ProcessState::ALREADY_RUNNING: + label->setText(tr("Already Running")); break; - case kSyncthingProcessState::PAUSED: - mpAppSpawnedLabel->setText(tr("Paused")); + case ProcessState::PAUSED: + label->setText(tr("Paused")); break; default: break; diff --git a/sources/qst/syncconnector.cpp b/sources/qst/syncconnector.cpp index 38cde36..2ccab23 100644 --- a/sources/qst/syncconnector.cpp +++ b/sources/qst/syncconnector.cpp @@ -199,13 +199,13 @@ void SyncConnector::syncThingProcessSpawned(QProcess::ProcessState newState) switch (newState) { case QProcess::Running: - emit(onProcessSpawned(kSyncthingProcessState::SPAWNED)); + emit(onProcessSpawned({{kSyncthingIdentifier, ProcessState::SPAWNED}})); break; case QProcess::NotRunning: - emit(onProcessSpawned(kSyncthingProcessState::NOT_RUNNING)); + emit(onProcessSpawned({{kSyncthingIdentifier, ProcessState::NOT_RUNNING}})); break; default: - emit(onProcessSpawned(kSyncthingProcessState::NOT_RUNNING)); + emit(onProcessSpawned({{kSyncthingIdentifier, ProcessState::NOT_RUNNING}})); } } @@ -359,7 +359,7 @@ void SyncConnector::onSettingsChanged() void SyncConnector::shutdownProcessPosted(QNetworkReply *reply) { - emit(onProcessSpawned(kSyncthingProcessState::PAUSED)); + emit(onProcessSpawned({{kSyncthingIdentifier, ProcessState::PAUSED}})); reply->deleteLater(); } @@ -393,7 +393,7 @@ void SyncConnector::spawnSyncthingProcess( } else { - emit(onProcessSpawned(kSyncthingProcessState::ALREADY_RUNNING)); + emit(onProcessSpawned({{kSyncthingIdentifier, ProcessState::ALREADY_RUNNING}})); } } } From 9c6d7dc39a37aaad46109ff5bebae921f9e30a2c Mon Sep 17 00:00:00 2001 From: sieren Date: Thu, 13 Oct 2016 12:10:25 +0200 Subject: [PATCH 3/3] Improve handling of inotify process Improve handling of iNotify process by cleaning up the launching logic and showing additional information about the process state on UI. --- includes/qst/startuptab.hpp | 6 +++-- includes/qst/syncconnector.h | 1 + sources/qst/startuptab.cpp | 48 ++++++++++++++++++----------------- sources/qst/syncconnector.cpp | 28 ++++++++++++++++++++ 4 files changed, 58 insertions(+), 25 deletions(-) diff --git a/includes/qst/startuptab.hpp b/includes/qst/startuptab.hpp index 5e82406..68f7ec6 100644 --- a/includes/qst/startuptab.hpp +++ b/includes/qst/startuptab.hpp @@ -69,19 +69,21 @@ class StartupTab : public QWidget ~StartupTab(); bool isPausingProcessRunning(); void spawnSyncthingApp(); + +public slots: void saveSettings(); - + private slots: void launchSyncthingBoxChanged(int state); void launchINotifyBoxChanged(int state); void shutdownOnExitBoxChanged(int state); - void pathEnterPressed(); void showFileBrowser(); void showINotifyFileBrowser(); void processSpawnedChanged(const ProcessStateInfo& info); private: void updateLabelWithState(QLabel* label, const ProcessState &state); + void startProcesses(); void loadSettings(); void initGUI(); template diff --git a/includes/qst/syncconnector.h b/includes/qst/syncconnector.h index 9d1a782..08669fa 100644 --- a/includes/qst/syncconnector.h +++ b/includes/qst/syncconnector.h @@ -101,6 +101,7 @@ namespace connector void netRequestfinished(QNetworkReply *reply); void checkConnectionHealth(); void syncThingProcessSpawned(QProcess::ProcessState newState); + void notifyProcessSpawned(QProcess::ProcessState newState); void shutdownProcessPosted(QNetworkReply *reply); void testUrlAvailability(); void webViewClosed(); diff --git a/sources/qst/startuptab.cpp b/sources/qst/startuptab.cpp index e0d46be..7415761 100644 --- a/sources/qst/startuptab.cpp +++ b/sources/qst/startuptab.cpp @@ -62,7 +62,7 @@ void StartupTab::initGUI() mpFilePathBrowse = new QPushButton(tr("Browse")); mpAppSpawnedLabel = new QLabel(tr("Not started")); - + mpShutdownOnExitBox = new QCheckBox(tr("Shutdown on Exit")); Qt::CheckState shutdownState = mShouldShutdownOnExit ? Qt::Checked : Qt::Unchecked; mpShutdownOnExitBox->setCheckState(shutdownState); @@ -78,7 +78,7 @@ void StartupTab::initGUI() mpFilePathGroupBox->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); connect(mpFilePathBrowse, SIGNAL(clicked()), this, SLOT(showFileBrowser())); - connect(mpFilePathLine, SIGNAL(returnPressed()), this, SLOT(pathEnterPressed())); + connect(mpFilePathLine, SIGNAL(returnPressed()), this, SLOT(saveSettings())); connect(mpShouldLaunchSyncthingBox, SIGNAL(stateChanged(int)), this, SLOT(launchSyncthingBoxChanged(int))); @@ -87,7 +87,8 @@ void StartupTab::initGUI() // mpiNotifyGroupBox = new QGroupBox(tr("iNotify Application")); - + + mpINotifySpawnedLabel = new QLabel(tr("Not started")); mpShouldLaunchINotify = new QCheckBox(tr("Launch iNotify")); Qt::CheckState iNotifylaunchState = mShouldLaunchINotify ? Qt::Checked : Qt::Unchecked; mpShouldLaunchINotify->setCheckState(iNotifylaunchState); @@ -98,6 +99,7 @@ void StartupTab::initGUI() iNotifyLayout->addWidget(mpINotifyFilePath,2, 0, 1, 4); iNotifyLayout->addWidget(mpINotifyBrowse,3, 0, 1, 1); + iNotifyLayout->addWidget(mpINotifySpawnedLabel, 3, 1, 1, 1); iNotifyLayout->addWidget(mpShouldLaunchINotify, 0, 0); mpiNotifyGroupBox->setLayout(iNotifyLayout); @@ -105,7 +107,7 @@ void StartupTab::initGUI() mpiNotifyGroupBox->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); connect(mpINotifyBrowse, SIGNAL(clicked()), this, SLOT(showINotifyFileBrowser())); - connect(mpINotifyFilePath, SIGNAL(returnPressed()), this, SLOT(pathEnterPressed())); + connect(mpINotifyFilePath, SIGNAL(returnPressed()), this, SLOT(saveSettings())); connect(mpShouldLaunchINotify, SIGNAL(stateChanged(int)), this, SLOT(launchINotifyBoxChanged(int))); connect(mpShutdownOnExitBox, SIGNAL(stateChanged(int)), this, @@ -130,6 +132,11 @@ void StartupTab::processSpawnedChanged(const ProcessStateInfo& info) { updateLabelWithState(mpAppSpawnedLabel, syncProcess->second); } + auto notifyprocess = info.find(kNotifyIdentifier); + if (notifyprocess != info.end()) + { + updateLabelWithState(mpINotifySpawnedLabel, notifyprocess->second); + } } @@ -180,11 +187,9 @@ void StartupTab::showINotifyFileBrowser() tr("Open iNotify"), "", tr("")); if (filename.toStdString() != "") { - mCurrentINotifyPath = filename.toStdString(); mpINotifyFilePath->setText(filename); } saveSettings(); - spawnSyncthingApp(); } //------------------------------------------------------------------------------------// @@ -213,7 +218,6 @@ void StartupTab::launchINotifyBoxChanged(int state) hideShowElements(state == Qt::Checked, mpINotifyFilePath, mpINotifyBrowse); mShouldLaunchINotify = state == Qt::Checked ? true : false; saveSettings(); - pathEnterPressed(); mpSyncConnector->checkAndSpawnINotifyProcess(false); } @@ -222,27 +226,33 @@ void StartupTab::launchINotifyBoxChanged(int state) void StartupTab::saveSettings() { + bool startServices = false; mCurrentSyncthingPath = mpFilePathLine->text().toStdString(); if (mSettings.value("syncthingpath").toString().toStdString() != mCurrentSyncthingPath) { - pathEnterPressed(); + startServices = true; } mSettings.setValue("syncthingpath", tr(mCurrentSyncthingPath.c_str())); if (mSettings.value("launchSyncthingAtStartup").toBool() != mShouldLaunchSyncthing) { - mpSyncConnector->spawnSyncthingProcess( - mCurrentSyncthingPath, mShouldLaunchSyncthing); + startServices = true; } mSettings.setValue("launchSyncthingAtStartup", mShouldLaunchSyncthing); mCurrentINotifyPath = mpINotifyFilePath->text().toStdString(); if (mSettings.value("inotifypath").toString().toStdString() != mCurrentINotifyPath) { - pathEnterPressed(); + startServices = true; } + mSettings.setValue("inotifypath", tr(mCurrentINotifyPath.c_str())); mSettings.setValue("launchINotifyAtStartup", mShouldLaunchINotify); mSettings.setValue("ShutdownOnExit", mShouldShutdownOnExit); + + if (startServices) + { + startProcesses(); + } mpSyncConnector->onSettingsChanged(); } @@ -261,21 +271,13 @@ void StartupTab::loadSettings() //------------------------------------------------------------------------------------// -void StartupTab::pathEnterPressed() + +void StartupTab::startProcesses() { - mCurrentSyncthingPath = mpFilePathLine->text().toStdString(); - mCurrentINotifyPath = mpINotifyFilePath->text().toStdString(); - if (mSettings.value("syncthingpath").toString().toStdString() != mCurrentSyncthingPath) - { - mpSyncConnector->spawnSyncthingProcess(mCurrentSyncthingPath, true, true); - } - if (mSettings.value("inotifypath").toString().toStdString() != mCurrentINotifyPath) - { - mpSyncConnector->checkAndSpawnINotifyProcess(true); - } + mpSyncConnector->spawnSyncthingProcess(mCurrentSyncthingPath, true, true); + mpSyncConnector->checkAndSpawnINotifyProcess(true); } - //------------------------------------------------------------------------------------// void StartupTab::spawnSyncthingApp() diff --git a/sources/qst/syncconnector.cpp b/sources/qst/syncconnector.cpp index 2ccab23..49c2ba7 100644 --- a/sources/qst/syncconnector.cpp +++ b/sources/qst/syncconnector.cpp @@ -210,6 +210,24 @@ void SyncConnector::syncThingProcessSpawned(QProcess::ProcessState newState) } +//------------------------------------------------------------------------------------// + +void SyncConnector::notifyProcessSpawned(QProcess::ProcessState newState) +{ + switch (newState) + { + case QProcess::Running: + emit(onProcessSpawned({{kNotifyIdentifier, ProcessState::SPAWNED}})); + break; + case QProcess::NotRunning: + emit(onProcessSpawned({{kNotifyIdentifier, ProcessState::NOT_RUNNING}})); + break; + default: + emit(onProcessSpawned({{kNotifyIdentifier, ProcessState::NOT_RUNNING}})); + } +} + + //------------------------------------------------------------------------------------// void SyncConnector::netRequestfinished(QNetworkReply* reply) @@ -406,6 +424,10 @@ void SyncConnector::checkAndSpawnINotifyProcess(bool isRequestedExternal) if (isRequestedExternal) { onSettingsChanged(); + if (mpSyncthingNotifierProcess) + { + mpSyncthingNotifierProcess->terminate(); + } } if (mShouldLaunchINotify) { @@ -422,8 +444,14 @@ void SyncConnector::checkAndSpawnINotifyProcess(bool isRequestedExternal) { mpSyncthingNotifierProcess = std::unique_ptr(new QProcess(this)); QString processPath = QDir::toNativeSeparators(mINotifyFilePath); + connect(mpSyncthingNotifierProcess.get(), SIGNAL(stateChanged(QProcess::ProcessState)), + this, SLOT(notifyProcessSpawned(QProcess::ProcessState))); mpSyncthingNotifierProcess->start(processPath, QStringList(), QIODevice::Unbuffered); } + else + { + emit(onProcessSpawned({{kNotifyIdentifier, ProcessState::ALREADY_RUNNING}})); + } } else {