hmm, asynchronous api is nontrivial
[jamaendo] / jamaui / showalbum.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 gtk
25 import hildon
26 import jamaendo
27 from playerwindow import open_playerwindow
28 from settings import settings
29 import util
30
31 _instance = None
32
33 def album_cover_receiver(albumid, size, cover):
34     if _instance:
35         playing = _instance.get_album_id()
36         if int(playing) == int(albumid):
37             _instance.set_album_cover(cover)
38
39 class ShowAlbum(hildon.StackableWindow):
40     def __init__(self, album):
41         hildon.StackableWindow.__init__(self)
42         self.set_title("Album")
43         self.album = album
44
45         global _instance
46         _instance = self
47         self.connect('destroy', self.on_destroy)
48
49         top_vbox = gtk.VBox()
50         top_hbox = gtk.HBox()
51         vbox1 = gtk.VBox()
52         self.cover = gtk.Image()
53         self.bbox = gtk.HButtonBox()
54         self.bbox.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
55         self.goto_artist = self.make_imagebutton('artist', self.on_goto_artist)
56         self.download = self.make_imagebutton('download', self.on_download)
57         self.favorite = self.make_imagebutton('favorite', self.on_favorite)
58         self.license = self.make_imagebutton('license', self.on_license)
59         self.playbtn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
60         self.playbtn.set_label("Play album")
61         self.playbtn.set_border_width(0)
62         self.playbtn.connect('clicked', self.on_play)
63
64         vbox2 = gtk.VBox()
65         self.albumtitle = gtk.Label()
66         self.albumtitle.set_markup('<big>%s</big>' % (album.name))
67         self.artist = gtk.Label()
68         self.artist.set_markup('<span color="#cccccc">%s</span>'%(album.artist_name))
69         self.trackarea = hildon.PannableArea()
70
71         self.album_store = gtk.ListStore(int, str, int)
72         self.album_view = gtk.TreeView(self.album_store)
73         col0 = gtk.TreeViewColumn('Num')
74         col = gtk.TreeViewColumn('Name')
75         self.album_view.append_column(col0)
76         self.album_view.append_column(col)
77         cell0 = gtk.CellRendererText()
78         col0.pack_start(cell0, True)
79         col0.add_attribute(cell0, 'text', 0)
80         cell = gtk.CellRendererText()
81         col.pack_start(cell, True)
82         col.add_attribute(cell, 'text', 1)
83         self.album_view.set_search_column(1)
84         col.set_sort_column_id(0)
85         self.album_view.connect('row-activated', self.row_activated)
86
87         for track in jamaendo.get_tracks(album.ID):
88             self.album_store.append([track.numalbum, track.name, track.ID])
89
90         top_vbox.pack_start(self.albumtitle, False)
91         top_vbox.pack_start(top_hbox)
92         top_hbox.pack_start(vbox1, False)
93         top_hbox.pack_start(vbox2, True)
94         vbox1.pack_start(self.cover, True)
95         vbox1.pack_start(self.playbtn, False)
96         vbox1.pack_start(self.bbox, False)
97         self.bbox.add(self.goto_artist)
98         self.bbox.add(self.download)
99         self.bbox.add(self.favorite)
100         self.bbox.add(self.license)
101         vbox2.pack_start(self.artist, False)
102         vbox2.pack_start(self.trackarea, True)
103         self.trackarea.add(self.album_view)
104
105         self.add(top_vbox)
106
107         # here it's probably ok to get the cover synchronously..
108         coverfile = jamaendo.get_album_cover(self.album.ID, size=200)
109         self.cover.set_from_file(coverfile)
110
111         self.show_all()
112
113     def on_destroy(self, wnd):
114         global _instance
115         _instance = None
116
117     def make_imagebutton(self, name, cb):
118         btn = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
119         btn.set_relief(gtk.RELIEF_NONE)
120         img = util.find_resource('icon_%s.png'%(name))
121         if img:
122             btn.set_image(gtk.image_new_from_file(img))
123         else:
124             btn.set_image(gtk.image_new_from_stock(gtk.STOCK_MEDIA_STOP, gtk.ICON_SIZE_SMALL_TOOLBAR))
125         btn.set_border_width(0)
126         btn.connect('clicked', cb)
127         return btn
128
129     def on_goto_artist(self, btn):
130         artist = jamaendo.get_artist(int(self.album.artist_id))
131         self.open_item(artist)
132
133     def on_download(self, btn):
134         banner = hildon.hildon_banner_show_information(self, '', "Downloads disabled in this version")
135         banner.set_timeout(2000)
136
137     def on_favorite(self, btn):
138         settings.favorite(self.album)
139         banner = hildon.hildon_banner_show_information(self, '', "Favorite added")
140         banner.set_timeout(2000)
141
142
143     def on_license(self, btn):
144         url = self.album.license_url
145         import webbrowser
146         webbrowser.open_new(url)
147
148     def on_play(self, btn):
149         self.open_item(self.album)
150
151     def row_activated(self, treeview, path, view_column):
152         pass
153
154     def open_item(self, item):
155         if isinstance(item, jamaendo.Album):
156             tracks = jamaendo.get_tracks(item.ID)
157             if tracks:
158                 wnd = open_playerwindow()
159                 wnd.play_tracks(tracks)
160         elif isinstance(item, jamaendo.Artist):
161             from showartist import ShowArtist
162             wnd = ShowArtist(item)
163             wnd.show_all()
164         elif isinstance(item, jamaendo.Track):
165             wnd = open_playerwindow()
166             wnd.play_tracks([item])