2 * rtcwakeaction.cpp - KAuth helper application to execute rtcwake commands
4 * Copyright © 2011 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "rtcwakeaction.h"
24 #include <KLocalizedString>
25 #include <kauthactionreply.h>
26 #include "kalarm_debug.h"
33 RtcWakeAction::RtcWakeAction()
35 KLocalizedString::setApplicationDomain("kalarm");
38 ActionReply
RtcWakeAction::settimer(const QVariantMap
& args
)
40 unsigned t
= args
[QStringLiteral("time")].toUInt();
41 qCDebug(KALARM_LOG
) << "RtcWakeAction::settimer(" << t
<< ")";
43 // Find the rtcwake executable
44 QString
exe(QStringLiteral("/usr/sbin/rtcwake")); // default location
45 FILE* wh
= popen("whereis -b rtcwake", "r");
48 char buff
[512] = { '\0' };
49 fgets(buff
, sizeof(buff
), wh
);
51 // The string should be in the form "rtcwake: /path/rtcwake"
52 char* start
= strchr(buff
, ':');
57 char* end
= strpbrk(start
, " \r\n");
62 exe
= QString::fromLocal8Bit(start
);
63 qCDebug(KALARM_LOG
) << "RtcWakeAction::settimer:" << exe
;
68 // Set the wakeup by executing the rtcwake command
69 int result
= -2; // default = command not found
73 // The wakeup time is set using a time from now ("-s") in preference to
74 // an absolute time ("-t") so that if the hardware clock is not in sync
75 // with the system clock, the alarm will still occur at the correct time.
76 // The "-m no" option sets the wakeup time without suspending the computer.
78 // If 't' is zero, the current wakeup is cancelled by setting a new wakeup
79 // time 2 seconds from now, which will then expire.
80 unsigned now
= QDateTime::currentDateTimeUtc().toTime_t();
82 proc
.setArguments({ QStringLiteral("-m"), QStringLiteral("no"), QStringLiteral("-s"), QString::number(t
? t
- now
: 2) });
84 proc
.waitForStarted(5000); // allow a timeout of 5 seconds
85 result
= proc
.exitCode();
91 return ActionReply::SuccessType
;
93 errmsg
= xi18nc("@text/plain", "Could not run <command>%1</command> to set wake from suspend", QStringLiteral("rtcwake"));
96 errmsg
= xi18nc("@text/plain", "Error setting wake from suspend.<nl/>Command was: <command>%1 %2</command><nl/>Error code: %3.", proc
.program(), proc
.arguments().join(QStringLiteral(" ")), result
);
100 ActionReply
reply(ActionReply::HelperErrorReply
);
101 reply
.setErrorCode(result
);
102 reply
.setErrorDescription(errmsg
);
103 qCDebug(KALARM_LOG
) , "RtcWakeAction::settimer: Code=" , reply
.errorCode() , reply
.errorDescription();
110 KAUTH_HELPER_MAIN("org.kde.kalarmrtcwake", RtcWakeAction
)