New year, big checkin
[jamaendo] / jamaui / favorites.py
1 import gtk
2 import hildon
3 import jamaendo
4 from playerwindow import open_playerwindow
5 from showartist import ShowArtist
6 from showalbum import ShowAlbum
7 from settings import settings
8 import logging
9
10 log = logging.getLogger(__name__)
11
12 def _alist(l, match):
13     for key, value in l:
14         if key == match:
15             return value
16     return None
17
18 class FavoritesWindow(hildon.StackableWindow):
19     def __init__(self):
20         hildon.StackableWindow.__init__(self)
21         self.set_title("Favorites")
22
23         if settings.user:
24             # Results list
25             self.panarea = hildon.PannableArea()
26             self.result_store = gtk.ListStore(str, int)
27             #self.result_store.append(['red'])
28             self.result_view = gtk.TreeView(self.result_store)
29             col = gtk.TreeViewColumn('Name')
30             self.result_view.append_column(col)
31             cell = gtk.CellRendererText()
32             col.pack_start(cell, True)
33             col.add_attribute(cell, 'text', 0)
34             self.result_view.set_search_column(0)
35             col.set_sort_column_id(0)
36             self.result_view.connect('row-activated', self.row_activated)
37
38             self.panarea.add(self.result_view)
39
40             self.idmap = {}
41             try:
42                 for item in jamaendo.favorite_albums(settings.user):
43                     self.idmap[item.ID] = item
44                     self.result_store.append([self.get_item_text(item), item.ID])
45             except jamaendo.JamendoAPIException, e:
46                 msg = "Query failed, is the user name '%s' correct?" % (settings.user)
47                 banner = hildon.hildon_banner_show_information(self, '',
48                                                                msg)
49                 banner.set_timeout(3000)
50
51
52             def add_album(albumid):
53                 album = jamaendo.get_album(albumid)
54                 self.idmap[albumid] = album
55                 self.result_store.append([self.get_item_text(album), albumid])
56
57             for item in settings.favorites:
58                 try:
59                     if isinstance(item, tuple) and len(item) == 2:
60                         ftype, fid = item
61                         if ftype == 'album':
62                             add_album(fid)
63
64                 except jamaendo.JamendoAPIException, e:
65                     log.exception("jamaendo.get_album(%s)"%(fid))
66
67             self.add(self.panarea)
68
69         else:
70             vbox = gtk.VBox()
71             lbl = gtk.Label()
72             lbl.set_markup("""<span size="xx-large">jamendo.com
73 in the settings dialog
74 enter your username</span>
75 """)
76             lbl.set_single_line_mode(False)
77             vbox.pack_start(lbl, True, False)
78             self.add(vbox)
79
80     def get_item_text(self, item):
81         if isinstance(item, jamaendo.Album):
82             return "%s - %s" % (item.artist_name, item.name)
83         elif isinstance(item, jamaendo.Track):
84             return "%s - %s" % (item.artist_name, item.name)
85         else:
86             return item.name
87
88     def make_button(self, text, subtext, callback):
89         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT,
90                                hildon.BUTTON_ARRANGEMENT_VERTICAL)
91         button.set_text(text, subtext)
92
93         if callback:
94             button.connect('clicked', callback)
95
96         #image = gtk.image_new_from_stock(gtk.STOCK_INFO, gtk.ICON_SIZE_BUTTON)
97         #button.set_image(image)
98         #button.set_image_position(gtk.POS_RIGHT)
99
100         return button
101
102     def row_activated(self, treeview, path, view_column):
103         treeiter = self.result_store.get_iter(path)
104         title, _id = self.result_store.get(treeiter, 0, 1)
105         item = self.idmap[_id]
106         print _id, item
107         self.open_item(item)
108
109     def open_item(self, item):
110         if isinstance(item, jamaendo.Album):
111             wnd = ShowAlbum(item)
112             wnd.show_all()
113         elif isinstance(item, jamaendo.Artist):
114             wnd = ShowArtist(item)
115             wnd.show_all()
116         elif isinstance(item, jamaendo.Track):
117             wnd = open_playerwindow()
118             wnd.play_tracks([item])