Updated the Tools menu to reflect current build.
[kdepim.git] / kalarm / rtcwakeaction.cpp
blob27fc8ebde993c54605def42dd8ee41b7d8cda31e
1 /*
2 * rtcwakeaction.cpp - KAuth helper application to execute rtcwake commands
3 * Program: kalarm
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"
23 #include <kglobal.h>
24 #include <klocale.h>
25 #include <kprocess.h>
26 #include <kdatetime.h>
28 #include <QDebug>
30 #include <stdio.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");
45 if (wh)
47 char buff[512] = { '\0' };
48 fgets(buff, sizeof(buff), wh);
49 pclose(wh);
50 // The string should be in the form "rtcwake: /path/rtcwake"
51 char* start = strchr(buff, ':');
52 if (start)
54 if (*++start == ' ')
55 ++start;
56 char* end = strpbrk(start, " \r\n");
57 if (end)
58 *end = 0;
59 if (*start)
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
69 KProcess proc;
70 if (!exe.isEmpty())
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
83 QString errmsg;
84 switch (result)
86 case 0:
87 return ActionReply::SuccessReply;
88 case -2:
89 errmsg = i18nc("@text/plain", "Could not run <command>%1</command> to set wake from suspend", "rtcwake");
90 break;
91 default:
92 errmsg = i18nc("@text/plain", "Error setting wake from suspend.<nl/>Command was: <command>%1</command><nl/>Error code: %2.", proc.program().join(" "), result);
93 break;
95 ActionReply reply(ActionReply::HelperError);
96 reply.setErrorCode(result);
97 reply.setErrorDescription(errmsg);
98 qDebug() << "RtcWakeAction::settimer: Code=" << reply.errorCode() << reply.errorDescription();
99 return reply;
102 KDE4_AUTH_HELPER_MAIN("org.kde.kalarmrtcwake", RtcWakeAction)
104 // vim: et sw=4: