ef581439f827deebde36f97988a2a3caa901140b
[jamaendo] / jamaui / favorites.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 try:
25     import hildon
26 except:
27     import helldon as hildon
28 import jamaendo
29 from playerwindow import open_playerwindow
30 from showartist import ShowArtist
31 from showalbum import ShowAlbum
32 from settings import settings
33 import logging
34 from fetcher import Fetcher
35 import itertools
36
37 from albumlist import MusicList
38
39 log = logging.getLogger(__name__)
40
41 def _alist(l, match):
42     for key, value in l:
43         if key == match:
44             return value
45     return None
46
47 class FavoritesWindow(hildon.StackableWindow):
48     def __init__(self):
49         hildon.StackableWindow.__init__(self)
50         self.set_title("Favorites")
51         self.connect('destroy', self.on_destroy)
52         self.fetcher = None
53         self.idmap = {}
54
55         self.panarea = hildon.PannableArea()
56         self.favorites = MusicList()
57         self.favorites.connect('row-activated', self.row_activated)
58         self.panarea.add(self.favorites)
59         self.add(self.panarea)
60
61         if not settings.user:
62             self.favorites.loading_message = """give your username
63 to the settings dialog
64 favorites appear
65 """
66         else:
67             self.favorites.loading_message = """Loading favorites"""
68
69         self.start_favorites_fetcher()
70
71     def on_destroy(self, wnd):
72         if self.fetcher:
73             self.fetcher.stop()
74             self.fetcher = None
75
76     def start_favorites_fetcher(self):
77         if self.fetcher:
78             self.fetcher.stop()
79             self.fetcher = None
80
81         def gen():
82             generated = []
83             for item in jamaendo.favorite_albums(settings.user):
84                 generated.append(item.ID)
85                 yield item
86             fav = [f[1] for f in settings.favorites \
87                        if isinstance(f, tuple) and \
88                        len(f) == 2 and \
89                        f[0] == 'album' and \
90                        f[1] not in generated]
91             for item in jamaendo.get_albums(fav):
92                 yield item
93
94         self.fetcher = Fetcher(gen,
95                                self,
96                                on_item = self.on_favorites_result,
97                                on_ok = self.on_favorites_complete,
98                                on_fail = self.on_favorites_complete)
99         self.fetcher.start()
100
101     def on_favorites_result(self, wnd, item):
102         if wnd is self:
103             if item.ID not in self.idmap:
104                 self.idmap[item.ID] = item
105                 self.favorites.add_items([item])
106
107     def on_favorites_complete(self, wnd, error=None):
108         if wnd is self:
109             self.fetcher.stop()
110             self.fetcher = None
111
112     def get_item_text(self, item):
113         if isinstance(item, jamaendo.Album):
114             return "%s - %s" % (item.artist_name, item.name)
115         elif isinstance(item, jamaendo.Track):
116             return "%s - %s" % (item.artist_name, item.name)
117         else:
118             return item.name
119
120     def row_activated(self, treeview, path, view_column):
121         _id = self.favorites.get_item_id(path)
122         item = self.idmap.get(_id)
123         if item:
124             self.open_item(item)
125
126     def open_item(self, item):
127         if isinstance(item, jamaendo.Album):
128             wnd = ShowAlbum(item)
129             wnd.show_all()
130         elif isinstance(item, jamaendo.Artist):
131             wnd = ShowArtist(item)
132             wnd.show_all()
133         elif isinstance(item, jamaendo.Track):
134             wnd = open_playerwindow()
135             wnd.play_tracks([item])