[gedit-plugins] [synctex] Make async DBus calls.



commit f88a76ffbc99882c856a7ca5b3c1d1b6ec0f0ddb
Author: Jose Aliste <jaliste src gnome org>
Date:   Thu Aug 12 20:22:24 2010 -0400

    [synctex] Make async DBus calls.

 plugins/synctex/synctex/evince_dbus.py |  107 ++++++++++++++++++++++++--------
 plugins/synctex/synctex/synctex.py     |    5 +-
 2 files changed, 84 insertions(+), 28 deletions(-)
---
diff --git a/plugins/synctex/synctex/evince_dbus.py b/plugins/synctex/synctex/evince_dbus.py
old mode 100644
new mode 100755
index 92e1059..c62ec0e
--- a/plugins/synctex/synctex/evince_dbus.py
+++ b/plugins/synctex/synctex/evince_dbus.py
@@ -20,64 +20,115 @@
 # Street, Fifth Floor, Boston, MA  02110-1301, USA
 
 import dbus, subprocess, time
-from traceback import print_exc
 
 RUNNING, CLOSED = range(2)
 
+EV_DAEMON_PATH = "/org/gnome/evince/Daemon"
+EV_DAEMON_NAME = "org.gnome.evince.Daemon"
+EV_DAEMON_IFACE = "org.gnome.evince.Daemon"
+
+EVINCE_PATH = "/org/gnome/evince/Evince"
+EVINCE_IFACE = "org.gnome.evince.Application"
+
+EV_WINDOW_IFACE = "org.gnome.evince.Window"
+
+
+
 class EvinceWindowProxy:
 	"""A DBUS proxy for an Evince Window."""
 	daemon = None
 	bus = None
-	def __init__(self, uri, spawn = False):
+
+	def __init__(self, uri, spawn = False, logger = None):
+		self._log = logger
 		self.uri = uri
 		self.spawn = spawn
 		self.status = CLOSED
 		self.source_handler = None
+		self.dbus_name = ''
+		self._handler = None
 		try:
 			if EvinceWindowProxy.bus is None:
 				EvinceWindowProxy.bus = dbus.SessionBus()
+
 			if EvinceWindowProxy.daemon is None:
-				EvinceWindowProxy.daemon = EvinceWindowProxy.bus.get_object('org.gnome.evince.Daemon',
-							   '/org/gnome/evince/Daemon')
+				EvinceWindowProxy.daemon = EvinceWindowProxy.bus.get_object(EV_DAEMON_NAME,
+											    EV_DAEMON_PATH,
+											    follow_name_owner_changes=True)
 			self._get_dbus_name(False)
+
 		except dbus.DBusException:
-			print_exc()
+			if self._log:
+				self._log.debug("Could not connect to the Evince Daemon")
 
 	def _get_dbus_name(self, spawn):
-		try:
-			self.dbus_name = self.daemon.FindDocument(self.uri,spawn, dbus_interface = "org.gnome.evince.Daemon")
-			if self.dbus_name != '':
-				self.status = RUNNING
-				self.window = EvinceWindowProxy.bus.get_object(self.dbus_name, '/org/gnome/evince/Window/0')
-				self.window.connect_to_signal("Closed", self.on_window_close, dbus_interface="org.gnome.evince.Window")
-				self.window.connect_to_signal("SyncSource", self.on_sync_source, dbus_interface="org.gnome.evince.Window")
-		except dbus.DBusException:
-			print_exc()
+		EvinceWindowProxy.daemon.FindDocument(self.uri,spawn,
+					 reply_handler=self.handle_find_document_reply,
+					 error_handler=self.handle_find_document_error,
+					 dbus_interface = EV_DAEMON_IFACE)
+	
+	def handle_find_document_error(self, error):
+		if self._log:
+			self._log.debug("FindDocument DBus call has failed")
+	
+	def handle_find_document_reply(self, evince_name):
+		if self._handler is not None:
+			handler = self._handler
+		else:
+			handler = self.handle_get_window_list_reply
+		if evince_name != '':
+			self.dbus_name = evince_name
+			self.status = RUNNING
+			self.evince = EvinceWindowProxy.bus.get_object(self.dbus_name, EVINCE_PATH)
+			self.evince.GetWindowList(dbus_interface = EVINCE_IFACE,
+						  reply_handler = handler,
+						  error_handler = self.handle_get_window_list_error)
+
+	def handle_get_window_list_error (self, e):
+		if self._log:
+			self._log.debug("GetWindowList DBus call has failed")
+
+	def handle_get_window_list_reply (self, window_list):
+		if len(window_list) > 0:
+			window_obj = EvinceWindowProxy.bus.get_object(self.dbus_name, window_list[0])
+			self.window = dbus.Interface(window_obj,EV_WINDOW_IFACE)
+			self.window.connect_to_signal("Closed", self.on_window_close)
+			self.window.connect_to_signal("SyncSource", self.on_sync_source)
+		else:
+			#That should never happen. 
+			if self._log:
+				self._log.debug("GetWindowList returned empty list")
+
+
 	def set_source_handler (self, source_handler):
 		self.source_handler = source_handler
 
 	def on_window_close(self):
-		self.status = CLOSED
 		self.window = None
+		self.status = CLOSED
 
 	def on_sync_source(self, input_file, source_link):
 		if self.source_handler is not None:
 			self.source_handler(input_file, source_link)
 
-	def name_owner_changed_cb(self, service_name, old_owner, new_owner):
-		if service_name == self.dbus_name and new_owner == '': 
-			self.status = CLOSED
-
 	def SyncView(self, input_file, data):
 		if self.status == CLOSED:
 			if self.spawn:
+				self._tmp_syncview = [input_file, data];
+				self._handler = self._syncview_handler
 				self._get_dbus_name(True)
-		# if self.status is still closed, it means there is a
-		# problem running evince.
+		else:
+			self.window.SyncView(input_file, data, dbus_interface = "org.gnome.evince.Window")
+
+	def _syncview_handler(self, window_list):
+		self.handle_get_window_list_reply(window_list)
+
 		if self.status == CLOSED: 
 			return False
-		self.window.SyncView(input_file, data, dbus_interface="org.gnome.evince.Window")
-		return False
+		self.window.SyncView(self._tmp_syncview[0],self._tmp_syncview[1], dbus_interface="org.gnome.evince.Window")
+		del self._tmp_syncview
+		self._handler = None
+		return True
 
 ## This file can be used as a script to support forward search and backward search in vim.
 ## It should be easy to adapt to other editors. 
@@ -107,9 +158,11 @@ The usage is evince_dbus output_file line_number input_file from the directory o
 		print_usage()
 
 	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
-	bus = dbus.SessionBus()
-
-	a = EvinceWindowProxy(bus, 'file://' + path_output, True )
-	glib.timeout_add(1000, a.SyncView, path_input, (line_number,1))
+	a = EvinceWindowProxy('file://' + path_output, True )
+	
+	def sync_view(ev_window, path_input, line_number):
+		ev_window.SyncView (path_input, (line_number, 1))
+	
+	glib.timeout_add(400, sync_view, a, path_input, line_number)
 	loop = gobject.MainLoop()
 	loop.run() 
diff --git a/plugins/synctex/synctex/synctex.py b/plugins/synctex/synctex/synctex.py
index f0e56f6..8e53816 100644
--- a/plugins/synctex/synctex/synctex.py
+++ b/plugins/synctex/synctex/synctex.py
@@ -23,6 +23,7 @@ import gtk, gedit, gio , gtk.gdk as gdk
 from gettext import gettext as _
 from evince_dbus import EvinceWindowProxy
 import dbus.mainloop.glib
+import logging
 
 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
 
@@ -42,6 +43,8 @@ ui_str = """<ui>
 VIEW_DATA_KEY = "SynctexPluginViewData"
 WINDOW_DATA_KEY = "SynctexPluginWindowData"
 
+_logger = logging.getLogger("SynctexPlugin")
+
 def apply_style (style, tag):
     import pango
     def apply_style_prop(tag, style, prop):
@@ -201,7 +204,7 @@ class SynctexViewHelper:
             style = self._doc.get_style_scheme().get_style('search-match')
             apply_style(style, self._highlight_tag)
             self._window.get_data(WINDOW_DATA_KEY)._action_group.set_sensitive(True)
-            self.window_proxy = EvinceWindowProxy (self.out_gfile.get_uri(), True)
+            self.window_proxy = EvinceWindowProxy (self.out_gfile.get_uri(), True, _logger)
             self.window_proxy.set_source_handler (self.source_view_handler)
 
         elif not self.active and self.window_proxy is not None:



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