diff --git a/src/gui/src/IpcClient.cpp b/src/gui/src/IpcClient.cpp index 4955a1c6..dc305d50 100644 --- a/src/gui/src/IpcClient.cpp +++ b/src/gui/src/IpcClient.cpp @@ -24,7 +24,8 @@ #include "Ipc.h" IpcClient::IpcClient() : -m_ReaderStarted(false) +m_ReaderStarted(false), +m_Enabled(false) { m_Socket = new QTcpSocket(this); connect(m_Socket, SIGNAL(connected()), this, SLOT(connected())); @@ -49,6 +50,8 @@ void IpcClient::connected() void IpcClient::connectToHost() { + m_Enabled = true; + infoMessage("connecting to service..."); m_Socket->connectToHost(QHostAddress(QHostAddress::LocalHost), IPC_PORT); @@ -58,6 +61,13 @@ void IpcClient::connectToHost() } } +void IpcClient::disconnectFromHost() +{ + infoMessage("service disconnect"); + m_Reader->stop(); + m_Socket->close(); +} + void IpcClient::error(QAbstractSocket::SocketError error) { QString text; @@ -69,7 +79,14 @@ void IpcClient::error(QAbstractSocket::SocketError error) errorMessage(QString("ipc connection error, %1").arg(text)); - QTimer::singleShot(1000, this, SLOT(connectToHost())); + QTimer::singleShot(1000, this, SLOT(retryConnect())); +} + +void IpcClient::retryConnect() +{ + if (m_Enabled) { + connectToHost(); + } } void IpcClient::write(int code, int length, const char* data) diff --git a/src/gui/src/IpcClient.h b/src/gui/src/IpcClient.h index d04617ab..7e232800 100644 --- a/src/gui/src/IpcClient.h +++ b/src/gui/src/IpcClient.h @@ -32,9 +32,11 @@ public: virtual ~IpcClient(); void write(int code, int length, const char* data); + void connectToHost(); + void disconnectFromHost(); public slots: - void connectToHost(); + void retryConnect(); private: void intToBytes(int value, char* buffer, int size); @@ -53,4 +55,5 @@ private: QTcpSocket* m_Socket; IpcReader* m_Reader; bool m_ReaderStarted; + bool m_Enabled; }; diff --git a/src/gui/src/IpcReader.cpp b/src/gui/src/IpcReader.cpp index eade55cf..c72b2bcf 100644 --- a/src/gui/src/IpcReader.cpp +++ b/src/gui/src/IpcReader.cpp @@ -36,6 +36,11 @@ void IpcReader::start() connect(m_Socket, SIGNAL(readyRead()), this, SLOT(read())); } +void IpcReader::stop() +{ + disconnect(m_Socket, SIGNAL(readyRead()), this, SLOT(read())); +} + void IpcReader::read() { QMutexLocker locker(&m_Mutex); diff --git a/src/gui/src/IpcReader.h b/src/gui/src/IpcReader.h index 5ca49127..1b0dec3b 100644 --- a/src/gui/src/IpcReader.h +++ b/src/gui/src/IpcReader.h @@ -30,6 +30,7 @@ public: IpcReader(QTcpSocket* socket); virtual ~IpcReader(); void start(); + void stop(); signals: void readLogLine(const QString& text); diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index 4a17cd84..e443b3eb 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -77,7 +77,13 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) : m_versionChecker.setApp(appPath(appConfig.synergycName())); m_SetupWizard = new SetupWizard(*this, false); - connect(m_SetupWizard, SIGNAL(finished(int)), this, SLOT(refreshStartButton())); + connect(m_SetupWizard, SIGNAL(finished(int)), this, SLOT(onModeChanged())); + + // ipc must always be enabled, so that we can disable command when switching to desktop mode. + connect(&m_IpcClient, SIGNAL(readLogLine(const QString&)), this, SLOT(appendLogRaw(const QString&))); + connect(&m_IpcClient, SIGNAL(errorMessage(const QString&)), this, SLOT(appendLogError(const QString&))); + connect(&m_IpcClient, SIGNAL(infoMessage(const QString&)), this, SLOT(appendLogInfo(const QString&))); + m_IpcClient.connectToHost(); } MainWindow::~MainWindow() @@ -89,21 +95,7 @@ MainWindow::~MainWindow() void MainWindow::start(bool firstRun) { - refreshStartButton(); - - if (appConfig().processMode() == Service) - { - connect(&m_IpcClient, SIGNAL(readLogLine(const QString&)), this, SLOT(appendLogRaw(const QString&))); - connect(&m_IpcClient, SIGNAL(errorMessage(const QString&)), this, SLOT(appendLogError(const QString&))); - connect(&m_IpcClient, SIGNAL(infoMessage(const QString&)), this, SLOT(appendLogInfo(const QString&))); - m_IpcClient.connectToHost(); - startSynergy(); - } - - if (appConfig().processMode() == Desktop && !firstRun && appConfig().autoConnect()) - { - startSynergy(); - } + onModeChanged(firstRun); createTrayIcon(); @@ -113,6 +105,33 @@ void MainWindow::start(bool firstRun) m_versionChecker.checkLatest(); } +void MainWindow::onModeChanged() +{ + onModeChanged(false); +} + +void MainWindow::onModeChanged(bool firstRun) +{ + refreshStartButton(); + + stopSynergy(); + + if (appConfig().processMode() == Service) + { + startSynergy(); + } + else + { + // cause the service to stop creating processes. + sendDaemonCommand("", false); + } + + if (appConfig().processMode() == Desktop && !firstRun && appConfig().autoConnect()) + { + startSynergy(); + } +} + void MainWindow::refreshStartButton() { if (appConfig().processMode() == Service) @@ -322,9 +341,6 @@ void MainWindow::startSynergy() if (desktopMode) { - // cause the service to stop creating processes. - sendDaemonCommand("", false); - stopSynergy(); setSynergyState(synergyConnecting); } diff --git a/src/gui/src/MainWindow.h b/src/gui/src/MainWindow.h index 0f5d64f5..0ab46745 100644 --- a/src/gui/src/MainWindow.h +++ b/src/gui/src/MainWindow.h @@ -112,6 +112,8 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase void logError(); void updateFound(const QString& version); void refreshStartButton(); + void onModeChanged(); + void onModeChanged(bool firstRun); protected: QSettings& settings() { return m_Settings; }