Some licensing stuff
[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 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 settings import settings
31 import logging
32
33 log = logging.getLogger(__name__)
34
35 def _alist(l, match):
36     for key, value in l:
37         if key == match:
38             return value
39     return None
40
41 class FavoritesWindow(hildon.StackableWindow):
42     def __init__(self):
43         hildon.StackableWindow.__init__(self)
44         self.set_title("Favorites")
45
46         if settings.user:
47             # Results list
48             self.panarea = hildon.PannableArea()
49             self.result_store = gtk.ListStore(str, int)
50             #self.result_store.append(['red'])
51             self.result_view = gtk.TreeView(self.result_store)
52             col = gtk.TreeViewColumn('Name')
53             self.result_view.append_column(col)
54             cell = gtk.CellRendererText()
55             col.pack_start(cell, True)
56             col.add_attribute(cell, 'text', 0)
57             self.result_view.set_search_column(0)
58             col.set_sort_column_id(0)
59             self.result_view.connect('row-activated', self.row_activated)
60
61             self.panarea.add(self.result_view)
62
63             self.idmap = {}
64             try:
65                 for item in jamaendo.favorite_albums(settings.user):
66                     self.idmap[item.ID] = item
67                     self.result_store.append([self.get_item_text(item), item.ID])
68             except jamaendo.JamendoAPIException, e:
69                 msg = "Query failed, is the user name '%s' correct?" % (settings.user)
70                 banner = hildon.hildon_banner_show_information(self, '',
71                                                                msg)
72                 banner.set_timeout(3000)
73
74
75             def add_album(albumid):
76                 album = jamaendo.get_album(albumid)
77                 self.idmap[albumid] = album
78                 self.result_store.append([self.get_item_text(album), albumid])
79
80             for item in settings.favorites:
81                 try:
82                     if isinstance(item, tuple) and len(item) == 2:
83                         ftype, fid = item
84                         if ftype == 'album':
85                             add_album(fid)
86
87                 except jamaendo.JamendoAPIException, e:
88                     log.exception("jamaendo.get_album(%s)"%(fid))
89
90             self.add(self.panarea)
91
92         else:
93             vbox = gtk.VBox()
94             lbl = gtk.Label()
95             lbl.set_markup("""<span size="xx-large">jamendo.com
96 in the settings dialog
97 enter your username</span>
98 """)
99             lbl.set_single_line_mode(False)
100             vbox.pack_start(lbl, True, False)
101             self.add(vbox)
102
103     def get_item_text(self, item):
104         if isinstance(item, jamaendo.Album):
105             return "%s - %s" % (item.artist_name, item.name)
106         elif isinstance(item, jamaendo.Track):
107             return "%s - %s" % (item.artist_name, item.name)
108         else:
109             return item.name
110
111     def make_button(self, text, subtext, callback):
112         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT,
113                                hildon.BUTTON_ARRANGEMENT_VERTICAL)
114         button.set_text(text, subtext)
115
116         if callback:
117             button.connect('clicked', callback)
118
119         #image = gtk.image_new_from_stock(gtk.STOCK_INFO, gtk.ICON_SIZE_BUTTON)
120         #button.set_image(image)
121         #button.set_image_position(gtk.POS_RIGHT)
122
123         return button
124
125     def row_activated(self, treeview, path, view_column):
126         treeiter = self.result_store.get_iter(path)
127         title, _id = self.result_store.get(treeiter, 0, 1)
128         item = self.idmap[_id]
129         print _id, item
130         self.open_item(item)
131
132     def open_item(self, item):
133         if isinstance(item, jamaendo.Album):
134             wnd = ShowAlbum(item)
135             wnd.show_all()
136         elif isinstance(item, jamaendo.Artist):
137             wnd = ShowArtist(item)
138             wnd.show_all()
139         elif isinstance(item, jamaendo.Track):
140             wnd = open_playerwindow()
141             wnd.play_tracks([item])