A thousand cuts..
[jamaendo] / jamaui / featured.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
31 def _alist(l, match):
32     for key, value in l:
33         if key == match:
34             return value
35     return None
36
37 class FeaturedWindow(hildon.StackableWindow):
38     features = (("Albums of the week",jamaendo.albums_of_the_week),
39                 ("Tracks of the week",jamaendo.tracks_of_the_week),
40                 ("New releases",jamaendo.new_releases)
41                 )
42
43     def __init__(self, feature):
44         hildon.StackableWindow.__init__(self)
45         self.set_title("Featured")
46
47         self.featurefn = _alist(self.features, feature)
48
49         # Results list
50         self.panarea = hildon.PannableArea()
51         self.result_store = gtk.ListStore(str, int)
52         #self.result_store.append(['red'])
53         self.result_view = gtk.TreeView(self.result_store)
54         col = gtk.TreeViewColumn('Name')
55         self.result_view.append_column(col)
56         cell = gtk.CellRendererText()
57         col.pack_start(cell, True)
58         col.add_attribute(cell, 'text', 0)
59         self.result_view.set_search_column(0)
60         col.set_sort_column_id(0)
61         self.result_view.connect('row-activated', self.row_activated)
62
63         self.panarea.add(self.result_view)
64
65         self.idmap = {}
66         for item in self.featurefn():
67             self.idmap[item.ID] = item
68             self.result_store.append([self.get_item_text(item), item.ID])
69
70         self.add(self.panarea)
71
72     def get_item_text(self, item):
73         if isinstance(item, jamaendo.Album):
74             return "%s - %s" % (item.artist_name, item.name)
75         elif isinstance(item, jamaendo.Track):
76             return "%s - %s" % (item.artist_name, item.name)
77         else:
78             return item.name
79
80     def make_button(self, text, subtext, callback):
81         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT,
82                                hildon.BUTTON_ARRANGEMENT_VERTICAL)
83         button.set_text(text, subtext)
84
85         if callback:
86             button.connect('clicked', callback)
87
88         #image = gtk.image_new_from_stock(gtk.STOCK_INFO, gtk.ICON_SIZE_BUTTON)
89         #button.set_image(image)
90         #button.set_image_position(gtk.POS_RIGHT)
91
92         return button
93
94     def row_activated(self, treeview, path, view_column):
95         treeiter = self.result_store.get_iter(path)
96         title, _id = self.result_store.get(treeiter, 0, 1)
97         item = self.idmap[_id]
98         #print _id, item
99         self.open_item(item)
100
101     def open_item(self, item):
102         if isinstance(item, jamaendo.Album):
103             wnd = ShowAlbum(item)
104             wnd.show_all()
105         elif isinstance(item, jamaendo.Artist):
106             wnd = ShowArtist(item)
107             wnd.show_all()
108         elif isinstance(item, jamaendo.Track):
109             wnd = open_playerwindow()
110             wnd.play_tracks([item])