• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Applets

itemhandlers.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2007 Robert Knight <robertknight@gmail.com>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 // Own
00021 #include "core/itemhandlers.h"
00022 
00023 // Qt
00024 #include <QTimer>
00025 
00026 // KDE
00027 #include <KDebug>
00028 #include <KJob>
00029 #include <KService>
00030 #include <KToolInvocation>
00031 #include <KUrl>
00032 #include <solid/control/powermanager.h>
00033 
00034 // KDE Base
00035 #include <kworkspace/kworkspace.h>
00036 
00037 // Local
00038 #include "core/recentapplications.h"
00039 
00040 // DBus
00041 #include "krunner_interface.h"
00042 #include "screensaver_interface.h"
00043 #include "ksmserver_interface.h"
00044 
00045 using namespace Kickoff;
00046 
00047 bool ServiceItemHandler::openUrl(const KUrl& url)
00048 {
00049     int result = KToolInvocation::startServiceByDesktopPath(url.pathOrUrl(), QStringList(), 0, 0, 0, "", true);
00050 
00051     if (result == 0) {
00052         KService::Ptr service = KService::serviceByDesktopPath(url.pathOrUrl());
00053 
00054         if (!service.isNull()) {
00055             RecentApplications::self()->add(service);
00056         } else {
00057             qWarning() << "Failed to find service for" << url;
00058             return false;
00059         }
00060     }
00061 
00062     return result == 0;
00063 }
00064 
00065 bool LeaveItemHandler::openUrl(const KUrl& url)
00066 {
00067     m_logoutAction = url.path().remove('/');
00068 
00069     if (m_logoutAction == "sleep") {
00070         // Check if powerdevil is running, and use its methods to suspend if available
00071         // otherwise go through Solid directly
00072         QStringList modules;
00073         QDBusInterface kdedInterface("org.kde.kded", "/kded", "org.kde.kded");
00074         QDBusReply<QStringList> reply = kdedInterface.call("loadedModules");
00075         if (reply.isValid() && reply.value().contains("powerdevil")) {
00076             kDebug() << "Using powerdevil to suspend";
00077             QDBusConnection dbus(QDBusConnection::sessionBus());
00078             QDBusInterface iface("org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil", dbus);
00079             iface.call("suspend", Solid::Control::PowerManager::ToRam);
00080         } else {
00081             kDebug() << "Powerdevil not available, using solid to suspend";
00082             KJob * job = Solid::Control::PowerManager::suspend(Solid::Control::PowerManager::ToRam);
00083             job->start();
00084         }
00085         return true;
00086     } else if (m_logoutAction == "hibernate") {
00087         // Check if powerdevil is running, and use its methods to hibernate if available
00088         // otherwise go through Solid directly
00089         QStringList modules;
00090         QDBusInterface kdedInterface("org.kde.kded", "/kded", "org.kde.kded");
00091         QDBusReply<QStringList> reply = kdedInterface.call("loadedModules");
00092         if (reply.isValid() && reply.value().contains("powerdevil")) {
00093             kDebug() << "Using powerdevil to hibernate";
00094             QDBusConnection dbus(QDBusConnection::sessionBus());
00095             QDBusInterface iface("org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil", dbus);
00096             iface.call("suspend", Solid::Control::PowerManager::ToDisk);
00097         } else {
00098             kDebug() << "Powerdevil not available, using solid to hibernate";
00099             KJob * job = Solid::Control::PowerManager::suspend(Solid::Control::PowerManager::ToDisk);
00100             job->start();
00101         }
00102         return true;
00103     } else if (m_logoutAction == "lock") {
00104         // decouple dbus call, otherwise we'll run into a dead-lock
00105         QTimer::singleShot(0, this, SLOT(lock()));
00106         return true;
00107     } else if (m_logoutAction == "switch") {
00108         // decouple dbus call, otherwise we'll run into a dead-lock
00109         QTimer::singleShot(0, this, SLOT(switchUser()));
00110         return true;
00111     } else if (m_logoutAction == "logout" || m_logoutAction == "logoutonly" ||
00112                m_logoutAction == "restart" || m_logoutAction == "shutdown") {
00113         // decouple dbus call, otherwise we'll run into a dead-lock
00114         QTimer::singleShot(0, this, SLOT(logout()));
00115         return true;
00116     } else if (m_logoutAction == "savesession") {
00117         // decouple dbus call, otherwise we'll run into a dead-lock
00118         QTimer::singleShot(0, this, SLOT(saveSession()));
00119         return true;
00120     } else if (m_logoutAction == "standby") {
00121         // decouple dbus call, otherwise we'll run into a dead-lock
00122         QTimer::singleShot(0, this, SLOT(standby()));
00123         return true;
00124     } else if (m_logoutAction == "suspendram") {
00125         // decouple dbus call, otherwise we'll run into a dead-lock
00126         QTimer::singleShot(0, this, SLOT(suspendRAM()));
00127         return true;
00128     } else if (m_logoutAction == "suspenddisk") {
00129         // decouple dbus call, otherwise we'll run into a dead-lock
00130         QTimer::singleShot(0, this, SLOT(suspendDisk()));
00131         return true;
00132     } else if (m_logoutAction == "run") {
00133         // decouple dbus call, otherwise we'll run into a dead-lock
00134         QTimer::singleShot(0, this, SLOT(runCommand()));
00135         return true;
00136     }
00137 
00138     return false;
00139 }
00140 
00141 void LeaveItemHandler::runCommand()
00142 {
00143     QString interface("org.kde.krunner");
00144     org::kde::krunner::App krunner(interface, "/App", QDBusConnection::sessionBus());
00145     krunner.display();
00146 }
00147 
00148 void LeaveItemHandler::logout()
00149 {
00150     KWorkSpace::ShutdownConfirm confirm = KWorkSpace::ShutdownConfirmDefault;
00151     KWorkSpace::ShutdownType type = KWorkSpace::ShutdownTypeNone;
00152 
00153     if (m_logoutAction == "logout") {
00154         type = KWorkSpace::ShutdownTypeNone;
00155     } else if (m_logoutAction == "logoutonly") {
00156         type = KWorkSpace::ShutdownTypeLogout;
00157     } else if (m_logoutAction == "lock") {
00158         kDebug() << "Locking screen";
00159     } else if (m_logoutAction == "switch") {
00160         kDebug() << "Switching user";
00161     } else if (m_logoutAction == "restart") {
00162         type = KWorkSpace::ShutdownTypeReboot;
00163     } else if (m_logoutAction == "shutdown") {
00164         type = KWorkSpace::ShutdownTypeHalt;
00165     }
00166 
00167 //FIXME: the proper fix is to implement the KWorkSpace methods for Windows
00168 #ifndef Q_WS_WIN
00169     KWorkSpace::requestShutDown(confirm, type);
00170 #endif
00171 }
00172 
00173 void LeaveItemHandler::lock()
00174 {
00175     QString interface("org.freedesktop.ScreenSaver");
00176     org::freedesktop::ScreenSaver screensaver(interface, "/ScreenSaver",
00177             QDBusConnection::sessionBus());
00178     screensaver.Lock();
00179 }
00180 
00181 void LeaveItemHandler::switchUser()
00182 {
00183     QString interface("org.kde.krunner");
00184     org::kde::krunner::App krunner(interface, "/App", QDBusConnection::sessionBus());
00185     krunner.switchUser();
00186 }
00187 
00188 void LeaveItemHandler::saveSession()
00189 {
00190     QString interface("org.kde.ksmserver");
00191 
00192     org::kde::KSMServerInterface ksmserver(interface, "/KSMServer",
00193                                            QDBusConnection::sessionBus());
00194     if (ksmserver.isValid()) {
00195         ksmserver.saveCurrentSession();
00196     }
00197 }
00198 
00199 void LeaveItemHandler::standby()
00200 {
00201     Solid::Control::PowerManager::SuspendMethod spdMethod = Solid::Control::PowerManager::Standby;
00202     KJob *job = Solid::Control::PowerManager::suspend(spdMethod);
00203     if (job != 0)
00204         job->start();
00205 }
00206 
00207 void LeaveItemHandler::suspendRAM()
00208 {
00209     Solid::Control::PowerManager::SuspendMethod spdMethod = Solid::Control::PowerManager::ToRam;
00210     KJob *job = Solid::Control::PowerManager::suspend(spdMethod);
00211     if (job != 0)
00212         job->start();
00213 }
00214 
00215 void LeaveItemHandler::suspendDisk()
00216 {
00217     Solid::Control::PowerManager::SuspendMethod spdMethod = Solid::Control::PowerManager::ToDisk;
00218     KJob *job = Solid::Control::PowerManager::suspend(spdMethod);
00219     if (job != 0)
00220         job->start();
00221 }

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal