~ubuntu-branches/ubuntu/maverick/unity/maverick

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
 * Copyright (C) 2010 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by Neil Jagdish Patel <neil.patel@canonical.com>
 *
 */

using GLib;
using Gee;

namespace Unity.Places
{
  /**
   * Represents a Place through a .place file ("offline") and then through
   * DBus ("online").
   **/
  public class Place : Object
  {
    const string PLACE_GROUP = "Place";
    const string ENTRY_PREFIX = "Entry:";
    const string ACTIVATION_GROUP = "Activation";

    /* Properties */
    public string dbus_name { get; construct; }
    public string dbus_path { get; construct; }

    public int n_entries {
      get { return entries_array.size; }
    }

    /* Whether the Place is available on the bus, this is only when we want to
     * do optimisations for startup (showing the entries before actually
     * starting the daemon. We can also abuse this for testing :)
     */
    public bool   online    { get; set; }

    private DBus.Connection?     connection;
    private dynamic DBus.Object? service;
    private dynamic DBus.Object? activation_service;

    private ArrayList<PlaceEntry> entries_array;

    /* Activate API stuff */
    public Regex? uri_regex;
    public Regex? mime_regex;

    /* Signals */
    public signal void entry_added   (PlaceEntry entry);
    public signal void entry_removed (PlaceEntry entry);

    /* Constructors */
    public Place (string dbus_name, string dbus_path)
    {
      Object (dbus_name:dbus_name, dbus_path:dbus_path);
    }

    construct
    {
      online = false;
      entries_array = new ArrayList<PlaceEntry> ();
    }

    /* Public Methods */
    public static Place? new_from_keyfile (KeyFile file, string id = "Unknown")
    {
      string errmsg = @"Unable to load place '$id': %s";

      if (file.has_group (PLACE_GROUP) == false)
        {
          warning (errmsg,"Does not contain 'Place' group");
          return null;
        }

      try {
        var dbus_name = file.get_string (PLACE_GROUP, "DBusName");
        var dbus_path = file.get_string (PLACE_GROUP, "DBusObjectPath");

        var place = new Place (dbus_name, dbus_path);
        place.load_keyfile_entries (file);

        /* Activation hooks */
        try {
          var uri_match = file.get_string (ACTIVATION_GROUP, "URIPattern");
          if (uri_match != null && uri_match != "")
            {
              try {
                place.uri_regex = new Regex (uri_match);
              } catch (Error e) {
                warning (@"Unable to compile regex pattern $uri_match: $(e.message)");
              }
            }

          var mime_match = file.get_string (ACTIVATION_GROUP, "MimetypePattern");
          if (mime_match != null && mime_match != "")
            {
              try {
                place.mime_regex = new Regex (mime_match);
              } catch (Error e) {
                warning (@"Unable to compile regex pattern $mime_match: $(e.message)");
              }
            }
        } catch (Error e) {
          //fail silently, not all places will have activation hooks
        }

        return place;

      } catch  (Error e) {
          warning (errmsg, e.message);
          return null;
      }
    }

    public unowned ArrayList<PlaceEntry> get_entries ()
    {
      return entries_array;
    }

    public PlaceEntry? get_nth_entry (int index)
    {
      return entries_array.get (index);
    }

    /*
     * Connect to the place over DBus, refresh the entries, and finally ask
     * the updated list of entries to connect
     */
    public new void connect ()
    {
      if (online)
        return;

      /* Grab the place off DBus */
      try {
        connection = DBus.Bus.get (DBus.BusType.SESSION);
        service = connection.get_object (dbus_name,
                                         dbus_path,
                                         "com.canonical.Unity.Place");
      } catch (Error e) {
        warning (@"Unable to connect to $dbus_path on $dbus_name: %s",
                 e.message);
        return;
      }

      /* Connect to necessary signals */
      service.EntryAdded.connect (on_service_entry_added);
      service.EntryRemoved.connect (on_service_entry_removed);

      /* Make sure our entries are up-to-date */
      /* FIXME: In a world without Vala this would be async, however due to
       * it's troubles with marshalling complex types (which I concede cannot
       * be easy), we need to do a poor-mans async and call it in an idle
       * instead.
       */
      Idle.add (() => {
        ValueArray[] entries = service.get_entries ();
        for (int i = 0; i < entries.length; i++)
          {
            unowned ValueArray array = entries[i];
            var path = array.get_nth (0).get_string ();
            bool existing = false;

            foreach (PlaceEntry e in entries_array)
              {
                var entry = e as PlaceEntryDbus;
                if (entry.dbus_path == path)
                  {
                    entry.update_info (array);
                    entry.connect ();
                    entry.parent = this;
                    existing = true;
                  }
              }

            if (existing == false)
              {
                on_service_entry_added (service, array);
              }
          }

        /* Now remove those that couldn't connect or did not exist in the live
         * place
         */
        /* FIXME: Make this cleaner */
        ArrayList<PlaceEntry> old = new ArrayList<PlaceEntry> ();
        foreach (PlaceEntry entry in entries_array)
          {
            if (entry.online == false)
              old.add (entry);
          }
        foreach (PlaceEntry entry in old)
          {
            entry_removed (entry);
            entries_array.remove (entry);
          }
        return false;
      });

      online = true;
    }

    /* Dispatch an activation request to the place daemon,
     * if it has any registered handlers */
    public ActivationStatus activate (string uri, string mimetype)
    {
      bool remote_activation = false;
    
      if (uri == ".")
        remote_activation = true;
      else if (uri_regex != null && uri_regex.match (uri))
        remote_activation = true;
      else if (mime_regex != null && mime_regex.match (mimetype))
        remote_activation = true;
      
      if (!remote_activation)
        {
          activate_fallback.begin (uri);
          return ActivationStatus.ACTIVATED_FALLBACK;
        }
      
      if (activation_service == null)
        {
            activation_service = connection.get_object (dbus_name,
                                                        dbus_path,
                                                        "com.canonical.Unity.Activation");
        }
      
      // FIXME: This should be async
      uint32 result = activation_service.activate (uri);
      
      switch (result)
      {
        case 0:
          activate_fallback.begin (uri);
          return ActivationStatus.ACTIVATED_FALLBACK;
        case 1:
          return ActivationStatus.ACTIVATED_SHOW_DASH;
        case 2:
          return ActivationStatus.ACTIVATED_HIDE_DASH;
        default:
          warning ("Illegal response from com.canonical.Unity.Activation.Activate: %u", result);
          return ActivationStatus.NOT_ACTIVATED;
      }
    }

    public static async void activate_fallback (string uri)
    {
      if (uri.has_prefix ("application://"))
        {
          var id = uri.offset ("application://".length);

          AppInfo info;
          try {
            //var appinfos = AppInfoManager.get_instance ();
            // FIXME: Use async IO with: yield appinfos.lookup_async (id);
            // But this ^^ would cause the appinfo to be created without
            // an id which causes the zeitgeist-gio module not to log the
            // application launch event in Zeitgeist
            info = new DesktopAppInfo (id);
          } catch (Error ee) {
            warning ("Unable to read .desktop file '%s': %s", uri, ee.message);
            return;
          }

          if (info is AppInfo)
            {
              try {
                info.launch (null,null);
              } catch (Error e) {
                warning ("Unable to launch desktop file %s: %s\n",
                         id,
                         e.message);
              }
            }
          else
            {
              warning ("%s is an invalid DesktopAppInfo id\n", id);
            }
          return;
        }

      try {
        Gtk.show_uri (Gdk.Screen.get_default (),
                      uri,
                      0);
       } catch (GLib.Error eee) {
         warning ("Unable to launch: %s\n", eee.message);
       }
    }
   
    /* Private Methods */
    private void on_service_entry_added (dynamic DBus.Object   dbus_object,
                                         ValueArray            info)
    {
      debug ("EntryAdded %s", info.get_nth (0).get_string ());
      /* This is a new entry */
      var entry = new PlaceEntryDbus (dbus_name,
                                  info.get_nth (0).get_string ());
      entry.update_info (info);
      entries_array.add (entry);
      entry.connect ();
      entry_added (entry);
    }

    private void on_service_entry_removed (dynamic DBus.Object dbus_object,
                                           string              entry_path)
    {
      debug (@"EntryRemoved: $entry_path");
      PlaceEntry? entry = null;

      foreach (PlaceEntry ent in entries_array)
        {
          var e = ent as PlaceEntryDbus;
          if (e.dbus_path == entry_path)
            {
              entry = e;
              break;
            }
        }
      if (entry is PlaceEntry)
        {
          entry_removed (entry);
          entries_array.remove (entry);
        }
    }

    private void load_keyfile_entries (KeyFile file)
    {
      /* We've got the basic Place, now to try and load in any entry information
       * that exists
       */
      var groups = file.get_groups ();
      foreach (string group in groups)
        {
          if (group.has_prefix (ENTRY_PREFIX))
            {
              PlaceEntry? entry = load_entry_from_keyfile (file, group);
              if (entry is PlaceEntry)
                {
                  entries_array.add (entry);
                  entry_added (entry);
                }
            }
        }
    }

    private PlaceEntry? load_entry_from_keyfile (KeyFile file, string group)
    {
      string path = "";
      string name = "";
      string icon = "";
      string desc = "";
      bool show_global = false;
      bool show_entry = false;

      try {
          path = file.get_string (group, "DBusObjectPath");

          /* As we use the path to match those entries from the online daemon
           * to the loaded state, we really need the path
           */
          if (path == null || path == "" || path[0] != '/')
            {
              warning (@"Cannot load entry '$group': Does not contain valid DBusObjectPath: $path");
              return null;
            }

          var domain = file.get_string (KeyFileDesktop.GROUP,
                                        "X-Ubuntu-Gettext-Domain");
          name = dgettext (domain, file.get_string (group, "Name"));
          icon = file.get_string (group, "Icon");
          desc = dgettext (domain, file.get_string (group, "Description"));
      } catch (Error e) {
        warning (@"Cannot load entry '$group': %s", e.message);
        return null;
      }

      try {
        show_global = file.get_boolean (group, "ShowGlobal");
        show_entry = file.get_boolean (group, "ShowEntry");
      } catch (Error e) {
          show_global = true;
          show_entry = true;
      }

      return new PlaceEntryDbus.with_info (dbus_name,
                                           path,
                                           name,
                                           icon,
                                           desc,
                                           show_global,
                                           show_entry);
    }
  }
  
  /**
   * Return values for Unity.Places.Place.activate().
   * NOTE: These values do *not* coincide with the dbus wire
   *       values fo com.canonical.Unity.Activation.Activate()
   */
  public enum ActivationStatus
  {
    NOT_ACTIVATED,       /* Redrum! Something bad happened */
    ACTIVATED_FALLBACK,  /* No remote activation, local fallback */
    ACTIVATED_SHOW_DASH, /* Remote activation. Keep showing the dash */
    ACTIVATED_HIDE_DASH  /* Remote activation. Hide the dash */
  }
}