Plasma
sessionrunner.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00102
00103
00104 bool listAll = (term == "SESSIONS");
00105
00106 if (!listAll) {
00107
00108 if (term.startsWith(i18nc("switch user command","switch"), Qt::CaseInsensitive)) {
00109
00110
00111
00112
00113
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
00120 if (matchUser && user.isEmpty()) {
00121 return;
00122 }
00123 } else {
00124
00125
00126 matchCommands(matches, term);
00127 }
00128 }
00129
00130
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
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
00167
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
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"