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):
94 postoffice.disconnect('images', self)
99 def start_album_fetcher(self):
103 self.fetcher = Fetcher(lambda: jamaendo.get_albums(self.artist.ID), self,
104 on_item = self.on_album_result,
105 on_ok = self.on_album_complete,
106 on_fail = self.on_album_complete)
109 def on_album_result(self, wnd, item):
111 self.albums.add_album(item)
112 self.albumlist.append(item)
114 def on_album_complete(self, wnd, error=None):
119 def create_menu(self):
120 def on_player(*args):
121 from playerwindow import open_playerwindow
123 self.menu = hildon.AppMenu()
124 player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
125 player.set_label("Open player")
126 player.connect("clicked", on_player)
127 self.menu.append(player)
128 player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
129 player.set_label("Add to playlist")
130 player.connect("clicked", self.on_add_to_playlist)
131 self.menu.append(player)
133 self.set_app_menu(self.menu)
135 def start_addpl_fetcher(self):
140 for album in self.albumlist:
141 yield jamaendo.get_tracks(album.ID)
143 self.fetcher = Fetcher(fetchgen, self,
144 on_item = self.on_addpl_result,
145 on_ok = self.on_addpl_complete,
146 on_fail = self.on_addpl_complete)
147 self.fetcher.tracklist = []
148 self.fetcher.pdlg = gtk.Dialog("Fetching album tracks", self)
149 self.fetcher.pbar = gtk.ProgressBar()
150 self.fetcher.pbar.set_fraction(0)
151 self.fetcher.pdlg.vbox.add(self.fetcher.pbar)
152 self.fetcher.pdlg.show_all()
153 self.fetcher.ppos = 1
156 def on_addpl_result(self, wnd, items):
158 self.fetcher.tracklist.extend(items)
159 self.fetcher.ppos += 1
160 self.fetcher.pbar.set_fraction(float(self.fetcher.ppos)/float(len(self.albumlist)+1))
162 def on_addpl_complete(self, wnd, error=None):
165 self.fetcher.pdlg.destroy()
166 if self.fetcher.tracklist:
167 add_to_playlist(self, self.fetcher.tracklist)
171 def on_add_to_playlist(self, button, user_data=None):
173 self.start_addpl_fetcher()
175 show_banner(self, "Error when opening track list")
177 def get_pixbuf(self, img):
179 return gtk.gdk.pixbuf_new_from_file_at_size(img,
182 except gobject.GError:
183 log.error("Broken image in cache: %s", img)
187 log.warning("Failed to unlink broken image.")
188 if img != self.default_pixbuf:
189 return self.get_default_pixbuf()
193 def get_default_pixbuf(self):
194 if self.default_pixbuf:
195 return self.get_pixbuf(self.default_pixbuf)
197 def on_images(self, images):
198 for url, image in images:
199 if url == self.artist.image:
200 pb = self.get_pixbuf(image)
202 self.image.set_from_pixbuf(pb)
204 def start_albumplay_fetcher(self, ID):
209 yield jamaendo.get_album(ID)
211 self.fetcher = Fetcher(albumgen, self,
212 on_item = self.on_albumplay_result,
213 on_ok = self.on_albumplay_complete,
214 on_fail = self.on_albumplay_complete)
215 self.fetcher.album = None
218 def on_albumplay_result(self, wnd, item):
220 if isinstance(item, list):
221 self.fetcher.album = item[0]
223 self.fetcher.album = item
225 def on_albumplay_complete(self, wnd, error=None):
228 if self.fetcher.album:
229 self.open_item(self.fetcher.album)
232 def row_activated(self, treeview, path, view_column):
233 _id = self.albums.get_album_id(path)
234 self.start_albumplay_fetcher(_id)
236 def open_item(self, item):
237 if isinstance(item, jamaendo.Album):
238 from showalbum import ShowAlbum
239 wnd = ShowAlbum(item)
241 elif isinstance(item, jamaendo.Artist):
242 wnd = ShowArtist(item)
244 elif isinstance(item, jamaendo.Track):
245 wnd = open_playerwindow()
246 wnd.play_tracks([item])