409204870401ea92703e9e1036aa232d8d0a8f2e
[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 from albumlist import MusicList
31
32 class SearchWindow(hildon.StackableWindow):
33     def __init__(self):
34         hildon.StackableWindow.__init__(self)
35         self.set_title("Search")
36         self.idmap = {}
37
38         vbox = gtk.VBox(False, 0)
39
40
41         # Results list
42         self.panarea = hildon.PannableArea()
43         self.musiclist = MusicList()
44         self.result_store = gtk.ListStore(str, int)
45         #self.result_store.append(['red'])
46         self.result_view = gtk.TreeView(self.result_store)
47         col = gtk.TreeViewColumn('Name')
48         self.result_view.append_column(col)
49         cell = gtk.CellRendererText()
50         col.pack_start(cell, True)
51         col.add_attribute(cell, 'text', 0)
52         self.result_view.set_search_column(0)
53         col.set_sort_column_id(0)
54         self.result_view.connect('row-activated', self.row_activated)
55
56         self.panarea.add(self.result_view)
57         vbox.pack_start(self.panarea, True, True, 0)
58
59
60         # Create selector for search mode
61         self.mode_selector = hildon.TouchSelector(text=True)
62
63         self.mode_selector.append_text("Artists")
64         self.mode_selector.append_text("Albums")
65         self.mode_selector.append_text("Tracks")
66         self.mode = hildon.PickerButton(gtk.HILDON_SIZE_FINGER_HEIGHT,
67                                         hildon.BUTTON_ARRANGEMENT_VERTICAL)
68         self.mode.set_title("Search for")
69         self.mode.set_selector(self.mode_selector)
70         self.mode_selector.connect("changed", self.mode_changed)
71         #vbox.pack_start(self.mode, False)
72         self.mode.set_active(1)
73
74
75         # Search box
76         hbox = gtk.HBox(False, 0)
77         self.entry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
78         self.entry.set_placeholder("Search")
79         self.entry.connect('activate', self.on_search)
80         btn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
81         btn.set_label(">>")
82         btn.connect('clicked', self.on_search)
83         hbox.pack_start(self.mode, False)
84         hbox.pack_start(self.entry, True, True, 0)
85         hbox.pack_start(btn, False)
86         vbox.pack_start(hbox, False)
87         self.add(vbox)
88
89         self.create_menu()
90
91     def create_menu(self):
92         def on_player(*args):
93             from playerwindow import open_playerwindow
94             open_playerwindow()
95         self.menu = hildon.AppMenu()
96         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
97         player.set_label("Open player")
98         player.connect("clicked", on_player)
99         self.menu.append(player)
100         self.menu.show_all()
101         self.set_app_menu(self.menu)
102
103     def mode_changed(self, selector, user_data):
104         pass
105         #current_selection = selector.get_current_text()
106
107     def on_search(self, w):
108         mode = self.mode.get_active()
109         txt = self.entry.get_text()
110         self.result_store.clear()
111         if mode == 0:
112             for artist in jamaendo.search_artists(query=txt):
113                 title = artist.name
114                 self.idmap[artist.ID] = artist
115                 self.result_store.append([title, artist.ID])
116         elif mode == 1:
117             for album in jamaendo.search_albums(query=txt):
118                 title = "%s - %s" % (album.artist_name, album.name)
119                 self.idmap[album.ID] = album
120                 self.result_store.append([title, album.ID])
121         elif mode == 2:
122             for track in jamaendo.search_tracks(query=txt):
123                 title = "%s - %s" % (track.artist_name, track.name)
124                 self.idmap[track.ID] = track
125                 self.result_store.append([title, track.ID])
126
127     def row_activated(self, treeview, path, view_column):
128         treeiter = self.result_store.get_iter(path)
129         title, _id = self.result_store.get(treeiter, 0, 1)
130         item = self.idmap[_id]
131         #print _id, item
132         self.open_item(item)
133
134     def open_item(self, item):
135         if isinstance(item, jamaendo.Album):
136             wnd = ShowAlbum(item)
137             wnd.show_all()
138         elif isinstance(item, jamaendo.Artist):
139             wnd = ShowArtist(item)
140             wnd.show_all()
141         elif isinstance(item, jamaendo.Track):
142             wnd = open_playerwindow()
143             wnd.play_tracks([item])