Changeset View
Changeset View
Standalone View
Standalone View
runners/powerdevil/PowerDevilRunner.cpp
Show First 20 Lines • Show All 46 Lines • ▼ Show 20 Line(s) | 41 | { | |||
---|---|---|---|---|---|
47 | updateStatus(); | 47 | updateStatus(); | ||
48 | initUpdateTriggers(); | 48 | initUpdateTriggers(); | ||
49 | 49 | | |||
50 | /* Let's define all the words here. m_words contains all the words that | 50 | /* Let's define all the words here. m_words contains all the words that | ||
51 | * will eventually trigger a match in the runner. | 51 | * will eventually trigger a match in the runner. | ||
52 | */ | 52 | */ | ||
53 | 53 | | |||
54 | QStringList commands; | 54 | QStringList commands; | ||
55 | commands << i18nc("Note this is a KRunner keyword", "power profile") | 55 | commands << i18nc("Note this is a KRunner keyword", "suspend") | ||
56 | << i18nc("Note this is a KRunner keyword", "suspend") | | |||
57 | << i18nc("Note this is a KRunner keyword", "sleep") | 56 | << i18nc("Note this is a KRunner keyword", "sleep") | ||
58 | << i18nc("Note this is a KRunner keyword", "hibernate") | 57 | << i18nc("Note this is a KRunner keyword", "hibernate") | ||
59 | << i18nc("Note this is a KRunner keyword", "to disk") | 58 | << i18nc("Note this is a KRunner keyword", "to disk") | ||
60 | << i18nc("Note this is a KRunner keyword", "to ram") | 59 | << i18nc("Note this is a KRunner keyword", "to ram") | ||
61 | << i18nc("Note this is a KRunner keyword", "screen brightness") | 60 | << i18nc("Note this is a KRunner keyword", "screen brightness") | ||
62 | << i18nc("Note this is a KRunner keyword", "dim screen"); | 61 | << i18nc("Note this is a KRunner keyword", "dim screen"); | ||
63 | 62 | | |||
64 | foreach (const QString &command, commands) { | 63 | foreach (const QString &command, commands) { | ||
65 | if (command.length() < m_shortestCommand) { | 64 | if (command.length() < m_shortestCommand) { | ||
66 | m_shortestCommand = command.length(); | 65 | m_shortestCommand = command.length(); | ||
67 | } | 66 | } | ||
68 | } | 67 | } | ||
69 | } | 68 | } | ||
70 | 69 | | |||
71 | void PowerDevilRunner::updateSyntaxes() | 70 | void PowerDevilRunner::updateSyntaxes() | ||
72 | { | 71 | { | ||
73 | QList<Plasma::RunnerSyntax> syntaxes; | 72 | QList<Plasma::RunnerSyntax> syntaxes; | ||
74 | syntaxes.append(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "power profile"), | | |||
75 | i18n("Lists all power profiles and allows them to be activated"))); | | |||
76 | syntaxes.append(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "suspend"), | 73 | syntaxes.append(Plasma::RunnerSyntax(i18nc("Note this is a KRunner keyword", "suspend"), | ||
77 | i18n("Lists system suspend (e.g. sleep, hibernate) options " | 74 | i18n("Lists system suspend (e.g. sleep, hibernate) options " | ||
78 | "and allows them to be activated"))); | 75 | "and allows them to be activated"))); | ||
79 | 76 | | |||
80 | QSet< Solid::PowerManagement::SleepState > states = Solid::PowerManagement::supportedSleepStates(); | 77 | QSet< Solid::PowerManagement::SleepState > states = Solid::PowerManagement::supportedSleepStates(); | ||
81 | 78 | | |||
82 | if (states.contains(Solid::PowerManagement::SuspendState)) { | 79 | if (states.contains(Solid::PowerManagement::SuspendState)) { | ||
83 | Plasma::RunnerSyntax sleepSyntax(i18nc("Note this is a KRunner keyword", "sleep"), | 80 | Plasma::RunnerSyntax sleepSyntax(i18nc("Note this is a KRunner keyword", "sleep"), | ||
Show All 25 Lines | |||||
109 | void PowerDevilRunner::initUpdateTriggers() | 106 | void PowerDevilRunner::initUpdateTriggers() | ||
110 | { | 107 | { | ||
111 | // Also receive updates triggered through the DBus | 108 | // Also receive updates triggered through the DBus | ||
112 | QDBusConnection dbus = QDBusConnection::sessionBus(); | 109 | QDBusConnection dbus = QDBusConnection::sessionBus(); | ||
113 | if (dbus.interface()->isServiceRegistered(QStringLiteral("org.kde.Solid.PowerManagement"))) { | 110 | if (dbus.interface()->isServiceRegistered(QStringLiteral("org.kde.Solid.PowerManagement"))) { | ||
114 | if (!dbus.connect(QStringLiteral("org.kde.Solid.PowerManagement"), | 111 | if (!dbus.connect(QStringLiteral("org.kde.Solid.PowerManagement"), | ||
115 | QStringLiteral("/org/kde/Solid/PowerManagement"), | 112 | QStringLiteral("/org/kde/Solid/PowerManagement"), | ||
116 | QStringLiteral("org.kde.Solid.PowerManagement"), | 113 | QStringLiteral("org.kde.Solid.PowerManagement"), | ||
117 | QStringLiteral("profileChanged"), this, SLOT(updateStatus()))) { | | |||
118 | qDebug() << "error!"; | | |||
119 | } | | |||
120 | if (!dbus.connect(QStringLiteral("org.kde.Solid.PowerManagement"), | | |||
121 | QStringLiteral("/org/kde/Solid/PowerManagement"), | | |||
122 | QStringLiteral("org.kde.Solid.PowerManagement"), | | |||
123 | QStringLiteral("configurationReloaded"), this, SLOT(updateStatus()))) { | 114 | QStringLiteral("configurationReloaded"), this, SLOT(updateStatus()))) { | ||
124 | qDebug() << "error!"; | 115 | qDebug() << "error!"; | ||
125 | } | 116 | } | ||
126 | } | 117 | } | ||
127 | } | 118 | } | ||
128 | 119 | | |||
129 | void PowerDevilRunner::updateStatus() | 120 | void PowerDevilRunner::updateStatus() | ||
130 | { | 121 | { | ||
131 | // Profiles and their icons | | |||
132 | { | | |||
133 | KSharedConfigPtr profilesConfig = KSharedConfig::openConfig(QStringLiteral("powerdevil2profilesrc"), KConfig::SimpleConfig); | | |||
134 | // Request profiles to the daemon | | |||
135 | QDBusMessage call = QDBusMessage::createMethodCall(QStringLiteral("org.kde.Solid.PowerManagement"), QStringLiteral("/org/kde/Solid/PowerManagement"), | | |||
136 | QStringLiteral("org.kde.Solid.PowerManagement"), QStringLiteral("availableProfiles")); | | |||
137 | QDBusPendingReply< StringStringMap > reply = QDBusConnection::sessionBus().asyncCall(call); | | |||
138 | reply.waitForFinished(); | | |||
139 | | ||||
140 | if (!reply.isValid()) { | | |||
141 | qDebug() << "Error contacting the daemon!"; | | |||
142 | return; | | |||
143 | } | | |||
144 | | ||||
145 | m_availableProfiles = reply.value(); | | |||
146 | | ||||
147 | if (m_availableProfiles.isEmpty()) { | | |||
148 | qDebug() << "No available profiles!"; | | |||
149 | return; | | |||
150 | } | | |||
151 | | ||||
152 | for (StringStringMap::const_iterator i = m_availableProfiles.constBegin(); i != m_availableProfiles.constEnd(); ++i) { | | |||
153 | KConfigGroup settings(profilesConfig, i.key()); | | |||
154 | if (settings.readEntry< QString >("icon", QString()).isEmpty()) { | | |||
155 | m_profileIcon[i.key()] = QLatin1String("preferences-system-power-management"); | | |||
156 | } else { | | |||
157 | m_profileIcon[i.key()] = settings.readEntry< QString >("icon", QString()); | | |||
158 | } | | |||
159 | } | | |||
160 | } | | |||
161 | | ||||
162 | updateSyntaxes(); | 122 | updateSyntaxes(); | ||
163 | } | 123 | } | ||
164 | 124 | | |||
165 | | ||||
166 | bool PowerDevilRunner::parseQuery(const QString& query, const QList<QRegExp>& rxList, QString& parameter) const | 125 | bool PowerDevilRunner::parseQuery(const QString& query, const QList<QRegExp>& rxList, QString& parameter) const | ||
167 | { | 126 | { | ||
168 | foreach (const QRegExp& rx, rxList) { | 127 | foreach (const QRegExp& rx, rxList) { | ||
169 | if (rx.exactMatch(query)) { | 128 | if (rx.exactMatch(query)) { | ||
170 | parameter = rx.cap(1).trimmed(); | 129 | parameter = rx.cap(1).trimmed(); | ||
171 | return true; | 130 | return true; | ||
172 | } | 131 | } | ||
173 | } | 132 | } | ||
174 | return false; | 133 | return false; | ||
175 | } | 134 | } | ||
176 | 135 | | |||
177 | void PowerDevilRunner::match(Plasma::RunnerContext &context) | 136 | void PowerDevilRunner::match(Plasma::RunnerContext &context) | ||
178 | { | 137 | { | ||
179 | const QString term = context.query(); | 138 | const QString term = context.query(); | ||
180 | if (term.length() < m_shortestCommand) { | 139 | if (term.length() < m_shortestCommand) { | ||
181 | return; | 140 | return; | ||
182 | } | 141 | } | ||
183 | 142 | | |||
184 | QList<Plasma::QueryMatch> matches; | 143 | QList<Plasma::QueryMatch> matches; | ||
185 | 144 | | |||
186 | QString parameter; | 145 | QString parameter; | ||
187 | 146 | | |||
188 | if (parseQuery(term, | 147 | if (parseQuery(term, | ||
189 | QList<QRegExp>() << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "power profile %1", "(.*)"), Qt::CaseInsensitive) | | |||
190 | << QRegExp(i18nc("Note this is a KRunner keyword", "power profile"), Qt::CaseInsensitive), | | |||
191 | parameter)) { | | |||
192 | for (StringStringMap::const_iterator i = m_availableProfiles.constBegin(); i != m_availableProfiles.constEnd(); ++i) { | | |||
193 | if (!parameter.isEmpty()) { | | |||
194 | if (!i.value().startsWith(parameter, Qt::CaseInsensitive)) { | | |||
195 | continue; | | |||
196 | } | | |||
197 | } | | |||
198 | Plasma::QueryMatch match(this); | | |||
199 | match.setType(Plasma::QueryMatch::ExactMatch); | | |||
200 | match.setIconName(m_profileIcon[i.key()]); | | |||
201 | match.setText(i18n("Set Profile to '%1'", i.value())); | | |||
202 | match.setData(i.key()); | | |||
203 | match.setRelevance(1); | | |||
204 | match.setId("ProfileChange "+ i.key()); | | |||
205 | matches.append(match); | | |||
206 | } | | |||
207 | } else if (parseQuery(term, | | |||
208 | QList<QRegExp>() << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "screen brightness %1", "(.*)"), Qt::CaseInsensitive) | 148 | QList<QRegExp>() << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "screen brightness %1", "(.*)"), Qt::CaseInsensitive) | ||
209 | << QRegExp(i18nc("Note this is a KRunner keyword", "screen brightness"), Qt::CaseInsensitive) | 149 | << QRegExp(i18nc("Note this is a KRunner keyword", "screen brightness"), Qt::CaseInsensitive) | ||
210 | << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "dim screen %1", "(.*)"), Qt::CaseInsensitive) | 150 | << QRegExp(i18nc("Note this is a KRunner keyword; %1 is a parameter", "dim screen %1", "(.*)"), Qt::CaseInsensitive) | ||
211 | << QRegExp(i18nc("Note this is a KRunner keyword", "dim screen"), Qt::CaseInsensitive), | 151 | << QRegExp(i18nc("Note this is a KRunner keyword", "dim screen"), Qt::CaseInsensitive), | ||
212 | parameter)) { | 152 | parameter)) { | ||
213 | if (!parameter.isEmpty()) { | 153 | if (!parameter.isEmpty()) { | ||
214 | bool test; | 154 | bool test; | ||
215 | int b = parameter.toInt(&test); | 155 | int b = parameter.toInt(&test); | ||
▲ Show 20 Lines • Show All 108 Lines • Show Last 20 Lines |