~kroq-gar78/unity-lens-music/fix-972987

« back to all changes in this revision

Viewing changes to src/rhythmbox-scope.vala

  • Committer: Tarmac
  • Author(s): David Callé, Michal Hruby
  • Date: 2012-03-21 12:40:37 UTC
  • mfrom: (69.1.33 rb-scope)
  • Revision ID: tarmac-20120321124037-p1lsomndx12g9qt7
Implement a Rhythmbox scope.. Fixes: https://bugs.launchpad.net/bugs/948860. Approved by Michal Hruby, Mikkel Kamstrup Erlandsen, Neil J. Patel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Canonical Ltd
 
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 version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by Alex Launi <alex.launi@canonical.com>
 
17
 *
 
18
 */
 
19
 
 
20
using GLib;
 
21
 
 
22
namespace Unity.MusicLens {
 
23
     
 
24
  public class RhythmboxScope : SimpleScope
 
25
  { 
 
26
    private RhythmboxCollection collection;
 
27
    private bool db_ready;
 
28
 
 
29
    public RhythmboxScope ()
 
30
    {
 
31
      base ();
 
32
 
 
33
      scope = new Unity.Scope ("/com/canonical/unity/scope/rhythmbox");
 
34
      scope.search_in_global = true;
 
35
      scope.activate_uri.connect (activate);
 
36
      scope.sources.add_option ("rhythmbox", _("Rhythmbox"), null);
 
37
 
 
38
      base.initialize ();
 
39
      collection = new RhythmboxCollection ();
 
40
      db_ready = false;
 
41
    }
 
42
 
 
43
    protected override int num_results_without_search { get {return 100; } }
 
44
    protected override int num_results_global_search { get { return 20; } }
 
45
    protected override int num_results_lens_search { get { return 50; } }
 
46
 
 
47
    /**
 
48
     * Tells banshee to play the selected uri(s)
 
49
     */
 
50
    public Unity.ActivationResponse activate (string uri)
 
51
    {
 
52
      string[] exec = {"rhythmbox-client", "--play-uri"};
 
53
 
 
54
      try {
 
55
        if (Uri.parse_scheme (uri) == "album")
 
56
          {
 
57
            debug (@"searching for tracks for $uri");
 
58
            string[] split = uri.split ("/");
 
59
            string artist = split[2];
 
60
            string title = split[3];
 
61
        
 
62
            Album album = new Album ();
 
63
            album.artist = artist;
 
64
            album.title = title;
 
65
            // FIXME there must be a better way..
 
66
//            foreach (string track in collection.get_track_uris (album))
 
67
//              exec += track;
 
68
          }
 
69
        else
 
70
          {
 
71
            exec += uri;
 
72
          }
 
73
    
 
74
        exec += null;
 
75
    
 
76
        debug ("Spawning rb '%s'", string.joinv (" ", exec));
 
77
        Process.spawn_async (null, 
 
78
                 exec,
 
79
                 null, 
 
80
                 SpawnFlags.SEARCH_PATH,
 
81
                 null, 
 
82
                 null);
 
83
    
 
84
        return new Unity.ActivationResponse (Unity.HandledType.HIDE_DASH);
 
85
      } catch (SpawnError error) {
 
86
        warning ("Failed to launch URI %s", uri);
 
87
        return new Unity.ActivationResponse (Unity.HandledType.NOT_HANDLED);
 
88
      }
 
89
    }
 
90
 
 
91
    public override async void perform_search (LensSearch search,
 
92
                                               SearchType search_type, 
 
93
                                               owned List<FilterParser> filters,
 
94
                                               int max_results = -1,
 
95
                                               Cancellable? cancellable = null)
 
96
    {
 
97
      int category_override = -1;
 
98
      if (search_type == SearchType.GLOBAL)
 
99
      {
 
100
        category_override = Category.MUSIC;
 
101
        // the lens shouldn't display anything for empty search
 
102
        if (is_search_empty (search)) return;
 
103
      }
 
104
 
 
105
      if (!db_ready)
 
106
      {
 
107
        // parse the DB lazily
 
108
        collection.parse_file ("%s/.local/share/rhythmbox/rhythmdb.xml".printf (Environment.get_home_dir ()));
 
109
        db_ready = true;
 
110
      }
 
111
 
 
112
      collection.search (search, search_type, filters,
 
113
                         max_results, category_override);
 
114
    }
 
115
  }
 
116
}