• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm

  • sources
  • kde-4.12
  • kdepim
  • kalarm
rtcwakeaction.cpp
Go to the documentation of this file.
1 /*
2  * rtcwakeaction.cpp - KAuth helper application to execute rtcwake commands
3  * Program: kalarm
4  * Copyright © 2011 by David Jarvie <djarvie@kde.org>
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include "rtcwakeaction.h"
22 
23 #include <kglobal.h>
24 #include <klocale.h>
25 #include <kprocess.h>
26 #include <kdatetime.h>
27 
28 #include <QDebug>
29 
30 #include <stdio.h>
31 
32 RtcWakeAction::RtcWakeAction()
33 {
34  KGlobal::locale()->insertCatalog(QLatin1String("kalarm"));
35 }
36 
37 ActionReply RtcWakeAction::settimer(const QVariantMap& args)
38 {
39  unsigned t = args[QLatin1String("time")].toUInt();
40  qDebug() << "RtcWakeAction::settimer(" << t << ")";
41 
42  // Find the rtcwake executable
43  QString exe(QLatin1String("/usr/sbin/rtcwake")); // default location
44  FILE* wh = popen("whereis -b rtcwake", "r");
45  if (wh)
46  {
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)
53  {
54  if (*++start == ' ')
55  ++start;
56  char* end = strpbrk(start, " \r\n");
57  if (end)
58  *end = 0;
59  if (*start)
60  {
61  exe = QString::fromLocal8Bit(start);
62  qDebug() << "RtcWakeAction::settimer:" << exe;
63  }
64  }
65  }
66 
67  // Set the wakeup by executing the rtcwake command
68  int result = -2; // default = command not found
69  KProcess proc;
70  if (!exe.isEmpty())
71  {
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.
76 
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 << QLatin1String("-m") << QLatin1String("no") << QLatin1String("-s") << QString::number(t ? t - now : 2);
81  result = proc.execute(5000); // allow a timeout of 5 seconds
82  }
83  QString errmsg;
84  switch (result)
85  {
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", QLatin1String("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(QLatin1String(" ")), result);
93  break;
94  }
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;
100 }
101 
102 KDE4_AUTH_HELPER_MAIN("org.kde.kalarmrtcwake", RtcWakeAction)
103 
104 // vim: et sw=4:
KProcess
rtcwakeaction.h
RtcWakeAction
Definition: rtcwakeaction.h:28
RtcWakeAction::settimer
ActionReply settimer(const QVariantMap &args)
Definition: rtcwakeaction.cpp:37
RtcWakeAction::RtcWakeAction
RtcWakeAction()
Definition: rtcwakeaction.cpp:32
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:59:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal