Beispiel #1
0
class AptInstallation(GObject.GObject):

    __gsignals__ = {
        "finished": (GObject.SIGNAL_RUN_FIRST, GObject.TYPE_NONE, ())
    }

    def __init__(self, replacement):
        GObject.GObject.__init__(self)

        self.replacement = replacement
        self.apt_client = AptClient()

    def run(self):
        apt_cache = open_apt_cache()

        if not apt_cache.has_key(self.replacement["apt"]):
            self.do_update()
        else:
            self.do_install()

    def do_update(self):
        trans_update = self.apt_client.update_cache()
        trans_update.connect("finished", self.on_finished_update)

        dia = AptProgressDialog(trans_update)
        dia.run(close_on_finished=True,
                show_error=False,
                reply_handler=lambda: True,
                error_handler=self.on_error)
        return

    def on_finished_update(self, trans, exit):
        if exit == "exit-success":
            GLib.timeout_add(200, self.do_install)
        return True

    def do_install(self):
        trans_inst = self.apt_client.install_packages(
            package_names=[self.replacement["apt"]])
        trans_inst.connect("finished", self.on_finished_install)
        dia = AptProgressDialog(transaction=trans_inst)
        dia.connect("finished", self.on_install_dialog_finished)
        dia.run(close_on_finished=True,
                show_error=False,
                reply_handler=lambda: True,
                error_handler=self.on_error)
        return

    def on_install_dialog_finished(self, dia):
        if self.exit == "exit-success":
            try:
                replacement_desktop = Gio.DesktopAppInfo.new(
                    self.replacement["desktopLauncher"])
                launch_desktop_app(replacement_desktop)
            except:
                self.finished_dialog(
                    _("%s has been installed") % self.replacement["name"])

        self.emit("finished")

    def on_finished_install(self, trans, exit):
        self.exit = exit
        return

    def finished_dialog(self, message):
        dialog = Gtk.MessageDialog(message_type=Gtk.MessageType.INFO,
                                   buttons=Gtk.ButtonsType.OK,
                                   text=message)
        dialog.run()
        dialog.destroy()
        return

    def on_error(self, error):
        if isinstance(error, aptdaemon.errors.NotAuthorizedError):
            # Silently ignore auth failures
            return
        elif not isinstance(error, aptdaemon.errors.TransactionFailed):
            # Catch internal errors of the client
            error = aptdaemon.errors.TransactionFailed(ERROR_UNKNOWN,
                                                       str(error))
        dia = AptErrorDialog(error)
        dia.run()
        dia.hide()