File: pinot-live.py

package info (click to toggle)
pinot 0.85-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 5,524 kB
  • ctags: 3,868
  • sloc: cpp: 33,107; sh: 8,801; ansic: 3,049; makefile: 557; xml: 366; python: 250
file content (150 lines) | stat: -rw-r--r-- 4,597 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#
# Copyright 2005-2007 Fabrice Colin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import gnome
import deskbar
import deskbar.Handler, deskbar
import gobject
from gettext import gettext as _

from os.path import expanduser
HOME = expanduser ("~/")

def _check_requirements ():
	try:
		import dbus
		try :
			if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
				import dbus.glib
		except:
			return (deskbar.Handler.HANDLER_IS_NOT_APPLICABLE, "Python dbus.glib bindings not found.", None)
		return (deskbar.Handler.HANDLER_IS_HAPPY, None, None)
	except:
		return (deskbar.Handler.HANDLER_IS_NOT_APPLICABLE, "Python dbus bindings not found.", None)
	
HANDLERS = {
	"PinotFileSearchHandler" : {
		"name": "Pinot",
		"description": _("Search documents indexed by Pinot"),
		"requirements" : _check_requirements,
	}
}

class PinotFileMatch (deskbar.Match.Match):
	def __init__(self, handler, name, url, mime_type, main_language, **args):
		deskbar.Match.Match.__init__ (self, handler, **args)
		# The mailbox scheme is specific to Pinot
		self.url = url
		url_scheme = self.url[:url.index("://")]
		if url_scheme == "mailbox":
			print "Mailbox hit: ", self.url
			self.url = "file://" + url[len("mailbox://"):url.index('?')]
			url_scheme = "file"

		# Replace HOME with ~
		file_path = self.url[self.url.index("://") + 3:]
		if url_scheme == "file":
			file_path = file_path.replace(HOME, "~/")
			print "File hit: ", file_path

		self.name = name + " (" + file_path + ")"
		self.mime_type = mime_type
		self.main_language = main_language
		self._icon = deskbar.Utils.load_icon_for_file(self.url)

	def get_verb(self):
		return "%(name)s"

	def action(self, text=None):
		print "Opening Pinot hit: ", self.url
		# Prefer deskbar's url_show is available
		try:
			deskbar.Utils.url_show (self.url)
		except AttributeError:
			gnome.url_show (self.url)

	def get_category (self):
		return "files"

class PinotFileSearchHandler(deskbar.Handler.SignallingHandler):
	def __init__(self):
		deskbar.Handler.SignallingHandler.__init__(self, ("system-search", "pinot"))

		self.query_string = ""
		self.results = []
		self.results_count = 10

		import dbus
		# We have to do this or it won't work
		if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
			import dbus.glib
		try:
			# Set up the dbus connection
			self.bus = dbus.SessionBus()
			self.proxy_obj = self.bus.get_object('de.berlios.Pinot', '/de/berlios/Pinot')
			self.pinot_iface = dbus.Interface(self.proxy_obj, 'de.berlios.Pinot')
		except Exception, msg:
			print 'Caught D-Bus exception: ', msg
		self.set_delay (500)

	def query (self, qstring, max):
		print "SimpleQuery: ", qstring
		doc_ids = []
		try :
			import dbus
			doc_ids = self.pinot_iface.SimpleQuery (qstring, dbus.UInt32(max))
		except Exception, msg:
			print 'Caught D-Bus exception: ', msg
		# Save the query's details
		self.query_string = qstring
		self.results = []
		self.results_count = len(doc_ids)
		try :
			# Get the details of each hit
			for doc_id in doc_ids:
				print "Hit on document ", doc_id
				self.pinot_iface.GetDocumentInfo (dbus.UInt32(doc_id),
					reply_handler=self.__receive_hits, error_handler=self.__receive_error)
		except Exception, msg:
			print 'Caught exception: ', msg

	def __receive_hits (self, fields):
		name = "Unknown"
		url = ""
		mime_type = "application/octet-stream"
		main_language = ""

		# Get the fields we need to build a match
		for field in fields:
			if field[0] == "caption":
				name = field[1]
			elif field[0] == "url":
				url = field[1]
			elif field[0] == "type":
				mime_type = field[1]
			elif field[0] == "language":
				main_language = field[1]

		self.results.append( PinotFileMatch(self, name, url, mime_type, main_language) )
		print "Got hit ", len(self.results)
		if len(self.results) == self.results_count:
			self.emit_query_ready(self.query_string, self.results)

	def __receive_error (self, error):
		print 'D-Bus error: ', error