Merge lp:~stolowski/unity-lens-shopping/u1ms-preview into lp:unity-lens-shopping

Proposed by Paweł Stołowski
Status: Superseded
Proposed branch: lp:~stolowski/unity-lens-shopping/u1ms-preview
Merge into: lp:unity-lens-shopping
Diff against target: 431 lines (+350/-24)
6 files modified
po/POTFILES.in (+2/-0)
po/POTFILES.skip (+2/-0)
src/Makefile.am (+2/-0)
src/preview-player-client.vala (+112/-0)
src/scope.vala (+38/-24)
src/u1ms-preview.vala (+194/-0)
To merge this branch: bzr merge lp:~stolowski/unity-lens-shopping/u1ms-preview
Reviewer Review Type Date Requested Status
Michal Hruby (community) Needs Fixing
Review via email: mp+126211@code.launchpad.net

This proposal has been superseded by a proposal from 2012-09-26.

Commit message

Reuse u1ms preview code from musicstore scope for u1ms results. The code has been refactored and copied over to have it isolated as much as possible in new classes/files. After Q we need to move it to a library and clean both shopping lens and musicstore scope.

Description of the change

Reuse u1ms preview code from musicstore scope for u1ms results. The code has been refactored and copied over to have it isolated as much as possible in new classes/files. After Q we need to move it to a library and clean both shopping lens and musicstore scope.

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

Can you please add huge WARNING / FIXME comment on the top saying that the file is a copy, and should be kept in sync with the original / moved in a common lib.

Otherwise looks and works fine.

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

+1

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

Oh actually POTFILES need update, make check fails.

review: Needs Fixing
Revision history for this message
Paweł Stołowski (stolowski) wrote :

Fixed.

Revision history for this message
Unity Merger (unity-merger) wrote :

There are additional revisions which have not been approved in review. Please seek review and approval of these new revisions.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'po/POTFILES.in'
--- po/POTFILES.in 2012-07-25 13:07:48 +0000
+++ po/POTFILES.in 2012-09-26 09:58:22 +0000
@@ -1,5 +1,7 @@
1[encoding: UTF-8]1[encoding: UTF-8]
2src/daemon.vala2src/daemon.vala
3src/main.vala3src/main.vala
4src/u1ms-preview.vala
5src/scope.vala
4[type: gettext/ini]shopping.lens.in.in6[type: gettext/ini]shopping.lens.in.in
57
68
=== modified file 'po/POTFILES.skip'
--- po/POTFILES.skip 2012-09-17 21:39:10 +0000
+++ po/POTFILES.skip 2012-09-26 09:58:22 +0000
@@ -5,3 +5,5 @@
5src/banshee-scope.c5src/banshee-scope.c
6src/rhythmbox-scope.c6src/rhythmbox-scope.c
7src/scope.c7src/scope.c
8src/preview-player-client.c
9src/u1ms-preview.c
810
=== modified file 'src/Makefile.am'
--- src/Makefile.am 2012-09-17 18:04:15 +0000
+++ src/Makefile.am 2012-09-26 09:58:22 +0000
@@ -44,6 +44,8 @@
44 main.vala \44 main.vala \
45 scope.vala \45 scope.vala \
46 markup-cleaner.vala \46 markup-cleaner.vala \
47 preview-player-client.vala \
48 u1ms-preview.vala \
47 $(NULL)49 $(NULL)
4850
49unity_shopping_daemon_SOURCES = \51unity_shopping_daemon_SOURCES = \
5052
=== added file 'src/preview-player-client.vala'
--- src/preview-player-client.vala 1970-01-01 00:00:00 +0000
+++ src/preview-player-client.vala 2012-09-26 09:58:22 +0000
@@ -0,0 +1,112 @@
1/*
2 * Copyright (C) 2012 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 Pawel Stolowski <pawel.stolowski@canonical.com>
17 */
18
19
20/*
21 * FIXME: this code was copied from music-lens; it needs to be kept in sync and eventually moved to a library !!!
22 */
23namespace Unity.ShoppingLens {
24
25 static const string PREVIEW_PLAYER_DBUS_NAME = "com.canonical.Unity.Lens.Music.PreviewPlayer";
26 static const string PREVIEW_PLAYER_DBUS_PATH = "/com/canonical/Unity/Lens/Music/PreviewPlayer";
27
28 [DBus (name = "com.canonical.Unity.Lens.Music.PreviewPlayer")]
29 public interface PreviewPlayerService: GLib.Object
30 {
31 public signal void progress(string uri, uint32 state, double progress);
32
33 public abstract async void play (string uri) throws Error;
34 public abstract async void pause () throws Error;
35 public abstract async void pause_resume () throws Error;
36 public abstract async void resume () throws Error;
37 public abstract async void stop () throws Error;
38 public abstract async void close () throws Error;
39 }
40
41 public class PreviewPlayer: GLib.Object
42 {
43 public signal void progress(string uri, Unity.MusicPreview.TrackState state, double progress);
44
45 public async void connect_to () throws Error
46 {
47 _preview_player_service = Bus.get_proxy_sync (BusType.SESSION, PREVIEW_PLAYER_DBUS_NAME, PREVIEW_PLAYER_DBUS_PATH);
48 _preview_player_service.progress.connect (on_progress_signal);
49 }
50
51 public async void play (string uri) throws Error
52 {
53 if (_preview_player_service == null)
54 {
55 yield connect_to ();
56 }
57 yield _preview_player_service.play (uri);
58 }
59
60 public async void pause () throws Error
61 {
62 if (_preview_player_service == null)
63 {
64 yield connect_to ();
65 }
66 yield _preview_player_service.pause ();
67 }
68
69 public async void pause_resume () throws Error
70 {
71 if (_preview_player_service == null)
72 {
73 yield connect_to ();
74 }
75 yield _preview_player_service.pause_resume ();
76 }
77
78 public async void resume () throws Error
79 {
80 if (_preview_player_service == null)
81 {
82 yield connect_to ();
83 }
84 yield _preview_player_service.resume ();
85 }
86
87 public async void stop () throws Error
88 {
89 if (_preview_player_service == null)
90 {
91 yield connect_to ();
92 }
93 yield _preview_player_service.stop ();
94 }
95
96 public async void close () throws Error
97 {
98 if (_preview_player_service == null)
99 {
100 yield connect_to ();
101 }
102 yield _preview_player_service.close ();
103 }
104
105 internal void on_progress_signal (string uri, uint32 state, double progress_value)
106 {
107 progress(uri, (Unity.MusicPreview.TrackState)state, progress_value);
108 }
109
110 private PreviewPlayerService _preview_player_service;
111 }
112}
0\ No newline at end of file113\ No newline at end of file
1114
=== modified file 'src/scope.vala'
--- src/scope.vala 2012-09-20 11:00:09 +0000
+++ src/scope.vala 2012-09-26 09:58:22 +0000
@@ -27,6 +27,7 @@
2727
28 private HashTable<string, string> results_details_map;28 private HashTable<string, string> results_details_map;
29 private HashTable<string, string> global_results_details_map;29 private HashTable<string, string> global_results_details_map;
30 private PreviewPlayerHandler player;
3031
31 public ShoppingScope ()32 public ShoppingScope ()
32 {33 {
@@ -136,30 +137,43 @@
136 {137 {
137 var parser = get_json_reply (details_uri, null);138 var parser = get_json_reply (details_uri, null);
138139
139 var root = parser.get_root ().get_object ();140 if (U1MSPreviewFactory.is_u1ms_details (parser))
140 unowned string title = root.get_string_member ("title");141 {
141 unowned string description = root.get_string_member ("description_html");142 var u1mspf = new U1MSPreviewFactory ();
142 unowned string price = root.get_string_member ("formatted_price");143 var preview = u1mspf.create_preview (parser);
143 if (price == null) price = root.get_string_member ("price");144 u1mspf.add_download_action (preview); // download will be handled by normal activation
144145 if (player == null)
145 var img_obj = root.get_object_member ("images");146 player = new PreviewPlayerHandler ();
146 string image_uri = extract_image_uri (img_obj, int.MAX);147 player.music_preview = preview;
147148 return preview;
148 Icon? image = null;149 }
149 if (image_uri != "")150 else
150 {151 {
151 image = new FileIcon (File.new_for_uri (image_uri));152 var root = parser.get_root ().get_object ();
152 }153 unowned string title = root.get_string_member ("title");
153154 unowned string description = root.get_string_member ("description_html");
154 var preview = new GenericPreview (title, MarkupCleaner.html_to_pango_markup (description), image);155 unowned string price = root.get_string_member ("formatted_price");
155 var icon_dir = File.new_for_path (ICON_PATH);156 if (price == null) price = root.get_string_member ("price");
156 var icon = new FileIcon (icon_dir.get_child ("service-amazon.svg"));157
157 var buy_action = new PreviewAction ("buy", _("Buy"), icon);158 var img_obj = root.get_object_member ("images");
158 if (price != null) buy_action.extra_text = price;159 string image_uri = extract_image_uri (img_obj, int.MAX);
159 /* Leaving the activation on unity for now */160
160 // buy_action.activated.connect ((uri) => { });161 Icon? image = null;
161 preview.add_action (buy_action);162 if (image_uri != "")
162 return preview;163 {
164 image = new FileIcon (File.new_for_uri (image_uri));
165 }
166
167 var preview = new GenericPreview (title, MarkupCleaner.html_to_pango_markup (description), image);
168 var icon_dir = File.new_for_path (ICON_PATH);
169 var icon = new FileIcon (icon_dir.get_child ("service-amazon.svg"));
170 var buy_action = new PreviewAction ("buy", _("Buy"), icon);
171 if (price != null) buy_action.extra_text = price;
172 /* Leaving the activation on unity for now */
173 // buy_action.activated.connect ((uri) => { });
174 preview.add_action (buy_action);
175 return preview;
176 }
163 }177 }
164 catch (Error err)178 catch (Error err)
165 {179 {
166180
=== added file 'src/u1ms-preview.vala'
--- src/u1ms-preview.vala 1970-01-01 00:00:00 +0000
+++ src/u1ms-preview.vala 2012-09-26 09:58:22 +0000
@@ -0,0 +1,194 @@
1/*
2 * Copyright (C) 2011 Canonical, Ltd.
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License
6 * version 3.0 as published by the Free Software Foundation.
7 *
8 * This library 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 Lesser General Public License version 3.0 for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library. If not, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 * Authored by Pawel Stolowski <pawel.stolowski@canonical.com>
18 *
19 */
20
21/*
22 * FIXME: this code is based on musicstore-scope preview handling code; it needs to be kept in sync and eventually moved to a library !!!
23 */
24namespace Unity.ShoppingLens
25{
26
27 public class PreviewPlayerHandler
28 {
29 private PreviewPlayer preview_player;
30 private Unity.MusicPreview music_preview_;
31
32 public Unity.MusicPreview music_preview
33 {
34 get
35 {
36 return music_preview_;
37 }
38 set
39 {
40 music_preview_ = value;
41 if (value != null)
42 {
43 music_preview.play.connect (play);
44 music_preview.pause.connect (pause);
45 music_preview.closed.connect (closed);
46 }
47 }
48 }
49
50 public PreviewPlayerHandler (Unity.MusicPreview? preview = null)
51 {
52 music_preview = preview;
53 }
54
55 private void on_progress_changed (string uri, Unity.MusicPreview.TrackState state, double progress)
56 {
57 if (music_preview != null)
58 {
59 music_preview.current_track_uri = uri;
60 music_preview.current_track_state = state;
61 music_preview.current_progress = (float)progress;
62 }
63 }
64
65 private void closed (Unity.Preview preview)
66 {
67 if (preview_player != null)
68 {
69 try
70 {
71 preview_player.close ();
72 }
73 catch (Error e)
74 {
75 warning ("Failed to close preview player: %s", e.message);
76 }
77 }
78 }
79
80 private void play (Unity.Preview preview, string uri)
81 {
82 debug ("play request: '%s'", uri);
83
84 try
85 {
86 if (preview_player == null)
87 {
88 preview_player = new PreviewPlayer ();
89 preview_player.progress.connect (on_progress_changed);
90 preview_player.connect_to ();
91 }
92
93 // we will receive state back in on_progress_changed, but set it now so that it's immediately reflected in the dash
94 music_preview.current_track_uri = uri;
95 music_preview.current_progress = 0.0f;
96 music_preview.current_track_state = Unity.MusicPreview.TrackState.PLAYING;
97
98 preview_player.play (uri);
99 }
100 catch (Error e)
101 {
102 warning ("Failed to play '%s': %s", uri, e.message);
103 }
104 }
105
106 public void pause (Unity.Preview preview, string uri)
107 {
108 debug ("pause request: '%s'", uri);
109
110 try
111 {
112 if (preview_player != null)
113 {
114 // we will receive state back in on_progress_changed, but set it now so that it's immediately reflected in the dash
115 music_preview.current_track_uri = uri;
116 music_preview.current_track_state = Unity.MusicPreview.TrackState.PAUSED;
117
118 preview_player.pause ();
119 }
120 }
121 catch (Error e)
122 {
123 warning ("Failed to pause '%s': %s", uri, e.message);
124 }
125 }
126 }
127
128
129 public class U1MSPreviewFactory
130 {
131 public string formatted_price { get; internal set; }
132
133 public static bool is_u1ms_details (Json.Parser parser)
134 {
135 var root_obj = parser.get_root ().get_object ();
136 return root_obj.has_member ("source") && root_obj.get_string_member ("source") == "Ubuntu One Music Store" && root_obj.has_member ("tracks");
137 }
138
139 public Unity.MusicPreview? create_preview (Json.Parser parser)
140 {
141 var root_obj = parser.get_root ().get_object ();
142
143 var title = root_obj.get_string_member ("title");
144 var artwork_path = root_obj.get_string_member ("image");
145 File cover_file = File.new_for_uri (artwork_path); //artwork path is a remote uri
146 var cover = new FileIcon (cover_file);
147
148 var artist = root_obj.get_string_member ("artist");
149
150 var preview = new Unity.MusicPreview (title, artist, cover);
151
152 if (root_obj.has_member ("formatted_price"))
153 formatted_price = root_obj.get_string_member ("formatted_price");
154 else
155 formatted_price = "";
156
157 if (root_obj.has_member ("tracks"))
158 {
159 var tracks_node = root_obj.get_array_member ("tracks");
160 int i = 1;
161 foreach (var track_node in tracks_node.get_elements ())
162 {
163 var track_obj = track_node.get_object ();
164 TrackMetadata tm = new TrackMetadata ();
165 tm.uri = track_obj.get_string_member ("preview");
166 tm.track_no = i++; //FIXME: u1ms search doesn't provide track numbers *yet*
167 tm.title = track_obj.get_string_member ("title");
168 tm.length = (int)track_obj.get_member ("duration").get_int ();
169 preview.add_track (tm);
170 }
171 }
172 else // details for single track
173 {
174 TrackMetadata tm = new TrackMetadata ();
175 tm.uri = root_obj.get_string_member ("preview");
176 tm.title = root_obj.get_string_member ("title");
177 tm.length = (int)root_obj.get_member ("duration").get_int ();
178 preview.add_track (tm);
179 }
180 return preview;
181 }
182
183 public Unity.PreviewAction? add_download_action (Unity.MusicPreview preview)
184 {
185 GLib.Icon? icon = new GLib.FileIcon (File.new_for_path (Config.DATADIR + "/icons/unity-icon-theme/places/svg/service-u1.svg"));
186 var download_action = new Unity.PreviewAction ("download_album", _("Download"), icon);
187 if (formatted_price != null)
188 download_action.extra_text = formatted_price;
189 preview.add_action (download_action);
190 return download_action;
191 }
192 }
193
194}

Subscribers

People subscribed via source and target branches