dtor first
[personal-kdebase.git] / workspace / powerdevil / daemon / SuspensionLockHandler.cpp
blob14c36c9da416d04a25ff026d9552665890084c91
1 /***************************************************************************
2 * Copyright (C) 2008 by Dario Freddi <drf@kdemod.ath.cx> *
3 * Copyright (C) 2008 by Kevin Ottens <ervin@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
19 ***************************************************************************/
21 #include "SuspensionLockHandler.h"
23 #include <KDebug>
24 #include <klocalizedstring.h>
26 SuspensionLockHandler::SuspensionLockHandler(QObject *parent)
27 : QObject(parent),
28 m_isJobOngoing(false),
29 m_isOnNotification(false),
30 m_latestInhibitCookie(0)
34 SuspensionLockHandler::~SuspensionLockHandler()
38 bool SuspensionLockHandler::canStartSuspension(bool automated)
40 if (automated) {
41 if (hasInhibit(true)) {
42 return false;
46 return !m_isJobOngoing;
49 bool SuspensionLockHandler::canStartNotification(bool automated)
51 if (automated) {
52 if (hasInhibit(true)) {
53 return false;
57 if (!m_isJobOngoing && !m_isOnNotification) {
58 return true;
59 } else {
60 return false;
64 bool SuspensionLockHandler::hasInhibit(bool notify)
66 if (m_inhibitRequests.isEmpty()) {
67 return false;
68 } else {
69 kDebug() << "Inhibition detected!!";
70 // TODO: uhm... maybe a better notification here?
71 if (notify) {
72 emit streamCriticalNotification("inhibition", i18n("The application %1 "
73 "is inhibiting suspension for the following reason:\n%2",
74 m_inhibitRequests[m_latestInhibitCookie].application,
75 m_inhibitRequests[m_latestInhibitCookie].reason),
76 0, "dialog-cancel");
78 return true;
82 bool SuspensionLockHandler::setNotificationLock(bool automated)
84 if (!canStartNotification(automated)) {
85 kDebug() << "Notification lock present, aborting";
86 return false;
89 m_isOnNotification = true;
91 return true;
94 bool SuspensionLockHandler::setJobLock(bool automated)
96 if (!canStartSuspension(automated)) {
97 kDebug() << "Suspension lock present, aborting";
98 return false;
101 m_isJobOngoing = true;
103 return true;
106 int SuspensionLockHandler::inhibit(const QString &application, const QString &reason)
108 ++m_latestInhibitCookie;
110 InhibitRequest req;
111 //TODO: Keep track of the service name too, to cleanup cookie in case of a crash.
112 req.application = application;
113 req.reason = reason;
114 req.cookie = m_latestInhibitCookie;
115 m_inhibitRequests[m_latestInhibitCookie] = req;
117 emit inhibitChanged(true);
119 return m_latestInhibitCookie;
122 void SuspensionLockHandler::releaseNotificationLock()
124 kDebug() << "Releasing notification lock";
125 m_isOnNotification = false;
128 void SuspensionLockHandler::releaseAllLocks()
130 kDebug() << "Releasing locks";
131 m_isJobOngoing = false;
132 m_isOnNotification = false;
135 void SuspensionLockHandler::releaseAllInhibitions()
137 m_inhibitRequests.clear();
138 emit inhibitChanged(false);
141 void SuspensionLockHandler::releaseInhibiton(int cookie)
143 kDebug() << "Removing cookie" << cookie;
144 m_inhibitRequests.remove(cookie);
145 if (m_inhibitRequests.isEmpty()) {
146 emit inhibitChanged(false);
150 #include "SuspensionLockHandler.moc"