2 Copyright 2007 Robert Knight <robertknight@gmail.com>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
21 #include "core/itemhandlers.h"
30 #include <KToolInvocation>
32 #include <solid/control/powermanager.h>
35 #include <kworkspace/kworkspace.h>
38 #include "core/recentapplications.h"
41 #include "krunner_interface.h"
42 #include "screensaver_interface.h"
43 #include "ksmserver_interface.h"
45 using namespace Kickoff
;
47 bool ServiceItemHandler::openUrl(const KUrl
& url
)
49 int result
= KToolInvocation::startServiceByDesktopPath(url
.pathOrUrl(), QStringList(), 0, 0, 0, "", true);
52 KService::Ptr service
= KService::serviceByDesktopPath(url
.pathOrUrl());
54 if (!service
.isNull()) {
55 RecentApplications::self()->add(service
);
57 qWarning() << "Failed to find service for" << url
;
65 bool LeaveItemHandler::openUrl(const KUrl
& url
)
67 m_logoutAction
= url
.path().remove('/');
69 if (m_logoutAction
== "sleep") {
70 // Check if powerdevil is running, and use its methods to suspend if available
71 // otherwise go through Solid directly
73 QDBusInterface
kdedInterface("org.kde.kded", "/kded", "org.kde.kded");
74 QDBusReply
<QStringList
> reply
= kdedInterface
.call("loadedModules");
75 if (reply
.isValid() && reply
.value().contains("powerdevil")) {
76 kDebug() << "Using powerdevil to suspend";
77 QDBusConnection
dbus(QDBusConnection::sessionBus());
78 QDBusInterface
iface("org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil", dbus
);
79 iface
.call("suspend", Solid::Control::PowerManager::ToRam
);
81 kDebug() << "Powerdevil not available, using solid to suspend";
82 KJob
* job
= Solid::Control::PowerManager::suspend(Solid::Control::PowerManager::ToRam
);
86 } else if (m_logoutAction
== "hibernate") {
87 // Check if powerdevil is running, and use its methods to hibernate if available
88 // otherwise go through Solid directly
90 QDBusInterface
kdedInterface("org.kde.kded", "/kded", "org.kde.kded");
91 QDBusReply
<QStringList
> reply
= kdedInterface
.call("loadedModules");
92 if (reply
.isValid() && reply
.value().contains("powerdevil")) {
93 kDebug() << "Using powerdevil to hibernate";
94 QDBusConnection
dbus(QDBusConnection::sessionBus());
95 QDBusInterface
iface("org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil", dbus
);
96 iface
.call("suspend", Solid::Control::PowerManager::ToDisk
);
98 kDebug() << "Powerdevil not available, using solid to hibernate";
99 KJob
* job
= Solid::Control::PowerManager::suspend(Solid::Control::PowerManager::ToDisk
);
103 } else if (m_logoutAction
== "lock") {
104 // decouple dbus call, otherwise we'll run into a dead-lock
105 QTimer::singleShot(0, this, SLOT(lock()));
107 } else if (m_logoutAction
== "switch") {
108 // decouple dbus call, otherwise we'll run into a dead-lock
109 QTimer::singleShot(0, this, SLOT(switchUser()));
111 } else if (m_logoutAction
== "logout" || m_logoutAction
== "logoutonly" ||
112 m_logoutAction
== "restart" || m_logoutAction
== "shutdown") {
113 // decouple dbus call, otherwise we'll run into a dead-lock
114 QTimer::singleShot(0, this, SLOT(logout()));
116 } else if (m_logoutAction
== "savesession") {
117 // decouple dbus call, otherwise we'll run into a dead-lock
118 QTimer::singleShot(0, this, SLOT(saveSession()));
120 } else if (m_logoutAction
== "standby") {
121 // decouple dbus call, otherwise we'll run into a dead-lock
122 QTimer::singleShot(0, this, SLOT(standby()));
124 } else if (m_logoutAction
== "suspendram") {
125 // decouple dbus call, otherwise we'll run into a dead-lock
126 QTimer::singleShot(0, this, SLOT(suspendRAM()));
128 } else if (m_logoutAction
== "suspenddisk") {
129 // decouple dbus call, otherwise we'll run into a dead-lock
130 QTimer::singleShot(0, this, SLOT(suspendDisk()));
132 } else if (m_logoutAction
== "run") {
133 // decouple dbus call, otherwise we'll run into a dead-lock
134 QTimer::singleShot(0, this, SLOT(runCommand()));
141 void LeaveItemHandler::runCommand()
143 QString
interface("org.kde.krunner");
144 org::kde::krunner::App
krunner(interface
, "/App", QDBusConnection::sessionBus());
148 void LeaveItemHandler::logout()
150 KWorkSpace::ShutdownConfirm confirm
= KWorkSpace::ShutdownConfirmDefault
;
151 KWorkSpace::ShutdownType type
= KWorkSpace::ShutdownTypeNone
;
153 if (m_logoutAction
== "logout") {
154 type
= KWorkSpace::ShutdownTypeNone
;
155 } else if (m_logoutAction
== "logoutonly") {
156 type
= KWorkSpace::ShutdownTypeLogout
;
157 } else if (m_logoutAction
== "lock") {
158 kDebug() << "Locking screen";
159 } else if (m_logoutAction
== "switch") {
160 kDebug() << "Switching user";
161 } else if (m_logoutAction
== "restart") {
162 type
= KWorkSpace::ShutdownTypeReboot
;
163 } else if (m_logoutAction
== "shutdown") {
164 type
= KWorkSpace::ShutdownTypeHalt
;
167 //FIXME: the proper fix is to implement the KWorkSpace methods for Windows
169 KWorkSpace::requestShutDown(confirm
, type
);
173 void LeaveItemHandler::lock()
175 QString
interface("org.freedesktop.ScreenSaver");
176 org::freedesktop::ScreenSaver
screensaver(interface
, "/ScreenSaver",
177 QDBusConnection::sessionBus());
181 void LeaveItemHandler::switchUser()
183 QString
interface("org.kde.krunner");
184 org::kde::krunner::App
krunner(interface
, "/App", QDBusConnection::sessionBus());
185 krunner
.switchUser();
188 void LeaveItemHandler::saveSession()
190 QString
interface("org.kde.ksmserver");
192 org::kde::KSMServerInterface
ksmserver(interface
, "/KSMServer",
193 QDBusConnection::sessionBus());
194 if (ksmserver
.isValid()) {
195 ksmserver
.saveCurrentSession();
199 void LeaveItemHandler::standby()
201 Solid::Control::PowerManager::SuspendMethod spdMethod
= Solid::Control::PowerManager::Standby
;
202 KJob
*job
= Solid::Control::PowerManager::suspend(spdMethod
);
207 void LeaveItemHandler::suspendRAM()
209 Solid::Control::PowerManager::SuspendMethod spdMethod
= Solid::Control::PowerManager::ToRam
;
210 KJob
*job
= Solid::Control::PowerManager::suspend(spdMethod
);
215 void LeaveItemHandler::suspendDisk()
217 Solid::Control::PowerManager::SuspendMethod spdMethod
= Solid::Control::PowerManager::ToDisk
;
218 KJob
*job
= Solid::Control::PowerManager::suspend(spdMethod
);