~elementary-apps/slingshot/synapse-desktop-only

« back to all changes in this revision

Viewing changes to lib/synapse-plugins/system-management.vala

  • Committer: Tom Beckmann
  • Date: 2014-06-12 09:14:57 UTC
  • Revision ID: tomjonabc@gmail.com-20140612091457-1spb2qtytxybnvkg
Remove all plugins but desktop-file and command one, fix code style

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
22
 
namespace 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
 
  public class SystemManagementPlugin: Object, Activatable, ItemProvider
51
 
  {
52
 
    public bool enabled { get; set; default = true; }
53
 
 
54
 
    public void activate ()
55
 
    {
56
 
      
57
 
    }
58
 
 
59
 
    public void deactivate ()
60
 
    {
61
 
      
62
 
    }
63
 
 
64
 
    private abstract class SystemAction: Object, Match
65
 
    {
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; }
73
 
 
74
 
      public void execute (Match? match)
75
 
      {
76
 
        this.do_action ();
77
 
      }
78
 
 
79
 
      public abstract void do_action ();
80
 
      public abstract bool action_allowed ();
81
 
    }
82
 
 
83
 
    private class SuspendAction: SystemAction
84
 
    {
85
 
      public SuspendAction ()
86
 
      {
87
 
        Object (match_type: MatchType.ACTION, title: _ ("Suspend"),
88
 
                description: _ ("Put your computer into suspend mode"),
89
 
                icon_name: "system-suspend", has_thumbnail: false);
90
 
      }
91
 
 
92
 
      construct
93
 
      {
94
 
        check_allowed.begin ();
95
 
      }
96
 
 
97
 
      private async void check_allowed ()
98
 
      {
99
 
        try
100
 
        {
101
 
          UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
102
 
                                           UPowerObject.UNIQUE_NAME,
103
 
                                           UPowerObject.OBJECT_PATH);
104
 
 
105
 
          allowed = yield dbus_interface.suspend_allowed ();
106
 
        }
107
 
        catch (IOError err)
108
 
        {
109
 
          warning ("%s", err.message);
110
 
          allowed = false;
111
 
        }
112
 
      }
113
 
 
114
 
      private bool allowed = false;
115
 
 
116
 
      public override bool action_allowed ()
117
 
      {
118
 
        return allowed;
119
 
      }
120
 
      
121
 
      private async void do_suspend ()
122
 
      {
123
 
        try
124
 
        {
125
 
          UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
126
 
                                           UPowerObject.UNIQUE_NAME,
127
 
                                           UPowerObject.OBJECT_PATH);
128
 
 
129
 
          try
130
 
          {
131
 
            yield dbus_interface.about_to_sleep ();
132
 
          }
133
 
          catch (Error not_there_error) { }
134
 
          // yea kinda nasty
135
 
          GnomeScreenSaverPlugin.lock_screen ();
136
 
          // wait 2 seconds
137
 
          Timeout.add (2000, do_suspend.callback);
138
 
          yield;
139
 
 
140
 
          yield dbus_interface.suspend ();
141
 
        }
142
 
        catch (IOError err)
143
 
        {
144
 
          warning ("%s", err.message);
145
 
        }
146
 
      }
147
 
 
148
 
      public override void do_action ()
149
 
      {
150
 
        do_suspend.begin ();
151
 
      }
152
 
    }
153
 
 
154
 
    private class HibernateAction: SystemAction
155
 
    {
156
 
      public HibernateAction ()
157
 
      {
158
 
        Object (match_type: MatchType.ACTION, title: _ ("Hibernate"),
159
 
                description: _ ("Put your computer into hibernation mode"),
160
 
                icon_name: "system-hibernate", has_thumbnail: false);
161
 
      }
162
 
 
163
 
      construct
164
 
      {
165
 
        check_allowed.begin ();
166
 
      }
167
 
 
168
 
      private async void check_allowed ()
169
 
      {
170
 
        try
171
 
        {
172
 
          UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
173
 
                                           UPowerObject.UNIQUE_NAME,
174
 
                                           UPowerObject.OBJECT_PATH);
175
 
 
176
 
          allowed = yield dbus_interface.hibernate_allowed ();
177
 
        }
178
 
        catch (IOError err)
179
 
        {
180
 
          warning ("%s", err.message);
181
 
          allowed = false;
182
 
        }
183
 
      }
184
 
 
185
 
      private bool allowed = false;
186
 
 
187
 
      public override bool action_allowed ()
188
 
      {
189
 
        return allowed;
190
 
      }
191
 
      
192
 
      private async void do_hibernate ()
193
 
      {
194
 
        try
195
 
        {
196
 
          UPowerObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
197
 
                                           UPowerObject.UNIQUE_NAME,
198
 
                                           UPowerObject.OBJECT_PATH);
199
 
 
200
 
          try
201
 
          {
202
 
            yield dbus_interface.about_to_sleep ();
203
 
          }
204
 
          catch (Error not_there_error) { }
205
 
          // yea kinda nasty
206
 
          GnomeScreenSaverPlugin.lock_screen ();
207
 
          // wait 2 seconds
208
 
          Timeout.add (2000, do_hibernate.callback);
209
 
          yield;
210
 
          dbus_interface.hibernate ();
211
 
        }
212
 
        catch (IOError err)
213
 
        {
214
 
          warning ("%s", err.message);
215
 
        }
216
 
      }
217
 
 
218
 
      public override void do_action ()
219
 
      {
220
 
        do_hibernate.begin ();
221
 
      }
222
 
    }
223
 
 
224
 
    private class ShutdownAction: SystemAction
225
 
    {
226
 
      public ShutdownAction ()
227
 
      {
228
 
        Object (match_type: MatchType.ACTION, title: _ ("Shut Down"),
229
 
                description: _ ("Turn your computer off"),
230
 
                icon_name: "system-shutdown", has_thumbnail: false);
231
 
      }
232
 
 
233
 
      construct
234
 
      {
235
 
        check_allowed.begin ();
236
 
      }
237
 
 
238
 
      private async void check_allowed ()
239
 
      {
240
 
        try
241
 
        {
242
 
          ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
243
 
                                           ConsoleKitObject.UNIQUE_NAME,
244
 
                                           ConsoleKitObject.OBJECT_PATH);
245
 
 
246
 
          allowed = yield dbus_interface.can_stop ();
247
 
        }
248
 
        catch (IOError err)
249
 
        {
250
 
          warning ("%s", err.message);
251
 
          allowed = false;
252
 
        }
253
 
      }
254
 
 
255
 
      private bool allowed = false;
256
 
 
257
 
      public override bool action_allowed ()
258
 
      {
259
 
        return allowed;
260
 
      }
261
 
 
262
 
      public override void do_action ()
263
 
      {
264
 
        try
265
 
        {
266
 
          ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
267
 
                                           ConsoleKitObject.UNIQUE_NAME,
268
 
                                           ConsoleKitObject.OBJECT_PATH);
269
 
 
270
 
          dbus_interface.stop ();
271
 
        }
272
 
        catch (IOError err)
273
 
        {
274
 
          warning ("%s", err.message);
275
 
        }
276
 
      }
277
 
    }
278
 
 
279
 
    private class RestartAction: SystemAction
280
 
    {
281
 
      public RestartAction ()
282
 
      {
283
 
        Object (match_type: MatchType.ACTION, title: _ ("Restart"),
284
 
                description: _ ("Restart your computer"),
285
 
                icon_name: "system-restart", has_thumbnail: false);
286
 
      }
287
 
 
288
 
      construct
289
 
      {
290
 
        check_allowed.begin ();
291
 
      }
292
 
 
293
 
      private async void check_allowed ()
294
 
      {
295
 
        try
296
 
        {
297
 
          ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
298
 
                                           ConsoleKitObject.UNIQUE_NAME,
299
 
                                           ConsoleKitObject.OBJECT_PATH);
300
 
 
301
 
          allowed = yield dbus_interface.can_restart ();
302
 
        }
303
 
        catch (IOError err)
304
 
        {
305
 
          warning ("%s", err.message);
306
 
          allowed = false;
307
 
        }
308
 
      }
309
 
 
310
 
      private bool allowed = false;
311
 
 
312
 
      public override bool action_allowed ()
313
 
      {
314
 
        return allowed;
315
 
      }
316
 
 
317
 
      public override void do_action ()
318
 
      {
319
 
        try
320
 
        {
321
 
          ConsoleKitObject dbus_interface = Bus.get_proxy_sync (BusType.SYSTEM,
322
 
                                           ConsoleKitObject.UNIQUE_NAME,
323
 
                                           ConsoleKitObject.OBJECT_PATH);
324
 
 
325
 
          dbus_interface.restart ();
326
 
        }
327
 
        catch (IOError err)
328
 
        {
329
 
          warning ("%s", err.message);
330
 
        }
331
 
      }
332
 
    }
333
 
 
334
 
    static void register_plugin ()
335
 
    {
336
 
      DataSink.PluginRegistry.get_default ().register_plugin (
337
 
        typeof (SystemManagementPlugin),
338
 
        "System Management",
339
 
        _ ("Suspend, hibernate, restart or shutdown your computer."),
340
 
        "system-restart",
341
 
        register_plugin,
342
 
        DBusService.get_default ().service_is_available (ConsoleKitObject.UNIQUE_NAME),
343
 
        _ ("ConsoleKit wasn't found")
344
 
      );
345
 
    }
346
 
 
347
 
    static construct
348
 
    {
349
 
      register_plugin ();
350
 
    }
351
 
 
352
 
    private Gee.List<SystemAction> actions;
353
 
 
354
 
    construct
355
 
    {
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 ());
361
 
    }
362
 
 
363
 
    public async ResultSet? search (Query q) throws SearchError
364
 
    {
365
 
      // we only search for actions
366
 
      if (!(QueryFlags.ACTIONS in q.query_type)) return null;
367
 
 
368
 
      var result = new ResultSet ();
369
 
      
370
 
      var matchers = Query.get_matchers_for_query (q.query_string, 0,
371
 
        RegexCompileFlags.OPTIMIZE | RegexCompileFlags.CASELESS);
372
 
 
373
 
      foreach (var action in actions)
374
 
      {
375
 
        if (!action.action_allowed ()) continue;
376
 
        foreach (var matcher in matchers)
377
 
        {
378
 
          if (matcher.key.match (action.title))
379
 
          {
380
 
            result.add (action, matcher.value - Match.Score.INCREMENT_SMALL);
381
 
            break;
382
 
          }
383
 
        }
384
 
      }
385
 
 
386
 
      q.check_cancellable ();
387
 
 
388
 
      return result;
389
 
    }
390
 
  }
391
 
}