Background loading, initially implemented for search window
[jamaendo] / jamaui / search.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 fetcher import Fetcher
35
36 class SearchWindow(hildon.StackableWindow):
37     def __init__(self):
38         hildon.StackableWindow.__init__(self)
39         self.set_title("Search")
40         self.idmap = {}
41
42         vbox = gtk.VBox(False, 0)
43
44         self.fetcher = None
45         self.connect('destroy', self.on_destroy)
46
47         # Results list
48         self.panarea = hildon.PannableArea()
49         self.musiclist = MusicList()
50         self.musiclist.loading_message = "Nothing found yet"
51         self.musiclist.empty_message = "No matching results"
52         self.musiclist.connect('row-activated', self.row_activated)
53         self.panarea.add(self.musiclist)
54         vbox.pack_start(self.panarea, True, True, 0)
55
56
57         # Create selector for search mode
58         self.mode_selector = hildon.TouchSelector(text=True)
59
60         self.mode_selector.append_text("Artists")
61         self.mode_selector.append_text("Albums")
62         self.mode_selector.append_text("Tracks")
63         self.mode = hildon.PickerButton(gtk.HILDON_SIZE_FINGER_HEIGHT,
64                                         hildon.BUTTON_ARRANGEMENT_VERTICAL)
65         self.mode.set_title("Search for")
66         self.mode.set_selector(self.mode_selector)
67         self.mode_selector.connect("changed", self.mode_changed)
68         #vbox.pack_start(self.mode, False)
69         self.mode.set_active(1)
70
71
72         # Search box
73         hbox = gtk.HBox(False, 0)
74         self.entry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
75         self.entry.set_placeholder("Search")
76         self.entry.connect('activate', self.on_search)
77         btn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
78         btn.set_label(">>")
79         btn.connect('clicked', self.on_search)
80         hbox.pack_start(self.mode, False)
81         hbox.pack_start(self.entry, True, True, 0)
82         hbox.pack_start(btn, False)
83         vbox.pack_start(hbox, False)
84         self.add(vbox)
85
86         self.create_menu()
87
88     def create_menu(self):
89         def on_player(*args):
90             from playerwindow import open_playerwindow
91             open_playerwindow()
92         self.menu = hildon.AppMenu()
93         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
94         player.set_label("Open player")
95         player.connect("clicked", on_player)
96         self.menu.append(player)
97         self.menu.show_all()
98         self.set_app_menu(self.menu)
99
100     def on_destroy(self, wnd):
101         if self.fetcher:
102             self.fetcher.stop()
103             self.fetcher = None
104
105     def mode_changed(self, selector, user_data):
106         pass
107         #current_selection = selector.get_current_text()
108
109     def on_search(self, w):
110         mode = self.mode.get_active()
111         txt = self.entry.get_text()
112         self.musiclist.set_loading(False)
113         self.musiclist.get_model().clear()
114
115         if self.fetcher:
116             self.fetcher.stop()
117             self.fetcher = None
118
119         itemgen = None
120         if mode == 0:
121             itemgen = lambda: jamaendo.search_artists(query=txt)
122         elif mode == 1:
123             itemgen = lambda: jamaendo.search_albums(query=txt)
124         elif mode == 2:
125             itemgen = lambda: jamaendo.search_tracks(query=txt)
126         else:
127             return
128
129         self.fetcher = Fetcher(itemgen, self,
130                                on_item = self.on_add_result,
131                                on_ok = self.on_add_ok,
132                                on_fail = self.on_add_fail)
133         self.fetcher.start()
134         '''
135         try:
136             if mode == 0:
137                 items = jamaendo.search_artists(query=txt)
138             elif mode == 1:
139                 items = jamaendo.search_albums(query=txt)
140             elif mode == 2:
141                 items = jamaendo.search_tracks(query=txt)
142
143             for item in items:
144                 self.idmap[item.ID] = item
145
146             self.musiclist.add_items(items)
147         except jamaendo.JamaendoAPIException:
148             # nothing found, force redraw
149             self.musiclist.queue_draw()
150         '''
151
152     def on_add_result(self, wnd, item):
153         if wnd is self:
154             self.musiclist.add_items([item])
155             self.idmap[item.ID] = item
156
157     def on_add_ok(self, wnd):
158         if wnd is self:
159             self.fetcher.stop()
160             self.fetcher = None
161
162     def on_add_fail(self, wnd, error):
163         if wnd is self:
164             self.musiclist.queue_draw()
165             self.fetcher.stop()
166             self.fetcher = None
167
168     def row_activated(self, treeview, path, view_column):
169         _id = self.musiclist.get_item_id(path)
170         item = self.idmap[_id]
171         self.open_item(item)
172
173     def open_item(self, item):
174         if isinstance(item, jamaendo.Album):
175             wnd = ShowAlbum(item)
176             wnd.show_all()
177         elif isinstance(item, jamaendo.Artist):
178             wnd = ShowArtist(item)
179             wnd.show_all()
180         elif isinstance(item, jamaendo.Track):
181             wnd = open_playerwindow()
182             wnd.play_tracks([item])