Simplify Qtimer code in ExternalCommandHelper.

This commit is contained in:
Andrius Štikonas 2018-03-23 22:45:51 +00:00
parent 8cdf70171e
commit f04416beba
2 changed files with 10 additions and 26 deletions

View File

@ -17,9 +17,7 @@
#include "externalcommandhelper.h" #include "externalcommandhelper.h"
#include <QDate>
#include <QtDBus> #include <QtDBus>
#include <QDBusContext>
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
#include <QString> #include <QString>
@ -28,6 +26,9 @@
#include <KLocalizedString> #include <KLocalizedString>
// exit helper if no ping for 42s
#define TIMEOUT 42000
/** Initialize ExternalCommandHelper Daemon and prepare DBus interface /** Initialize ExternalCommandHelper Daemon and prepare DBus interface
*/ */
ActionReply ExternalCommandHelper::init(const QVariantMap& args) ActionReply ExternalCommandHelper::init(const QVariantMap& args)
@ -47,11 +48,9 @@ ActionReply ExternalCommandHelper::init(const QVariantMap& args)
} }
QDBusConnection::systemBus().registerObject(QStringLiteral("/Helper"), this, QDBusConnection::ExportAllSlots); QDBusConnection::systemBus().registerObject(QStringLiteral("/Helper"), this, QDBusConnection::ExportAllSlots);
m_pingTime = new QDateTime(QDateTime::currentDateTime()); timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [=] () { exit(m_callerUuid); });
QTimer *timer = new QTimer(this); timer->start(TIMEOUT);
connect(timer, &QTimer::timeout, this, &ExternalCommandHelper::checkPing);
timer->start(20000); // check ping every 20 secs
HelperSupport::progressStep(QVariantMap()); HelperSupport::progressStep(QVariantMap());
m_loop.exec(); m_loop.exec();
@ -250,20 +249,7 @@ void ExternalCommandHelper::ping(const QString &Uuid)
if (!isCallerAuthorized(Uuid)) if (!isCallerAuthorized(Uuid))
return; return;
// update ping timer->setInterval(TIMEOUT);
m_pingTime->setDate(QDate::currentDate());
m_pingTime->setTime(QTime::currentTime());
}
void ExternalCommandHelper::checkPing()
{
qint64 mSecsSinceLastPing = m_pingTime->msecsTo(QDateTime::currentDateTime());
qDebug() << mSecsSinceLastPing / 1000.0 << " seconds since the last ping.";
if (mSecsSinceLastPing >= 42000) { // more than 42 seconds since the last ping
exit(m_callerUuid);
}
} }
void ExternalCommandHelper::onReadOutput() void ExternalCommandHelper::onReadOutput()

View File

@ -20,11 +20,12 @@
#include <KAuth> #include <KAuth>
#include <QDateTime>
#include <QEventLoop> #include <QEventLoop>
#include <QString> #include <QString>
#include <QProcess> #include <QProcess>
class QTimer;
using namespace KAuth; using namespace KAuth;
class ExternalCommandHelper : public QObject class ExternalCommandHelper : public QObject
@ -47,9 +48,6 @@ public Q_SLOTS:
Q_SCRIPTABLE void exit(const QString& Uuid); Q_SCRIPTABLE void exit(const QString& Uuid);
Q_SCRIPTABLE void ping(const QString& Uuid); Q_SCRIPTABLE void ping(const QString& Uuid);
private Q_SLOTS:
void checkPing();
private: private:
void onReadOutput(); void onReadOutput();
bool isCallerAuthorized(const QString& Uuid); bool isCallerAuthorized(const QString& Uuid);
@ -60,7 +58,7 @@ private:
QString m_sourceDevice; QString m_sourceDevice;
QProcess m_cmd; QProcess m_cmd;
QDateTime *m_pingTime; QTimer *timer;
// QByteArray output; // QByteArray output;
}; };