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

Plasma

sessionrunner.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License version 2 as
00006  *   published by the Free Software Foundation
00007  *
00008  *   This program is distributed in the hope that it will be useful,
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *   GNU General Public License for more details
00012  *
00013  *   You should have received a copy of the GNU Library General Public
00014  *   License along with this program; if not, write to the
00015  *   Free Software Foundation, Inc.,
00016  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00017  */
00018 
00019 #include "sessionrunner.h"
00020 
00021 #include <QWidget>
00022 #include <QAction>
00023 
00024 #include <KAction>
00025 #include <KActionCollection>
00026 #include <KAuthorized>
00027 #include <KDebug>
00028 #include <KLocale>
00029 #include <KMessageBox>
00030 
00031 #include "kworkspace/kworkspace.h"
00032 
00033 #include "screensaver_interface.h"
00034 
00035 SessionRunner::SessionRunner(QObject *parent, const QVariantList &args)
00036     : Plasma::AbstractRunner(parent, args)
00037 {
00038     KGlobal::locale()->insertCatalog("krunner_sessions");
00039     Q_UNUSED(args)
00040 
00041     setObjectName(i18n("Sessions"));
00042     setPriority(LowPriority);
00043     setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File | 
00044                     Plasma::RunnerContext::NetworkLocation);
00045 }
00046 
00047 SessionRunner::~SessionRunner()
00048 {
00049 }
00050 
00051 void SessionRunner::matchCommands(QList<Plasma::QueryMatch> &matches, const QString& term)
00052 {
00053       if (term.compare(i18nc("log out command","logout"), Qt::CaseInsensitive) == 0 ||
00054           term.compare(i18n("log out"), Qt::CaseInsensitive) == 0) {
00055           Plasma::QueryMatch match(this);
00056           match.setText(i18nc("log out command","Logout"));
00057           match.setIcon(KIcon("system-log-out"));
00058           match.setData(LogoutAction);
00059           match.setType(Plasma::QueryMatch::ExactMatch);
00060           match.setRelevance(0.9);
00061           matches << match;
00062       } else if (term.compare(i18nc("restart computer command", "restart"), Qt::CaseInsensitive) == 0 ||
00063                  term.compare(i18nc("restart computer command", "reboot"), Qt::CaseInsensitive) == 0) {
00064           Plasma::QueryMatch match(this);
00065           match.setText(i18n("Restart the computer"));
00066           match.setIcon(KIcon("system-restart"));
00067           match.setData(RestartAction);
00068           match.setType(Plasma::QueryMatch::ExactMatch);
00069           match.setRelevance(0.9);
00070           matches << match;
00071       } else if (term.compare(i18nc("shutdown computer command","shutdown"), Qt::CaseInsensitive) == 0) {
00072           Plasma::QueryMatch match(this);
00073           match.setText(i18n("Shutdown the computer"));
00074           match.setIcon(KIcon("system-shutdown"));
00075           match.setData(ShutdownAction);
00076           match.setType(Plasma::QueryMatch::ExactMatch);
00077           match.setRelevance(0.9);
00078           matches << match;
00079       } else if (term.compare(i18nc("lock screen command","lock"), Qt::CaseInsensitive) == 0) {
00080           Plasma::QueryMatch match(this);
00081           match.setText(i18n("Lock the screen"));
00082           match.setIcon(KIcon("system-lock-screen"));
00083           match.setData(LockAction);
00084           match.setType(Plasma::QueryMatch::ExactMatch);
00085           match.setRelevance(0.9);
00086           matches << match;
00087       }
00088 }
00089 
00090 void SessionRunner::match(Plasma::RunnerContext &context)
00091 {
00092     const QString term = context.query();
00093     QString user;
00094     bool matchUser = false;
00095 
00096     QList<Plasma::QueryMatch> matches;
00097 
00098     if (term.size() < 3) {
00099         return;
00100     }
00101     // first compare with SESSIONS. this must *NOT* be translated (i18n)
00102     // as it is used as an internal command trigger (e.g. via d-bus),
00103     // not as a user supplied query. and yes, "Ugh, magic strings"
00104     bool listAll = (term == "SESSIONS");
00105 
00106     if (!listAll) {
00107         //no luck, try switch user command
00108         if (term.startsWith(i18nc("switch user command","switch"), Qt::CaseInsensitive)) {
00109             // interestingly, this means that if one wants to switch to a
00110             // session named "switch", they'd have to enter
00111             // switch switch. ha!
00112 
00113             // we don't know the size of 'switch' translated to your language, do we?
00114             QStringList words = term.split(" ");
00115             int switchCmdSize = words.at(0).size();
00116 
00117             user = term.right(term.size() - switchCmdSize).trimmed();
00118             matchUser = true;
00119             // can't match anything below, since it's just "switch"
00120            if (matchUser && user.isEmpty()) {       
00121                 return;
00122            }
00123         } else {
00124             // we know it's not SESSION or "switch <something>", so let's
00125             // try some other possibilities
00126             matchCommands(matches, term);
00127         }
00128     }
00129 
00130     //kDebug() << "session switching to" << (listAll ? "all sessions" : term);
00131 
00132     bool switchUser = listAll ||
00133                       term.compare(i18n("switch user"), Qt::CaseInsensitive) == 0 ||
00134                       term.compare(i18n("new session"), Qt::CaseInsensitive) == 0;
00135 
00136     if (switchUser &&
00137         KAuthorized::authorizeKAction("start_new_session") &&
00138         dm.isSwitchable() &&
00139         dm.numReserve() >= 0) {
00140         Plasma::QueryMatch match(this);
00141         match.setType(Plasma::QueryMatch::ExactMatch);
00142         match.setIcon(KIcon("system-switch-user"));
00143         match.setText(i18n("New Session"));
00144         matches << match;
00145     }
00146 
00147     // now add the active sessions
00148     if (listAll || matchUser) {
00149         SessList sessions;
00150         dm.localSessions(sessions);
00151 
00152         foreach (const SessEnt& session, sessions) {
00153             if (!session.vt || session.self) {
00154                 continue;
00155             }
00156 
00157             QString name = KDisplayManager::sess2Str(session);
00158             Plasma::QueryMatch::Type type = Plasma::QueryMatch::NoMatch;
00159             qreal relevance = 0.7;
00160 
00161             if (listAll) {
00162                 type = Plasma::QueryMatch::ExactMatch;
00163                 relevance = 1;
00164             } else if (matchUser) {
00165                 if (name.compare(user, Qt::CaseInsensitive) == 0) {
00166                     // we need an elif branch here because we don't
00167                     // want the last conditional to be checked if !listAll
00168                     type = Plasma::QueryMatch::ExactMatch;
00169                     relevance = 1;
00170                 } else if (name.contains(user, Qt::CaseInsensitive)) {
00171                     type = Plasma::QueryMatch::PossibleMatch;
00172                 }
00173             }
00174 
00175             if (type != Plasma::QueryMatch::NoMatch) {
00176                 Plasma::QueryMatch match(this);
00177                 match.setType(type);
00178                 match.setRelevance(relevance);
00179                 match.setIcon(KIcon("user-identity"));
00180                 match.setText(name);
00181                 match.setData(session.session);
00182                 matches << match;
00183             }
00184         }
00185     }
00186 
00187     context.addMatches(term, matches);
00188 }
00189 
00190 void SessionRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
00191 {
00192     Q_UNUSED(context);
00193     if (match.data().type() == QVariant::Int) {
00194         KWorkSpace::ShutdownType type = KWorkSpace::ShutdownTypeDefault;
00195 
00196         switch (match.data().toInt()) {
00197             case LogoutAction:
00198                 type = KWorkSpace::ShutdownTypeNone;
00199                 break;
00200             case RestartAction:
00201                 type = KWorkSpace::ShutdownTypeReboot;
00202                 break;
00203             case ShutdownAction:
00204                 type = KWorkSpace::ShutdownTypeHalt;
00205                 break;
00206             case LockAction:
00207                 lock();
00208                 return;
00209                 break;
00210         }
00211 
00212         if (type != KWorkSpace::ShutdownTypeDefault) {
00213             KWorkSpace::ShutdownConfirm confirm = KWorkSpace::ShutdownConfirmDefault;
00214             KWorkSpace::requestShutDown(confirm, type);
00215             return;
00216         }
00217     }
00218 
00219     if (!match.data().toString().isEmpty()) {
00220         QString sessionName = match.text();
00221 
00222         SessList sessions;
00223         if (dm.localSessions(sessions)) {
00224             foreach (const SessEnt &session, sessions) {
00225                 if (sessionName == KDisplayManager::sess2Str(session)) {
00226                     dm.lockSwitchVT(session.vt);
00227                     break;
00228                 }
00229             }
00230         }
00231 
00232         return;
00233     }
00234 
00235     //TODO: this message is too verbose and too technical.
00236     int result = KMessageBox::warningContinueCancel(
00237             0,
00238             i18n("<p>You have chosen to open another desktop session.<br />"
00239                 "The current session will be hidden "
00240                 "and a new login screen will be displayed.<br />"
00241                 "An F-key is assigned to each session; "
00242                 "F%1 is usually assigned to the first session, "
00243                 "F%2 to the second session and so on. "
00244                 "You can switch between sessions by pressing "
00245                 "Ctrl, Alt and the appropriate F-key at the same time. "
00246                 "Additionally, the KDE Panel and Desktop menus have "
00247                 "actions for switching between sessions.</p>",
00248                 7, 8),
00249             i18n("Warning - New Session"),
00250             KGuiItem(i18n("&Start New Session"), "fork"),
00251             KStandardGuiItem::cancel(),
00252             ":confirmNewSession",
00253             KMessageBox::PlainCaption | KMessageBox::Notify);
00254 
00255     if (result == KMessageBox::Cancel) {
00256         return;
00257     }
00258 
00259     lock();
00260     dm.startReserve();
00261 }
00262 
00263 void SessionRunner::lock()
00264 {
00265     QString interface("org.freedesktop.ScreenSaver");
00266     org::freedesktop::ScreenSaver screensaver(interface, "/ScreenSaver",
00267                                               QDBusConnection::sessionBus());
00268     if (screensaver.isValid()) {
00269         screensaver.Lock();
00270     }
00271 }
00272 
00273 #include "sessionrunner.moc"

Plasma

Skip menu "Plasma"
  • 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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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