Continue to port to QRegularExpression
[kdepim.git] / kalarm / rtcwakeaction.cpp
bloba47b113ad0a58138184918ebb8d3f41ce051ce0f
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 <KLocalizedString>
25 #include <kauthactionreply.h>
26 #include "kalarm_debug.h"
28 #include <QProcess>
29 #include <QDateTime>
31 #include <stdio.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");
46 if (wh)
48 char buff[512] = { '\0' };
49 fgets(buff, sizeof(buff), wh);
50 pclose(wh);
51 // The string should be in the form "rtcwake: /path/rtcwake"
52 char* start = strchr(buff, ':');
53 if (start)
55 if (*++start == ' ')
56 ++start;
57 char* end = strpbrk(start, " \r\n");
58 if (end)
59 *end = 0;
60 if (*start)
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
70 QProcess proc;
71 if (!exe.isEmpty())
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();
81 proc.setProgram(exe);
82 proc.setArguments({ QStringLiteral("-m"), QStringLiteral("no"), QStringLiteral("-s"), QString::number(t ? t - now : 2) });
83 proc.start();
84 proc.waitForStarted(5000); // allow a timeout of 5 seconds
85 result = proc.exitCode();
87 QString errmsg;
88 switch (result)
90 case 0:
91 return ActionReply::SuccessType;
92 case -2:
93 errmsg = xi18nc("@text/plain", "Could not run <command>%1</command> to set wake from suspend", QStringLiteral("rtcwake"));
94 break;
95 default:
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);
97 break;
99 #if 0 //QT5
100 ActionReply reply(ActionReply::HelperErrorReply);
101 reply.setErrorCode(result);
102 reply.setErrorDescription(errmsg);
103 qCDebug(KALARM_LOG) , "RtcWakeAction::settimer: Code=" , reply.errorCode() , reply.errorDescription();
104 return reply;
105 #else
106 return 0;
107 #endif
110 KAUTH_HELPER_MAIN("org.kde.kalarmrtcwake", RtcWakeAction)
112 // vim: et sw=4: