00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "PowerDevilRunner.h"
00022
00023 #include <QDBusInterface>
00024 #include <QDBusReply>
00025
00026 #include <KIcon>
00027 #include <KLocale>
00028 #include <KDebug>
00029 #include <KDirWatch>
00030 #include <KStandardDirs>
00031 #include <KRun>
00032
00033 #include <solid/control/powermanager.h>
00034
00035 PowerDevilRunner::PowerDevilRunner(QObject *parent, const QVariantList &args)
00036 : Plasma::AbstractRunner(parent),
00037 m_dbus(QDBusConnection::sessionBus()),
00038 m_shortestCommand(1000)
00039 {
00040 Q_UNUSED(args)
00041
00042 setObjectName("PowerDevil");
00043 setIgnoredTypes(Plasma::RunnerContext::Directory | Plasma::RunnerContext::File |
00044 Plasma::RunnerContext::NetworkLocation | Plasma::RunnerContext::Help);
00045 updateStatus();
00046 initUpdateTriggers();
00047
00048
00049
00050
00051
00052 QStringList commands;
00053 commands << i18nc("Note this is a KRunner keyword", "power profile")
00054 << i18nc("Note this is a KRunner keyword", "power profile")
00055 << i18nc("Note this is a KRunner keyword", "suspend")
00056 << i18nc("Note this is a KRunner keyword", "sleep")
00057 << i18nc("Note this is a KRunner keyword", "hibernate")
00058 << i18nc("Note this is a KRunner keyword", "to disk")
00059 << i18nc("Note this is a KRunner keyword", "to ram")
00060 << i18nc("Note this is a KRunner keyword", "cpu policy")
00061 << i18nc("Note this is a KRunner keyword", "power governor")
00062 << i18nc("Note this is a KRunner keyword", "screen brightness")
00063 << i18nc("Note this is a KRunner keyword", "dim screen");
00064
00065 foreach (const QString &command, commands) {
00066 if (command.length() < m_shortestCommand) {
00067 m_shortestCommand = command.length();
00068 }
00069 }
00070 }
00071
00072 void PowerDevilRunner::updateSyntaxes()
00073 {
00074 QList<Plasma::RunnerSyntax> syntaxes;
00075 syntaxes.append(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "power profile"),
00076 i18n("Lists all power profiles and allows them to be activated")));
00077 syntaxes.append(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "power profile"),
00078 i18n("Lists all power saving schemes and allows them to be activated")));
00079 syntaxes.append(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "suspend"),
00080 i18n("Lists system suspend (e.g. sleep, hibernate) options "
00081 "and allows them to be activated")));
00082
00083 if (m_synonyms.contains("sleep")) {
00084 Plasma::RunnerSyntax sleepSyntax(i18nc("Note this is a KRunner keyword", "sleep"),
00085 i18n("Suspends the system to RAM"));
00086 sleepSyntax.addExampleQuery(i18nc("Note this is a KRunner keyword", "to ram"));
00087 syntaxes.append(sleepSyntax);
00088 }
00089
00090 if (m_synonyms.contains("hibernate")) {
00091 Plasma::RunnerSyntax hibernateSyntax(i18nc("Note this is a KRunner keyword", "hibernate"),
00092 i18n("Suspends the system to disk"));
00093 hibernateSyntax.addExampleQuery(i18nc("Note this is a KRunner keyword", "to disk"));
00094 syntaxes.append(hibernateSyntax);
00095 }
00096
00097 Plasma::RunnerSyntax cpuFreqSyntax(i18nc("Note this is a KRunner keyword", "cpu policy"),
00098 i18n("Lists all CPU frequency scaling policies and allows them to be activated"));
00099 cpuFreqSyntax.addExampleQuery(i18nc("Note this is a KRunner keyword", "power governor"));
00100 syntaxes.append(cpuFreqSyntax);
00101
00102 Plasma::RunnerSyntax brightnessSyntax(i18nc("Note this is a KRunner keyword", "screen brightness"),
00103
00104 i18n("Lists screen brightness options or sets it to the brightness defined by :q:; "
00105 "e.g. screen brightness 50 would dim the screen to 50% maximum brightness"));
00106 brightnessSyntax.addExampleQuery(i18nc("Note this is a KRunner keyword", "dim screen"));
00107 syntaxes.append(brightnessSyntax);
00108 setSyntaxes(syntaxes);
00109 }
00110
00111 PowerDevilRunner::~PowerDevilRunner()
00112 {
00113 }
00114
00115 void PowerDevilRunner::initUpdateTriggers()
00116 {
00117
00118
00119 KDirWatch *profilesWatch = new KDirWatch(this);
00120 profilesWatch->addFile(KStandardDirs::locate("config", "powerdevilprofilesrc"));
00121 connect(profilesWatch,SIGNAL(dirty(QString)),this,SLOT(updateStatus()));
00122 connect(profilesWatch,SIGNAL(created(QString)),this,SLOT(updateStatus()));
00123 connect(profilesWatch,SIGNAL(deleted(QString)),this,SLOT(updateStatus()));
00124
00125
00126 QStringList modules;
00127 QDBusInterface kdedInterface("org.kde.kded", "/kded", "org.kde.kded");
00128 QDBusReply<QStringList> reply = kdedInterface.call("loadedModules");
00129
00130 if (!reply.isValid()) {
00131 return;
00132 }
00133
00134 modules = reply.value();
00135
00136 if (modules.contains("powerdevil")) {
00137 if (!m_dbus.connect("org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil",
00138 "profileChanged", this, SLOT(updateStatus()))) {
00139 kDebug() << "error!";
00140 }
00141 if (!m_dbus.connect("org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil",
00142 "stateChanged", this, SLOT(updateStatus()))) {
00143 kDebug() << "error!";
00144 }
00145
00146 QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kded", "/modules/powerdevil",
00147 "org.kde.PowerDevil", "streamData");
00148 m_dbus.call(msg);
00149 }
00150 }
00151
00152 void PowerDevilRunner::updateStatus()
00153 {
00154
00155 {
00156 QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kded",
00157 "/modules/powerdevil", "org.kde.PowerDevil", "getSupportedGovernors");
00158 QDBusReply<QVariantMap> govs = m_dbus.call(msg);
00159 m_supportedGovernors = govs.value().keys();
00160 foreach(const QString &governor, m_supportedGovernors) {
00161 m_governorData[governor] = govs.value()[governor].toInt();
00162 }
00163 }
00164
00165
00166 {
00167 KConfig *profilesConfig = new KConfig("powerdevilprofilesrc", KConfig::SimpleConfig);
00168 m_availableProfiles = profilesConfig->groupList();
00169 foreach(const QString &profile, m_availableProfiles) {
00170 KConfigGroup *settings = new KConfigGroup(profilesConfig, profile);
00171 if (settings->readEntry("iconname").isEmpty()) {
00172 m_profileIcon[profile] = "preferences-system-power-management";
00173 } else {
00174 m_profileIcon[profile] = settings->readEntry("iconname");
00175 }
00176 delete settings;
00177 }
00178 delete profilesConfig;
00179 }
00180
00181
00182 {
00183 QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kded",
00184 "/modules/powerdevil", "org.kde.PowerDevil", "getSupportedSchemes");
00185 QDBusReply<QStringList> schemes = m_dbus.call(msg);
00186 m_supportedSchemes = schemes.value();
00187 }
00188
00189
00190 {
00191 QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kded",
00192 "/modules/powerdevil", "org.kde.PowerDevil", "getSupportedSuspendMethods");
00193 QDBusReply<QVariantMap> methods = m_dbus.call(msg);
00194 QMapIterator<QString, QVariant> it(methods);
00195 m_suspendMethods.clear();
00196 m_synonyms.clear();
00197 while (it.hasNext()) {
00198 it.next();
00199 const int value = it.value().toInt();
00200 m_suspendMethods[value] = it.key();
00201 if (it.key() == i18n("Suspend to RAM")) {
00202 m_synonyms[i18nc("Note this is a KRunner keyword", "sleep")] = value;
00203 m_synonyms[i18nc("Note this is a KRunner keyword", "to ram")] = value;
00204 } else if (it.key() == i18n("Suspend to Disk")) {
00205 m_synonyms[i18nc("Note this is a KRunner keyword", "hibernate")] = value;
00206 m_synonyms[i18nc("Note this is a KRunner keyword", "to disk")] = value;
00207 }
00208 }
00209 }
00210
00211 updateSyntaxes();
00212 }
00213
00214
00215 bool PowerDevilRunner::parseQuery(const QString& query, const QList<QRegExp>& rxList, QString& parameter) const
00216 {
00217 foreach (const QRegExp& rx, rxList) {
00218 if (rx.exactMatch(query)) {
00219 parameter = rx.cap(1).trimmed();
00220 return true;
00221 }
00222 }
00223 return false;
00224 }
00225
00226 void PowerDevilRunner::match(Plasma::RunnerContext &context)
00227 {
00228 const QString term = context.query();
00229 if (term.length() < m_shortestCommand) {
00230 return;
00231 }
00232
00233 QList<Plasma::QueryMatch> matches;
00234
00235 QString parameter;
00236
00237 if (parseQuery(term,
00238 QList<QRegExp>() << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "power profile %1", "(.*)"), Qt::CaseInsensitive)
00239 << QRegExp(i18nc("Note this is a KRunner keyword", "power profile"), Qt::CaseInsensitive),
00240 parameter)) {
00241 foreach(const QString &profile, m_availableProfiles) {
00242 if (!parameter.isEmpty()) {
00243 if (!profile.startsWith(parameter, Qt::CaseInsensitive)) {
00244 continue;
00245 }
00246 }
00247 Plasma::QueryMatch match(this);
00248 match.setType(Plasma::QueryMatch::ExactMatch);
00249 match.setIcon(KIcon(m_profileIcon[profile]));
00250 match.setText(i18n("Set Profile to '%1'", profile));
00251 match.setData(profile);
00252 match.setRelevance(1);
00253 match.setId("ProfileChange "+profile);
00254 matches.append(match);
00255 }
00256 } else if (parseQuery(term,
00257 QList<QRegExp>() << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "cpu policy %1", "(.*)"), Qt::CaseInsensitive)
00258 << QRegExp(i18nc("Note this is a KRunner keyword", "cpu policy"), Qt::CaseInsensitive)
00259 << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "power governor %1", "(.*)"), Qt::CaseInsensitive)
00260 << QRegExp(i18nc("Note this is a KRunner keyword", "power governor"), Qt::CaseInsensitive),
00261 parameter)) {
00262 foreach(const QString &ent, m_supportedGovernors) {
00263 if (!parameter.isEmpty()) {
00264 if (!ent.startsWith(parameter, Qt::CaseInsensitive)) {
00265 continue;
00266 }
00267 }
00268 Plasma::QueryMatch match(this);
00269 match.setType(Plasma::QueryMatch::ExactMatch);
00270
00271 switch (m_governorData[ent]) {
00272 case (int) Solid::Control::PowerManager::Performance:
00273 match.setIcon(KIcon("preferences-system-performance"));
00274 break;
00275 case (int) Solid::Control::PowerManager::OnDemand:
00276 match.setIcon(KIcon("system-switch-user"));
00277 break;
00278 case (int) Solid::Control::PowerManager::Conservative:
00279 match.setIcon(KIcon("user-invisible"));
00280 break;
00281 case (int) Solid::Control::PowerManager::Powersave:
00282 match.setIcon(KIcon("preferences-system-power-management"));
00283 break;
00284 case (int) Solid::Control::PowerManager::Userspace:
00285 match.setIcon(KIcon("kuser"));
00286 break;
00287 default:
00288 match.setIcon(KIcon("preferences-system-power-management"));
00289 break;
00290 }
00291
00292 match.setText(i18n("Set CPU frequency scaling policy to '%1'", ent));
00293 match.setData(m_governorData[ent]);
00294 match.setRelevance(1);
00295 match.setId("GovernorChange "+ent);
00296 matches.append(match);
00297 }
00298 } else if (parseQuery(term,
00299 QList<QRegExp>() << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "power scheme %1", "(.*)"), Qt::CaseInsensitive)
00300 << QRegExp(i18nc("Note this is a KRunner keyword", "power scheme"), Qt::CaseInsensitive),
00301 parameter)) {
00302 foreach(const QString &ent, m_supportedSchemes) {
00303 if (!parameter.isEmpty()) {
00304 if (!ent.startsWith(parameter, Qt::CaseInsensitive)) {
00305 continue;
00306 }
00307 }
00308
00309 Plasma::QueryMatch match(this);
00310
00311 match.setType(Plasma::QueryMatch::ExactMatch);
00312
00313 match.setIcon(KIcon("preferences-system-power-management"));
00314 match.setText(i18n("Set Powersaving Scheme to '%1'", ent));
00315 match.setData(ent);
00316
00317 match.setRelevance(1);
00318 match.setId("SchemeChange "+ent);
00319 matches.append(match);
00320 }
00321 } else if (parseQuery(term,
00322 QList<QRegExp>() << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "screen brightness %1", "(.*)"), Qt::CaseInsensitive)
00323 << QRegExp(i18nc("Note this is a KRunner keyword", "screen brightness"), Qt::CaseInsensitive)
00324 << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "dim screen %1", "(.*)"), Qt::CaseInsensitive)
00325 << QRegExp(i18nc("Note this is a KRunner keyword", "dim screen"), Qt::CaseInsensitive),
00326 parameter)) {
00327 if (!parameter.isEmpty()) {
00328 bool test;
00329 int b = parameter.toInt(&test);
00330 if (test) {
00331 int brightness = qBound(0, b, 100);
00332 Plasma::QueryMatch match(this);
00333 match.setType(Plasma::QueryMatch::ExactMatch);
00334 match.setIcon(KIcon("preferences-system-power-management"));
00335 match.setText(i18n("Set Brightness to %1", brightness));
00336 match.setData(brightness);
00337 match.setRelevance(1);
00338 match.setId("BrightnessChange");
00339 matches.append(match);
00340 }
00341 } else {
00342 Plasma::QueryMatch match1(this);
00343 match1.setType(Plasma::QueryMatch::ExactMatch);
00344 match1.setIcon(KIcon("preferences-system-power-management"));
00345 match1.setText(i18n("Dim screen totally"));
00346 match1.setRelevance(1);
00347 match1.setId("DimTotal");
00348 matches.append(match1);
00349
00350 Plasma::QueryMatch match2(this);
00351 match2.setType(Plasma::QueryMatch::ExactMatch);
00352 match2.setIcon(KIcon("preferences-system-power-management"));
00353 match2.setText(i18n("Dim screen by half"));
00354 match2.setRelevance(1);
00355 match2.setId("DimHalf");
00356 matches.append(match2);
00357
00358 Plasma::QueryMatch match3(this);
00359 match3.setType(Plasma::QueryMatch::ExactMatch);
00360 match3.setIcon(KIcon("video-display"));
00361 match3.setText(i18n("Turn off screen"));
00362 match3.setRelevance(1);
00363 match3.setId("TurnOffScreen");
00364 matches.append(match3);
00365 }
00366 } else if (term.startsWith(i18nc("Note this is a KRunner keyword", "suspend"))) {
00367 QHashIterator<int, QString> it(m_suspendMethods);
00368 while (it.hasNext()) {
00369 it.next();
00370 addSuspendMatch(it.key(), matches);
00371 }
00372 } else if (m_synonyms.contains(term)) {
00373 addSuspendMatch(m_synonyms.value(term), matches);
00374 }
00375
00376 if (!matches.isEmpty()) {
00377 context.addMatches(term, matches);
00378 }
00379 }
00380
00381 void PowerDevilRunner::addSuspendMatch(int value, QList<Plasma::QueryMatch> &matches)
00382 {
00383 Plasma::QueryMatch match(this);
00384 match.setType(Plasma::QueryMatch::ExactMatch);
00385
00386 switch (value) {
00387 case 1:
00388 case 2:
00389 match.setIcon(KIcon("system-suspend"));
00390 break;
00391 case 4:
00392 match.setIcon(KIcon("system-suspend-hibernate"));
00393 break;
00394 default:
00395 match.setIcon(KIcon("preferences-system-power-management"));
00396 break;
00397 }
00398
00399 match.setText(m_suspendMethods[value]);
00400 match.setData(value);
00401 match.setRelevance(1);
00402 match.setId("Suspend " + m_suspendMethods[value]);
00403 matches.append(match);
00404 }
00405
00406 void PowerDevilRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
00407 {
00408 Q_UNUSED(context)
00409
00410 QDBusInterface iface("org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil", m_dbus);
00411 if (match.id().startsWith("PowerDevil_ProfileChange")) {
00412 iface.call("setProfile", match.data().toString());
00413 } else if (match.id().startsWith("PowerDevil_GovernorChange")) {
00414 iface.call("setGovernor", match.data().toInt());
00415 } else if (match.id().startsWith("PowerDevil_SchemeChange")) {
00416 iface.call("setPowersavingScheme", match.data().toString());
00417 } else if (match.id() == "PowerDevil_BrightnessChange") {
00418 iface.call("setBrightness", match.data().toInt());
00419 } else if (match.id() == "PowerDevil_DimTotal") {
00420 iface.call("setBrightness", 0);
00421 } else if (match.id() == "PowerDevil_DimHalf") {
00422 iface.call("setBrightness", -2);
00423 } else if (match.id() == "PowerDevil_TurnOffScreen") {
00424 iface.call("turnOffScreen");
00425 } else if (match.id().startsWith("PowerDevil_Suspend")) {
00426 iface.call("suspend", match.data().toInt());
00427 }
00428 }
00429
00430 #include "PowerDevilRunner.moc"