2
* Copyright (C) 2010 Michal Hruby <michal.mhr@gmail.com>
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.
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.
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.
18
* Authored by Michal Hruby <michal.mhr@gmail.com>
24
[DBus (name = "org.freedesktop.UPower")]
25
public interface UPowerObject: Object
27
public const string UNIQUE_NAME = "org.freedesktop.UPower";
28
public const string OBJECT_PATH = "/org/freedesktop/UPower";
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;
35
public abstract async void about_to_sleep () throws IOError;
38
[DBus (name = "org.freedesktop.ConsoleKit.Manager")]
39
public interface ConsoleKitObject: Object
41
public const string UNIQUE_NAME = "org.freedesktop.ConsoleKit";
42
public const string OBJECT_PATH = "/org/freedesktop/ConsoleKit/Manager";
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;
50
public class SystemManagementPlugin: Object, Activatable, ItemProvider
52
public bool enabled { get; set; default = true; }
54
public void activate ()
59
public void deactivate ()
64
private abstract class SystemAction: Object, Match
66
// for Match interface
67
public string title { get; construct set; }
68
public string description { get; set; default = ""; }
69
public string icon_name { get; construct set; default = ""; }
70
public bool has_thumbnail { get; construct set; default = false; }
71
public string thumbnail_path { get; construct set; }
72
public MatchType match_type { get; construct set; }
74
public void execute (Match? match)
79
public abstract void do_action ();
80
public abstract bool action_allowed ();
83
private class SuspendAction: SystemAction
85
public SuspendAction ()
87
Object (match_type: MatchType.ACTION, title: _ ("Suspend"),
88
description: _ ("Put your computer into suspend mode"),
89
icon_name: "system-suspend", has_thumbnail: false);
94
check_allowed.begin ();
97
private async void check_allowed ()
101
UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
102
UPowerObject.UNIQUE_NAME,
103
UPowerObject.OBJECT_PATH);
105
allowed = yield dbus_interface.suspend_allowed ();
109
warning ("%s", err.message);
114
private bool allowed = false;
116
public override bool action_allowed ()
121
private async void do_suspend ()
125
UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
126
UPowerObject.UNIQUE_NAME,
127
UPowerObject.OBJECT_PATH);
131
yield dbus_interface.about_to_sleep ();
133
catch (Error not_there_error) { }
135
GnomeScreenSaverPlugin.lock_screen ();
137
Timeout.add (2000, do_suspend.callback);
140
yield dbus_interface.suspend ();
144
warning ("%s", err.message);
148
public override void do_action ()
154
private class HibernateAction: SystemAction
156
public HibernateAction ()
158
Object (match_type: MatchType.ACTION, title: _ ("Hibernate"),
159
description: _ ("Put your computer into hibernation mode"),
160
icon_name: "system-hibernate", has_thumbnail: false);
165
check_allowed.begin ();
168
private async void check_allowed ()
172
UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
173
UPowerObject.UNIQUE_NAME,
174
UPowerObject.OBJECT_PATH);
176
allowed = yield dbus_interface.hibernate_allowed ();
180
warning ("%s", err.message);
185
private bool allowed = false;
187
public override bool action_allowed ()
192
private async void do_hibernate ()
196
UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
197
UPowerObject.UNIQUE_NAME,
198
UPowerObject.OBJECT_PATH);
202
yield dbus_interface.about_to_sleep ();
204
catch (Error not_there_error) { }
206
GnomeScreenSaverPlugin.lock_screen ();
208
Timeout.add (2000, do_hibernate.callback);
210
dbus_interface.hibernate ();
214
warning ("%s", err.message);
218
public override void do_action ()
220
do_hibernate.begin ();
224
private class ShutdownAction: SystemAction
226
public ShutdownAction ()
228
Object (match_type: MatchType.ACTION, title: _ ("Shut Down"),
229
description: _ ("Turn your computer off"),
230
icon_name: "system-shutdown", has_thumbnail: false);
235
check_allowed.begin ();
238
private async void check_allowed ()
242
ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
243
ConsoleKitObject.UNIQUE_NAME,
244
ConsoleKitObject.OBJECT_PATH);
246
allowed = yield dbus_interface.can_stop ();
250
warning ("%s", err.message);
255
private bool allowed = false;
257
public override bool action_allowed ()
262
public override void do_action ()
266
ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
267
ConsoleKitObject.UNIQUE_NAME,
268
ConsoleKitObject.OBJECT_PATH);
270
dbus_interface.stop ();
274
warning ("%s", err.message);
279
private class RestartAction: SystemAction
281
public RestartAction ()
283
Object (match_type: MatchType.ACTION, title: _ ("Restart"),
284
description: _ ("Restart your computer"),
285
icon_name: "system-restart", has_thumbnail: false);
290
check_allowed.begin ();
293
private async void check_allowed ()
297
ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
298
ConsoleKitObject.UNIQUE_NAME,
299
ConsoleKitObject.OBJECT_PATH);
301
allowed = yield dbus_interface.can_restart ();
305
warning ("%s", err.message);
310
private bool allowed = false;
312
public override bool action_allowed ()
317
public override void do_action ()
321
ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
322
ConsoleKitObject.UNIQUE_NAME,
323
ConsoleKitObject.OBJECT_PATH);
325
dbus_interface.restart ();
329
warning ("%s", err.message);
334
static void register_plugin ()
336
DataSink.PluginRegistry.get_default ().register_plugin (
337
typeof (SystemManagementPlugin),
339
_ ("Suspend, hibernate, restart or shutdown your computer."),
342
DBusService.get_default ().service_is_available (ConsoleKitObject.UNIQUE_NAME),
343
_ ("ConsoleKit wasn't found")
352
private Gee.List<SystemAction> actions;
356
actions = new Gee.LinkedList<SystemAction> ();
357
actions.add (new SuspendAction ());
358
actions.add (new HibernateAction ());
359
actions.add (new ShutdownAction ());
360
actions.add (new RestartAction ());
363
public async ResultSet? search (Query q) throws SearchError
365
// we only search for actions
366
if (!(QueryFlags.ACTIONS in q.query_type)) return null;
368
var result = new ResultSet ();
370
var matchers = Query.get_matchers_for_query (q.query_string, 0,
371
RegexCompileFlags.OPTIMIZE | RegexCompileFlags.CASELESS);
373
foreach (var action in actions)
375
if (!action.action_allowed ()) continue;
376
foreach (var matcher in matchers)
378
if (matcher.key.match (action.title))
380
result.add (action, matcher.value - Match.Score.INCREMENT_SMALL);
386
q.check_cancellable ();