99a497704fa3fd3edf06afc7e4c19889c052d488
[jamaendo] / jamaui / featured.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
8 def _alist(l, match):
9     for key, value in l:
10         if key == match:
11             return value
12     return None
13
14 class FeaturedWindow(hildon.StackableWindow):
15     features = (("Albums of the week",jamaendo.albums_of_the_week),
16                 ("Tracks of the week",jamaendo.tracks_of_the_week),
17                 ("New releases",jamaendo.new_releases)
18                 )
19
20     def __init__(self, feature):
21         hildon.StackableWindow.__init__(self)
22         self.set_title("Featured")
23
24         self.featurefn = _alist(self.features, feature)
25
26         # Results list
27         self.panarea = hildon.PannableArea()
28         self.result_store = gtk.ListStore(str, int)
29         #self.result_store.append(['red'])
30         self.result_view = gtk.TreeView(self.result_store)
31         col = gtk.TreeViewColumn('Name')
32         self.result_view.append_column(col)
33         cell = gtk.CellRendererText()
34         col.pack_start(cell, True)
35         col.add_attribute(cell, 'text', 0)
36         self.result_view.set_search_column(0)
37         col.set_sort_column_id(0)
38         self.result_view.connect('row-activated', self.row_activated)
39
40         self.panarea.add(self.result_view)
41
42         self.idmap = {}
43         for item in self.featurefn():
44             self.idmap[item.ID] = item
45             self.result_store.append([self.get_item_text(item), item.ID])
46
47         self.add(self.panarea)
48
49     def get_item_text(self, item):
50         if isinstance(item, jamaendo.Album):
51             return "%s - %s" % (item.artist_name, item.name)
52         elif isinstance(item, jamaendo.Track):
53             return "%s - %s" % (item.artist_name, item.name)
54         else:
55             return item.name
56
57     def make_button(self, text, subtext, callback):
58         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT,
59                                hildon.BUTTON_ARRANGEMENT_VERTICAL)
60         button.set_text(text, subtext)
61
62         if callback:
63             button.connect('clicked', callback)
64
65         #image = gtk.image_new_from_stock(gtk.STOCK_INFO, gtk.ICON_SIZE_BUTTON)
66         #button.set_image(image)
67         #button.set_image_position(gtk.POS_RIGHT)
68
69         return button
70
71     def row_activated(self, treeview, path, view_column):
72         treeiter = self.result_store.get_iter(path)
73         title, _id = self.result_store.get(treeiter, 0, 1)
74         item = self.idmap[_id]
75         print _id, item
76         self.open_item(item)
77
78     def open_item(self, item):
79         if isinstance(item, jamaendo.Album):
80             wnd = ShowAlbum(item)
81             wnd.show_all()
82         elif isinstance(item, jamaendo.Artist):
83             wnd = ShowArtist(item)
84             wnd.show_all()
85         elif isinstance(item, jamaendo.Track):
86             wnd = open_playerwindow()
87             wnd.play_tracks([item])