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

View File

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