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"
27
#include <KAuthorized>
31
#include <KToolInvocation>
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"
44
#include <QtDBus/QDBusConnection>
45
#include <QtDBus/QDBusConnectionInterface>
47
using namespace Kickoff;
49
bool ServiceItemHandler::openUrl(const KUrl& url)
51
int result = KToolInvocation::startServiceByDesktopPath(url.pathOrUrl(), QStringList(), 0, 0, 0, "", true);
54
KService::Ptr service = KService::serviceByDesktopPath(url.pathOrUrl());
56
if (!service.isNull()) {
57
RecentApplications::self()->add(service);
59
qWarning() << "Failed to find service for" << url;
67
bool LeaveItemHandler::openUrl(const KUrl& url)
69
m_logoutAction = url.path().remove('/');
71
if (m_logoutAction == "sleep") {
72
// Check if KDE Power Management System is running, and use its methods to suspend if available
73
if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.Solid.PowerManagement")) {
74
kDebug() << "Using KDE Power Management System to suspend";
75
QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
76
"/org/kde/Solid/PowerManagement",
77
"org.kde.Solid.PowerManagement",
79
QDBusConnection::sessionBus().asyncCall(call);
82
kDebug() << "KDE Power Management System not available, suspend failed";
85
} else if (m_logoutAction == "hibernate") {
86
// Check if KDE Power Management System is running, and use its methods to hibernate if available
87
if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.Solid.PowerManagement")) {
88
kDebug() << "Using KDE Power Management System to hibernate";
89
QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
90
"/org/kde/Solid/PowerManagement",
91
"org.kde.Solid.PowerManagement",
93
QDBusConnection::sessionBus().asyncCall(call);
96
kDebug() << "KDE Power Management System not available, hibernate failed";
99
} else if (m_logoutAction == "lock") {
100
// decouple dbus call, otherwise we'll run into a dead-lock
101
QTimer::singleShot(0, this, SLOT(lock()));
103
} else if (m_logoutAction == "switch") {
104
// decouple dbus call, otherwise we'll run into a dead-lock
105
QTimer::singleShot(0, this, SLOT(switchUser()));
107
} else if (m_logoutAction == "logout" || m_logoutAction == "logoutonly" ||
108
m_logoutAction == "restart" || m_logoutAction == "shutdown") {
109
// decouple dbus call, otherwise we'll run into a dead-lock
110
QTimer::singleShot(0, this, SLOT(logout()));
112
} else if (m_logoutAction == "savesession") {
113
// decouple dbus call, otherwise we'll run into a dead-lock
114
QTimer::singleShot(0, this, SLOT(saveSession()));
116
} else if (m_logoutAction == "standby") {
117
// decouple dbus call, otherwise we'll run into a dead-lock
118
QTimer::singleShot(0, this, SLOT(standby()));
120
} else if (m_logoutAction == "suspendram") {
121
// decouple dbus call, otherwise we'll run into a dead-lock
122
QTimer::singleShot(0, this, SLOT(suspendRAM()));
124
} else if (m_logoutAction == "suspenddisk") {
125
// decouple dbus call, otherwise we'll run into a dead-lock
126
QTimer::singleShot(0, this, SLOT(suspendDisk()));
128
} else if (m_logoutAction == "run") {
129
// decouple dbus call, otherwise we'll run into a dead-lock
130
QTimer::singleShot(0, this, SLOT(runCommand()));
137
void LeaveItemHandler::runCommand()
139
if (KAuthorized::authorize("run_command")) {
140
QString interface("org.kde.krunner");
141
org::kde::krunner::App krunner(interface, "/App", QDBusConnection::sessionBus());
146
void LeaveItemHandler::logout()
148
KWorkSpace::ShutdownConfirm confirm = KWorkSpace::ShutdownConfirmDefault;
149
KWorkSpace::ShutdownType type = KWorkSpace::ShutdownTypeNone;
151
if (m_logoutAction == "logout" || m_logoutAction == "logoutonly") {
152
type = KWorkSpace::ShutdownTypeNone;
153
} else if (m_logoutAction == "lock") {
154
kDebug() << "Locking screen";
155
} else if (m_logoutAction == "switch") {
156
kDebug() << "Switching user";
157
} else if (m_logoutAction == "restart") {
158
type = KWorkSpace::ShutdownTypeReboot;
159
} else if (m_logoutAction == "shutdown") {
160
type = KWorkSpace::ShutdownTypeHalt;
163
//FIXME: the proper fix is to implement the KWorkSpace methods for Windows
165
KWorkSpace::requestShutDown(confirm, type);
169
void LeaveItemHandler::lock()
171
QString interface("org.freedesktop.ScreenSaver");
172
org::freedesktop::ScreenSaver screensaver(interface, "/ScreenSaver", QDBusConnection::sessionBus());
176
void LeaveItemHandler::switchUser()
178
QString interface("org.kde.krunner");
179
org::kde::krunner::App krunner(interface, "/App", QDBusConnection::sessionBus());
180
krunner.switchUser();
183
void LeaveItemHandler::saveSession()
185
QString interface("org.kde.ksmserver");
187
org::kde::KSMServerInterface ksmserver(interface, "/KSMServer", QDBusConnection::sessionBus());
188
if (ksmserver.isValid()) {
189
ksmserver.saveCurrentSession();
193
void LeaveItemHandler::standby()
195
// FIXME: Use standby from KDE Power Management System's interface
199
void LeaveItemHandler::suspendRAM()
201
QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
202
"/org/kde/Solid/PowerManagement",
203
"org.kde.Solid.PowerManagement",
205
QDBusConnection::sessionBus().asyncCall(call);
208
void LeaveItemHandler::suspendDisk()
210
QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
211
"/org/kde/Solid/PowerManagement",
212
"org.kde.Solid.PowerManagement",
214
QDBusConnection::sessionBus().asyncCall(call);