~ubuntu-app-review-contributors/ubuntu-app-reviews/beatbox

« back to all changes in this revision

Viewing changes to src/Widgets/InfoPanel.vala

  • Committer: App Bot
  • Author(s): Scott Ringwelski
  • Date: 2012-07-08 12:10:10 UTC
  • Revision ID: appbot@holba.ch-20120708121010-6bhuqykl5wo3pq8t
Tags: 0.5+r675-0~quantal1
Auto build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-
 
2
 * Copyright (c) 2011-2012       Scott Ringwelski <sgringwe@mtu.edu>
 
3
 *
 
4
 * Originally Written by Scott Ringwelski for BeatBox Music Player
 
5
 * BeatBox Music Player: http://www.launchpad.net/beat-box
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public
 
18
 * License along with this library; if not, write to the
 
19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
20
 * Boston, MA 02111-1307, USA.
 
21
 */
 
22
 
 
23
using Gtk;
 
24
using Gee;
 
25
 
 
26
public class BeatBox.InfoPanel : ScrolledWindow {
 
27
        private LibraryManager lm;
 
28
        private LibraryWindow lw;
 
29
        
 
30
        private Label title;
 
31
        private Label artist;
 
32
        private Button loveMedia;
 
33
        private Button banMedia;
 
34
        private Gtk.Image coverArt;
 
35
        private RatingWidget rating; // need to make custom widget in future
 
36
        private Label album;
 
37
        private Label year;
 
38
        
 
39
        bool similars_fetched;
 
40
        private SimilarMediasView ssv;
 
41
        
 
42
        public InfoPanel(LibraryManager lmm, LibraryWindow lww) {
 
43
                lm = lmm;
 
44
                lw = lww;
 
45
                
 
46
                buildUI();
 
47
                
 
48
                similars_fetched = false;
 
49
                lm.medias_updated.connect(medias_updated);
 
50
                lm.media_played.connect(media_played);
 
51
                lm.lfm.logged_in.connect(logged_in_to_lastfm);
 
52
                lm.lfm.similar_retrieved.connect(similar_retrieved);
 
53
        }
 
54
        
 
55
        private void buildUI() {
 
56
                /* put it in event box so we can color background white */
 
57
                EventBox eb = new EventBox();
 
58
                
 
59
                // add View class
 
60
                eb.get_style_context ().add_class (Gtk.STYLE_CLASS_VIEW);
 
61
 
 
62
                eb.set_size_request (Icons.ALBUM_VIEW_IMAGE_SIZE + 6, -1);
 
63
 
 
64
                title = new Label("Title");
 
65
                artist = new Label("Artist");
 
66
                loveMedia = new Button();
 
67
                banMedia = new Button();
 
68
                coverArt = new Gtk.Image();
 
69
                coverArt.set_size_request (Icons.ALBUM_VIEW_IMAGE_SIZE, Icons.ALBUM_VIEW_IMAGE_SIZE);
 
70
                rating = new RatingWidget(true, IconSize.MENU, false, null);
 
71
                album = new Label("Album");
 
72
                year = new Label("Year");
 
73
                ssv = new SimilarMediasView(lm, lw);
 
74
                
 
75
                coverArt.set_no_show_all(true);
 
76
                loveMedia.set_no_show_all(true);
 
77
                banMedia.set_no_show_all(true);
 
78
                
 
79
                /* use markup */
 
80
                title.set_markup("<span size=\"large\"><b>%s</b></span>".printf(_("Title")));
 
81
                year.set_markup("<span size=\"x-small\">%s</span>".printf(_("Year")));
 
82
                
 
83
                /* ellipsize */
 
84
                title.ellipsize = Pango.EllipsizeMode.END;
 
85
                artist.ellipsize = Pango.EllipsizeMode.END;
 
86
                album.ellipsize = Pango.EllipsizeMode.END;
 
87
                year.ellipsize = Pango.EllipsizeMode.END;
 
88
                
 
89
                loveMedia.relief = ReliefStyle.NONE;
 
90
                banMedia.relief = ReliefStyle.NONE;
 
91
                
 
92
                var lastfm_love_icon = Icons.LASTFM_LOVE.render (IconSize.MENU);
 
93
                var lastfm_ban_icon = Icons.LASTFM_BAN.render (IconSize.MENU);
 
94
                
 
95
                loveMedia.set_image(new Image.from_pixbuf(lastfm_love_icon));
 
96
                banMedia.set_image(new Image.from_pixbuf(lastfm_ban_icon));
 
97
                
 
98
                HBox padding = new HBox(false, 10);
 
99
                VBox content = new VBox(false, 0);
 
100
                
 
101
                HBox buttons = new HBox(false, 0);
 
102
                buttons.pack_start(new Label(""), true, true, 0);
 
103
                buttons.pack_start(loveMedia, false, false, 0);
 
104
                buttons.pack_end(new Label(""), true, true, 0);
 
105
                buttons.pack_end(banMedia, false, false, 0);
 
106
                
 
107
                content.pack_start(wrap_alignment(title, 5, 0, 0, 5), false, true, 0);
 
108
                content.pack_start(wrap_alignment(artist, 2, 0, 0, 5), false, true, 0);
 
109
                content.pack_start(buttons, false, true, 0);
 
110
                content.pack_start(wrap_alignment(coverArt, 5, 5, 0, 5), false, true, 0);
 
111
                content.pack_start(wrap_alignment(rating, 5, 0, 0, 5), false, true, 0);
 
112
                content.pack_start(wrap_alignment(album, 5, 0, 0, 5), false, true, 0);
 
113
                content.pack_start(wrap_alignment(year, 0, 0, 20, 5), false, true, 0);
 
114
                content.pack_start(ssv, true, true, 0);
 
115
                
 
116
                eb.add(content);
 
117
                
 
118
                padding.pack_start(eb, true, true, 0);
 
119
                
 
120
                Viewport vp = new Viewport(null, null);
 
121
                vp.set_shadow_type(ShadowType.NONE);
 
122
                vp.add(padding);
 
123
                
 
124
                add(vp);
 
125
                
 
126
                this.set_policy(PolicyType.NEVER, PolicyType.AUTOMATIC);
 
127
                this.set_shadow_type(ShadowType.NONE);
 
128
                
 
129
                // signals here
 
130
                rating.rating_changed.connect(ratingChanged);
 
131
                title.button_press_event.connect(titleClicked);
 
132
                loveMedia.clicked.connect(loveButtonClicked);
 
133
                banMedia.clicked.connect(banButtonClicked);
 
134
                
 
135
                drag_dest_set(this, DestDefaults.ALL, {}, Gdk.DragAction.MOVE);
 
136
                Gtk.drag_dest_add_uri_targets(this);
 
137
                
 
138
                drag_motion.connect(drag_motion_signal);
 
139
                drag_leave.connect(drag_leave_signal);
 
140
                drag_data_received.connect(drag_received);
 
141
 
 
142
                update_visibilities();
 
143
        }
 
144
        
 
145
        Gtk.Alignment wrap_alignment (Gtk.Widget widget, int top, int right, int bottom, int left) {
 
146
                var alignment = new Gtk.Alignment(0.0f, 0.0f, 1.0f, 1.0f);
 
147
                alignment.top_padding = top;
 
148
                alignment.right_padding = right;
 
149
                alignment.bottom_padding = bottom;
 
150
                alignment.left_padding = left;
 
151
                
 
152
                alignment.add(widget);
 
153
                return alignment;
 
154
        }
 
155
        
 
156
        public void logged_in_to_lastfm() {
 
157
                update_visibilities();
 
158
        }
 
159
 
 
160
        public void update_visibilities() {
 
161
                var lastfm_elements_visible = lm.settings.getLastFMSessionKey() != "";
 
162
 
 
163
                loveMedia.set_no_show_all (!lastfm_elements_visible);
 
164
                banMedia.set_no_show_all (!lastfm_elements_visible);
 
165
                ssv.set_no_show_all (!lastfm_elements_visible);
 
166
                loveMedia.set_visible(lastfm_elements_visible);
 
167
                banMedia.set_visible(lastfm_elements_visible);
 
168
                ssv.set_visible(similars_fetched);
 
169
        }
 
170
        
 
171
        void media_played(Media m, Media? old) {
 
172
                update_metadata ();
 
173
                update_cover_art ();
 
174
                update_visibilities ();
 
175
                
 
176
                similars_fetched = false;
 
177
        }
 
178
        
 
179
        void medias_updated(Collection<int> ids) {
 
180
                if(ids.contains(lm.media_info.media.rowid)) {
 
181
                        update_metadata ();
 
182
                        update_cover_art ();
 
183
                        update_visibilities ();
 
184
                }
 
185
        }
 
186
        
 
187
        void update_metadata() {
 
188
                Media s = lm.media_info.media;
 
189
                
 
190
                title.set_markup("<span size=\"large\"><b>" + Markup.escape_text(s.title) + "</b></span>");
 
191
                artist.set_text(s.artist);
 
192
                album.set_text(s.album);
 
193
                
 
194
                // do rating stuff
 
195
                rating.set_rating((int)s.rating);
 
196
                
 
197
                if(s.year > 1900)
 
198
                        year.set_markup("<span size=\"x-small\">" + s.year.to_string() + "</span>");
 
199
                else
 
200
                        year.set_markup("");
 
201
        }
 
202
        
 
203
        void update_cover_art() {
 
204
                if(lm.media_info.media == null) {
 
205
                        coverArt.hide();
 
206
                        return;
 
207
                }
 
208
 
 
209
                var coverart_pixbuf = lm.get_cover_album_art(lm.media_info.media.rowid);
 
210
 
 
211
                if(coverart_pixbuf != null) {
 
212
                        coverArt.show();
 
213
                        coverArt.set_from_pixbuf(coverart_pixbuf);
 
214
                }
 
215
                else {
 
216
                        coverArt.hide();
 
217
                }
 
218
        }
 
219
        
 
220
        void similar_retrieved(LinkedList<int> similar_internal, LinkedList<Media> similar_external) {
 
221
                update_similar_list(similar_external);
 
222
        }
 
223
        
 
224
        public void update_similar_list(Collection<Media> medias) {
 
225
                if(medias.size > 8) {
 
226
                        similars_fetched = true;
 
227
                        ssv.populateView(medias);
 
228
                }
 
229
                
 
230
                update_visibilities ();
 
231
        }
 
232
        
 
233
        void ratingChanged(int new_rating) {
 
234
                lm.media_info.media.rating = new_rating;
 
235
                lm.update_media(lm.media_info.media, false, true, true);
 
236
        }
 
237
        
 
238
        bool titleClicked(Gdk.EventButton event) {
 
239
                if(lm.media_info.track == null)
 
240
                        return false;
 
241
                
 
242
                try {
 
243
                        Thread.create<void*>(() => {
 
244
                                try {
 
245
                                        GLib.AppInfo.launch_default_for_uri (lm.media_info.track.url, null);
 
246
                                }
 
247
                                catch(GLib.Error err) {
 
248
                                        message("Could not open url in Last FM: %s\n", err.message);
 
249
                                }
 
250
                                
 
251
                                return null;
 
252
                        }, false);
 
253
                }
 
254
                catch(GLib.ThreadError err) {
 
255
                        message("Could not create thread to open title:%s\n", err.message);
 
256
                        
 
257
                }
 
258
                
 
259
                return false;
 
260
        }
 
261
        
 
262
        void loveButtonClicked() {
 
263
                lm.lfm.loveTrack(lm.media_info.media.title, lm.media_info.media.artist);
 
264
        }
 
265
        
 
266
        void banButtonClicked() {
 
267
                lm.lfm.banTrack(lm.media_info.media.title, lm.media_info.media.artist);
 
268
        }
 
269
        
 
270
        bool is_valid_image_type(string type) {
 
271
                var typeDown = type.down();
 
272
                
 
273
                return (typeDown.has_suffix(".jpg") || typeDown.has_suffix(".jpeg") ||
 
274
                                typeDown.has_suffix(".png"));
 
275
        }
 
276
        
 
277
        bool drag_motion_signal(Gdk.DragContext context, int x, int y, uint time) {
 
278
                drag_highlight(coverArt);
 
279
                coverArt.show();
 
280
                coverArt.set_from_pixbuf(Icons.DROP_ALBUM_PIXBUF);
 
281
                
 
282
                return false;
 
283
        }
 
284
        
 
285
        void drag_leave_signal(Gdk.DragContext context, uint time) {
 
286
                drag_unhighlight(coverArt);
 
287
                update_cover_art();
 
288
        }
 
289
        
 
290
        void drag_received(Gdk.DragContext context, int x, int y, Gtk.SelectionData data, uint info, uint timestamp) {
 
291
                if(lm.media_info.media == null || lm.media_info.media.isTemporary)
 
292
                        return;
 
293
                
 
294
                bool success = true;
 
295
                
 
296
                foreach(string uri in data.get_uris()) {
 
297
                        
 
298
                        if(is_valid_image_type(uri) && lm.media_info.media != null) {
 
299
                                message("Saving dragged album art as image\n");
 
300
                                lm.save_album_locally(lm.media_info.media.rowid, uri);
 
301
                        }
 
302
                        else {
 
303
                                message("Dragged album art is not valid image\n");
 
304
                        }
 
305
                        
 
306
                        Gtk.drag_finish (context, success, false, timestamp);
 
307
                        return;
 
308
                }
 
309
    }
 
310
}