From b9416da9c876c6c971288132f791d4f8b0935282 Mon Sep 17 00:00:00 2001 From: Tilman Kranz Date: Wed, 19 Jul 2023 05:19:14 +0200 Subject: [PATCH] implement accelerators as application actions --- bin/simple-apt-update | 111 ++++++++++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 37 deletions(-) diff --git a/bin/simple-apt-update b/bin/simple-apt-update index 8064de1..b25f1e1 100755 --- a/bin/simple-apt-update +++ b/bin/simple-apt-update @@ -1,23 +1,28 @@ #!/usr/bin/env python3 -import subprocess -import selectors -import html import gi +import html +import logging import os +import selectors +import signal +import subprocess +import sys -gi.require_version('Gtk', '3.0') gi.require_version('Gdk', '3.0') - -try: - from gi.repository import Gdk -except ImportError: - print("ERROR: Could not import Gdk") +gi.require_version('Gtk', '3.0') try: from gi.repository import Gtk except ImportError: - print("ERROR: Could not import Gtk") + logging.error("Could not import Gtk") + sys.exit(1) + +try: + from gi.repository import Gio +except ImportError: + logging.error("Could not import Gio") + sys.exit(1) class UpdateWindow(Gtk.ApplicationWindow): @@ -78,12 +83,12 @@ class UpdateWindow(Gtk.ApplicationWindow): def run(self, args, ignore_stderr=False, output_msg=None, empty_msg=None, env={}): - self.clear() self.lock() - self.spinner.start() + self.clear() self.prepend_mesg( "INFO", "Running command \"%s\" ..." % " ".join(args)) + p = subprocess.Popen( args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False, env=dict(os.environ, **env)) @@ -101,9 +106,11 @@ class UpdateWindow(Gtk.ApplicationWindow): if data: if key.fileobj is p.stdout: + logging.debug("STDOUT: " + data) self.append_mesg("STDOUT", data) output = True elif not ignore_stderr: + logging.debug("STDERR: " + data) self.append_mesg("STDERR", data) exit_code = p.poll() @@ -119,7 +126,6 @@ class UpdateWindow(Gtk.ApplicationWindow): done = True break - self.spinner.stop() self.unlock() if error: @@ -137,11 +143,13 @@ class UpdateWindow(Gtk.ApplicationWindow): self.update_button.set_sensitive(False) self.upgrade_button.set_sensitive(False) self.list_button.set_sensitive(False) + self.spinner.start() def unlock(self): self.update_button.set_sensitive(True) self.upgrade_button.set_sensitive(True) self.list_button.set_sensitive(True) + self.spinner.stop() def upgrade(self): args = ['/usr/bin/apt-get', '-yqq', 'full-upgrade'] @@ -173,26 +181,19 @@ class UpdateWindow(Gtk.ApplicationWindow): def on_list(self, *args): self.list() - def on_ctrl_w(self, *args): - self.app.quit() + def on_quit(self, *args): + self.application.quit() - def __init__(self, app): + def __init__(self, application): super(UpdateWindow, self).__init__( - application=app, + application=application, title="Simple APT Update") - self.app = app + self.application = application self.init_ui() def init_ui(self): - self.set_icon_from_file('/usr/share/icons/simple-apt-update.svg') self.set_border_width(10) self.set_default_size(630, 390) - accel = Gtk.AccelGroup() - accel.connect(Gdk.keyval_from_name('W'), Gdk.ModifierType.CONTROL_MASK, - 0, self.on_ctrl_w) - accel.connect(Gdk.keyval_from_name('Q'), Gdk.ModifierType.CONTROL_MASK, - 0, self.on_ctrl_w) - self.add_accel_group(accel) hbox = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL) self.add(hbox) @@ -221,13 +222,13 @@ class UpdateWindow(Gtk.ApplicationWindow): self.spinner = Gtk.Spinner() self.spinner.set_hexpand(True) - grid.attach(self.spinner, 4, 0, 1, 1) + grid.attach(self.spinner, 3, 0, 1, 1) self.quit_button = Gtk.Button.new_from_icon_name( - "gtk-cancel", Gtk.IconSize.BUTTON) + "exit", Gtk.IconSize.BUTTON) self.quit_button.set_tooltip_text("Exit the program") self.quit_button.set_halign(Gtk.Align.END) - self.quit_button.connect("clicked", self.on_ctrl_w) + self.quit_button.connect("clicked", self.on_quit) grid.attach(self.quit_button, 4, 0, 1, 1) self.scrolledwindow = Gtk.ScrolledWindow() @@ -246,14 +247,50 @@ class UpdateWindow(Gtk.ApplicationWindow): hbox.pack_start(self.scrolledwindow, True, True, 0) -def on_activate(app): - win = UpdateWindow(app) - win.present() - win.show_all() - win.update() - win.list() +class SimpleAptUpdate(Gtk.Application): + def __init__(self): + super().__init__(application_id='de.linuxfoo.SimpleAptUpdate', + flags=Gio.ApplicationFlags.FLAGS_NONE) + self.connect('activate', self.on_activate) + signal.signal(signal.SIGINT, signal.SIG_DFL) + + def do_command_line(self, cmdline): + pass + + def on_activate(self, application): + self.window = UpdateWindow(application) + + action = Gio.SimpleAction.new("quit", None) + action.connect("activate", self.window.on_quit) + self.add_action(action) + self.set_accels_for_action('app.quit', ['q', 'w']) + + action = Gio.SimpleAction.new("update", None) + action.connect("activate", self.window.on_update) + self.add_action(action) + self.set_accels_for_action('app.update', ['u']) + + action = Gio.SimpleAction.new("upgrade", None) + action.connect("activate", self.window.on_upgrade) + self.add_action(action) + self.set_accels_for_action('app.upgrade', ['g']) + + action = Gio.SimpleAction.new("list", None) + action.connect("activate", self.window.on_list) + self.add_action(action) + self.set_accels_for_action('app.list', ['l']) + + self.window.present() + self.window.show_all() + self.window.update() + self.window.list() -app = Gtk.Application(application_id='org.linuxfoo.SimpleAptUpdate') -app.connect('activate', on_activate) -app.run(None) +def main(): + application = SimpleAptUpdate() + exit_status = application.run() + sys.exit(exit_status) + + +if __name__ == '__main__': + main()