Merge lp:~parnold-x/slingshot/slingshot-session-plugin into lp:~elementary-pantheon/slingshot/trunk

Proposed by Daniel Fore on 2015-05-20
Status: Merged
Approved by: Cody Garver on 2015-05-29
Approved revision: 534
Merged at revision: 539
Proposed branch: lp:~parnold-x/slingshot/slingshot-session-plugin
Merge into: lp:~elementary-pantheon/slingshot/trunk
Diff against target: 555 lines (+527/-1)
3 files modified
lib/synapse-plugins/CMakeLists.txt (+1/-0)
lib/synapse-plugins/system-managment.vala (+524/-0)
src/Backend/SynapseSearch.vala (+2/-1)
To merge this branch: bzr merge lp:~parnold-x/slingshot/slingshot-session-plugin
Reviewer Review Type Date Requested Status
elementary Pantheon team 2015-05-20 Pending
Review via email: mp+259656@code.launchpad.net

Commit message

Add session actions plugin (lp:1380794)

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/synapse-plugins/CMakeLists.txt'
--- lib/synapse-plugins/CMakeLists.txt 2014-06-29 14:50:36 +0000
+++ lib/synapse-plugins/CMakeLists.txt 2015-05-20 17:20:28 +0000
@@ -14,6 +14,7 @@
14 calculator-plugin.vala14 calculator-plugin.vala
15 command-plugin.vala15 command-plugin.vala
16 desktop-file-plugin.vala16 desktop-file-plugin.vala
17 system-managment.vala
17)18)
1819
19set(LINK_MODE STATIC)20set(LINK_MODE STATIC)
2021
=== added file 'lib/synapse-plugins/system-managment.vala'
--- lib/synapse-plugins/system-managment.vala 1970-01-01 00:00:00 +0000
+++ lib/synapse-plugins/system-managment.vala 2015-05-20 17:20:28 +0000
@@ -0,0 +1,524 @@
1/*
2 * Copyright (C) 2010 Michal Hruby <michal.mhr@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Authored by Michal Hruby <michal.mhr@gmail.com>
19 *
20 */
21
22namespace Synapse
23{
24 [DBus (name = "org.freedesktop.UPower")]
25 public interface UPowerObject : Object
26 {
27 public const string UNIQUE_NAME = "org.freedesktop.UPower";
28 public const string OBJECT_PATH = "/org/freedesktop/UPower";
29
30 public abstract async void hibernate () throws IOError;
31 public abstract async void suspend () throws IOError;
32 public abstract async bool hibernate_allowed () throws IOError;
33 public abstract async bool suspend_allowed () throws IOError;
34
35 public abstract async void about_to_sleep () throws IOError;
36 }
37
38 [DBus (name = "org.freedesktop.ConsoleKit.Manager")]
39 public interface ConsoleKitObject : Object
40 {
41 public const string UNIQUE_NAME = "org.freedesktop.ConsoleKit";
42 public const string OBJECT_PATH = "/org/freedesktop/ConsoleKit/Manager";
43
44 public abstract void restart () throws IOError;
45 public abstract void stop () throws IOError;
46 public abstract async bool can_restart () throws IOError;
47 public abstract async bool can_stop () throws IOError;
48 }
49
50 [DBus (name = "org.freedesktop.login1.Manager")]
51 public interface SystemdObject : Object
52 {
53 public const string UNIQUE_NAME = "org.freedesktop.login1";
54 public const string OBJECT_PATH = "/org/freedesktop/login1";
55
56 public abstract void reboot (bool interactive) throws IOError;
57 public abstract void suspend (bool interactive) throws IOError;
58 public abstract void hibernate (bool interactive) throws IOError;
59 public abstract void power_off (bool interactive) throws IOError;
60 public abstract string can_suspend () throws IOError;
61 public abstract string can_hibernate () throws IOError;
62 public abstract string can_reboot () throws IOError;
63 public abstract string can_power_off () throws IOError;
64 }
65
66 public class SystemManagementPlugin : Object, Activatable, ItemProvider
67 {
68 public bool enabled { get; set; default = true; }
69
70 public void activate ()
71 {
72
73 }
74
75 public void deactivate ()
76 {
77
78 }
79
80 private abstract class SystemAction : Object, Match
81 {
82 // for Match interface
83 public string title { get; construct set; }
84 public string description { get; set; default = ""; }
85 public string icon_name { get; construct set; default = ""; }
86 public bool has_thumbnail { get; construct set; default = false; }
87 public string thumbnail_path { get; construct set; }
88 public MatchType match_type { get; construct set; }
89
90 public abstract void do_action ();
91
92 public abstract bool action_allowed ();
93
94 public void execute (Match? match) {
95 do_action ();
96 }
97 }
98
99 private class SuspendAction : SystemAction
100 {
101 public SuspendAction ()
102 {
103 Object (title: _("Suspend"), match_type: MatchType.ACTION,
104 description: _("Put your computer into suspend mode"),
105 icon_name: "system-suspend", has_thumbnail: false);
106 }
107
108 construct
109 {
110 check_allowed.begin ();
111 }
112
113 private async void check_allowed ()
114 {
115 try
116 {
117 SystemdObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
118 SystemdObject.UNIQUE_NAME,
119 SystemdObject.OBJECT_PATH);
120
121 allowed = (dbus_interface.can_suspend () == "yes");
122 return;
123 }
124 catch (IOError err)
125 {
126 warning ("%s", err.message);
127 allowed = false;
128 }
129
130 try
131 {
132 UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
133 UPowerObject.UNIQUE_NAME,
134 UPowerObject.OBJECT_PATH);
135
136 allowed = yield dbus_interface.suspend_allowed ();
137 }
138 catch (IOError err)
139 {
140 warning ("%s", err.message);
141 allowed = false;
142 }
143 }
144
145 private bool allowed = false;
146
147 public override bool action_allowed ()
148 {
149 return allowed;
150 }
151
152 private async void do_suspend ()
153 {
154 try
155 {
156 SystemdObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
157 SystemdObject.UNIQUE_NAME,
158 SystemdObject.OBJECT_PATH);
159
160 dbus_interface.suspend (true);
161 return;
162 }
163 catch (IOError err)
164 {
165 warning ("%s", err.message);
166 }
167
168 try
169 {
170 UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
171 UPowerObject.UNIQUE_NAME,
172 UPowerObject.OBJECT_PATH);
173
174 try
175 {
176 yield dbus_interface.about_to_sleep ();
177 }
178 catch (Error not_there_error) { }
179 // yea kinda nasty
180 //GnomeScreenSaverPlugin.lock_screen ();
181 // wait 2 seconds
182 Timeout.add (2000, do_suspend.callback);
183 yield;
184
185 yield dbus_interface.suspend ();
186 }
187 catch (IOError err)
188 {
189 warning ("%s", err.message);
190 }
191 }
192
193 public override void do_action ()
194 {
195 do_suspend.begin ();
196 }
197 }
198
199 private class HibernateAction : SystemAction
200 {
201 public HibernateAction ()
202 {
203 Object (title: _("Hibernate"), match_type: MatchType.ACTION,
204 description: _("Put your computer into hibernation mode"),
205 icon_name: "system-hibernate", has_thumbnail: false);
206 }
207
208 construct
209 {
210 check_allowed.begin ();
211 }
212
213 private async void check_allowed ()
214 {
215 try
216 {
217 SystemdObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
218 SystemdObject.UNIQUE_NAME,
219 SystemdObject.OBJECT_PATH);
220
221 allowed = (dbus_interface.can_hibernate () == "yes");
222 return;
223 }
224 catch (IOError err)
225 {
226 warning ("%s", err.message);
227 allowed = false;
228 }
229
230 try
231 {
232 UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
233 UPowerObject.UNIQUE_NAME,
234 UPowerObject.OBJECT_PATH);
235
236 allowed = yield dbus_interface.hibernate_allowed ();
237 }
238 catch (IOError err)
239 {
240 warning ("%s", err.message);
241 allowed = false;
242 }
243 }
244
245 private bool allowed = false;
246
247 public override bool action_allowed ()
248 {
249 return allowed;
250 }
251
252 private async void do_hibernate ()
253 {
254 try
255 {
256 SystemdObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
257 SystemdObject.UNIQUE_NAME,
258 SystemdObject.OBJECT_PATH);
259
260 dbus_interface.hibernate (true);
261 return;
262 }
263 catch (IOError err)
264 {
265 warning ("%s", err.message);
266 }
267
268 try
269 {
270 UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
271 UPowerObject.UNIQUE_NAME,
272 UPowerObject.OBJECT_PATH);
273
274 try
275 {
276 yield dbus_interface.about_to_sleep ();
277 }
278 catch (Error not_there_error) { }
279 // yea kinda nasty
280 //GnomeScreenSaverPlugin.lock_screen ();
281 // wait 2 seconds
282 Timeout.add (2000, do_hibernate.callback);
283 yield;
284 dbus_interface.hibernate.begin ();
285 }
286 catch (IOError err)
287 {
288 warning ("%s", err.message);
289 }
290 }
291
292 public override void do_action ()
293 {
294 do_hibernate.begin ();
295 }
296 }
297
298 private class ShutdownAction : SystemAction
299 {
300 public ShutdownAction ()
301 {
302 Object (title: _("Shut Down"), match_type: MatchType.ACTION,
303 description: _("Turn your computer off"),
304 icon_name: "system-shutdown", has_thumbnail: false);
305 }
306
307 construct
308 {
309 check_allowed.begin ();
310 }
311
312 private async void check_allowed ()
313 {
314 try
315 {
316 SystemdObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
317 SystemdObject.UNIQUE_NAME,
318 SystemdObject.OBJECT_PATH);
319
320 allowed = (dbus_interface.can_power_off () == "yes");
321 return;
322 }
323 catch (IOError err)
324 {
325 warning ("%s", err.message);
326 allowed = false;
327 }
328
329 try
330 {
331 ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
332 ConsoleKitObject.UNIQUE_NAME,
333 ConsoleKitObject.OBJECT_PATH);
334
335 allowed = yield dbus_interface.can_stop ();
336 }
337 catch (IOError err)
338 {
339 warning ("%s", err.message);
340 allowed = false;
341 }
342 }
343
344 private bool allowed = false;
345
346 public override bool action_allowed ()
347 {
348 return allowed;
349 }
350
351 public override void do_action ()
352 {
353 try
354 {
355 SystemdObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
356 SystemdObject.UNIQUE_NAME,
357 SystemdObject.OBJECT_PATH);
358
359 dbus_interface.power_off (true);
360 return;
361 }
362 catch (IOError err)
363 {
364 warning ("%s", err.message);
365 }
366
367 try
368 {
369 ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
370 ConsoleKitObject.UNIQUE_NAME,
371 ConsoleKitObject.OBJECT_PATH);
372
373 dbus_interface.stop ();
374 }
375 catch (IOError err)
376 {
377 warning ("%s", err.message);
378 }
379 }
380 }
381
382 private class RestartAction : SystemAction
383 {
384 public RestartAction ()
385 {
386 Object (title: _("Restart"), match_type: MatchType.ACTION,
387 description: _("Restart your computer"),
388 icon_name: "system-restart", has_thumbnail: false);
389 }
390
391 construct
392 {
393 check_allowed.begin ();
394 }
395
396 private async void check_allowed ()
397 {
398 try
399 {
400 SystemdObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
401 SystemdObject.UNIQUE_NAME,
402 SystemdObject.OBJECT_PATH);
403
404 allowed = (dbus_interface.can_reboot () == "yes");
405 return;
406 }
407 catch (IOError err)
408 {
409 warning ("%s", err.message);
410 allowed = false;
411 }
412
413 try
414 {
415 ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
416 ConsoleKitObject.UNIQUE_NAME,
417 ConsoleKitObject.OBJECT_PATH);
418
419 allowed = yield dbus_interface.can_restart ();
420 }
421 catch (IOError err)
422 {
423 warning ("%s", err.message);
424 allowed = false;
425 }
426 }
427
428 private bool allowed = false;
429
430 public override bool action_allowed ()
431 {
432 return allowed;
433 }
434
435 public override void do_action ()
436 {
437 try
438 {
439 SystemdObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
440 SystemdObject.UNIQUE_NAME,
441 SystemdObject.OBJECT_PATH);
442
443 dbus_interface.reboot (true);
444 return;
445 }
446 catch (IOError err)
447 {
448 warning ("%s", err.message);
449 }
450
451 try
452 {
453 ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
454 ConsoleKitObject.UNIQUE_NAME,
455 ConsoleKitObject.OBJECT_PATH);
456
457 dbus_interface.restart ();
458 }
459 catch (IOError err)
460 {
461 warning ("%s", err.message);
462 }
463 }
464 }
465
466 static void register_plugin ()
467 {
468 DataSink.PluginRegistry.get_default ().register_plugin (
469 typeof (SystemManagementPlugin),
470 "System Management",
471 _("Suspend, hibernate, restart or shutdown your computer."),
472 "system-restart",
473 register_plugin,
474 DBusService.get_default ().service_is_available (SystemdObject.UNIQUE_NAME) ||
475 DBusService.get_default ().service_is_available (ConsoleKitObject.UNIQUE_NAME),
476 _("ConsoleKit wasn't found")
477 );
478 }
479
480 static construct
481 {
482 register_plugin ();
483 }
484
485 private Gee.List<SystemAction> actions;
486
487 construct
488 {
489 actions = new Gee.LinkedList<SystemAction> ();
490 actions.add (new SuspendAction ());
491 actions.add (new HibernateAction ());
492 actions.add (new ShutdownAction ());
493 actions.add (new RestartAction ());
494 }
495
496 public async ResultSet? search (Query q) throws SearchError
497 {
498 // we only search for actions
499 if (!(QueryFlags.ACTIONS in q.query_type)) return null;
500
501 var result = new ResultSet ();
502
503 var matchers = Query.get_matchers_for_query (q.query_string, 0,
504 RegexCompileFlags.OPTIMIZE | RegexCompileFlags.CASELESS);
505
506 foreach (var action in actions)
507 {
508 if (!action.action_allowed ()) continue;
509 foreach (var matcher in matchers)
510 {
511 if (matcher.key.match (action.title))
512 {
513 result.add (action, matcher.value - Match.Score.INCREMENT_SMALL);
514 break;
515 }
516 }
517 }
518
519 q.check_cancellable ();
520
521 return result;
522 }
523 }
524}
0\ No newline at end of file525\ No newline at end of file
1526
=== modified file 'src/Backend/SynapseSearch.vala'
--- src/Backend/SynapseSearch.vala 2014-06-29 14:50:36 +0000
+++ src/Backend/SynapseSearch.vala 2015-05-20 17:20:28 +0000
@@ -23,7 +23,8 @@
23 private static Type[] plugins = {23 private static Type[] plugins = {
24 typeof (Synapse.CalculatorPlugin),24 typeof (Synapse.CalculatorPlugin),
25 typeof (Synapse.CommandPlugin),25 typeof (Synapse.CommandPlugin),
26 typeof (Synapse.DesktopFilePlugin)26 typeof (Synapse.DesktopFilePlugin),
27 typeof (Synapse.SystemManagementPlugin)
27 };28 };
2829
29 private static Synapse.DataSink? sink = null;30 private static Synapse.DataSink? sink = null;

Subscribers

People subscribed via source and target branches