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

Subscribers

People subscribed via source and target branches