Merge lp:~stolowski/unity-lens-music/banshee-previews into lp:unity-lens-music

Proposed by Paweł Stołowski
Status: Merged
Approved by: Michal Hruby
Approved revision: 111
Merged at revision: 111
Proposed branch: lp:~stolowski/unity-lens-music/banshee-previews
Merge into: lp:unity-lens-music
Diff against target: 274 lines (+206/-49)
1 file modified
src/banshee-scope.vala (+206/-49)
To merge this branch: bzr merge lp:~stolowski/unity-lens-music/banshee-previews
Reviewer Review Type Date Requested Status
Michal Hruby (community) Approve
Review via email: mp+126663@code.launchpad.net

Commit message

Implemented support for banshee tracks/albums previews.

Description of the change

Implemented support for banshee tracks/albums previews.

To post a comment you must log in.
Revision history for this message
Michal Hruby (mhr3) wrote :

148 + string[] split = uri.split ("/"); //FIXME: there must be more reliable way
149 + string artist = split[2];
150 + string title = split[3];

Looks somewhat crash-prone, could you add a check of the array length at least? (same around 219)

review: Needs Fixing
111. By Paweł Stołowski

Check number of elements when splitting uri in preview & show_in_folder methods.

Revision history for this message
Michal Hruby (mhr3) wrote :

Ok, /me happy now. :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/banshee-scope.vala'
2--- src/banshee-scope.vala 2012-09-14 16:02:22 +0000
3+++ src/banshee-scope.vala 2012-09-28 10:31:21 +0000
4@@ -24,6 +24,8 @@
5 public class BansheeScopeProxy : SimpleScope
6 {
7 private BansheeCollection collection;
8+ private PreviewPlayer preview_player;
9+ private Unity.MusicPreview? music_preview;
10
11 public BansheeScopeProxy ()
12 {
13@@ -50,57 +52,212 @@
14 protected override int num_results_global_search { get { return 20; } }
15 protected override int num_results_lens_search { get { return 50; } }
16
17+
18+ public void closed (Unity.Preview preview)
19+ {
20+ if (preview_player != null)
21+ {
22+ try
23+ {
24+ preview_player.close ();
25+ }
26+ catch (Error e)
27+ {
28+ warning ("Failed to close preview player: %s", e.message);
29+ }
30+ }
31+ }
32+
33+ public void play (Unity.Preview preview, string uri)
34+ {
35+ debug ("play request: '%s'", uri);
36+
37+ try
38+ {
39+ if (preview_player == null)
40+ {
41+ preview_player = new PreviewPlayer ();
42+ preview_player.progress.connect (on_progress_changed);
43+ preview_player.connect_to ();
44+ }
45+
46+ // we will receive state back in on_progress_changed, but set it now so that it's immediately reflected in the dash
47+ music_preview.current_track_uri = uri;
48+ music_preview.current_progress = 0.0f;
49+ music_preview.current_track_state = Unity.MusicPreview.TrackState.PLAYING;
50+
51+ preview_player.play (uri);
52+ }
53+ catch (Error e)
54+ {
55+ warning ("Failed to play '%s': %s", uri, e.message);
56+ }
57+ }
58+
59+ public void pause (Unity.Preview preview, string uri)
60+ {
61+ debug ("pause request: '%s'", uri);
62+
63+ try
64+ {
65+ if (preview_player != null)
66+ {
67+ // we will receive state back in on_progress_changed, but set it now so that it's immediately reflected in the dash
68+ music_preview.current_track_uri = uri;
69+ music_preview.current_track_state = Unity.MusicPreview.TrackState.PAUSED;
70+
71+ preview_player.pause ();
72+ }
73+ }
74+ catch (Error e)
75+ {
76+ warning ("Failed to pause '%s': %s", uri, e.message);
77+ }
78+ }
79+
80+ private void on_progress_changed (string uri, Unity.MusicPreview.TrackState state, double progress)
81+ {
82+ if (preview != null)
83+ {
84+ music_preview.current_track_uri = uri;
85+ music_preview.current_track_state = state;
86+ music_preview.current_progress = (float)progress;
87+ }
88+ }
89+
90+
91 public Unity.Preview preview (string uri)
92 {
93- Unity.MusicPreview? preview = null;
94- GLib.Icon? icon_file = null;
95- GLib.Icon? missing_icon_file = new GLib.FileIcon (File.new_for_path (ALBUM_MISSING_PREVIEW_ICON_PATH));
96-
97- if (Uri.parse_scheme (uri) == "album")
98- {
99- string[] split = uri.split ("/");
100- string artist = split[2];
101- string title = split[3];
102-
103- foreach (unowned Track track in collection.get_album_tracks_detailed (title, artist))
104- {
105- if (preview == null)
106- {
107- if (track.artwork_path != null && track.artwork_path != "" && FileUtils.test(track.artwork_path, FileTest.EXISTS))
108- icon_file = new GLib.ThemedIcon(track.artwork_path);
109- else
110- icon_file = missing_icon_file;
111- preview = new Unity.MusicPreview (title, artist, icon_file);
112- }
113- var tm = new Unity.TrackMetadata();
114- tm.uri = track.uri;
115- tm.track_no = track.track_number;
116- tm.title = track.title ?? "";
117- tm.length = track.duration;
118- preview.add_track(tm);
119- }
120- }
121- else // preview single track
122- {
123- Track? track = collection.get_album_track (uri);
124- if (track != null)
125- {
126- if (track.artwork_path != null && track.artwork_path != "" && FileUtils.test(track.artwork_path, FileTest.EXISTS))
127- icon_file = new GLib.ThemedIcon(track.artwork_path);
128- else
129- icon_file = missing_icon_file;
130- preview = new Unity.MusicPreview (track.title, track.artist, icon_file);
131- var tm = new Unity.TrackMetadata();
132- tm.uri = track.uri;
133- tm.track_no = track.track_number;
134- tm.title = track.title ?? "";
135- tm.length = track.duration;
136- preview.add_track(tm);
137- }
138- }
139-
140- return preview;
141- }
142+ music_preview = null;
143+ GLib.Icon? icon_file = null;
144+ GLib.Icon? missing_icon_file = new GLib.FileIcon (File.new_for_path (ALBUM_MISSING_PREVIEW_ICON_PATH));
145+
146+ if (Uri.parse_scheme (uri) == "album")
147+ {
148+ string[] split = uri.split ("/"); //FIXME: there must be more reliable way
149+ if (split.length >= 4)
150+ {
151+ string artist = split[2];
152+ string title = split[3];
153+
154+ foreach (unowned Track track in collection.get_album_tracks_detailed (title, artist))
155+ {
156+ if (music_preview == null)
157+ {
158+ if (track.artwork_path != null && track.artwork_path != "" && FileUtils.test(track.artwork_path, FileTest.EXISTS))
159+ icon_file = new GLib.ThemedIcon(track.artwork_path);
160+ else
161+ icon_file = missing_icon_file;
162+ music_preview = new Unity.MusicPreview (title, artist, icon_file);
163+ }
164+ var tm = new Unity.TrackMetadata();
165+ tm.uri = track.uri;
166+ tm.track_no = track.track_number;
167+ tm.title = track.title ?? "";
168+ tm.length = track.duration;
169+ music_preview.add_track(tm);
170+ }
171+ }
172+ else
173+ {
174+ warning ("Invalid uri: %s", uri);
175+ }
176+ }
177+ else // preview single track
178+ {
179+ Track? track = collection.get_album_track (uri);
180+ if (track != null)
181+ {
182+ if (track.artwork_path != null && track.artwork_path != "" && FileUtils.test(track.artwork_path, FileTest.EXISTS))
183+ icon_file = new GLib.ThemedIcon(track.artwork_path);
184+ else
185+ icon_file = missing_icon_file;
186+ music_preview = new Unity.MusicPreview (track.title, track.artist, icon_file);
187+ var tm = new Unity.TrackMetadata();
188+ tm.uri = track.uri;
189+ tm.track_no = track.track_number;
190+ tm.title = track.title ?? "";
191+ tm.length = track.duration;
192+ music_preview.add_track(tm);
193+ }
194+ }
195+
196+ if (preview != null)
197+ {
198+ var play_action = new Unity.PreviewAction ("play", _("Play"), null);
199+ play_action.activated.connect (play_in_banshee);
200+ music_preview.add_action (play_action);
201+
202+ var show_folder_action = new Unity.PreviewAction ("show_in_folder", _("Show in Folder"), null);
203+ show_folder_action.activated.connect (show_in_folder);
204+ music_preview.add_action (show_folder_action);
205+
206+ music_preview.play.connect (play);
207+ music_preview.pause.connect (pause);
208+ music_preview.closed.connect (closed);
209+ }
210+
211+ return music_preview;
212+ }
213+
214+ private Unity.ActivationResponse play_in_banshee (string uri)
215+ {
216+ return activate (uri);
217+ }
218+
219+ private Unity.ActivationResponse show_in_folder (string uri)
220+ {
221+ File? track_file = null;
222+
223+ if (Uri.parse_scheme (uri) == "album")
224+ {
225+ string[] split = uri.split ("/");
226+ if (split.length >= 4)
227+ {
228+ string artist = split[2];
229+ string title = split[3];
230+ var album = new Album ()
231+ {
232+ title = split[3],
233+ artist = split[2]
234+ };
235+ var tracks = collection.get_track_uris (album);
236+ if (tracks.length > 0)
237+ {
238+ track_file = File.new_for_uri (tracks[0]);
239+ }
240+ }
241+ else
242+ {
243+ warning ("Invalid uri: %s", uri);
244+ return new Unity.ActivationResponse (Unity.HandledType.NOT_HANDLED);
245+ }
246+ }
247+ else //file
248+ {
249+ track_file = File.new_for_uri (uri);
250+ }
251+
252+ File? parent_dir = track_file.get_parent ();
253+ if (parent_dir != null)
254+ {
255+ try
256+ {
257+ GLib.AppInfo.launch_default_for_uri (parent_dir.get_uri (), null);
258+ return new Unity.ActivationResponse (Unity.HandledType.HIDE_DASH);
259+ }
260+ catch (GLib.Error e)
261+ {
262+ warning ("Failed to launch application for uri '%s': %s", parent_dir.get_uri (), e.message);
263+ }
264+ }
265+ else
266+ {
267+ warning ("Failed to get parent dir for uri: '%s'", uri);
268+ }
269+ return new Unity.ActivationResponse (Unity.HandledType.NOT_HANDLED);
270+ }
271+
272
273 /**
274 * Tells banshee to play the selected uri(s)

Subscribers

People subscribed via source and target branches

to all changes: