~ricmm/+junk/unity-lens_music-sc

« back to all changes in this revision

Viewing changes to src/banshee-scope.vala

  • Committer: Ricardo Mendoza
  • Date: 2012-09-05 14:20:15 UTC
  • Revision ID: ricardo.mendoza@canonical.com-20120905142015-prem6hiyfshwgm8q
Initial commit

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 BansheeScopeProxy : SimpleScope
 
25
  { 
 
26
    private BansheeCollection collection;
 
27
 
 
28
    public BansheeScopeProxy ()
 
29
    {
 
30
      base ();
 
31
 
 
32
      scope = new Unity.Scope ("/com/canonical/unity/scope/banshee");
 
33
      scope.search_in_global = true;
 
34
      scope.activate_uri.connect (activate);
 
35
      scope.preview_uri.connect (preview);
 
36
      scope.sources.add_option ("banshee", _("Banshee"), null);
 
37
 
 
38
      base.initialize ();
 
39
 
 
40
      try {
 
41
        collection = new BansheeCollection ();
 
42
      } catch (DatabaseError error) {
 
43
        printerr ("Failed to open the Banshee collection database\n");
 
44
        return;
 
45
      }
 
46
    }
 
47
 
 
48
    protected override int num_results_without_search { get {return 100; } }
 
49
    protected override int num_results_global_search { get { return 20; } }
 
50
    protected override int num_results_lens_search { get { return 50; } }
 
51
 
 
52
    public Unity.Preview preview (string uri)
 
53
    {
 
54
        Unity.MusicPreview? preview = null;
 
55
        GLib.ThemedIcon? icon_file = null;
 
56
 
 
57
        if (Uri.parse_scheme (uri) == "album")
 
58
        {
 
59
            string[] split = uri.split ("/");
 
60
            string artist = split[2];
 
61
            string title = split[3];
 
62
 
 
63
            foreach (unowned Track track in collection.get_album_tracks_detailed (title, artist))
 
64
            {
 
65
                if (preview == null)
 
66
                {
 
67
                    if (track.artwork_path != null)
 
68
                    {
 
69
                        icon_file = new GLib.ThemedIcon(track.artwork_path);
 
70
                    }
 
71
                    preview = new Unity.MusicPreview (title, artist, icon_file);
 
72
                }
 
73
                var tm = new Unity.TrackMetadata();
 
74
                tm.uri = track.uri;
 
75
                tm.track_no = track.track_number;
 
76
                tm.title = track.title ?? "";
 
77
                tm.length = track.duration;
 
78
                preview.add_track(tm);
 
79
            }
 
80
        }
 
81
        else // preview single track
 
82
        {
 
83
            Track? track = collection.get_album_track (uri);
 
84
            if (track != null)
 
85
            {
 
86
                if (track.artwork_path != null)
 
87
                {
 
88
                    icon_file = new GLib.ThemedIcon(track.artwork_path);
 
89
                }
 
90
                preview = new Unity.MusicPreview (track.title, track.artist, icon_file);
 
91
                var tm = new Unity.TrackMetadata();
 
92
                tm.uri = track.uri;
 
93
                tm.track_no = track.track_number;
 
94
                tm.title = track.title ?? "";
 
95
                tm.length = track.duration;
 
96
                preview.add_track(tm);
 
97
            }
 
98
        }
 
99
 
 
100
        return preview;
 
101
    }
 
102
 
 
103
    /**
 
104
     * Tells banshee to play the selected uri(s)
 
105
     */
 
106
    public Unity.ActivationResponse activate (string uri)
 
107
    {
 
108
      string[] exec = {"banshee", "--play-enqueued"};
 
109
 
 
110
      try {
 
111
        if (Uri.parse_scheme (uri) == "album")
 
112
          {
 
113
            debug (@"searching for tracks for $uri");
 
114
            string[] split = uri.split ("/");
 
115
            string artist = split[2];
 
116
            string title = split[3];
 
117
            
 
118
            Album album = new Album ();
 
119
            album.artist = artist;
 
120
            album.title = title;
 
121
            // FIXME there must be a better way..
 
122
            foreach (string track in collection.get_track_uris (album))
 
123
              exec += track;
 
124
          }
 
125
        else
 
126
          {
 
127
            exec += uri;
 
128
          }
 
129
        
 
130
        exec += null;
 
131
        
 
132
        debug (@"Spawning banshee '%s'", string.joinv (" ", exec));
 
133
        Process.spawn_async (null, 
 
134
                             exec,
 
135
                             null, 
 
136
                             SpawnFlags.SEARCH_PATH,
 
137
                             null, 
 
138
                             null);
 
139
        
 
140
        return new Unity.ActivationResponse (Unity.HandledType.HIDE_DASH);
 
141
      } catch (SpawnError error) {
 
142
        warning ("Failed to launch URI %s", uri);
 
143
        return new Unity.ActivationResponse (Unity.HandledType.NOT_HANDLED);
 
144
      }
 
145
    }
 
146
 
 
147
    public override async void perform_search (LensSearch search,
 
148
                                               SearchType search_type, 
 
149
                                               owned List<FilterParser> filters,
 
150
                                               int max_results = -1,
 
151
                                               Cancellable? cancellable = null)
 
152
    {
 
153
      int category_override = -1;
 
154
      if (search_type == SearchType.GLOBAL)
 
155
      {
 
156
        category_override = Category.MUSIC;
 
157
        // the lens shouldn't display anything for empty search
 
158
        if (is_search_empty (search)) return;
 
159
      }
 
160
 
 
161
      collection.search (search, search_type, filters,
 
162
                         max_results, category_override);
 
163
    }
 
164
  }
 
165
}