def show(self, notebook_filename, version=None, task=None):
        self.xml = gtk.glade.XML(get_resource("rc", "keepnote.glade"),
                                 "update_notebook_dialog",
                                 keepnote.GETTEXT_DOMAIN)
        self.dialog = self.xml.get_widget("update_notebook_dialog")
        self.xml.signal_autoconnect(self)
        self.dialog.connect("close", lambda w:
                            self.dialog.response(gtk.RESPONSE_CANCEL))
        self.dialog.set_transient_for(self.main_window)

        self.text = self.xml.get_widget("update_message_label")
        self.saved = self.xml.get_widget("save_backup_check")

        if version is None:
            version = notebooklib.get_notebook_version(notebook_filename)

        self.text.set_text(MESSAGE_TEXT %
                           (version,
                            notebooklib.NOTEBOOK_FORMAT_VERSION))

        ret = False
        response = self.dialog.run()

        if response == gtk.RESPONSE_OK:

            # do backup
            if self.saved.get_active():
                if not self.backup(notebook_filename):
                    self.dialog.destroy()
                    return False

            self.dialog.destroy()

            # do update
            def func(task):
                update.update_notebook(
                    notebook_filename,
                    notebooklib.NOTEBOOK_FORMAT_VERSION)

            # TODO: reuse existing task
            task = tasklib.Task(func)
            dialog2 = dialog_wait.WaitDialog(self.main_window)
            dialog2.show(_("Updating Notebook"),
                         _("Updating notebook..."),
                         task, cancel=False)

            ret = not task.aborted()
            ty, err, tb = task.exc_info()
            if err:
                self.main_window.error(_("Error while updating."), err, tb)
                ret = False
        else:
            self.dialog.destroy()

        if ret:
            self.app.message(_("Notebook updated successfully"),
                             _("Notebook Update Complete"),
                             self.main_window)

        return ret
    def show(self, notebook_filename, version=None, task=None):

        self.xml = gtk.glade.XML(get_resource("rc", "keepnote.glade"),
                                 "update_notebook_dialog",
                                 keepnote.GETTEXT_DOMAIN)
        self.dialog = self.xml.get_widget("update_notebook_dialog")
        self.xml.signal_autoconnect(self)
        self.dialog.connect(
            "close", lambda w: self.dialog.response(gtk.RESPONSE_CANCEL))
        self.dialog.set_transient_for(self.main_window)

        self.text = self.xml.get_widget("update_message_label")
        self.saved = self.xml.get_widget("save_backup_check")

        if version is None:
            version = notebooklib.get_notebook_version(notebook_filename)

        self.text.set_text(MESSAGE_TEXT %
                           (version, notebooklib.NOTEBOOK_FORMAT_VERSION))

        ret = False
        response = self.dialog.run()

        if response == gtk.RESPONSE_OK:

            # do backup
            if self.saved.get_active():
                if not self.backup(notebook_filename):
                    self.dialog.destroy()
                    return False

            self.dialog.destroy()

            # do update
            def func(task):
                update.update_notebook(notebook_filename,
                                       notebooklib.NOTEBOOK_FORMAT_VERSION)

            # TODO: reuse existing task
            task = tasklib.Task(func)
            dialog2 = dialog_wait.WaitDialog(self.main_window)
            dialog2.show(_("Updating Notebook"),
                         _("Updating notebook..."),
                         task,
                         cancel=False)

            ret = not task.aborted()
            ty, err, tb = task.exc_info()
            if err:
                self.main_window.error(_("Error while updating."), err, tb)
                ret = False
        else:
            self.dialog.destroy()

        if ret:
            self.app.message(_("Notebook updated successfully"),
                             _("Notebook Update Complete"), self.main_window)

        return ret
Esempio n. 3
0
    def open_notebook(self, filename, window=None, task=None):
        """Open notebook"""

        from keepnote.gui import dialog_update_notebook

        try:
            version = notebooklib.get_notebook_version(filename)
        except Exception, e:
            self.error(_("Could not load notebook '%s'.") % filename,
                       e, sys.exc_info()[2])
            return None
Esempio n. 4
0
    def open_notebook(self, filename, window=None, task=None):
        """Open notebook"""

        from keepnote.gui import dialog_update_notebook

        try:
            version = notebooklib.get_notebook_version(filename)
        except Exception, e:
            self.error(
                _("Could not load notebook '%s'.") % filename, e,
                sys.exc_info()[2])
            return None
Esempio n. 5
0
    def open_notebook(self, filename, window=None, task=None):
        """Open notebook"""

        from keepnote.gui import dialog_update_notebook

        # HACK
        if isinstance(self._conns.get(filename), keepnote.notebook.connection.fs.NoteBookConnectionFS):

            try:
                version = notebooklib.get_notebook_version(filename)
            except Exception, e:
                self.error(_("Could not load notebook '%s'.") % filename, e, sys.exc_info()[2])
                return None

            if version < notebooklib.NOTEBOOK_FORMAT_VERSION:
                dialog = dialog_update_notebook.UpdateNoteBookDialog(self, window)
                if not dialog.show(filename, version=version, task=task):
                    self.error(_("Cannot open notebook (version too old)"))
                    gtk.gdk.threads_leave()
                    return None
Esempio n. 6
0
    def open_notebook(self, filename, window=None, task=None):
        """Open notebook"""
        from keepnote.gui import dialog_update_notebook

        # HACK
        if isinstance(self._conns.get(filename),
                      keepnote.notebook.connection.fs.NoteBookConnectionFS):

            try:
                version = notebooklib.get_notebook_version(filename)
            except Exception, e:
                self.error(
                    _("Could not load notebook '%s'.") % filename, e,
                    sys.exc_info()[2])
                return None

            if version < notebooklib.NOTEBOOK_FORMAT_VERSION:
                dialog = dialog_update_notebook.UpdateNoteBookDialog(
                    self, window)
                if not dialog.show(filename, version=version, task=task):
                    self.error(_("Cannot open notebook (version too old)"))
                    gtk.gdk.threads_leave()
                    return None
Esempio n. 7
0
def update_notebook(filename, desired_version, warn=lambda w: False,
                    verify=True):
    """Updates a notebook to the desired version (downgrading not implemented)"""

    # try to open notebook (may raise exceptions)
    version = notebooklib.get_notebook_version(filename)
    if version >= desired_version:
        return        


    while version < desired_version:

        # upgrade 1 --> 2
        if version == 1:
            notebook_update_v1_2.update_notebook(filename, 2, warn=warn,
                                                 verify=verify)
            version = 2

        # upgrade 2 --> 3
        elif version == 2:
            from keepnote.compat import notebook_v2 as old_notebooklib

            # try to load old notebook (may raise exceptions)
            notebook = old_notebooklib.NoteBook()
            notebook.load(filename)

            # write new notebook preference file
            notebook.pref.version = 3
            notebook.write_preferences()

            # recursively upgrade notes
            def walk(node):                        
                try:
                    node._version = 3
                    node.write_meta_data()
                except Exception, e:
                    if not warn(e):
                        raise notebooklib.NoteBookError("Could not update notebook", e)

                # recurse
                for child in node.get_children():
                    walk(child)
            walk(notebook)

            version = notebook.pref.version


            # verify notebook updated successfully
            if verify:
                notebook = notebooklib.NoteBook()
                notebook.load(filename)

                def walk(node):
                    for child in node.get_children():
                        walk(child)
                walk(notebook)
                notebook.close()

        # upgrade 3 --> 4
        elif version == 3:
            from keepnote.compat import notebook_v3 as old_notebooklib
            
            # try to load old notebook (may raise exceptions)
            notebook = old_notebooklib.NoteBook()
            notebook.load(filename)
            notebook.pref.version = 4
            index_file = os.path.join(notebook.get_pref_dir(), "index.sqlite")
            if os.path.exists(index_file):
                os.remove(index_file)
            old_notebooklib.write_new_preferences(notebook.pref, 
                                                  notebook.get_pref_file())
            notebook.close()
            version = 4
Esempio n. 8
0
def update_notebook(filename, desired_version, warn=lambda w: False,
                    verify=True):
    """Updates a notebook to the desired version (downgrading not implemented)"""

    # try to open notebook (may raise exceptions)
    version = notebooklib.get_notebook_version(filename)
    if version >= desired_version:
        return        


    while version < desired_version:

        # upgrade 1 --> 2
        if version == 1:
            notebook_update_v1_2.update_notebook(filename, 2, warn=warn,
                                                 verify=verify)
            version = 2

        # upgrade 2 --> 3
        elif version == 2:
            from keepnote.compat import notebook_v2 as old_notebooklib

            # try to load old notebook (may raise exceptions)
            notebook = old_notebooklib.NoteBook()
            notebook.load(filename)

            # write new notebook preference file
            notebook.pref.version = 3
            notebook.write_preferences()

            # recursively upgrade notes
            def walk(node):                        
                try:
                    node._version = 3
                    node.write_meta_data()
                except Exception, e:
                    if not warn(e):
                        raise notebooklib.NoteBookError("Could not update notebook", e)

                # recurse
                for child in node.get_children():
                    walk(child)
            walk(notebook)

            version = notebook.pref.version


            # verify notebook updated successfully
            if verify:
                notebook = notebooklib.NoteBook()
                notebook.load(filename)

                def walk(node):
                    for child in node.get_children():
                        walk(child)
                walk(notebook)
                notebook.close()

        # upgrade 3 --> 4
        elif version == 3:
            from keepnote.compat import notebook_v3 as old_notebooklib
            
            # try to load old notebook (may raise exceptions)
            notebook = old_notebooklib.NoteBook()
            notebook.load(filename)
            notebook.pref.version = 4
            old_notebooklib.write_new_preferences(notebook.pref, 
                                                  notebook.get_pref_file())
            notebook.close()
            version = 4
Esempio n. 9
0
def update_notebook(filename,
                    desired_version=notebooklib.NOTEBOOK_FORMAT_VERSION,
                    warn=lambda w: False,
                    verify=True):
    """
    Updates a notebook to the desired version (downgrading not implemented)
    """

    # try to open notebook (may raise exceptions)
    version = notebooklib.get_notebook_version(filename)
    if version >= desired_version:
        return

    while version < desired_version:

        # upgrade 1 --> 2
        if version == 1:
            notebook_update_v1_2.update_notebook(filename, 2, warn=warn,
                                                 verify=verify)
            version = 2

        # upgrade 2 --> 3
        elif version == 2:
            from keepnote.compat import notebook_v2
            from keepnote.compat import notebook_v3

            # try to load old notebook (may raise exceptions)
            notebook = notebook_v2.NoteBook()
            notebook.load(filename)

            # write new notebook preference file
            notebook.pref.version = 3
            notebook.write_preferences()

            # recursively upgrade notes
            def walk(node):
                try:
                    node._version = 3
                    node.write_meta_data()
                except Exception, e:
                    if not warn(e):
                        raise notebooklib.NoteBookError(
                            "Could not update notebook", e)

                # recurse
                for child in node.get_children():
                    walk(child)
            walk(notebook)

            version = notebook.pref.version

            # verify notebook updated successfully by walking all nodes.
            if verify:
                notebook = notebook_v3.NoteBook()
                notebook.load(filename)

                def walk(node):
                    for child in node.get_children():
                        walk(child)
                walk(notebook)
                notebook.close()

        # upgrade 3 --> 4
        elif version == 3:
            from keepnote.compat import notebook_v3

            # try to load old notebook (may raise exceptions)
            notebook = notebook_v3.NoteBook()
            notebook.load(filename)
            notebook.pref.version = 4

            # Clear out old index.
            index_file = os.path.join(notebook.get_pref_dir(), "index.sqlite")
            if os.path.exists(index_file):
                os.remove(index_file)

            # Write new preferences.
            notebook_v3.write_new_preferences(notebook.pref,
                                              notebook.get_pref_file())
            notebook.close()
            version = 4
Esempio n. 10
0
    def open_notebook(self, filename, window=None, task=None):
        """Open notebook"""

        from keepnote.gui import dialog_update_notebook

        # HACK
        if isinstance(self._conns.get(filename),
                      keepnote.notebook.connection.fs.NoteBookConnectionFS):

            try:
                version = notebooklib.get_notebook_version(filename)
            except Exception as e:
                self.error(
                    _("Could not load notebook '%s'.") % filename, e,
                    sys.exc_info()[2])
                return None

            if version < notebooklib.NOTEBOOK_FORMAT_VERSION:
                dialog = dialog_update_notebook.UpdateNoteBookDialog(
                    self, window)
                if not dialog.show(filename, version=version, task=task):
                    self.error(_("Cannot open notebook (version too old)"))
                    gtk.gdk.threads_leave()
                    return None

        # load notebook in background
        def update(task):
            sem = threading.Semaphore()
            sem.acquire()

            # perform notebook load in gui thread.
            # Ideally, this should be in the background, but it is very
            # slow.  If updating the wait dialog wasn't so expensive, I would
            # simply do loading in the background thread.
            def func():
                try:
                    conn = self._conns.get(filename)
                    notebook = notebooklib.NoteBook()
                    notebook.load(filename, conn)
                    task.set_result(notebook)
                except Exception as e:
                    task.set_exc_info()
                    task.stop()
                sem.release()  # notify that notebook is loaded
                return False

            gobject.idle_add(func)

            # wait for notebook to load
            sem.acquire()

        def update_old(task):
            notebook = notebooklib.NoteBook()
            notebook.load(filename)
            task.set_result(notebook)

        task = tasklib.Task(update)
        dialog = keepnote.gui.dialog_wait.WaitDialog(window)
        dialog.show(_("Opening notebook"), _("Loading..."), task, cancel=False)

        # detect errors
        try:
            if task.aborted():
                raise task.exc_info()[1]
            else:
                notebook = task.get_result()
                if notebook is None:
                    return None

        except notebooklib.NoteBookVersionError as e:
            self.error(
                _("This version of %s cannot read this notebook.\n"
                  "The notebook has version %d.  %s can only read %d.") %
                (keepnote.PROGRAM_NAME, e.notebook_version,
                 keepnote.PROGRAM_NAME, e.readable_version), e,
                task.exc_info()[2])
            return None

        except NoteBookError as e:
            self.error(
                _("Could not load notebook '%s'.") % filename, e,
                task.exc_info()[2])
            return None

        except Exception as e:
            # give up opening notebook
            self.error(
                _("Could not load notebook '%s'.") % filename, e,
                task.exc_info()[2])
            return None

        self._init_notebook(notebook)

        return notebook