~timo-jyrinki/ubuntu/quantal/libunity/ubuntu.6120

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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
 * Copyright (C) 2011 Canonical, Ltd.
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * version 3.0 as published by the Free Software Foundation.
 *
 * This library 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 Lesser General Public License version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
 *
 */

/*
 * IMPLEMENTATION NOTE:
 * We want the generated C API to be nice and not too Vala-ish. We must
 * anticipate that libunity consumers will be written in both Vala , C,
 * and through GObject Introspection
 *
 */
 
using GLib;

namespace Unity {  

  /* Private class to wire up the DBus stuff. Private so that we don't
   * leak DBus implementation details into the public API */
  [DBus (name = "com.canonical.Unity.LauncherEntry")]
  private class LauncherEntryDBusImpl : Object
  {
    
    public weak LauncherEntry? owner;
    
    public LauncherEntryDBusImpl (DBusConnection conn,
                                  ObjectPath     object_path,
                                  LauncherEntry  owner)
    {
      try {
        conn.register_object (object_path, this);
      } catch (IOError e) {
        warning ("Unable to connecto to session bus. Unable to control " +
                 "LauncherEntry for %s", object_path);
      }
      
      this.owner = owner;
    }

    public signal void update (string app_uri,
                               HashTable<string,Variant> properties);
    
    public HashTable<string,Variant> query ()
    {
      /*var props = new HashTable<string,Variant>(str_hash, str_equal);
      
      if (owner == null)
        return props;
      
      props.insert ("count", owner.count);
      props.insert ("count-visible", owner.count_visible);
      props.insert ("progress", owner.progress);
      props.insert ("progress-visible", owner.progress_visible);
      
      if (owner.quicklist != null)
        props.insert ("quicklist", owner._object_path);
      
      return props;*/
      if (owner == null)
        return new HashTable<string,Variant>(str_hash, str_equal);
      
      return collect_launcher_entry_properties (owner);
    }
  }

  /**
   * This class represents your control point for your application's icon
   * in the Unity Launcher. You can control properties such as a counter,
   * progress, or emblem that will be overlaid on your application's launcher
   * icon. You can also set a quicklist on it by setting the "quicklist"
   * property to point at the Dbusmenu.Menuitem which is the root of your
   * quicklist.
   *
   * Create a LauncherEntry by giving your desktop file id to the constructor
   * (eg. "myapp.desktop").
   */
  public class LauncherEntry : Dee.Serializable, Object
  {
    public string app_uri {get; set construct; }
    
    public int64 count { get; set; default = 0; }
    public bool count_visible { get; set; default = false; }
    
    public double progress { get; set; default = 0.0; }
    public bool progress_visible { get; set; default = false; }
    
    public bool urgent { get; set; default = false; }
    
    private Dbusmenu.Menuitem? _quicklist;
    public Dbusmenu.Menuitem? quicklist {
      get { return _quicklist; }
      set {
        _quicklist = value;
        if (_quicklist != null) {
          _quicklist_server = new Dbusmenu.Server (_object_path);
          _quicklist_server.root_node = _quicklist;
        }
      }
    }
        
    private HashTable<string,Variant> _queued_properties;
    private Dbusmenu.Server?          _quicklist_server;
    private uint                      _property_source_id;
    private DBusConnection            _bus;
    private LauncherEntryDBusImpl     _dbus_impl;
    internal ObjectPath                _object_path;
    
    /* Global map of LauncherEntries indexed by their application:// URIs.
     * We use this to avoid having more than one instance of a LauncherEntry
     * for the same app per process. That could give some confusing results :-)
     */
    private static HashTable<string, LauncherEntry> global_entries_map = null;     
    
    static construct {
    	Dee.Serializable.register_parser (typeof (LauncherEntry),
    	                                  new VariantType ("(sa{sv})"),
    	                                  LauncherEntry.parse_serializable);
    }
  
    construct
    {     
      _queued_properties = new HashTable<string,Variant>(str_hash, str_equal);
      _quicklist_server = null;
      _property_source_id = 0;
     
     try {       
        _bus = Bus.get_sync (BusType.SESSION);
        _object_path = new ObjectPath (@"/com/canonical/unity/launcherentry/$(app_uri.hash())");
        _dbus_impl = new LauncherEntryDBusImpl (_bus, _object_path, this);
      
        var inspector = Inspector.get_default();
        inspector.notify["unity-running"].connect (on_unity_running_changed);
      
        /* Only start queueing property change notifications if we've acquired
         * a connection to the bus (above code guaratees that). Witout a bus
         * connection things would go boom later on otherwise */
        this.notify.connect (queue_property_notification);
      } catch (IOError e) {
        critical ("Unable to connect to session bus: %s", e.message);
      }
      
    }
  
    /**
     * Create a new LauncherEntry for the desktop file id of your application.
     *
     * This constructor is private because consumers should create instances
     * via the static getter methods on this class to avoid duplicate entries
     * for the same application.
     *
     * The desktop file id is defined as the basename of your application's
     * .desktop file (including the extension), eg. myapp.desktop.
     */
    private LauncherEntry(string app_uri)
    {
      Object (app_uri : app_uri);
    }
    
    public static LauncherEntry get_for_app_uri (string app_uri)
    {
      if (global_entries_map == null)
        global_entries_map =
                new HashTable<string, LauncherEntry> (str_hash, str_equal);
      
      LauncherEntry? entry = global_entries_map.lookup (app_uri);
      if (entry != null)
        {
          return entry;
        }
      
      entry = new LauncherEntry (app_uri);
      global_entries_map.insert (app_uri, entry);
      return entry;
    }
    
    public static LauncherEntry get_for_desktop_id (string desktop_id)
    {
      return LauncherEntry.get_for_app_uri ("application://" + desktop_id);
    }
    
    public static LauncherEntry get_for_desktop_file (string desktop_file)
    {
      return LauncherEntry.get_for_desktop_id (Path.get_basename (desktop_file));
    }
    
    /* Implement interface Dee.Serializable */
    public Variant serialize ()
    {
      /* Vala will automagically marhshal the properties into a 'a{sv}' Variant */
      HashTable<string,Variant> hash = collect_launcher_entry_properties (this);
      Variant props = hash;
      Variant _app_uri = new Variant.string (app_uri);
      return new Variant.tuple(new Variant[2]{_app_uri, props});
    }
    
    private static Object parse_serializable (Variant data)
    {
      /* Dee guarantees that data has signature "(sa{sv})" as we registered */
      string app_uri = data.get_child_value(0).get_string();
      Variant props = data.get_child_value(1);
      
      var self = LauncherEntry.get_for_app_uri (app_uri);
      
      int64 count;
      if (props.lookup("count", "x", out count))
        self.count = count;
      
      bool visible;
      if (props.lookup("count-visible", "b", out visible))
        self.count_visible = visible;
      
      double progress;
      if (props.lookup("count-visible", "d", out progress))
        self.progress = progress;
      
      if (props.lookup("progress-visible", "b", out visible))
        self.progress_visible = visible;
      
      bool urgent;
      if (props.lookup("urgent", "b", out urgent))
        self.urgent = urgent;
      
      string quicklist_path;
	  if (props.lookup("quicklist", "s", out quicklist_path))
	    {
	      if (quicklist_path != "")
          	self._object_path = new ObjectPath (quicklist_path);
        }
      
      return self;
    }
    
    private void queue_property_notification (Object self, ParamSpec pspec)
    {
      Variant? v;
      string object_path;
      
      switch (pspec.name)
      {
        case "count":
          v = this.count;
          break;
        case "count-visible":
          v = this.count_visible;
          break;
        case "progress":
          v = this.progress;
          break;
        case "progress-visible":
          v = this.progress_visible;
          break;
        case "urgent":
          v = this.urgent;
          break;
        case "quicklist":
          if (_quicklist_server != null)
            {
              _quicklist_server.get ("dbus-object", out object_path);
              v = object_path;
            }
          else
            {
              v = "";
            }
          break;
        default:
          /* Assume that this is a property we want to ignore wrt DBus */
          v = null;
          break;
      }
      
      if (v != null)
        {
          _queued_properties.insert (pspec.name, v);
        }
      
      if (_property_source_id == 0)
        {
          _property_source_id = Idle.add (dispatch_property_notification);
        }
    }
    
    private bool dispatch_property_notification ()
    {
      /* Emit DBus signal with our changes if Unity is running.
       * If it's not running at this point we'll sync all our state
       * when it gets up */
      if (Inspector.get_default().unity_running)
        _dbus_impl.update (app_uri, _queued_properties);

      /* Reset state */
      _property_source_id = 0;
      _queued_properties.remove_all ();
      
      return false;
    }
    
    /* Callback for when Unity comes or goes */
    private void on_unity_running_changed (Object _inspector, ParamSpec pspec)
    {
      Inspector inspector = _inspector as Inspector;
      
      /* If Unity has just come online sync all out props to it */
      if (inspector.unity_running)
        {
          try{
            _bus.emit_signal (null, _object_path,
                              "com.canonical.Unity.LauncherEntry",
                              "Update", this.serialize ());
          } catch (Error e) {
            warning ("Failed to emit com.canonical.Unity.LauncherEntry.Update on the session bus: %s", e.message);
          }
        }
    }
    
  } /* class Unity.LauncherEntry */

  private static HashTable<string,Variant> collect_launcher_entry_properties (LauncherEntry l)
  {
    var props = new HashTable<string,Variant>(str_hash, str_equal);
    
    props.insert ("count", l.count);
    props.insert ("count-visible", l.count_visible);
    props.insert ("progress", l.progress);
    props.insert ("progress-visible", l.progress_visible);
    props.insert ("urgent", l.urgent);
    
    if (l.quicklist != null)
      props.insert ("quicklist", l._object_path);
    
    return props;
  }
  
  public class LauncherFavorites : Object
  {
    public signal void changed ();
  
    private const string LAUNCHER_SCHEMA_NAME = "com.canonical.Unity.Launcher";
    private static LauncherFavorites? singleton = null;
    private Settings settings;
    
    /* We keep both a map and a list in order to have the correct sorting */
    private HashTable<string,AppInfo?> fav_cache;
    private string[] fav_list;

    private LauncherFavorites ()
    {
      fav_cache = new HashTable<string,AppInfo?> (str_hash, str_equal);
      if (schema_exists (LAUNCHER_SCHEMA_NAME))
      {
        settings = new Settings (LAUNCHER_SCHEMA_NAME);
        reset_fav_cache ();

        settings.changed["favorites"].connect (reset_fav_cache);
      }
      else
      {
        warning ("Schema \"%s\" is not installed!", LAUNCHER_SCHEMA_NAME);
      }
    }

    private static bool schema_exists (string schema)
    {
      unowned string[] installed_schemas = Settings.list_schemas ();
      return schema in installed_schemas;
    }

    private void reset_fav_cache ()
    {
      fav_cache.remove_all ();
      fav_list = {};

      const string APP_PREFIX = "application://";
      const string FILE_PREFIX = "file://";

      foreach (string id in settings.get_strv ("favorites"))
      {
        if (id.has_prefix (APP_PREFIX))
          id = id.substring (APP_PREFIX.length);
        else if (id.has_prefix (FILE_PREFIX))
          id = id.substring (FILE_PREFIX.length);
        else if (id.index_of ("://") != -1)
          continue;

        fav_list += id;
        fav_cache.insert (id, null);
      }

      changed ();
    }

    /**
     * Get the default singleton Unity.LauncherFavorites instance, creating it
     * dynamically if necessary.
     *
     * @return The singleton Unity.LauncherFavorites.
     *         If calling from C do not free this instance.
     *
     */
    public static unowned LauncherFavorites get_default ()
    {
      if (singleton == null)
        singleton = new LauncherFavorites ();
        
      return singleton;
    }
    
    public bool has_app_info (AppInfo appinfo) {
      if (appinfo.get_id () == null)
        {
          critical ("Can not look up favorite for AppInfo with NULL id");
          return false;
        }
      
      return has_app_id (appinfo.get_id ());
    }
    
    public bool has_app_id (string app_id)
    {
      return fav_cache.lookup_extended (app_id, null, null);
    }
    
    public AppInfo? lookup (string app_id)
    {
      AppInfo? appinfo;
      bool has_id = fav_cache.lookup_extended (app_id, null, out appinfo);
      
      /* If we don't have the appinfo cached, pull it out of the
       * AppInfoManager and cache it */
      if (has_id)
        {
          if (appinfo != null)
            return appinfo;
          
          var appman = AppInfoManager.get_default ();
          appinfo = appman.lookup (app_id);
          fav_cache.insert (app_id, appinfo);
          
          // FIXME: We really should return a dummy AppInfo for faves
          //        where we can't find the .desktop file...
          if (appinfo == null)
            {
              critical ("Can't find AppInfo for favorite '%s'", app_id);
              return null;
            }
          
          return appinfo;
        }
      return null;
    }
    
    public string[] enumerate_ids ()
    {
      return fav_list;
    }
    
    public AppInfo[] enumerate_app_infos ()
    {
      int i = 0;
      var infos = new AppInfo[fav_cache.size ()];
      
      foreach (string id in fav_list)
      {
        var appinfo = lookup (id);
        infos[i] = appinfo;
        i++;
      }
      
      return infos;
    }
  }
  
} /* namespace */