From f4dce72d415ccd1c1903d2573b785f0c9c2c974f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=80lex=20Fiestas?= Date: Mon, 3 Mar 2014 19:58:05 +0100 Subject: [PATCH] Never block the kded Never block the kded even if things go wrong. In order to do this we need to switch to a completely asynchronous way of doing things, meaning that instead of returning bool on start/stop we have to emit signals. REVIEW: 116575 --- kded/kded.cpp | 44 ++++++++++++++++++++++++-------------------- kded/kded.h | 10 ++++++++-- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/kded/kded.cpp b/kded/kded.cpp index 21465ecdf..4eecb4a38 100644 --- a/kded/kded.cpp +++ b/kded/kded.cpp @@ -44,50 +44,38 @@ Kded::~Kded() kDebug(kdeconnect_kded()) << "kded_kdeconnect stopped"; } -bool Kded::start() +void Kded::start() { if (m_daemon) { - return true; + return; } const QString daemon = KStandardDirs::locate("exe", "kdeconnectd"); kDebug(kdeconnect_kded()) << "Starting daemon " << daemon; m_daemon = new KProcess(this); + connect(m_daemon, SIGNAL(started()), SLOT(daemonStarted())); connect(m_daemon, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onError(QProcess::ProcessError))); connect(m_daemon, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(onFinished(int,QProcess::ExitStatus))); connect(m_daemon, SIGNAL(finished(int,QProcess::ExitStatus)), m_daemon, SLOT(deleteLater())); m_daemon->setProgram(daemon); m_daemon->setOutputChannelMode(KProcess::SeparateChannels); - m_daemon->start(); - if (!m_daemon->waitForStarted(2000)) {//FIXME: KDEDs should be non-blocking, do we really need to wait for it to start? - kError(kdeconnect_kded()) << "Can't start " << daemon; - return false; - } - m_daemon->closeReadChannel(KProcess::StandardOutput); - - kDebug(kdeconnect_kded()) << "Daemon successfuly started"; - return true; + m_daemon->start(); } void Kded::stop() { if (!m_daemon) { - return true; + return; } m_daemon->terminate(); - if (m_daemon->waitForFinished(10000)) { - kDebug(kdeconnect_kded()) << "Daemon successfuly stopped"; - } else { - m_daemon->kill(); - kWarning(kdeconnect_kded()) << "Daemon killed"; - } - m_daemon = 0; + m_daemon->setProperty("terminate", true); + QTimer::singleShot(10000, this, SLOT(checkIfDaemonTerminated())); } -bool Kded::restart() +void Kded::restart() { stop(); return start(); @@ -98,6 +86,12 @@ void Kded::onError(QProcess::ProcessError errorCode) kError(kdeconnect_kded()) << "Process error code=" << errorCode; } +void Kded::daemonStarted() +{ + kDebug(kdeconnect_kded()) << "Daemon successfuly started"; + Q_EMIT started(); +} + void Kded::onFinished(int exitCode, QProcess::ExitStatus status) { if (status == QProcess::CrashExit) { @@ -109,6 +103,16 @@ void Kded::onFinished(int exitCode, QProcess::ExitStatus status) kWarning(kdeconnect_kded()) << "Process finished with code=" << exitCode; } + Q_EMIT stopped(); m_daemon = 0; } +void Kded::checkIfDaemonTerminated() +{ + if (!m_daemon || !m_daemon->property("terminate").isValid()) { + return; + } + + m_daemon->kill(); + kWarning(kdeconnect_kded()) << "Daemon killed"; +} diff --git a/kded/kded.h b/kded/kded.h index 5914d9688..9ae70310d 100644 --- a/kded/kded.h +++ b/kded/kded.h @@ -36,13 +36,19 @@ public: public Q_SLOTS: - Q_SCRIPTABLE bool start(); + Q_SCRIPTABLE void start(); Q_SCRIPTABLE void stop(); - Q_SCRIPTABLE bool restart(); + Q_SCRIPTABLE void restart(); + +Q_SIGNALS: + Q_SCRIPTABLE void started(); + Q_SCRIPTABLE void stopped(); private Q_SLOTS: void onError(QProcess::ProcessError); void onFinished(int, QProcess::ExitStatus); + void daemonStarted(); + void checkIfDaemonTerminated(); private: KProcess* m_daemon;