3 # This file is part of Jamaendo.
4 # Copyright (c) 2010 Kristoffer Gronlund
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.
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.
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/>.
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)
29 import helldon as hildon
31 from playerwindow import open_playerwindow
32 from albumlist import AlbumList
33 from postoffice import postoffice
36 from playlists import add_to_playlist, show_banner
37 from fetcher import Fetcher
41 log = logging.getLogger(__name__)
43 class ShowArtist(hildon.StackableWindow):
46 def __init__(self, artist):
47 hildon.StackableWindow.__init__(self)
48 self.connect('destroy', self.on_destroy)
49 self.set_title(artist.name)
52 self.connect('destroy', self.on_destroy)
56 self.image = gtk.Image()
57 self.default_pixbuf = util.find_resource('album.png')
58 self.image.set_from_pixbuf(self.get_default_pixbuf())
60 self.panarea = hildon.PannableArea()
61 vbox = gtk.VBox(False, 0)
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)
69 self.panarea.add(self.albums)
70 vbox.pack_start(self.panarea, True, True, 0)
73 #imgalign = gtk.Alignment(xalign=0.2, yalign=0.4, xscale=1.0)
76 self.image.set_alignment(0.5, 0.0)
78 top_hbox.pack_start(self.image, False)
79 top_hbox.pack_start(vbox)
85 postoffice.connect('images', self, self.on_images)
88 postoffice.notify('request-images', [self.artist.image])
91 self.start_album_fetcher()
93 def on_destroy(self, wnd):
98 def start_album_fetcher(self):
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)
108 def on_album_result(self, wnd, item):
110 self.albums.add_album(item)
111 self.albumlist.append(item)
113 def on_album_complete(self, wnd, error=None):
118 def create_menu(self):
119 def on_player(*args):
120 from playerwindow import 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)
132 self.set_app_menu(self.menu)
134 def on_add_to_playlist(self, button, user_data=None):
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)
144 show_banner(self, "Error when opening track list")
146 def get_pixbuf(self, img):
148 return gtk.gdk.pixbuf_new_from_file_at_size(img,
151 except gobject.GError:
152 log.error("Broken image in cache: %s", img)
156 log.warning("Failed to unlink broken image.")
157 if img != self.default_pixbuf:
158 return self.get_default_pixbuf()
162 def get_default_pixbuf(self):
163 if self.default_pixbuf:
164 return self.get_pixbuf(self.default_pixbuf)
166 def on_images(self, images):
167 for url, image in images:
168 if url == self.artist.image:
169 pb = self.get_pixbuf(image)
171 self.image.set_from_pixbuf(pb)
173 def on_destroy(self, wnd):
174 postoffice.disconnect('images', self)
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):
181 self.open_item(album)
183 def open_item(self, item):
184 if isinstance(item, jamaendo.Album):
185 from showalbum import ShowAlbum
186 wnd = ShowAlbum(item)
188 elif isinstance(item, jamaendo.Artist):
189 wnd = ShowArtist(item)
191 elif isinstance(item, jamaendo.Track):
192 wnd = open_playerwindow()
193 wnd.play_tracks([item])