[Deskbar] Tracker handler updated (better hit descriptions)



Thanks to my Barracuda adventures deskbar will now also have more spiffy Tracker results!

Hits are now displayed like:

<b>MyFile.txt</b>
in folder <i>Documents</i>

if the file path is /home/myname/Documents/MyFile.txt, or just

<b>MyOtherFile.txt</b>
in home folder

if the file is directly in ~/

As usual, drop it in ~/.gnome2/deskbar-applet/handlers, and install tracker-0.0.4 (or 0.0.3 if you are old fashion) and indulge in live search sweetness!

Cheers,
Mikkel
import gnome

import deskbar
from deskbar.Handler import SignallingHandler
from deskbar.Match import Match
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 = {
	"TrackerFileSearchHandler" : {
		"name": "Search for files using Tracker",
		"description": _("Search all of your documents (using Tracker), as you type"),
		"requirements" : _check_requirements,
	}
}

class TrackerFileMatch (Match):
	def __init__(self, handler, **args):
		Match.__init__ (self, handler, **args)
		
		try:
			self._icon = deskbar.Utils.load_icon_for_file(self.name)
		except Exception:
			pass
		
		self.tracker_id = self.name
		self.name = self.calc_display_string ()
				
	def get_verb(self):
		return "%(name)s"
		
	def action(self, text=None):
		print "Opening Tracker hit:", self.name
		gnome.url_show ("file://" + self.tracker_id)
		#args = ["gnome-open", str(self.name)] # FIXME for some odd reason we need the str() cast
		#gobject.spawn_async(args, flags=gobject.SPAWN_SEARCH_PATH)
		
	def get_category (self):
		return "files"

	def calc_display_string (self):
		dirname = self.name
		filename = ""
		
		# Don't include the /home/myuser part of the path
		if dirname.startswith (HOME):
			dirname = dirname [len(HOME):]
			filename = dirname
		
		last_dir_idx = filename.rfind("/")
		
		if  last_dir_idx == -1:
			# There are no "/"s left in the path string,
			# this must be a file directly in the home folder
			return "<b>" + filename +"</b>\nin home folder"
			
		filename = filename[last_dir_idx+1:]
		dirname = dirname[:last_dir_idx]
		
		return "<b>%s</b>\nin folder <i>%s</i>" % (filename,dirname)


class TrackerFileSearchHandler(SignallingHandler):
	def __init__(self):
		SignallingHandler.__init__(self, "stock_file")
		
		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

		# Set up dbus conenction to trackerd
		self.bus = dbus.SessionBus()
		self.proxy_obj = self.bus.get_object('org.freedesktop.Tracker', '/org/freedesktop/tracker')
		self.tracker_iface = dbus.Interface(self.proxy_obj, 'org.freedesktop.Tracker')
		
		self.set_delay (500)
		
	def query (self, qstring, max):		
		self.tracker_iface.SearchMetadataText ("Files", qstring, 100, False, reply_handler=lambda hits : self.__recieve_hits(qstring, hits, max), error_handler=self.__recieve_error)
		print "Tracker query:", qstring

	def __recieve_hits (self, qstring, hits, max):
		matches = []
		for filename in hits:
			matches.append( TrackerFileMatch(self, name=filename) )
			#if len (matches) > max:
			#	break
		self.emit_query_ready(qstring, matches)
		print "Tracker response for %s, - %s hits returned, %s shown" % (qstring, len(hits), len(matches))
		
	def __recieve_error (self, error):
		print "*** Tracker dbus error:", error
		
	def stop_query(self):
		pass





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]