add more spacing
[personal-kdebase.git] / workspace / plasma / applets / kickoff / core / itemhandlers.cpp
blob3b9aa3a9430b97ec054dae410aa39e3cd99f8754
1 /*
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.
20 // Own
21 #include "core/itemhandlers.h"
23 // Qt
24 #include <QTimer>
26 // KDE
27 #include <KDebug>
28 #include <KJob>
29 #include <KService>
30 #include <KToolInvocation>
31 #include <KUrl>
32 #include <solid/control/powermanager.h>
34 // KDE Base
35 #include <kworkspace/kworkspace.h>
37 // Local
38 #include "core/recentapplications.h"
40 // DBus
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);
51 if (result == 0) {
52 KService::Ptr service = KService::serviceByDesktopPath(url.pathOrUrl());
54 if (!service.isNull()) {
55 RecentApplications::self()->add(service);
56 } else {
57 qWarning() << "Failed to find service for" << url;
58 return false;
62 return result == 0;
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
72 QStringList modules;
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);
80 } else {
81 kDebug() << "Powerdevil not available, using solid to suspend";
82 KJob * job = Solid::Control::PowerManager::suspend(Solid::Control::PowerManager::ToRam);
83 job->start();
85 return true;
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
89 QStringList modules;
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);
97 } else {
98 kDebug() << "Powerdevil not available, using solid to hibernate";
99 KJob * job = Solid::Control::PowerManager::suspend(Solid::Control::PowerManager::ToDisk);
100 job->start();
102 return true;
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()));
106 return true;
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()));
110 return true;
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()));
115 return true;
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()));
119 return true;
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()));
123 return true;
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()));
127 return true;
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()));
131 return true;
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()));
135 return true;
138 return false;
141 void LeaveItemHandler::runCommand()
143 QString interface("org.kde.krunner");
144 org::kde::krunner::App krunner(interface, "/App", QDBusConnection::sessionBus());
145 krunner.display();
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
168 #ifndef Q_WS_WIN
169 KWorkSpace::requestShutDown(confirm, type);
170 #endif
173 void LeaveItemHandler::lock()
175 QString interface("org.freedesktop.ScreenSaver");
176 org::freedesktop::ScreenSaver screensaver(interface, "/ScreenSaver",
177 QDBusConnection::sessionBus());
178 screensaver.Lock();
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);
203 if (job != 0)
204 job->start();
207 void LeaveItemHandler::suspendRAM()
209 Solid::Control::PowerManager::SuspendMethod spdMethod = Solid::Control::PowerManager::ToRam;
210 KJob *job = Solid::Control::PowerManager::suspend(spdMethod);
211 if (job != 0)
212 job->start();
215 void LeaveItemHandler::suspendDisk()
217 Solid::Control::PowerManager::SuspendMethod spdMethod = Solid::Control::PowerManager::ToDisk;
218 KJob *job = Solid::Control::PowerManager::suspend(spdMethod);
219 if (job != 0)
220 job->start();