1 /***************************************************************************
2 * Copyright (C) 2008 by Dario Freddi <drf@kdemod.ath.cx> *
3 * Copyright (C) 2008 by Kevin Ottens <ervin@kde.org> *
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. *
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. *
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"
24 #include <klocalizedstring.h>
26 SuspensionLockHandler::SuspensionLockHandler(QObject
*parent
)
28 m_isJobOngoing(false),
29 m_isOnNotification(false),
30 m_latestInhibitCookie(0)
34 SuspensionLockHandler::~SuspensionLockHandler()
38 bool SuspensionLockHandler::canStartSuspension(bool automated
)
41 if (hasInhibit(true)) {
46 return !m_isJobOngoing
;
49 bool SuspensionLockHandler::canStartNotification(bool automated
)
52 if (hasInhibit(true)) {
57 if (!m_isJobOngoing
&& !m_isOnNotification
) {
64 bool SuspensionLockHandler::hasInhibit(bool notify
)
66 if (m_inhibitRequests
.isEmpty()) {
69 kDebug() << "Inhibition detected!!";
70 // TODO: uhm... maybe a better notification here?
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
),
82 bool SuspensionLockHandler::setNotificationLock(bool automated
)
84 if (!canStartNotification(automated
)) {
85 kDebug() << "Notification lock present, aborting";
89 m_isOnNotification
= true;
94 bool SuspensionLockHandler::setJobLock(bool automated
)
96 if (!canStartSuspension(automated
)) {
97 kDebug() << "Suspension lock present, aborting";
101 m_isJobOngoing
= true;
106 int SuspensionLockHandler::inhibit(const QString
&application
, const QString
&reason
)
108 ++m_latestInhibitCookie
;
111 //TODO: Keep track of the service name too, to cleanup cookie in case of a crash.
112 req
.application
= application
;
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"