Skip to content

Commit

Permalink
Add sleep suppression while performing backup jobs
Browse files Browse the repository at this point in the history
Prevents computer from going to sleep due to user inactivity, as long
as kup is busy working.
  • Loading branch information
spersson committed Apr 18, 2017
1 parent c5d02b6 commit f89f38d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
41 changes: 40 additions & 1 deletion daemon/planexecutor.cpp
Expand Up @@ -25,6 +25,8 @@
#include "kupdaemon.h"
#include "rsyncjob.h"

#include <QDBusConnection>
#include <QDBusReply>
#include <QDir>
#include <QTimer>

Expand All @@ -33,10 +35,14 @@
#include <KNotification>
#include <KRun>

static QString sPwrMgmtServiceName = QStringLiteral("org.freedesktop.PowerManagement");
static QString sPwrMgmtPath = QStringLiteral("/org/freedesktop/PowerManagement");
static QString sPwrMgmtInhibitInterface = QStringLiteral("org.freedesktop.PowerManagement.Inhibit");

PlanExecutor::PlanExecutor(BackupPlan *pPlan, KupDaemon *pKupDaemon)
:QObject(pKupDaemon), mState(NOT_AVAILABLE), mPlan(pPlan), mQuestion(NULL),
mFailNotification(NULL), mIntegrityNotification(NULL), mRepairNotification(NULL),
mKupDaemon(pKupDaemon)
mKupDaemon(pKupDaemon), mSleepCookie(0)
{
QString lCachePath = QString::fromLocal8Bit(qgetenv("XDG_CACHE_HOME").constData());
if(lCachePath.isEmpty()) {
Expand Down Expand Up @@ -226,6 +232,7 @@ void PlanExecutor::startIntegrityCheck() {
mLastState = mState;
mState = INTEGRITY_TESTING;
emit stateChanged();
startSleepInhibit();
}

void PlanExecutor::startRepairJob() {
Expand All @@ -238,6 +245,7 @@ void PlanExecutor::startRepairJob() {
mLastState = mState;
mState = REPAIRING;
emit stateChanged();
startSleepInhibit();
}

void PlanExecutor::startBackupSaveJob() {
Expand All @@ -248,6 +256,7 @@ void PlanExecutor::startBackupSaveJob() {
}

void PlanExecutor::integrityCheckFinished(KJob *pJob) {
endSleepInhibit();
discardIntegrityNotification();
mIntegrityNotification = new KNotification(QStringLiteral("IntegrityCheckCompleted"), KNotification::Persistent);
mIntegrityNotification->setTitle(xi18nc("@title:window", "Integrity Check Completed"));
Expand Down Expand Up @@ -282,6 +291,7 @@ void PlanExecutor::discardIntegrityNotification() {
}

void PlanExecutor::repairFinished(KJob *pJob) {
endSleepInhibit();
discardRepairNotification();
mRepairNotification = new KNotification(QStringLiteral("RepairCompleted"), KNotification::Persistent);
mRepairNotification->setTitle(xi18nc("@title:window", "Repair Completed"));
Expand All @@ -307,14 +317,43 @@ void PlanExecutor::discardRepairNotification() {
}
}

void PlanExecutor::startSleepInhibit() {
if(mSleepCookie != 0) {
return;
}
QDBusMessage lMsg = QDBusMessage::createMethodCall(sPwrMgmtServiceName,
sPwrMgmtPath,
sPwrMgmtInhibitInterface,
QStringLiteral("Inhibit"));
lMsg << i18n("Kup Backup System");
lMsg << currentActivityTitle();
QDBusReply<uint> lReply = QDBusConnection::sessionBus().call(lMsg);
mSleepCookie = lReply.value();
}

void PlanExecutor::endSleepInhibit() {
if(mSleepCookie == 0) {
return;
}
QDBusMessage lMsg = QDBusMessage::createMethodCall(sPwrMgmtServiceName,
sPwrMgmtPath,
sPwrMgmtInhibitInterface,
QStringLiteral("UnInhibit"));
lMsg << mSleepCookie;
QDBusConnection::sessionBus().asyncCall(lMsg);
mSleepCookie = 0;
}

void PlanExecutor::enterBackupRunningState() {
discardUserQuestion();
mState = BACKUP_RUNNING;
emit stateChanged();
startSleepInhibit();
startBackup();
}

void PlanExecutor::exitBackupRunningState(bool pWasSuccessful) {
endSleepInhibit();
if(pWasSuccessful) {
if(mPlan->mScheduleType == BackupPlan::USAGE) {
//reset usage time after successful backup
Expand Down
4 changes: 4 additions & 0 deletions daemon/planexecutor.h
Expand Up @@ -101,6 +101,9 @@ protected slots:
void repairFinished(KJob *pJob);
void discardRepairNotification();

void startSleepInhibit();
void endSleepInhibit();

protected:
BackupJob *createBackupJob();

Expand All @@ -111,6 +114,7 @@ protected slots:
KNotification *mRepairNotification;
ExecutorState mLastState;
KupDaemon *mKupDaemon;
uint mSleepCookie;
};

#endif // PLANEXECUTOR_H

0 comments on commit f89f38d

Please sign in to comment.