9b0e9ba42fe6f101017f020c374b73a5dea2a5b0
[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
37 import logging
38
39 log = logging.getLogger(__name__)
40
41 class ShowArtist(hildon.StackableWindow):
42     ICON_SIZE = 200
43
44     def __init__(self, artist):
45         hildon.StackableWindow.__init__(self)
46         self.connect('destroy', self.on_destroy)
47         self.set_title(artist.name)
48         self.artist = artist
49
50         top_hbox = gtk.HBox()
51         self.image = gtk.Image()
52         self.default_pixbuf = util.find_resource('album.png')
53         self.image.set_from_pixbuf(self.get_default_pixbuf())
54
55         self.panarea = hildon.PannableArea()
56         vbox = gtk.VBox(False, 0)
57
58         self.albums = AlbumList()
59         self.albums.loading_message = "No albums"
60         self.albums.empty_message = "No albums"
61         self.albums.show_artist(False)
62         self.albums.connect('row-activated', self.row_activated)
63
64         self.panarea.add(self.albums)
65         vbox.pack_start(self.panarea, True, True, 0)
66         #self.add(vbox)
67
68         #imgalign = gtk.Alignment(xalign=0.2, yalign=0.4, xscale=1.0)
69         #alignment.add(bbox)
70
71         self.image.set_alignment(0.5, 0.0)
72
73         top_hbox.pack_start(self.image, False)
74         top_hbox.pack_start(vbox)
75
76         self.add(top_hbox)
77
78         try:
79             for album in jamaendo.get_albums(artist.ID):
80                 self.albums.add_album(album)
81         except jamaendo.JamendoAPIException:
82             log.exception("Failed in get_albums(%s)"%(artist.ID))
83
84         postoffice.connect('images', self, self.on_images)
85
86         if self.artist.image:
87             postoffice.notify('request-images', [self.artist.image])
88
89     def get_pixbuf(self, img):
90         try:
91             return gtk.gdk.pixbuf_new_from_file_at_size(img,
92                                                         self.ICON_SIZE,
93                                                         self.ICON_SIZE)
94         except gobject.GError:
95             log.error("Broken image in cache: %s", img)
96             try:
97                 os.unlink(img)
98             except OSError, e:
99                 log.warning("Failed to unlink broken image.")
100             if img != self.default_pixbuf:
101                 return self.get_default_pixbuf()
102             else:
103                 return None
104
105     def get_default_pixbuf(self):
106         if self.default_pixbuf:
107             return self.get_pixbuf(self.default_pixbuf)
108
109     def on_images(self, images):
110         for url, image in images:
111             if url == self.artist.image:
112                 pb = self.get_pixbuf(image)
113                 if pb:
114                     self.image.set_from_pixbuf(pb)
115
116     def on_destroy(self, wnd):
117         postoffice.disconnect('images', self)
118
119     def row_activated(self, treeview, path, view_column):
120         _id = self.albums.get_album_id(path)
121         album = jamaendo.get_album(_id)
122         if isinstance(album, list):
123             album = album[0]
124         self.open_item(album)
125
126     def open_item(self, item):
127         if isinstance(item, jamaendo.Album):
128             from showalbum import ShowAlbum
129             wnd = ShowAlbum(item)
130             wnd.show_all()
131         elif isinstance(item, jamaendo.Artist):
132             wnd = ShowArtist(item)
133             wnd.show_all()
134         elif isinstance(item, jamaendo.Track):
135             wnd = open_playerwindow()
136             wnd.play_tracks([item])