1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "sessionrunner.h"
#include <KAuthorized>
#include <QDebug>
#include <KLocalizedString>
#include <QMessageBox>
#include "kworkspace.h"
#include "screensaver_interface.h"
K_EXPORT_PLASMA_RUNNER(calculatorrunner, SessionRunner)
SessionRunner::SessionRunner(QObject *parent, const QVariantList &args)
: Plasma::AbstractRunner(parent, args)
{
setObjectName( QLatin1String("Sessions" ));
setPriority(LowPriority);
setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File |
Plasma::RunnerContext::NetworkLocation);
m_canLogout = KAuthorized::authorizeAction(QStringLiteral("logout")) && KAuthorized::authorize(QStringLiteral("logout"));
if (m_canLogout) {
addSyntax(Plasma::RunnerSyntax(i18nc("log out command", "logout"),
i18n("Logs out, exiting the current desktop session")));
addSyntax(Plasma::RunnerSyntax(i18nc("shutdown computer command", "shutdown"),
i18n("Turns off the computer")));
}
if (KAuthorized::authorizeAction(QStringLiteral("lock_screen")) && m_canLogout) {
addSyntax(Plasma::RunnerSyntax(i18nc("lock screen command", "lock"),
i18n("Locks the current sessions and starts the screen saver")));
}
Plasma::RunnerSyntax rebootSyntax(i18nc("restart computer command", "restart"), i18n("Reboots the computer"));
rebootSyntax.addExampleQuery(i18nc("restart computer command", "reboot"));
addSyntax(rebootSyntax);
m_triggerWord = i18nc("switch user command", "switch");
addSyntax(Plasma::RunnerSyntax(i18nc("switch user command", "switch :q:"),
i18n("Switches to the active session for the user :q:, "
"or lists all active sessions if :q: is not provided")));
Plasma::RunnerSyntax fastUserSwitchSyntax(i18n("switch user"),
i18n("Starts a new session as a different user"));
fastUserSwitchSyntax.addExampleQuery(i18n("new session"));
addSyntax(fastUserSwitchSyntax);
//"SESSIONS" should not be translated; it's used programmaticaly
setDefaultSyntax(Plasma::RunnerSyntax(QStringLiteral("SESSIONS"), i18n("Lists all sessions")));
}
SessionRunner::~SessionRunner()
{
}
void SessionRunner::matchCommands(QList<Plasma::QueryMatch> &matches, const QString& term)
{
if (!m_canLogout) {
return;
}
if (term.compare(i18nc("log out command","logout"), Qt::CaseInsensitive) == 0 ||
term.compare(i18n("log out"), Qt::CaseInsensitive) == 0) {
Plasma::QueryMatch match(this);
match.setText(i18nc("log out command","Logout"));
match.setIconName(QStringLiteral("system-log-out"));
match.setData(LogoutAction);
match.setType(Plasma::QueryMatch::ExactMatch);
match.setRelevance(0.9);
matches << match;
} else if (term.compare(i18nc("restart computer command", "restart"), Qt::CaseInsensitive) == 0 ||
term.compare(i18nc("restart computer command", "reboot"), Qt::CaseInsensitive) == 0) {
Plasma::QueryMatch match(this);
match.setText(i18n("Restart the computer"));
match.setIconName(QStringLiteral("system-reboot"));
match.setData(RestartAction);
match.setType(Plasma::QueryMatch::ExactMatch);
match.setRelevance(0.9);
matches << match;
} else if (term.compare(i18nc("shutdown computer command","shutdown"), Qt::CaseInsensitive) == 0) {
Plasma::QueryMatch match(this);
match.setText(i18n("Shutdown the computer"));
match.setIconName(QStringLiteral("system-shutdown"));
match.setData(ShutdownAction);
match.setType(Plasma::QueryMatch::ExactMatch);
match.setRelevance(0.9);
matches << match;
} else if (term.compare(i18nc("lock screen command","lock"), Qt::CaseInsensitive) == 0) {
if (KAuthorized::authorizeAction(QStringLiteral("lock_screen"))) {
Plasma::QueryMatch match(this);
match.setText(i18n("Lock the screen"));
match.setIconName(QStringLiteral("system-lock-screen"));
match.setData(LockAction);
match.setType(Plasma::QueryMatch::ExactMatch);
match.setRelevance(0.9);
matches << match;
}
}
}
void SessionRunner::match(Plasma::RunnerContext &context)
{
const QString term = context.query();
QString user;
bool matchUser = false;
QList<Plasma::QueryMatch> matches;
if (term.size() < 3) {
return;
}
// first compare with SESSIONS. this must *NOT* be translated (i18n)
// as it is used as an internal command trigger (e.g. via d-bus),
// not as a user supplied query. and yes, "Ugh, magic strings"
bool listAll = term.compare(QLatin1String("SESSIONS"), Qt::CaseInsensitive) == 0 ||
term.compare(i18nc("User sessions", "sessions"), Qt::CaseInsensitive) == 0;
if (!listAll) {
//no luck, try the "switch" user command
if (term.startsWith(m_triggerWord, Qt::CaseInsensitive)) {
user = term.right(term.size() - m_triggerWord.length()).trimmed();
listAll = user.isEmpty();
matchUser = !listAll;
} else {
// we know it's not SESSION or "switch <something>", so let's
// try some other possibilities
matchCommands(matches, term);
}
}
qDebug() << "session switching to" << (listAll ? QStringLiteral("all sessions") : term);
bool switchUser = listAll ||
term.compare(i18n("switch user"), Qt::CaseInsensitive) == 0 ||
term.compare(i18n("new session"), Qt::CaseInsensitive) == 0;
if (switchUser &&
KAuthorized::authorizeAction(QStringLiteral("start_new_session")) &&
dm.isSwitchable() &&
dm.numReserve() >= 0) {
Plasma::QueryMatch match(this);
match.setType(Plasma::QueryMatch::ExactMatch);
match.setIconName(QStringLiteral("system-switch-user"));
match.setText(i18n("New Session"));
matches << match;
}
// now add the active sessions
if (listAll || matchUser) {
SessList sessions;
dm.localSessions(sessions);
foreach (const SessEnt& session, sessions) {
if (!session.vt || session.self) {
continue;
}
QString name = KDisplayManager::sess2Str(session);
Plasma::QueryMatch::Type type = Plasma::QueryMatch::NoMatch;
qreal relevance = 0.7;
if (listAll) {
type = Plasma::QueryMatch::ExactMatch;
relevance = 1;
} else if (matchUser) {
if (name.compare(user, Qt::CaseInsensitive) == 0) {
// we need an elif branch here because we don't
// want the last conditional to be checked if !listAll
type = Plasma::QueryMatch::ExactMatch;
relevance = 1;
} else if (name.contains(user, Qt::CaseInsensitive)) {
type = Plasma::QueryMatch::PossibleMatch;
}
}
if (type != Plasma::QueryMatch::NoMatch) {
Plasma::QueryMatch match(this);
match.setType(type);
match.setRelevance(relevance);
match.setIconName(QStringLiteral("user-identity"));
match.setText(name);
match.setData(QString::number(session.vt));
matches << match;
}
}
}
context.addMatches(matches);
}
void SessionRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
Q_UNUSED(context);
if (match.data().type() == QVariant::Int) {
KWorkSpace::ShutdownType type = KWorkSpace::ShutdownTypeDefault;
switch (match.data().toInt()) {
case LogoutAction:
type = KWorkSpace::ShutdownTypeNone;
break;
case RestartAction:
type = KWorkSpace::ShutdownTypeReboot;
break;
case ShutdownAction:
type = KWorkSpace::ShutdownTypeHalt;
break;
case LockAction:
lock();
return;
break;
}
if (type != KWorkSpace::ShutdownTypeDefault) {
KWorkSpace::ShutdownConfirm confirm = KWorkSpace::ShutdownConfirmDefault;
KWorkSpace::requestShutDown(confirm, type);
return;
}
}
if (!match.data().toString().isEmpty()) {
dm.lockSwitchVT(match.data().toString().toInt());
return;
}
//TODO: this message is too verbose and too technical.
int result = QMessageBox::warning(
0,
i18n("Warning - New Session"),
i18n("<p>You have chosen to open another desktop session.<br />"
"The current session will be hidden "
"and a new login screen will be displayed.<br />"
"An F-key is assigned to each session; "
"F%1 is usually assigned to the first session, "
"F%2 to the second session and so on. "
"You can switch between sessions by pressing "
"Ctrl, Alt and the appropriate F-key at the same time. "
"Additionally, the KDE Panel and Desktop menus have "
"actions for switching between sessions.</p>",
7, 8));
if (result == QMessageBox::Cancel) {
return;
}
lock();
dm.startReserve();
}
void SessionRunner::lock()
{
QString interface(QStringLiteral("org.freedesktop.ScreenSaver"));
org::freedesktop::ScreenSaver screensaver(interface, QStringLiteral("/ScreenSaver"),
QDBusConnection::sessionBus());
if (screensaver.isValid()) {
screensaver.Lock();
}
}
#include "sessionrunner.moc"
|