d607a8ba4bb866af541542b7c7c09e1b6c35ab52
[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             for item in settings.favorites:
73                 try:
74                     if isinstance(item, tuple) and len(item) == 2:
75                         ftype, fid = item
76                         if ftype == 'album':
77                             add_album(fid, lambda: jamaendo.get_album(fid))
78
79                 except jamaendo.JamendoAPIException, e:
80                     log.exception("jamaendo.get_album(%s)"%(fid))
81
82             self.add(self.panarea)
83
84         else:
85             vbox = gtk.VBox()
86             lbl = gtk.Label()
87             lbl.set_markup("""<span size="xx-large">jamendo.com
88 in the settings dialog
89 enter your username</span>
90 """)
91             lbl.set_single_line_mode(False)
92             vbox.pack_start(lbl, True, False)
93             self.add(vbox)
94
95     def get_item_text(self, item):
96         if isinstance(item, jamaendo.Album):
97             return "%s - %s" % (item.artist_name, item.name)
98         elif isinstance(item, jamaendo.Track):
99             return "%s - %s" % (item.artist_name, item.name)
100         else:
101             return item.name
102
103     def make_button(self, text, subtext, callback):
104         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT,
105                                hildon.BUTTON_ARRANGEMENT_VERTICAL)
106         button.set_text(text, subtext)
107
108         if callback:
109             button.connect('clicked', callback)
110
111         #image = gtk.image_new_from_stock(gtk.STOCK_INFO, gtk.ICON_SIZE_BUTTON)
112         #button.set_image(image)
113         #button.set_image_position(gtk.POS_RIGHT)
114
115         return button
116
117     def row_activated(self, treeview, path, view_column):
118         _id = self.results.get_album_id(path)
119         item = self.idmap[_id]
120         print _id, item
121         self.open_item(item)
122
123     def open_item(self, item):
124         if isinstance(item, jamaendo.Album):
125             wnd = ShowAlbum(item)
126             wnd.show_all()
127         elif isinstance(item, jamaendo.Artist):
128             wnd = ShowArtist(item)
129             wnd.show_all()
130         elif isinstance(item, jamaendo.Track):
131             wnd = open_playerwindow()
132             wnd.play_tracks([item])