Possibly buggy updates of show artist / show album to do background fetching
[jamaendo] / jamaui / showartist.py
1 #!/usr/bin/env python
2 #
3 # This file is part of Jamaendo.
4 # Copyright (c) 2010 Kristoffer Gronlund
5 #
6 # Jamaendo is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Jamaendo is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Jamaendo.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Player code heavily based on http://thpinfo.com/2008/panucci/:
20 #  A resuming media player for Podcasts and Audiobooks
21 #  Copyright (c) 2008-05-26 Thomas Perl <thpinfo.com>
22 #  (based on http://pygstdocs.berlios.de/pygst-tutorial/seeking.html)
23 #
24 import os
25 import gtk
26 try:
27     import hildon
28 except:
29     import helldon as hildon
30 import jamaendo
31 from playerwindow import open_playerwindow
32 from albumlist import AlbumList
33 from postoffice import postoffice
34 import util
35 import gobject
36 from playlists import add_to_playlist, show_banner
37 from fetcher import Fetcher
38
39 import logging
40
41 log = logging.getLogger(__name__)
42
43 class ShowArtist(hildon.StackableWindow):
44     ICON_SIZE = 200
45
46     def __init__(self, artist):
47         hildon.StackableWindow.__init__(self)
48         self.connect('destroy', self.on_destroy)
49         self.set_title(artist.name)
50         self.artist = artist
51
52         self.connect('destroy', self.on_destroy)
53         self.fetcher = None
54
55         top_hbox = gtk.HBox()
56         self.image = gtk.Image()
57         self.default_pixbuf = util.find_resource('album.png')
58         self.image.set_from_pixbuf(self.get_default_pixbuf())
59
60         self.panarea = hildon.PannableArea()
61         vbox = gtk.VBox(False, 0)
62
63         self.albums = AlbumList()
64         self.albums.loading_message = "No albums"
65         self.albums.empty_message = "No albums"
66         self.albums.show_artist(False)
67         self.albums.connect('row-activated', self.row_activated)
68
69         self.panarea.add(self.albums)
70         vbox.pack_start(self.panarea, True, True, 0)
71         #self.add(vbox)
72
73         #imgalign = gtk.Alignment(xalign=0.2, yalign=0.4, xscale=1.0)
74         #alignment.add(bbox)
75
76         self.image.set_alignment(0.5, 0.0)
77
78         top_hbox.pack_start(self.image, False)
79         top_hbox.pack_start(vbox)
80
81         self.add(top_hbox)
82
83         self.albumlist = []
84
85         postoffice.connect('images', self, self.on_images)
86
87         if self.artist.image:
88             postoffice.notify('request-images', [self.artist.image])
89
90         self.create_menu()
91         self.start_album_fetcher()
92
93     def on_destroy(self, wnd):
94         if self.fetcher:
95             self.fetcher.stop()
96             self.fetcher = None
97
98     def start_album_fetcher(self):
99         if self.fetcher:
100             self.fetcher.stop()
101             self.fetcher = None
102         self.fetcher = Fetcher(lambda: jamaendo.get_albums(self.artist.ID), self,
103                                on_item = self.on_album_result,
104                                on_ok = self.on_album_complete,
105                                on_fail = self.on_album_complete)
106         self.fetcher.start()
107
108     def on_album_result(self, wnd, item):
109         if wnd is self:
110             self.albums.add_album(item)
111             self.albumlist.append(item)
112
113     def on_album_complete(self, wnd, error=None):
114         if wnd is self:
115             self.fetcher.stop()
116             self.fetcher = None
117
118     def create_menu(self):
119         def on_player(*args):
120             from playerwindow import open_playerwindow
121             open_playerwindow()
122         self.menu = hildon.AppMenu()
123         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
124         player.set_label("Open player")
125         player.connect("clicked", on_player)
126         self.menu.append(player)
127         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
128         player.set_label("Add to playlist")
129         player.connect("clicked", self.on_add_to_playlist)
130         self.menu.append(player)
131         self.menu.show_all()
132         self.set_app_menu(self.menu)
133
134     def on_add_to_playlist(self, button, user_data=None):
135         if self.albumlist:
136             try:
137                 tracklist = []
138                 for album in self.albumlist:
139                     tracklist.extend(jamaendo.get_tracks(album.ID))
140                 add_to_playlist(self, tracklist)
141             except jamaendo.JamendoAPIException:
142                 log.exception("Failed to get track list for artist %s", self.artist.ID)
143         else:
144             show_banner(self, "Error when opening track list")
145
146     def get_pixbuf(self, img):
147         try:
148             return gtk.gdk.pixbuf_new_from_file_at_size(img,
149                                                         self.ICON_SIZE,
150                                                         self.ICON_SIZE)
151         except gobject.GError:
152             log.error("Broken image in cache: %s", img)
153             try:
154                 os.unlink(img)
155             except OSError, e:
156                 log.warning("Failed to unlink broken image.")
157             if img != self.default_pixbuf:
158                 return self.get_default_pixbuf()
159             else:
160                 return None
161
162     def get_default_pixbuf(self):
163         if self.default_pixbuf:
164             return self.get_pixbuf(self.default_pixbuf)
165
166     def on_images(self, images):
167         for url, image in images:
168             if url == self.artist.image:
169                 pb = self.get_pixbuf(image)
170                 if pb:
171                     self.image.set_from_pixbuf(pb)
172
173     def on_destroy(self, wnd):
174         postoffice.disconnect('images', self)
175
176     def row_activated(self, treeview, path, view_column):
177         _id = self.albums.get_album_id(path)
178         album = jamaendo.get_album(_id)
179         if isinstance(album, list):
180             album = album[0]
181         self.open_item(album)
182
183     def open_item(self, item):
184         if isinstance(item, jamaendo.Album):
185             from showalbum import ShowAlbum
186             wnd = ShowAlbum(item)
187             wnd.show_all()
188         elif isinstance(item, jamaendo.Artist):
189             wnd = ShowArtist(item)
190             wnd.show_all()
191         elif isinstance(item, jamaendo.Track):
192             wnd = open_playerwindow()
193             wnd.play_tracks([item])