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