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