Added 'helldon' to transparently port the thing to the desktop :P
[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 try:
26     import hildon
27 except:
28     import helldon as hildon
29 import jamaendo
30 from playerwindow import open_playerwindow
31 from showartist import ShowArtist
32 from showalbum import ShowAlbum
33 from albumlist import MusicList
34 from player import Playlist
35
36 def _alist(l, match):
37     for key, value in l:
38         if key == match:
39             return value
40     return None
41
42 class FeaturedWindow(hildon.StackableWindow):
43     features = (
44         ("New releases",jamaendo.new_releases),
45         ("Top albums today", lambda: jamaendo.top_albums(order='ratingday_desc')),
46         ("Top tracks today", lambda: jamaendo.top_tracks(order='ratingday_desc')),
47         ("Albums of the week",jamaendo.albums_of_the_week),
48         ("Tracks of the week",jamaendo.tracks_of_the_week),
49         ("Top 50 tags", lambda: jamaendo.top_tags(count=50)),
50         ("Top 50 artists", lambda: jamaendo.top_artists(count=50)),
51         ("Top 50 albums", lambda: jamaendo.top_albums(count=50)),
52         ("Top 50 tracks", lambda: jamaendo.top_tracks(count=50)),
53         )
54
55     def __init__(self, feature):
56         hildon.StackableWindow.__init__(self)
57         self.set_title(feature)
58
59         self.featurefn = _alist(self.features, feature)
60
61         # Results list
62         self.panarea = hildon.PannableArea()
63         self.musiclist = MusicList()
64         self.musiclist.connect('row-activated', self.row_activated)
65         self.panarea.add(self.musiclist)
66
67         self.idmap = {}
68         self.items = self.featurefn()
69         for item in self.items:
70             self.idmap[item.ID] = item
71         self.musiclist.add_items(self.items)
72
73         self.add(self.panarea)
74
75         self.create_menu()
76
77     def create_menu(self):
78         def on_player(*args):
79             from playerwindow import open_playerwindow
80             open_playerwindow()
81         self.menu = hildon.AppMenu()
82         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
83         player.set_label("Open player")
84         player.connect("clicked", on_player)
85         self.menu.append(player)
86         self.menu.show_all()
87         self.set_app_menu(self.menu)
88
89     def row_activated(self, treeview, path, view_column):
90         _id = self.musiclist.get_item_id(path)
91         item = self.idmap[_id]
92         self.open_item(item)
93
94     def open_item(self, item):
95         if isinstance(item, jamaendo.Album):
96             wnd = ShowAlbum(item)
97             wnd.show_all()
98         elif isinstance(item, jamaendo.Artist):
99             wnd = ShowArtist(item)
100             wnd.show_all()
101         elif isinstance(item, jamaendo.Track):
102             playlist = Playlist(self.items)
103             playlist.jump_to(item.ID)
104             wnd = open_playerwindow()
105             wnd.play_tracks(playlist)
106         elif isinstance(item, jamaendo.Tag):
107             wnd = open_playerwindow()
108             wnd.play_tracks(jamaendo.get_tag_tracks(item.ID))