def update_active(self):
        # Activate the plugin only if the doc is a LaTeX file. 
        self.active = (self.mime_type in self.mime_types)

        if self.active and self.window_proxy is None:
            if self.uri.endswith(".tex"):
                uri_output = self.uri[:-3] + "pdf"
                self.window_proxy = EvinceWindowProxy (uri_output, True)
                self.window_proxy.set_source_handler (self.source_view_handler)
                print "activando"
        elif not self.active and self.window_proxy is not None:
            # destroy the evince window proxy.
            print "desactivando"
            pass
class SynctexViewHelper:

    mime_types = ['text/x-tex'];

    def __init__(self, view, window, tab):
        self._view = view
        self._window = window
        self._tab = tab
        self._doc = view.get_buffer()
        self.window_proxy = None
        self._handlers = [
            self._doc.connect('saved', self.on_saved_or_loaded),
            self._doc.connect('loaded', self.on_saved_or_loaded)
        ]
        self.active = False
        self.uri = self._doc.get_uri()
        self.mime_type = self._doc.get_mime_type()
        self.update_uri_mime_type()

    def on_saved_or_loaded(self, doc, data):
        self.update_uri_mime_type()
    
    def deactivate(self):
        pass

    def update_uri_mime_type(self):
        uri = self._doc.get_uri()
        if uri is not None and uri != self.uri:
            self._window.get_data(WINDOW_DATA_KEY).view_dict[uri] = self
            self.uri = uri
        if self.uri is not None:
            [self.directory, self.filename] = os.path.split(self.uri[7:])
        self.mime_type = self._doc.get_mime_type()
        self.update_active()

    def goto_line (self, line):
        self._doc.goto_line(line) 
        self._view.scroll_to_cursor()
        self._window.set_active_tab(self._tab)

    def source_view_handler(self, input_file, source_link):
        if self.filename == input_file:
            self.goto_line(source_link[0] - 1)
        else:
            uri = "file://" + os.path.join (self.directory,input_file)
            view_dict = self._window.get_data(WINDOW_DATA_KEY).view_dict
            if uri in view_dict:
                view_dict[uri].goto_line(source_link[0] - 1)
            else:
                self._window.create_tab_from_uri(uri, None, source_link[0]-1, False, True) 
        self._window.present()

    def sync_view(self):
        if self.active:
            cursor_iter =  self._doc.get_iter_at_mark(self._doc.get_insert())
            line = cursor_iter.get_line() + 1
            col = cursor_iter.get_line_offset()
            print "SyncView", self.uri[7:], (line, col)
            self.window_proxy.SyncView(self.uri[7:], (line, col))

    def update_active(self):
        # Activate the plugin only if the doc is a LaTeX file. 
        self.active = (self.mime_type in self.mime_types)

        if self.active and self.window_proxy is None:
            if self.uri.endswith(".tex"):
                uri_output = self.uri[:-3] + "pdf"
                self.window_proxy = EvinceWindowProxy (uri_output, True)
                self.window_proxy.set_source_handler (self.source_view_handler)
                print "activando"
        elif not self.active and self.window_proxy is not None:
            # destroy the evince window proxy.
            print "desactivando"
            pass