~mhr3/libunity/remote-scope-sources

« back to all changes in this revision

Viewing changes to src/unity-launcher.vala

  • Committer: Tarmac
  • Author(s): Mikkel Kamstrup Erlandsen
  • Date: 2011-12-06 12:23:20 UTC
  • mfrom: (94.1.5 faves-enumerator)
  • Revision ID: tarmac-20111206122320-kelksd90omdjles1
Add Unity.LauncherFavorites API for inspecting the favorite apps in the launcher

Note that this is a read-only API intentionally. The Unity policy is that the
user alone is in control of the launcher. You can listen for changes, and the
ordering is guaranteed to match that of Unity.. Fixes: https://bugs.launchpad.net/bugs/900219. Appoved by Michal Hruby.

Show diffs side-by-side

added added

removed removed

Lines of Context:
343
343
      props.insert ("quicklist", l._object_path);
344
344
    
345
345
    return props;
346
 
  }  
347
 
 
 
346
  }
 
347
  
 
348
  public class LauncherFavorites : Object
 
349
  {
 
350
    public signal void changed ();
 
351
    
 
352
    private static LauncherFavorites? singleton = null;
 
353
    private Settings settings;
 
354
    
 
355
    /* We keep both a map and a list in order to have the correct sorting */
 
356
    private HashTable<string,AppInfo?> fav_cache;
 
357
    private string[] fav_list;
 
358
    
 
359
    private LauncherFavorites ()
 
360
    {
 
361
      settings = new Settings ("com.canonical.Unity.Launcher");
 
362
      fav_cache = new HashTable<string,AppInfo?> (str_hash, str_equal);
 
363
      reset_fav_cache ();
 
364
      
 
365
      settings.changed["favorites"].connect (reset_fav_cache);
 
366
    }
 
367
    
 
368
    private void reset_fav_cache ()
 
369
    {
 
370
      fav_cache.remove_all ();
 
371
      fav_list = settings.get_strv ("favorites");
 
372
      foreach (string id in fav_list)
 
373
      {
 
374
        fav_cache.insert (id, null);
 
375
      }
 
376
      
 
377
      changed ();
 
378
    }
 
379
    
 
380
    /**
 
381
     * Get the default singleton Unity.LauncherFavorites instance, creating it
 
382
     * dynamically if necessary.
 
383
     *
 
384
     * @return: (transfer none): The singleton Unity.LauncherFavorites.
 
385
     *                          If calling from C do not free this instance.
 
386
     *
 
387
     */
 
388
    public static unowned LauncherFavorites get_default ()
 
389
    {
 
390
      if (singleton == null)
 
391
        singleton = new LauncherFavorites ();
 
392
        
 
393
      return singleton;
 
394
    }
 
395
    
 
396
    public bool has_app_info (AppInfo appinfo) {
 
397
      if (appinfo.get_id () == null)
 
398
        {
 
399
          critical ("Can not look up favorite for AppInfo with NULL id");
 
400
          return false;
 
401
        }
 
402
      
 
403
      return has_app_id (appinfo.get_id ());
 
404
    }
 
405
    
 
406
    public bool has_app_id (string app_id)
 
407
    {
 
408
      return fav_cache.lookup_extended (app_id, null, null);
 
409
    }
 
410
    
 
411
    public AppInfo? lookup (string app_id)
 
412
    {
 
413
      AppInfo? appinfo;
 
414
      bool has_id = fav_cache.lookup_extended (app_id, null, out appinfo);
 
415
      
 
416
      /* If we don't have the appinfo cached, pull it out of the
 
417
       * AppInfoManager and cache it */
 
418
      if (has_id)
 
419
        {
 
420
          if (appinfo != null)
 
421
            return appinfo;
 
422
          
 
423
          var appman = AppInfoManager.get_default ();
 
424
          appinfo = appman.lookup (app_id);
 
425
          fav_cache.insert (app_id, appinfo);
 
426
          
 
427
          // FIXME: We really should return a dummy AppInfo for faves
 
428
          //        where we can't find the .desktop file...
 
429
          if (appinfo == null)
 
430
            {
 
431
              critical ("Can't find AppInfo for favorite '%s'", app_id);
 
432
              return null;
 
433
            }
 
434
          
 
435
          return appinfo;
 
436
        }
 
437
      return null;
 
438
    }
 
439
    
 
440
    public string[] enumerate_ids ()
 
441
    {
 
442
      return fav_list;
 
443
    }
 
444
    
 
445
    public AppInfo[] enumerate_app_infos ()
 
446
    {
 
447
      int i = 0;
 
448
      var infos = new AppInfo[fav_cache.size ()];
 
449
      
 
450
      foreach (string id in fav_list)
 
451
      {
 
452
        var appinfo = lookup (id);
 
453
        infos[i] = appinfo;
 
454
        i++;
 
455
      }
 
456
      
 
457
      return infos;
 
458
    }
 
459
  }
 
460
  
348
461
} /* namespace */