~unity-team/unity-lens-music/6.0

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
/*
 * Copyright (C) 2011 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 Alex Launi <alex.launi@canonical.com>
 *
 */

using GLib;

namespace Unity.MusicLens {
 	
  public class BansheeScopeProxy : SimpleScope
  { 
    private BansheeCollection collection;

    public BansheeScopeProxy ()
    {
      base ();

      scope = new Unity.Scope ("/com/canonical/unity/scope/banshee");
      scope.search_in_global = true;
      scope.provides_personal_content = true;
      scope.activate_uri.connect (activate);
      scope.preview_uri.connect (preview);
      scope.sources.add_option ("banshee", _("Banshee"), null);

      base.initialize ();

      try {
	collection = new BansheeCollection ();
      } catch (DatabaseError error) {
	printerr ("Failed to open the Banshee collection database\n");
	return;
      }
    }

    protected override int num_results_without_search { get {return 100; } }
    protected override int num_results_global_search { get { return 20; } }
    protected override int num_results_lens_search { get { return 50; } }

    public Unity.Preview preview (string uri)
    {
        Unity.MusicPreview? preview = null;
        GLib.Icon? icon_file = null;
        GLib.Icon? missing_icon_file = new GLib.FileIcon (File.new_for_path (ALBUM_MISSING_PREVIEW_ICON_PATH));

        if (Uri.parse_scheme (uri) == "album")
        {
            string[] split = uri.split ("/");
            string artist = split[2];
            string title = split[3];

            foreach (unowned Track track in collection.get_album_tracks_detailed (title, artist))
            {
                if (preview == null)
                {
                    if (track.artwork_path != null && track.artwork_path != "" && FileUtils.test(track.artwork_path, FileTest.EXISTS))
                        icon_file = new GLib.ThemedIcon(track.artwork_path);
                    else
                        icon_file = missing_icon_file;
                    preview = new Unity.MusicPreview (title, artist, icon_file);
                }
                var tm = new Unity.TrackMetadata();
                tm.uri = track.uri;
                tm.track_no = track.track_number;
                tm.title = track.title ?? "";
                tm.length = track.duration;
                preview.add_track(tm);
            }
        }
        else // preview single track
        {
            Track? track = collection.get_album_track (uri);
            if (track != null)
            {
                if (track.artwork_path != null && track.artwork_path != "" && FileUtils.test(track.artwork_path, FileTest.EXISTS))
                    icon_file = new GLib.ThemedIcon(track.artwork_path);
                else
                    icon_file = missing_icon_file;
                preview = new Unity.MusicPreview (track.title, track.artist, icon_file);
                var tm = new Unity.TrackMetadata();
                tm.uri = track.uri;
                tm.track_no = track.track_number;
                tm.title = track.title ?? "";
                tm.length = track.duration;
                preview.add_track(tm);
            }
        }

        return preview;
    }

    /**
     * Tells banshee to play the selected uri(s)
     */
    public Unity.ActivationResponse activate (string uri)
    {
      string[] exec = {"banshee", "--play-enqueued"};

      try {
        if (Uri.parse_scheme (uri) == "album")
          {
            debug (@"searching for tracks for $uri");
            string[] split = uri.split ("/");
            string artist = split[2];
            string title = split[3];
	    
            Album album = new Album ();
            album.artist = artist;
            album.title = title;
            // FIXME there must be a better way..
            foreach (string track in collection.get_track_uris (album))
              exec += track;
          }
        else
          {
            exec += uri;
          }
	
        exec += null;
	
        debug (@"Spawning banshee '%s'", string.joinv (" ", exec));
        Process.spawn_async (null, 
			     exec,
			     null, 
			     SpawnFlags.SEARCH_PATH,
			     null, 
			     null);
	
        return new Unity.ActivationResponse (Unity.HandledType.HIDE_DASH);
      } catch (SpawnError error) {
        warning ("Failed to launch URI %s", uri);
        return new Unity.ActivationResponse (Unity.HandledType.NOT_HANDLED);
      }
    }

    public override async void perform_search (LensSearch search,
                                               SearchType search_type, 
                                               owned List<FilterParser> filters,
                                               int max_results = -1,
                                               Cancellable? cancellable = null)
    {
      int category_override = -1;
      if (search_type == SearchType.GLOBAL)
      {
        category_override = Category.MUSIC;
        // the lens shouldn't display anything for empty search
        if (is_search_empty (search)) return;
      }

      collection.search (search, search_type, filters,
                         max_results, category_override);
    }
  }
}