40779f71d869d1f5ed5b4b735b05b53cdba1e828
[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 import hildon
26 import jamaendo
27 from playerwindow import open_playerwindow
28 from showartist import ShowArtist
29 from showalbum import ShowAlbum
30
31 class SearchWindow(hildon.StackableWindow):
32     def __init__(self):
33         hildon.StackableWindow.__init__(self)
34         self.set_title("Search")
35
36         vbox = gtk.VBox(False, 0)
37
38
39         # Results list
40         self.panarea = hildon.PannableArea()
41         self.result_store = gtk.ListStore(str, int)
42         #self.result_store.append(['red'])
43         self.result_view = gtk.TreeView(self.result_store)
44         col = gtk.TreeViewColumn('Name')
45         self.result_view.append_column(col)
46         cell = gtk.CellRendererText()
47         col.pack_start(cell, True)
48         col.add_attribute(cell, 'text', 0)
49         self.result_view.set_search_column(0)
50         col.set_sort_column_id(0)
51         self.result_view.connect('row-activated', self.row_activated)
52
53         self.panarea.add(self.result_view)
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
85
86         self.add(vbox)
87
88         self.idmap = {}
89
90     def mode_changed(self, selector, user_data):
91         current_selection = selector.get_current_text()
92         print current_selection
93
94     def on_search(self, w):
95         mode = self.mode.get_active()
96         txt = self.entry.get_text()
97         self.result_store.clear()
98         if mode == 0:
99             for artist in jamaendo.search_artists(query=txt):
100                 title = artist.name
101                 self.idmap[artist.ID] = artist
102                 self.result_store.append([title, artist.ID])
103         elif mode == 1:
104             for album in jamaendo.search_albums(query=txt):
105                 title = "%s - %s" % (album.artist_name, album.name)
106                 self.idmap[album.ID] = album
107                 self.result_store.append([title, album.ID])
108         elif mode == 2:
109             for track in jamaendo.search_tracks(query=txt):
110                 title = "%s - %s" % (track.artist_name, track.name)
111                 self.idmap[track.ID] = track
112                 self.result_store.append([title, track.ID])
113
114     def row_activated(self, treeview, path, view_column):
115         treeiter = self.result_store.get_iter(path)
116         title, _id = self.result_store.get(treeiter, 0, 1)
117         item = self.idmap[_id]
118         print _id, item
119         self.open_item(item)
120
121     def open_item(self, item):
122         if isinstance(item, jamaendo.Album):
123             wnd = ShowAlbum(item)
124             wnd.show_all()
125         elif isinstance(item, jamaendo.Artist):
126             wnd = ShowArtist(item)
127             wnd.show_all()
128         elif isinstance(item, jamaendo.Track):
129             wnd = open_playerwindow()
130             wnd.play_tracks([item])