~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/desktop/applets/kickoff/core/itemhandlers.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright 2007 Robert Knight <robertknight@gmail.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
18
*/
 
19
 
 
20
// Own
 
21
#include "core/itemhandlers.h"
 
22
 
 
23
// Qt
 
24
#include <QTimer>
 
25
 
 
26
// KDE
 
27
#include <KAuthorized>
 
28
#include <KDebug>
 
29
#include <KJob>
 
30
#include <KService>
 
31
#include <KToolInvocation>
 
32
#include <KUrl>
 
33
 
 
34
// KDE Base
 
35
#include <kworkspace/kworkspace.h>
 
36
 
 
37
// Local
 
38
#include "core/recentapplications.h"
 
39
 
 
40
// DBus
 
41
#include "krunner_interface.h"
 
42
#include "screensaver_interface.h"
 
43
#include "ksmserver_interface.h"
 
44
#include <QtDBus/QDBusConnection>
 
45
#include <QtDBus/QDBusConnectionInterface>
 
46
 
 
47
using namespace Kickoff;
 
48
 
 
49
bool ServiceItemHandler::openUrl(const KUrl& url)
 
50
{
 
51
    int result = KToolInvocation::startServiceByDesktopPath(url.pathOrUrl(), QStringList(), 0, 0, 0, "", true);
 
52
 
 
53
    if (result == 0) {
 
54
        KService::Ptr service = KService::serviceByDesktopPath(url.pathOrUrl());
 
55
 
 
56
        if (!service.isNull()) {
 
57
            RecentApplications::self()->add(service);
 
58
        } else {
 
59
            qWarning() << "Failed to find service for" << url;
 
60
            return false;
 
61
        }
 
62
    }
 
63
 
 
64
    return result == 0;
 
65
}
 
66
 
 
67
bool LeaveItemHandler::openUrl(const KUrl& url)
 
68
{
 
69
    m_logoutAction = url.path().remove('/');
 
70
 
 
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",
 
78
                                                               "suspendToRam");
 
79
            QDBusConnection::sessionBus().asyncCall(call);
 
80
            return true;
 
81
        } else {
 
82
            kDebug() << "KDE Power Management System not available, suspend failed";
 
83
            return false;
 
84
        }
 
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",
 
92
                                                               "suspendToDisk");
 
93
            QDBusConnection::sessionBus().asyncCall(call);
 
94
            return true;
 
95
        } else {
 
96
            kDebug() << "KDE Power Management System not available, hibernate failed";
 
97
            return false;
 
98
        }
 
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()));
 
102
        return true;
 
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()));
 
106
        return true;
 
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()));
 
111
        return true;
 
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()));
 
115
        return true;
 
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()));
 
119
        return true;
 
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()));
 
123
        return true;
 
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()));
 
127
        return true;
 
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()));
 
131
        return true;
 
132
    }
 
133
 
 
134
    return false;
 
135
}
 
136
 
 
137
void LeaveItemHandler::runCommand()
 
138
{
 
139
    if (KAuthorized::authorize("run_command")) {
 
140
        QString interface("org.kde.krunner");
 
141
        org::kde::krunner::App krunner(interface, "/App", QDBusConnection::sessionBus());
 
142
        krunner.display();
 
143
    }
 
144
}
 
145
 
 
146
void LeaveItemHandler::logout()
 
147
{
 
148
    KWorkSpace::ShutdownConfirm confirm = KWorkSpace::ShutdownConfirmDefault;
 
149
    KWorkSpace::ShutdownType type = KWorkSpace::ShutdownTypeNone;
 
150
 
 
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;
 
161
    }
 
162
 
 
163
//FIXME: the proper fix is to implement the KWorkSpace methods for Windows
 
164
#ifndef Q_WS_WIN
 
165
    KWorkSpace::requestShutDown(confirm, type);
 
166
#endif
 
167
}
 
168
 
 
169
void LeaveItemHandler::lock()
 
170
{
 
171
    QString interface("org.freedesktop.ScreenSaver");
 
172
    org::freedesktop::ScreenSaver screensaver(interface, "/ScreenSaver", QDBusConnection::sessionBus());
 
173
    screensaver.Lock();
 
174
}
 
175
 
 
176
void LeaveItemHandler::switchUser()
 
177
{
 
178
    QString interface("org.kde.krunner");
 
179
    org::kde::krunner::App krunner(interface, "/App", QDBusConnection::sessionBus());
 
180
    krunner.switchUser();
 
181
}
 
182
 
 
183
void LeaveItemHandler::saveSession()
 
184
{
 
185
    QString interface("org.kde.ksmserver");
 
186
 
 
187
    org::kde::KSMServerInterface ksmserver(interface, "/KSMServer", QDBusConnection::sessionBus());
 
188
    if (ksmserver.isValid()) {
 
189
        ksmserver.saveCurrentSession();
 
190
    }
 
191
}
 
192
 
 
193
void LeaveItemHandler::standby()
 
194
{
 
195
    // FIXME: Use standby from KDE Power Management System's interface
 
196
    suspendRAM();
 
197
}
 
198
 
 
199
void LeaveItemHandler::suspendRAM()
 
200
{
 
201
    QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
 
202
                                                       "/org/kde/Solid/PowerManagement",
 
203
                                                       "org.kde.Solid.PowerManagement",
 
204
                                                       "suspendToRam");
 
205
    QDBusConnection::sessionBus().asyncCall(call);
 
206
}
 
207
 
 
208
void LeaveItemHandler::suspendDisk()
 
209
{
 
210
    QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
 
211
                                                       "/org/kde/Solid/PowerManagement",
 
212
                                                       "org.kde.Solid.PowerManagement",
 
213
                                                       "suspendToDisk");
 
214
    QDBusConnection::sessionBus().asyncCall(call);
 
215
}