Skip to content

Commit

Permalink
Merge pull request #170 from sieren/improve-notify-process
Browse files Browse the repository at this point in the history
Improve Process Handling
  • Loading branch information
sieren authored Oct 13, 2016
2 parents 7fc6b63 + 9c6d7dc commit 88f71f8
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 44 deletions.
9 changes: 6 additions & 3 deletions includes/qst/startuptab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +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(kSyncthingProcessState state);
void processSpawnedChanged(const ProcessStateInfo& info);

private:
void updateLabelWithState(QLabel* label, const ProcessState &state);
void startProcesses();
void loadSettings();
void initGUI();
template <typename T, typename ... TArgs>
Expand Down
9 changes: 7 additions & 2 deletions includes/qst/syncconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ typedef enum processState
NOT_RUNNING,
ALREADY_RUNNING,
PAUSED
} kSyncthingProcessState;
} ProcessState;

using ConnectionStateCallback = std::function<void(ConnectionState&)>;
using ProcessStateInfo = std::map<std::string, processState>;

static const std::string kSyncthingIdentifier{"syncthing"};
static const std::string kNotifyIdentifier{"inotify"};

namespace qst
{
Expand Down Expand Up @@ -89,14 +93,15 @@ namespace connector

signals:
void onConnectionHealthChanged(ConnectionHealthStatus healthState);
void onProcessSpawned(kSyncthingProcessState procState);
void onProcessSpawned(ProcessStateInfo procState);
void onNetworkActivityChanged(bool act);

private slots:
void onSslError(QNetworkReply* reply);
void netRequestfinished(QNetworkReply *reply);
void checkConnectionHealth();
void syncThingProcessSpawned(QProcess::ProcessState newState);
void notifyProcessSpawned(QProcess::ProcessState newState);
void shutdownProcessPosted(QNetworkReply *reply);
void testUrlAvailability();
void webViewClosed();
Expand Down
80 changes: 46 additions & 34 deletions sources/qst/startuptab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ 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"));

mpShutdownOnExitBox = new QCheckBox(tr("Shutdown on Exit"));
Qt::CheckState shutdownState = mShouldShutdownOnExit ? Qt::Checked : Qt::Unchecked;
mpShutdownOnExitBox->setCheckState(shutdownState);
Expand All @@ -79,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)));

Expand All @@ -88,26 +87,27 @@ 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);
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);
iNotifyLayout->addWidget(mpINotifyBrowse,3, 0, 1, 1);
iNotifyLayout->addWidget(mpINotifySpawnedLabel, 3, 1, 1, 1);
iNotifyLayout->addWidget(mpShouldLaunchINotify, 0, 0);

mpiNotifyGroupBox->setLayout(iNotifyLayout);
mpiNotifyGroupBox->setMinimumWidth(400);
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,
Expand All @@ -125,20 +125,37 @@ 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);
}
auto notifyprocess = info.find(kNotifyIdentifier);
if (notifyprocess != info.end())
{
updateLabelWithState(mpINotifySpawnedLabel, notifyprocess->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;
Expand Down Expand Up @@ -170,11 +187,9 @@ void StartupTab::showINotifyFileBrowser()
tr("Open iNotify"), "", tr(""));
if (filename.toStdString() != "")
{
mCurrentINotifyPath = filename.toStdString();
mpINotifyFilePath->setText(filename);
}
saveSettings();
spawnSyncthingApp();
}

//------------------------------------------------------------------------------------//
Expand Down Expand Up @@ -203,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);
}

Expand All @@ -212,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();
}

Expand All @@ -251,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()
Expand Down
38 changes: 33 additions & 5 deletions sources/qst/syncconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,31 @@ 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}}));
}
}


//------------------------------------------------------------------------------------//

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}}));
}
}

Expand Down Expand Up @@ -359,7 +377,7 @@ void SyncConnector::onSettingsChanged()

void SyncConnector::shutdownProcessPosted(QNetworkReply *reply)
{
emit(onProcessSpawned(kSyncthingProcessState::PAUSED));
emit(onProcessSpawned({{kSyncthingIdentifier, ProcessState::PAUSED}}));
reply->deleteLater();
}

Expand Down Expand Up @@ -393,7 +411,7 @@ void SyncConnector::spawnSyncthingProcess(
}
else
{
emit(onProcessSpawned(kSyncthingProcessState::ALREADY_RUNNING));
emit(onProcessSpawned({{kSyncthingIdentifier, ProcessState::ALREADY_RUNNING}}));
}
}
}
Expand All @@ -406,6 +424,10 @@ void SyncConnector::checkAndSpawnINotifyProcess(bool isRequestedExternal)
if (isRequestedExternal)
{
onSettingsChanged();
if (mpSyncthingNotifierProcess)
{
mpSyncthingNotifierProcess->terminate();
}
}
if (mShouldLaunchINotify)
{
Expand All @@ -422,8 +444,14 @@ void SyncConnector::checkAndSpawnINotifyProcess(bool isRequestedExternal)
{
mpSyncthingNotifierProcess = std::unique_ptr<QProcess>(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
{
Expand Down

0 comments on commit 88f71f8

Please # to comment.