Oops, my rate limiting was all broken...
[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
38 import logging
39
40 log = logging.getLogger(__name__)
41
42 class ShowArtist(hildon.StackableWindow):
43     ICON_SIZE = 200
44
45     def __init__(self, artist):
46         hildon.StackableWindow.__init__(self)
47         self.connect('destroy', self.on_destroy)
48         self.set_title(artist.name)
49         self.artist = artist
50
51         top_hbox = gtk.HBox()
52         self.image = gtk.Image()
53         self.default_pixbuf = util.find_resource('album.png')
54         self.image.set_from_pixbuf(self.get_default_pixbuf())
55
56         self.panarea = hildon.PannableArea()
57         vbox = gtk.VBox(False, 0)
58
59         self.albums = AlbumList()
60         self.albums.loading_message = "No albums"
61         self.albums.empty_message = "No albums"
62         self.albums.show_artist(False)
63         self.albums.connect('row-activated', self.row_activated)
64
65         self.panarea.add(self.albums)
66         vbox.pack_start(self.panarea, True, True, 0)
67         #self.add(vbox)
68
69         #imgalign = gtk.Alignment(xalign=0.2, yalign=0.4, xscale=1.0)
70         #alignment.add(bbox)
71
72         self.image.set_alignment(0.5, 0.0)
73
74         top_hbox.pack_start(self.image, False)
75         top_hbox.pack_start(vbox)
76
77         self.add(top_hbox)
78
79         try:
80             self.albumlist = jamaendo.get_albums(artist.ID)
81             for album in self.albumlist:
82                 self.albums.add_album(album)
83         except jamaendo.JamendoAPIException:
84             log.exception("Failed in get_albums(%s)"%(artist.ID))
85
86         postoffice.connect('images', self, self.on_images)
87
88         if self.artist.image:
89             postoffice.notify('request-images', [self.artist.image])
90
91         self.create_menu()
92
93     def create_menu(self):
94         def on_player(*args):
95             from playerwindow import open_playerwindow
96             open_playerwindow()
97         self.menu = hildon.AppMenu()
98         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
99         player.set_label("Open player")
100         player.connect("clicked", on_player)
101         self.menu.append(player)
102         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
103         player.set_label("Add to playlist")
104         player.connect("clicked", self.on_add_to_playlist)
105         self.menu.append(player)
106         self.menu.show_all()
107         self.set_app_menu(self.menu)
108
109     def on_add_to_playlist(self, button, user_data=None):
110         if self.albumlist:
111             try:
112                 tracklist = []
113                 for album in self.albumlist:
114                     tracklist.extend(jamaendo.get_tracks(album.ID))
115                 add_to_playlist(self, tracklist)
116             except jamaendo.JamendoAPIException:
117                 log.exception("Failed to get track list for artist %s", self.artist.ID)
118         else:
119             show_banner(self, "Error when opening track list")
120
121     def get_pixbuf(self, img):
122         try:
123             return gtk.gdk.pixbuf_new_from_file_at_size(img,
124                                                         self.ICON_SIZE,
125                                                         self.ICON_SIZE)
126         except gobject.GError:
127             log.error("Broken image in cache: %s", img)
128             try:
129                 os.unlink(img)
130             except OSError, e:
131                 log.warning("Failed to unlink broken image.")
132             if img != self.default_pixbuf:
133                 return self.get_default_pixbuf()
134             else:
135                 return None
136
137     def get_default_pixbuf(self):
138         if self.default_pixbuf:
139             return self.get_pixbuf(self.default_pixbuf)
140
141     def on_images(self, images):
142         for url, image in images:
143             if url == self.artist.image:
144                 pb = self.get_pixbuf(image)
145                 if pb:
146                     self.image.set_from_pixbuf(pb)
147
148     def on_destroy(self, wnd):
149         postoffice.disconnect('images', self)
150
151     def row_activated(self, treeview, path, view_column):
152         _id = self.albums.get_album_id(path)
153         album = jamaendo.get_album(_id)
154         if isinstance(album, list):
155             album = album[0]
156         self.open_item(album)
157
158     def open_item(self, item):
159         if isinstance(item, jamaendo.Album):
160             from showalbum import ShowAlbum
161             wnd = ShowAlbum(item)
162             wnd.show_all()
163         elif isinstance(item, jamaendo.Artist):
164             wnd = ShowArtist(item)
165             wnd.show_all()
166         elif isinstance(item, jamaendo.Track):
167             wnd = open_playerwindow()
168             wnd.play_tracks([item])