Added 'helldon' to transparently port the thing to the desktop :P
[jamaendo] / jamaui / favorites.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 try:
26     import hildon
27 except:
28     import helldon as hildon
29 import jamaendo
30 from playerwindow import open_playerwindow
31 from showartist import ShowArtist
32 from showalbum import ShowAlbum
33 from settings import settings
34 import logging
35
36 from albumlist import AlbumList
37
38 log = logging.getLogger(__name__)
39
40 def _alist(l, match):
41     for key, value in l:
42         if key == match:
43             return value
44     return None
45
46 class FavoritesWindow(hildon.StackableWindow):
47     def __init__(self):
48         hildon.StackableWindow.__init__(self)
49         self.set_title("Favorites")
50
51         if settings.user:
52             # Results list
53             self.panarea = hildon.PannableArea()
54             self.results = AlbumList()
55             self.results.connect('row-activated', self.row_activated)
56             self.panarea.add(self.results)
57
58             self.idmap = {}
59
60             def add_album(ID, album_factory):
61                 if ID not in self.idmap:
62                     album = album_factory()
63                     self.idmap[ID] = album
64                     self.results.add_album(album)
65
66             try:
67                 for item in jamaendo.favorite_albums(settings.user):
68                     add_album(item.ID, lambda: item)
69             except jamaendo.JamendoAPIException, e:
70                 msg = "Query failed, is the user name '%s' correct?" % (settings.user)
71                 banner = hildon.hildon_banner_show_information(self, '',
72                                                                msg)
73                 banner.set_timeout(3000)
74
75             favorite_albums = [f[1] for f in settings.favorites if isinstance(f, tuple) and len(f) == 2 and f[0] == 'album' and f[1] not in self.idmap]
76             try:
77                 for album in jamaendo.get_albums(favorite_albums):
78                     add_album(album.ID, lambda: album)
79
80             except jamaendo.JamendoAPIException, e:
81                 log.exception("jamaendo.get_albums(%s)"%(favorite_albums))
82
83             self.add(self.panarea)
84
85         else:
86             vbox = gtk.VBox()
87             lbl = gtk.Label()
88             lbl.set_markup("""<span size="xx-large">jamendo.com
89 in the settings dialog
90 enter your username</span>
91 """)
92             lbl.set_single_line_mode(False)
93             vbox.pack_start(lbl, True, False)
94             self.add(vbox)
95
96     def get_item_text(self, item):
97         if isinstance(item, jamaendo.Album):
98             return "%s - %s" % (item.artist_name, item.name)
99         elif isinstance(item, jamaendo.Track):
100             return "%s - %s" % (item.artist_name, item.name)
101         else:
102             return item.name
103
104     def make_button(self, text, subtext, callback):
105         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT,
106                                hildon.BUTTON_ARRANGEMENT_VERTICAL)
107         button.set_text(text, subtext)
108
109         if callback:
110             button.connect('clicked', callback)
111
112         #image = gtk.image_new_from_stock(gtk.STOCK_INFO, gtk.ICON_SIZE_BUTTON)
113         #button.set_image(image)
114         #button.set_image_position(gtk.POS_RIGHT)
115
116         return button
117
118     def row_activated(self, treeview, path, view_column):
119         _id = self.results.get_album_id(path)
120         item = self.idmap[_id]
121         #print _id, item
122         self.open_item(item)
123
124     def open_item(self, item):
125         if isinstance(item, jamaendo.Album):
126             wnd = ShowAlbum(item)
127             wnd.show_all()
128         elif isinstance(item, jamaendo.Artist):
129             wnd = ShowArtist(item)
130             wnd.show_all()
131         elif isinstance(item, jamaendo.Track):
132             wnd = open_playerwindow()
133             wnd.play_tracks([item])