Some minor fixes and tweaks
[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 import logging
36
37 log = logging.getLogger(__name__)
38
39 def _alist(l, match):
40     for key, value in l:
41         if key == match:
42             return value
43     return None
44
45 class FeaturedWindow(hildon.StackableWindow):
46     features = (
47         ("New releases",jamaendo.new_releases),
48         ("Top albums today", lambda: jamaendo.top_albums(order='ratingday_desc')),
49         ("Top tracks today", lambda: jamaendo.top_tracks(order='ratingday_desc')),
50         ("Albums of the week",jamaendo.albums_of_the_week),
51         ("Tracks of the week",jamaendo.tracks_of_the_week),
52         ("Top 50 tags", lambda: jamaendo.top_tags(count=50)),
53         ("Top 50 artists", lambda: jamaendo.top_artists(count=50)),
54         ("Top 50 albums", lambda: jamaendo.top_albums(count=50)),
55         ("Top 50 tracks", lambda: jamaendo.top_tracks(count=50)),
56         )
57
58     def __init__(self, feature):
59         hildon.StackableWindow.__init__(self)
60         self.set_title(feature)
61
62         self.featurefn = _alist(self.features, feature)
63
64         # Results list
65         self.panarea = hildon.PannableArea()
66         self.musiclist = MusicList()
67         self.musiclist.connect('row-activated', self.row_activated)
68         self.panarea.add(self.musiclist)
69
70         self.idmap = {}
71         try:
72             self.items = self.featurefn()
73             for item in self.items:
74                 self.idmap[item.ID] = item
75             self.musiclist.add_items(self.items)
76         except jamaendo.JamendoAPIException:
77             log.exception('failed to get %s' % (feature))
78             self.items = []
79
80         self.add(self.panarea)
81
82         self.create_menu()
83
84     def create_menu(self):
85         def on_player(*args):
86             from playerwindow import open_playerwindow
87             open_playerwindow()
88         self.menu = hildon.AppMenu()
89         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
90         player.set_label("Open player")
91         player.connect("clicked", on_player)
92         self.menu.append(player)
93         self.menu.show_all()
94         self.set_app_menu(self.menu)
95
96     def row_activated(self, treeview, path, view_column):
97         _id = self.musiclist.get_item_id(path)
98         item = self.idmap[_id]
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             playlist = Playlist(self.items)
110             playlist.jump_to(item.ID)
111             wnd = open_playerwindow()
112             wnd.play_tracks(playlist)
113         elif isinstance(item, jamaendo.Tag):
114             try:
115                 wnd = open_playerwindow()
116                 wnd.play_tracks(jamaendo.get_tag_tracks(item.ID))
117             except jamaendo.JamendoAPIException:
118                 log.exception('Failed to get tracks for %s' % (item.ID))
119