2014-01-17 13:55:04 +00:00
|
|
|
/**
|
2014-01-23 20:00:24 +00:00
|
|
|
* Copyright 2014 Yuri Samoilenko <kinnalru@gmail.com>
|
2014-01-17 13:55:04 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kded.h"
|
|
|
|
|
|
|
|
#include <QTimer>
|
2014-09-21 22:00:55 +01:00
|
|
|
#include <QDebug>
|
2014-01-17 13:55:04 +00:00
|
|
|
|
|
|
|
#include <KPluginFactory>
|
|
|
|
|
2014-07-05 12:28:42 +01:00
|
|
|
#include "config-kded.h"
|
2014-01-17 13:55:04 +00:00
|
|
|
|
|
|
|
K_PLUGIN_FACTORY(KdeConnectFactory, registerPlugin<Kded>();)
|
|
|
|
|
2014-09-21 22:00:55 +01:00
|
|
|
Q_LOGGING_CATEGORY(KDECONNECT_KDED, "kdeconnect.kded")
|
|
|
|
|
2014-01-17 13:55:04 +00:00
|
|
|
Kded::Kded(QObject *parent, const QList<QVariant>&)
|
|
|
|
: KDEDModule(parent)
|
|
|
|
, m_daemon(0)
|
|
|
|
{
|
2014-03-03 20:09:05 +00:00
|
|
|
QMetaObject::invokeMethod(this, "start", Qt::QueuedConnection);
|
2014-09-21 22:00:55 +01:00
|
|
|
qDebug(KDECONNECT_KDED) << "kded_kdeconnect started";
|
2014-01-17 13:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Kded::~Kded()
|
|
|
|
{
|
|
|
|
stop();
|
2014-09-21 22:00:55 +01:00
|
|
|
qDebug(KDECONNECT_KDED) << "kded_kdeconnect stopped";
|
2014-01-17 13:55:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 18:58:05 +00:00
|
|
|
void Kded::start()
|
2014-01-17 13:55:04 +00:00
|
|
|
{
|
2014-03-03 18:26:55 +00:00
|
|
|
if (m_daemon) {
|
2014-03-03 18:58:05 +00:00
|
|
|
return;
|
2014-01-17 13:55:04 +00:00
|
|
|
}
|
2014-09-21 22:01:08 +01:00
|
|
|
|
2014-07-05 12:28:42 +01:00
|
|
|
const QString daemon = QStringLiteral(KDECONNECTD_BIN);
|
2014-09-21 22:00:55 +01:00
|
|
|
qDebug(KDECONNECT_KDED) << "Starting daemon " << daemon;
|
2014-09-22 09:08:19 +01:00
|
|
|
m_daemon = new QProcess(this);
|
2014-03-03 18:58:05 +00:00
|
|
|
connect(m_daemon, SIGNAL(started()), SLOT(daemonStarted()));
|
2014-01-17 13:55:04 +00:00
|
|
|
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()));
|
2014-09-21 22:01:08 +01:00
|
|
|
|
2014-01-17 13:55:04 +00:00
|
|
|
m_daemon->setProgram(daemon);
|
2014-09-22 09:08:19 +01:00
|
|
|
m_daemon->closeReadChannel(QProcess::StandardOutput);
|
2014-03-03 18:58:05 +00:00
|
|
|
m_daemon->start();
|
2014-01-17 13:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Kded::stop()
|
|
|
|
{
|
2014-03-03 18:26:55 +00:00
|
|
|
if (!m_daemon) {
|
2014-03-03 18:58:05 +00:00
|
|
|
return;
|
2014-03-03 18:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_daemon->terminate();
|
2014-03-03 18:58:05 +00:00
|
|
|
m_daemon->setProperty("terminate", true);
|
|
|
|
QTimer::singleShot(10000, this, SLOT(checkIfDaemonTerminated()));
|
2014-01-17 13:55:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 18:58:05 +00:00
|
|
|
void Kded::restart()
|
2014-01-17 13:55:04 +00:00
|
|
|
{
|
|
|
|
stop();
|
|
|
|
return start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Kded::onError(QProcess::ProcessError errorCode)
|
|
|
|
{
|
2014-09-21 22:00:55 +01:00
|
|
|
qCWarning(KDECONNECT_KDED) << "Process error code=" << errorCode;
|
2014-01-17 13:55:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 18:58:05 +00:00
|
|
|
void Kded::daemonStarted()
|
|
|
|
{
|
2014-09-21 22:00:55 +01:00
|
|
|
qDebug(KDECONNECT_KDED) << "Daemon successfuly started";
|
2014-03-03 18:58:05 +00:00
|
|
|
Q_EMIT started();
|
|
|
|
}
|
|
|
|
|
2014-01-17 13:55:04 +00:00
|
|
|
void Kded::onFinished(int exitCode, QProcess::ExitStatus status)
|
|
|
|
{
|
2014-03-03 18:26:55 +00:00
|
|
|
if (status == QProcess::CrashExit) {
|
2014-09-21 22:00:55 +01:00
|
|
|
qCWarning(KDECONNECT_KDED) << "Process crashed with code=" << exitCode;
|
|
|
|
qCWarning(KDECONNECT_KDED) << m_daemon->readAllStandardError();
|
|
|
|
qCWarning(KDECONNECT_KDED) << "Restarting in 5 sec...";
|
2014-01-17 13:55:04 +00:00
|
|
|
QTimer::singleShot(5000, this, SLOT(start()));
|
2014-03-03 18:26:55 +00:00
|
|
|
} else {
|
2014-09-21 22:00:55 +01:00
|
|
|
qCWarning(KDECONNECT_KDED) << "Process finished with code=" << exitCode;
|
2014-01-17 13:55:04 +00:00
|
|
|
}
|
2014-03-03 18:26:55 +00:00
|
|
|
|
2014-03-03 18:58:05 +00:00
|
|
|
Q_EMIT stopped();
|
2014-01-17 22:06:47 +00:00
|
|
|
m_daemon = 0;
|
2014-01-17 13:55:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 18:58:05 +00:00
|
|
|
void Kded::checkIfDaemonTerminated()
|
|
|
|
{
|
|
|
|
if (!m_daemon || !m_daemon->property("terminate").isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_daemon->kill();
|
2014-09-21 22:00:55 +01:00
|
|
|
qCWarning(KDECONNECT_KDED) << "Daemon killed";
|
2014-03-03 18:58:05 +00:00
|
|
|
}
|
2014-06-16 19:02:07 +01:00
|
|
|
|
|
|
|
#include "kded.moc"
|