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"
26 #include <kdatetime.h>
32 RtcWakeAction::RtcWakeAction()
34 KGlobal::locale()->insertCatalog("kalarm");
37 ActionReply
RtcWakeAction::settimer(const QVariantMap
& args
)
39 unsigned t
= args
["time"].toUInt();
40 qDebug() << "RtcWakeAction::settimer(" << t
<< ")";
42 // Find the rtcwake executable
43 QString
exe("/usr/sbin/rtcwake"); // default location
44 FILE* wh
= popen("whereis -b rtcwake", "r");
47 char buff
[512] = { '\0' };
48 fgets(buff
, sizeof(buff
), wh
);
50 // The string should be in the form "rtcwake: /path/rtcwake"
51 char* start
= strchr(buff
, ':');
56 char* end
= strpbrk(start
, " \r\n");
61 exe
= QString::fromLocal8Bit(start
);
62 qDebug() << "RtcWakeAction::settimer:" << exe
;
67 // Set the wakeup by executing the rtcwake command
68 int result
= -2; // default = command not found
72 // The wakeup time is set using a time from now ("-s") in preference to
73 // an absolute time ("-t") so that if the hardware clock is not in sync
74 // with the system clock, the alarm will still occur at the correct time.
75 // The "-m no" option sets the wakeup time without suspending the computer.
77 // If 't' is zero, the current wakeup is cancelled by setting a new wakeup
78 // time 2 seconds from now, which will then expire.
79 unsigned now
= KDateTime::currentUtcDateTime().toTime_t();
80 proc
<< exe
<< "-m" << "no" << "-s" << QString::number(t
? t
- now
: 2);
81 result
= proc
.execute(5000); // allow a timeout of 5 seconds
87 return ActionReply::SuccessReply
;
89 errmsg
= i18nc("@text/plain", "Could not run <command>%1</command> to set wake from suspend", "rtcwake");
92 errmsg
= i18nc("@text/plain", "Error setting wake from suspend.<nl/>Command was: <command>%1</command><nl/>Error code: %2.", proc
.program().join(" "), result
);
95 ActionReply
reply(ActionReply::HelperError
);
96 reply
.setErrorCode(result
);
97 reply
.setErrorDescription(errmsg
);
98 qDebug() << "RtcWakeAction::settimer: Code=" << reply
.errorCode() << reply
.errorDescription();
102 KDE4_AUTH_HELPER_MAIN("org.kde.kalarmrtcwake", RtcWakeAction
)