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