# Path: plugins/synctex/synctex/evince_dbus.py
# class EvinceWindowProxy:
#     """A DBUS proxy for an Evince Window."""
#     daemon = None
#     bus = None
# 
#     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(EV_DAEMON_NAME,
#                                                 EV_DAEMON_PATH,
#                                                 follow_name_owner_changes=True)
#             EvinceWindowProxy.bus.add_signal_receiver(self._on_doc_loaded, signal_name="DocumentLoaded", 
#                                                       dbus_interface = EV_WINDOW_IFACE, 
#                                                       sender_keyword='sender')
#             self._get_dbus_name(False)
# 
#         except dbus.DBusException:
#             if self._log:
#                 self._log.debug("Could not connect to the Evince Daemon")
# 
#     def _on_doc_loaded(self, uri, **keyargs):
#         if uri == self.uri and self._handler is None:
#             self.handle_find_document_reply(keyargs['sender'])
#         
#     def _get_dbus_name(self, spawn):
#         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.window = None
#         self.status = CLOSED
# 
#     def on_sync_source(self, input_file, source_link, timestamp):
#         if self.source_handler is not None:
#             self.source_handler(input_file, source_link, timestamp)
# 
#     def SyncView(self, input_file, data, time):
#         if self.status == CLOSED:
#             if self.spawn:
#                 self._tmp_syncview = [input_file, data, time];
#                 self._handler = self._syncview_handler
#                 self._get_dbus_name(True)
#         else:
#             self.window.SyncView(input_file, data, time,  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(self._tmp_syncview[0],self._tmp_syncview[1], self._tmp_syncview[2], dbus_interface="org.gnome.evince.Window")
#         del self._tmp_syncview
#         self._handler = None
#         return True


from gi.repository import GObject, Pango, Gtk, Gedit, Peas, PeasGtk, Gio, Gdk
from .evince_dbus import EvinceWindowProxy
import dbus.mainloop.glib
import logging
import os
import re
    import gettext
        del view.synctex_view_helper

    def get_helper(self, view):
        if not hasattr(view, 'synctex_view_helper'):
            return None
        return view.synctex_view_helper

    def forward_search_cb(self, action, parameter, user_data=None):
        self.get_helper(self.window.get_active_view()).sync_view(Gtk.get_current_event_time())

    def source_view_handler(self, out_gfile, uri_input, source_link, time):

        if uri_input not in self.view_dict:
            window = self._proxy_dict[out_gfile.get_uri()][2]

            tab = window.create_tab_from_location(Gio.file_new_for_uri(uri_input),
                                                  None, source_link[0] - 1, 0, False, True)

            helper = self.get_helper(tab.get_view())
            helper._goto_handler = tab.get_document().connect_object("loaded", 
                                                SynctexViewHelper.goto_line_after_load,
                                                helper, source_link[0] - 1, time)
        else:
            self.view_dict[uri_input].goto_line(source_link[0] - 1, time)

    def ref_evince_proxy(self, gfile, window):
        uri = gfile.get_uri()
        proxy = None

        if uri not in self._proxy_dict:

# Path: plugins/synctex/synctex/evince_dbus.py
# class EvinceWindowProxy:
#     """A DBUS proxy for an Evince Window."""
#     daemon = None
#     bus = None
# 
#     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(EV_DAEMON_NAME,
#                                                 EV_DAEMON_PATH,
#                                                 follow_name_owner_changes=True)
#             EvinceWindowProxy.bus.add_signal_receiver(self._on_doc_loaded, signal_name="DocumentLoaded", 
#                                                       dbus_interface = EV_WINDOW_IFACE, 
#                                                       sender_keyword='sender')
#             self._get_dbus_name(False)
# 
#         except dbus.DBusException:
#             if self._log:
#                 self._log.debug("Could not connect to the Evince Daemon")
# 
#     def _on_doc_loaded(self, uri, **keyargs):
#         if uri == self.uri and self._handler is None:
#             self.handle_find_document_reply(keyargs['sender'])
#         
#     def _get_dbus_name(self, spawn):
#         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.window = None
#         self.status = CLOSED
# 
#     def on_sync_source(self, input_file, source_link, timestamp):
#         if self.source_handler is not None:
#             self.source_handler(input_file, source_link, timestamp)
# 
#     def SyncView(self, input_file, data, time):
#         if self.status == CLOSED:
#             if self.spawn:
#                 self._tmp_syncview = [input_file, data, time];
#                 self._handler = self._syncview_handler
#                 self._get_dbus_name(True)
#         else:
#             self.window.SyncView(input_file, data, time,  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(self._tmp_syncview[0],self._tmp_syncview[1], self._tmp_syncview[2], dbus_interface="org.gnome.evince.Window")
#         del self._tmp_syncview
#         self._handler = None
#         return True


# Path: plugins/synctex/synctex/synctex.py
from gi.repository import GObject, Pango, Gtk, Gedit, Peas, PeasGtk, Gio, Gdk
from .evince_dbus import EvinceWindowProxy
import dbus.mainloop.glib
import logging
import os
import re
    import gettext

        del view.synctex_view_helper

    def get_helper(self, view):
        if not hasattr(view, 'synctex_view_helper'):
            return None
        return view.synctex_view_helper

    def forward_search_cb(self, action, parameter, user_data=None):
        self.get_helper(self.window.get_active_view()).sync_view(Gtk.get_current_event_time())

    def source_view_handler(self, out_gfile, uri_input, source_link, time):

        if uri_input not in self.view_dict:
            window = self._proxy_dict[out_gfile.get_uri()][2]

            tab = window.create_tab_from_location(Gio.file_new_for_uri(uri_input),
                                                  None, source_link[0] - 1, 0, False, True)

            helper = self.get_helper(tab.get_view())
            helper._goto_handler = tab.get_document().connect_object("loaded", 
                                                SynctexViewHelper.goto_line_after_load,
                                                helper, source_link[0] - 1, time)
        else:
            self.view_dict[uri_input].goto_line(source_link[0] - 1, time)

    def ref_evince_proxy(self, gfile, window):
        uri = gfile.get_uri()
        proxy = None

        if uri not in self._proxy_dict:

            proxy = EvinceWindowProxy (uri, True, _logger)