Added bugtracker info
[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
35 class SearchWindow(hildon.StackableWindow):
36     def __init__(self):
37         hildon.StackableWindow.__init__(self)
38         self.set_title("Search")
39         self.idmap = {}
40
41         vbox = gtk.VBox(False, 0)
42
43
44         # Results list
45         self.panarea = hildon.PannableArea()
46         self.musiclist = MusicList()
47         self.musiclist.loading_message = "Nothing found yet"
48         self.musiclist.empty_message = "No matching results"
49         self.musiclist.connect('row-activated', self.row_activated)
50         self.panarea.add(self.musiclist)
51         vbox.pack_start(self.panarea, True, True, 0)
52
53
54         # Create selector for search mode
55         self.mode_selector = hildon.TouchSelector(text=True)
56
57         self.mode_selector.append_text("Artists")
58         self.mode_selector.append_text("Albums")
59         self.mode_selector.append_text("Tracks")
60         self.mode = hildon.PickerButton(gtk.HILDON_SIZE_FINGER_HEIGHT,
61                                         hildon.BUTTON_ARRANGEMENT_VERTICAL)
62         self.mode.set_title("Search for")
63         self.mode.set_selector(self.mode_selector)
64         self.mode_selector.connect("changed", self.mode_changed)
65         #vbox.pack_start(self.mode, False)
66         self.mode.set_active(1)
67
68
69         # Search box
70         hbox = gtk.HBox(False, 0)
71         self.entry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
72         self.entry.set_placeholder("Search")
73         self.entry.connect('activate', self.on_search)
74         btn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
75         btn.set_label(">>")
76         btn.connect('clicked', self.on_search)
77         hbox.pack_start(self.mode, False)
78         hbox.pack_start(self.entry, True, True, 0)
79         hbox.pack_start(btn, False)
80         vbox.pack_start(hbox, False)
81         self.add(vbox)
82
83         self.create_menu()
84
85     def create_menu(self):
86         def on_player(*args):
87             from playerwindow import open_playerwindow
88             open_playerwindow()
89         self.menu = hildon.AppMenu()
90         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
91         player.set_label("Open player")
92         player.connect("clicked", on_player)
93         self.menu.append(player)
94         self.menu.show_all()
95         self.set_app_menu(self.menu)
96
97     def mode_changed(self, selector, user_data):
98         pass
99         #current_selection = selector.get_current_text()
100
101     def on_search(self, w):
102         mode = self.mode.get_active()
103         txt = self.entry.get_text()
104         self.musiclist.set_loading(False)
105         self.musiclist.get_model().clear()
106
107         try:
108             if mode == 0:
109                 items = jamaendo.search_artists(query=txt)
110             elif mode == 1:
111                 items = jamaendo.search_albums(query=txt)
112             elif mode == 2:
113                 items = jamaendo.search_tracks(query=txt)
114
115             for item in items:
116                 self.idmap[item.ID] = item
117
118             self.musiclist.add_items(items)
119         except jamaendo.JamaendoAPIException:
120             # nothing found, force redraw
121             self.musiclist.queue_draw()
122
123     def row_activated(self, treeview, path, view_column):
124         _id = self.musiclist.get_item_id(path)
125         item = self.idmap[_id]
126         self.open_item(item)
127
128     def open_item(self, item):
129         if isinstance(item, jamaendo.Album):
130             wnd = ShowAlbum(item)
131             wnd.show_all()
132         elif isinstance(item, jamaendo.Artist):
133             wnd = ShowArtist(item)
134             wnd.show_all()
135         elif isinstance(item, jamaendo.Track):
136             wnd = open_playerwindow()
137             wnd.play_tracks([item])