Updated website
[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.empty_message = "Searching..."
114         self.musiclist.get_model().clear()
115
116         if self.fetcher:
117             self.fetcher.stop()
118             self.fetcher = None
119
120         itemgen = None
121         if mode == 0:
122             itemgen = lambda: jamaendo.search_artists(query=txt)
123         elif mode == 1:
124             itemgen = lambda: jamaendo.search_albums(query=txt)
125         elif mode == 2:
126             itemgen = lambda: jamaendo.search_tracks(query=txt)
127         else:
128             return
129
130         self.fetcher = Fetcher(itemgen, self,
131                                on_item = self.on_add_result,
132                                on_ok = self.on_add_complete,
133                                on_fail = self.on_add_complete)
134         self.fetcher.start()
135         '''
136         try:
137             if mode == 0:
138                 items = jamaendo.search_artists(query=txt)
139             elif mode == 1:
140                 items = jamaendo.search_albums(query=txt)
141             elif mode == 2:
142                 items = jamaendo.search_tracks(query=txt)
143
144             for item in items:
145                 self.idmap[item.ID] = item
146
147             self.musiclist.add_items(items)
148         except jamaendo.JamaendoAPIException:
149             # nothing found, force redraw
150             self.musiclist.queue_draw()
151         '''
152
153     def on_add_result(self, wnd, item):
154         if wnd is self:
155             self.musiclist.add_items([item])
156             self.idmap[item.ID] = item
157
158     def on_add_complete(self, wnd, error=None):
159         if wnd is self:
160             self.musiclist.empty_message = "No matching results"
161             self.musiclist.queue_draw()
162             self.fetcher.stop()
163             self.fetcher = None
164
165     def row_activated(self, treeview, path, view_column):
166         _id = self.musiclist.get_item_id(path)
167         item = self.idmap[_id]
168         self.open_item(item)
169
170     def open_item(self, item):
171         if isinstance(item, jamaendo.Album):
172             wnd = ShowAlbum(item)
173             wnd.show_all()
174         elif isinstance(item, jamaendo.Artist):
175             wnd = ShowArtist(item)
176             wnd.show_all()
177         elif isinstance(item, jamaendo.Track):
178             wnd = open_playerwindow()
179             wnd.play_tracks([item])