Allow entering a search string to search in some specific field.
[python-gnt.git] / example / xmms / xmms_search.py
blobb7a2f64561488b5f164376a551810e83fb4b4872
1 #!/usr/bin/env python
3 """
4 xmms-search : A usable and useful search system for XMMS2.
6 Copyright (C) 2007 Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>
8 This application is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 This application is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this application; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301
21 USA
22 """
24 import xmmsclient
25 import xmmsclient.glib as xg
26 import xmmsclient.collections as xc
27 import gobject
28 import gnt
29 import os
30 import sys
31 import itertools
33 import treerow
34 import songedit
35 import common
36 from xmms_list import *
38 __version__ = "0.0.1alpha"
39 __author__ = "Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>"
40 __copyright__ = "Copyright 2007, Sadrul Habib Chowdhury"
41 __license__ = "GPL"
43 reload(sys)
44 sys.setdefaultencoding("utf-8")
46 class XSearchResult(XList):
47 __gntbindings__ = {
48 'add-to-playlist': ('add_to_playlist', gnt.KEY_CTRL_A),
50 def add_to_playlist(self, null):
51 songs = self.get_rows()
52 for song in songs:
53 if not song.tagged: continue
54 song.toggle_tag()
55 self.update_row_flags(song)
56 id = song.get_data('song-id')
57 self.xmms.playlist_add_id(id)
58 return True
60 gobject.type_register(XSearchResult)
61 gnt.register_bindings(XSearchResult)
63 class XSearch(gnt.Box):
64 def __init__(self, xmms):
65 gnt.Box.__init__(self, False, True)
66 self.set_pad(0)
67 self.xmms = xmms
68 self.entry = gnt.Entry('')
69 self.list = XSearchResult(xmms)
70 self.list.hide_column(XList.POSITION)
71 self.list.hide_column(XList.ALBUM)
72 self.add_widget(self.entry)
73 self.add_widget(self.list)
74 self.entry.connect('activate', self.search)
76 def search(self, entry):
77 self.list.remove_all()
78 string = entry.get_text()
79 searchstring = ""
80 if ':' in string:
81 searchstring = string
82 else:
83 searchstring = "artist:*%s* OR title:*%s*" % (string, string)
84 coll = xmmsclient.xmmsapi.coll_parse(searchstring)
85 def got_coll_list(result):
86 self.list.remove_all()
87 songs = result.value()
88 pos = 1
89 for song in songs:
90 self.list.add_mlib_song(pos, song)
91 pos = pos + 1
92 self.list.draw()
93 ph = treerow.Row()
94 string = [""] * self.list.COLUMNS
95 string[self.list.TITLE] = "Searching..."
96 self.list.add_row_after(ph, string, None)
97 self.xmms.coll_query_ids(coll, cb = got_coll_list)
99 def standalone(self, title = "XMMS2 Search"):
100 self.set_toplevel(True)
101 self.set_title(title)
103 gobject.type_register(XSearch)
104 gnt.register_bindings(XSearch)
106 def setup_xmms():
107 xmms = xmmsclient.XMMS("pygnt-search")
108 try:
109 xmms.connect(os.getenv("XMMS_PATH"))
110 conn = xg.GLibConnector(xmms)
111 xs = XSearch(xmms)
112 xs.standalone()
113 xs.show()
114 except IOError, detail:
115 common.show_error("Connection failed: " + str(detail))
117 if __name__ == '__main__':
118 setup_xmms()
119 gnt.gnt_main()
120 gnt.gnt_quit()