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

Plasma

PowerDevilRunner.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright 2008 by Dario Freddi <drf@kdemod.ath.cx>                    *
00003  *   Copyright 2008 by Sebastian Kügler <sebas@kde.org>                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
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 {
00039     Q_UNUSED( args )
00040 
00041     /* Let's define all the words here. m_words contains all the words that
00042      * will eventually trigger a match in the runner.
00043      *
00044      * FIXME: I made all the words translatable, though I don't know if that's
00045      * the right way to go.
00046      */
00047 
00048     m_words << i18nc( "Note this is a KRunner keyword", "power profile" ) <<
00049     i18nc( "Note this is a KRunner keyword", "cpu policy" ) <<
00050     i18nc( "Note this is a KRunner keyword", "power governor" ) <<
00051     i18nc( "Note this is a KRunner keyword", "power scheme" ) <<
00052     i18nc( "Note this is a KRunner keyword", "screen brightness" ) <<
00053     i18nc( "Note this is a KRunner keyword", "suspend" );
00054 
00055     setObjectName( "PowerDevil" );
00056     updateStatus();
00057     initUpdateTriggers();
00058 }
00059 
00060 PowerDevilRunner::~PowerDevilRunner()
00061 {
00062 }
00063 
00064 void PowerDevilRunner::initUpdateTriggers()
00065 {
00066 
00067     // listen for changes to the profiles
00068     KDirWatch *profilesWatch = new KDirWatch(this);
00069     profilesWatch->addFile(KStandardDirs::locate("config", "powerdevilprofilesrc"));
00070     connect(profilesWatch,SIGNAL(dirty(QString)),this,SLOT(updateStatus()));
00071     connect(profilesWatch,SIGNAL(created(QString)),this,SLOT(updateStatus()));
00072     connect(profilesWatch,SIGNAL(deleted(QString)),this,SLOT(updateStatus()));
00073 
00074     // Also receive updates triggered through the DBus
00075     QStringList modules;
00076     QDBusInterface kdedInterface("org.kde.kded", "/kded", "org.kde.kded");
00077     QDBusReply<QStringList> reply = kdedInterface.call("loadedModules");
00078 
00079     if (!reply.isValid()) {
00080         return;
00081     }
00082 
00083     modules = reply.value();
00084 
00085     if (modules.contains("powerdevil")) {
00086         if (!m_dbus.connect("org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil",
00087                           "profileChanged", this, SLOT(updateStatus()))) {
00088             kDebug() << "error!";
00089         }
00090         if (!m_dbus.connect("org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil",
00091                           "stateChanged", this, SLOT(updateStatus()))) {
00092             kDebug() << "error!";
00093         }
00094 
00095         QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kded", "/modules/powerdevil",
00096                            "org.kde.PowerDevil", "streamData");
00097         m_dbus.call(msg);
00098     }
00099 }
00100 
00101 void PowerDevilRunner::updateStatus()
00102 {
00103     // Governors
00104     {
00105         QDBusMessage msg = QDBusMessage::createMethodCall( "org.kde.kded",
00106                             "/modules/powerdevil", "org.kde.PowerDevil", "getSupportedGovernors" );
00107         QDBusReply<QVariantMap> govs = m_dbus.call( msg );
00108         m_supportedGovernors = govs.value().keys();
00109         foreach( const QString &governor, m_supportedGovernors ) {
00110             m_governorData[governor] = govs.value()[governor].toInt();
00111         }
00112     }
00113 
00114     // Profiles and their icons
00115     {
00116         KConfig *profilesConfig = new KConfig( "powerdevilprofilesrc", KConfig::SimpleConfig );
00117         m_availableProfiles = profilesConfig->groupList();
00118         foreach( const QString &profile, m_availableProfiles ) {
00119             KConfigGroup *settings = new KConfigGroup( profilesConfig, profile );
00120             if ( settings->readEntry( "iconname" ).isEmpty() ) {
00121                 m_profileIcon[profile] = "preferences-system-power-management";
00122             } else {
00123                 m_profileIcon[profile] = settings->readEntry("iconname");
00124             }
00125             delete settings;
00126         }
00127         delete profilesConfig;
00128     }
00129 
00130     // Schemes
00131     {
00132         QDBusMessage msg = QDBusMessage::createMethodCall( "org.kde.kded",
00133                             "/modules/powerdevil", "org.kde.PowerDevil", "getSupportedSchemes" );
00134         QDBusReply<QStringList> schemes = m_dbus.call( msg );
00135         m_supportedSchemes = schemes.value();
00136     }
00137 
00138     // Suspend
00139     {
00140         QDBusMessage msg = QDBusMessage::createMethodCall( "org.kde.kded",
00141                                 "/modules/powerdevil", "org.kde.PowerDevil", "getSupportedSuspendMethods" );
00142         QDBusReply<QVariantMap> methods = m_dbus.call( msg );
00143         m_suspendMethods = methods.value().keys();
00144         foreach( const QString &method, m_suspendMethods ) {
00145             m_suspendData[method] = methods.value()[method].toInt();
00146         }
00147     }
00148 }
00149 
00150 void PowerDevilRunner::match( Plasma::RunnerContext &context )
00151 {
00152     QString term = context.query();
00153 
00154     foreach( const QString &word, m_words ) {
00155         if ( term.startsWith( word, Qt::CaseInsensitive ) ) {
00156             if ( word == i18nc( "Note this is a KRunner keyword", "power profile" ) ) {
00157                 foreach( const QString &profile, m_availableProfiles ) {
00158                     if ( term.split( ' ' ).count() == 3 ) {
00159                         if ( !profile.startsWith( term.split( ' ' ).at( 2 ) ) ) {
00160                             continue;
00161                         }
00162                     }
00163                     Plasma::QueryMatch match( this );
00164                     match.setType( Plasma::QueryMatch::ExactMatch );
00165                     match.setIcon( KIcon( m_profileIcon[profile] ) );
00166                     match.setText( i18n( "Set Profile to '%1'", profile ) );
00167                     match.setData( profile );
00168                     match.setRelevance( 1 );
00169                     match.setId( "ProfileChange" );
00170                     context.addMatch( term, match );
00171                 }
00172             } else if ( word == i18nc( "Note this is a KRunner keyword", "cpu policy" ) ||
00173                         word == i18nc( "Note this is a KRunner keyword", "power governor" ) ) {
00174                 foreach( const QString &ent, m_supportedGovernors ) {
00175                     if ( term.split( ' ' ).count() == 3 ) {
00176                         if ( !ent.startsWith( term.split( ' ' ).at( 2 ) ) ) {
00177                             continue;
00178                         }
00179                     }
00180                     Plasma::QueryMatch match( this );
00181                     match.setType( Plasma::QueryMatch::ExactMatch );
00182 
00183                     switch (m_governorData[ent]) {
00184                         case (int) Solid::Control::PowerManager::Performance:
00185                             match.setIcon( KIcon( "preferences-system-performance" ) );
00186                             break;
00187                         case (int) Solid::Control::PowerManager::OnDemand:
00188                             match.setIcon( KIcon( "system-switch-user" ) );
00189                             break;
00190                         case (int) Solid::Control::PowerManager::Conservative:
00191                             match.setIcon( KIcon( "user-invisible" ) );
00192                             break;
00193                         case (int) Solid::Control::PowerManager::Powersave:
00194                             match.setIcon( KIcon( "preferences-system-power-management" ) );
00195                             break;
00196                         case (int) Solid::Control::PowerManager::Userspace:
00197                             match.setIcon( KIcon( "kuser" ) );
00198                             break;
00199                         default:
00200                             match.setIcon( KIcon( "preferences-system-power-management" ) );
00201                             break;
00202                     }
00203 
00204                     match.setText( i18n( "Set CPU frequency scaling policy to '%1'", ent ) );
00205                     match.setData( m_governorData[ent] );
00206                     match.setRelevance( 1 );
00207                     match.setId( "GovernorChange" );
00208                     context.addMatch( term, match );
00209                 }
00210             } else if ( word == i18nc( "Note this is a KRunner keyword", "power scheme" ) ) {
00211                 foreach( const QString &ent, m_supportedSchemes ) {
00212                     if ( term.split( ' ' ).count() == 3 ) {
00213                         if ( !ent.startsWith( term.split( ' ' ).at( 2 ) ) ) {
00214                             continue;
00215                         }
00216                     }
00217 
00218                     Plasma::QueryMatch match( this );
00219 
00220                     match.setType( Plasma::QueryMatch::ExactMatch );
00221 
00222                     match.setIcon( KIcon( "preferences-system-power-management" ) );
00223                     match.setText( i18n( "Set Powersaving Scheme to '%1'", ent ) );
00224                     match.setData( ent );
00225 
00226                     match.setRelevance( 1 );
00227                     match.setId( "SchemeChange" );
00228                     context.addMatch( term, match );
00229                 }
00230             } else if ( word == i18nc( "Note this is a KRunner keyword", "screen brightness" ) ) {
00231                 if ( term.split( ' ' ).count() == 3 ) {
00232                     bool test;
00233                     int b = term.split( ' ' ).at( 2 ).toInt( &test );
00234                     if ( test ) {
00235                         int brightness = qBound( 0, b, 100 );
00236                         Plasma::QueryMatch match( this );
00237                         match.setType( Plasma::QueryMatch::ExactMatch );
00238                         match.setIcon( KIcon( "preferences-system-power-management" ) );
00239                         match.setText( i18n( "Set Brightness to %1", brightness ) );
00240                         match.setData( brightness );
00241                         match.setRelevance( 1 );
00242                         match.setId( "BrightnessChange" );
00243                         context.addMatch( term, match );
00244                     }
00245                 } else if ( term.split( ' ' ).count() == 2 ) {
00246                     Plasma::QueryMatch match1( this );
00247                     match1.setType( Plasma::QueryMatch::ExactMatch );
00248                     match1.setIcon( KIcon( "preferences-system-power-management" ) );
00249                     match1.setText( i18n( "Dim screen totally" ) );
00250                     match1.setRelevance( 1 );
00251                     match1.setId( "DimTotal" );
00252                     context.addMatch( term, match1 );
00253 
00254                     Plasma::QueryMatch match2( this );
00255                     match2.setType( Plasma::QueryMatch::ExactMatch );
00256                     match2.setIcon( KIcon( "preferences-system-power-management" ) );
00257                     match2.setText( i18n( "Dim screen by half" ) );
00258                     match2.setRelevance( 1 );
00259                     match2.setId( "DimHalf" );
00260                     context.addMatch( term, match2 );
00261 
00262                     Plasma::QueryMatch match3( this );
00263                     match3.setType( Plasma::QueryMatch::ExactMatch );
00264                     match3.setIcon( KIcon( "video-display" ) );
00265                     match3.setText( i18n( "Turn off screen" ) );
00266                     match3.setRelevance( 1 );
00267                     match3.setId( "TurnOffScreen" );
00268                     context.addMatch( term, match3 );
00269                 }
00270             } else if ( word == i18nc( "Note this is a KRunner keyword", "suspend" ) ) {
00271                 foreach( const QString &ent, m_suspendMethods ) {
00272                     Plasma::QueryMatch match( this );
00273                     match.setType( Plasma::QueryMatch::ExactMatch );
00274 
00275                     switch (m_suspendData[ent]) {
00276                         case 1:
00277                         case 2:
00278                             match.setIcon( KIcon( "system-suspend" ) );
00279                             break;
00280                         case 4:
00281                             match.setIcon( KIcon( "system-suspend-hibernate" ) );
00282                             break;
00283                         default:
00284                             match.setIcon( KIcon( "preferences-system-power-management" ) );
00285                             break;
00286                     }
00287 
00288                     match.setText( ent );
00289                     match.setData( m_suspendData[ent] );
00290                     match.setRelevance( 1 );
00291                     match.setId( "Suspend" );
00292                     context.addMatch( term, match );
00293                 }
00294             }
00295         }
00296     }
00297 }
00298 
00299 void PowerDevilRunner::run( const Plasma::RunnerContext &context, const Plasma::QueryMatch &match )
00300 {
00301     Q_UNUSED( context )
00302 
00303     QDBusInterface iface( "org.kde.kded", "/modules/powerdevil", "org.kde.PowerDevil", m_dbus );
00304     if ( match.id() == "PowerDevil_ProfileChange" ) {
00305         iface.call( "refreshStatus" );
00306         iface.call( "setProfile", match.data().toString() );
00307     } else if ( match.id() == "PowerDevil_GovernorChange" ) {
00308         iface.call( "setGovernor", match.data().toInt() );
00309     } else if ( match.id() == "PowerDevil_SchemeChange" ) {
00310         iface.call( "setPowersavingScheme", match.data().toString() );
00311     } else if ( match.id() == "PowerDevil_BrightnessChange" ) {
00312         iface.call( "setBrightness", match.data().toInt() );
00313     } else if ( match.id() == "PowerDevil_DimTotal" ) {
00314         iface.call( "setBrightness", 0 );
00315     } else if ( match.id() == "PowerDevil_DimHalf" ) {
00316         iface.call( "setBrightness", -2 );
00317     } else if ( match.id() == "PowerDevil_TurnOffScreen" ) {
00318         iface.call( "turnOffScreen" );
00319     } else if ( match.id() == "PowerDevil_Suspend" ) {
00320         iface.call( "suspend", match.data().toInt() );
00321     }
00322 }
00323 
00324 #include "PowerDevilRunner.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
  •   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