From 8a850eb0631104abec7bf3fa278fd60e56d5d584 Mon Sep 17 00:00:00 2001 From: trigg Date: Sun, 19 May 2024 20:26:40 +0100 Subject: [PATCH] Add a right click context menu to the systray applet (to "quit/exit" it and run arch-update) (#165) * - Add gettext translation to systray - Add right-click-menu with two options - - Update - - Exit - Added blank translation keys --------- Co-authored-by: Robin Candau --- po/arch-update.pot | 8 ++++++ po/fr.po | 8 ++++++ src/script/arch-update-tray.py | 49 +++++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/po/arch-update.pot b/po/arch-update.pot index cf24272..6bca749 100644 --- a/po/arch-update.pot +++ b/po/arch-update.pot @@ -458,3 +458,11 @@ msgstr "" #, sh-format msgid "The '${config_file}' configuration file has been generated" msgstr "" + +#: src/script/arch-update-tray.py:117 +msgid "Run Arch-Update" +msgstr "" + +#: src/script/arch-update-tray.py:118 +msgid "Exit" +msgstr "" diff --git a/po/fr.po b/po/fr.po index 7f8a25b..1944a53 100644 --- a/po/fr.po +++ b/po/fr.po @@ -493,3 +493,11 @@ msgstr "" #, sh-format msgid "The '${config_file}' configuration file has been generated" msgstr "Le fichier de configuration '${config_file}' a été généré" + +#: src/script/arch-update-tray.py:117 +msgid "Run Arch-Update" +msgstr "Lancer Arch-Update" + +#: src/script/arch-update-tray.py:118 +msgid "Exit" +msgstr "Quitter" diff --git a/src/script/arch-update-tray.py b/src/script/arch-update-tray.py index fc75967..16ea7d1 100755 --- a/src/script/arch-update-tray.py +++ b/src/script/arch-update-tray.py @@ -1,11 +1,12 @@ #!/usr/bin/env python3 """Arch-Update System Tray.""" +import gettext import logging import os import sys import subprocess -from PyQt6.QtGui import QIcon -from PyQt6.QtWidgets import QApplication, QSystemTrayIcon +from PyQt6.QtGui import QIcon, QAction +from PyQt6.QtWidgets import QApplication, QSystemTrayIcon, QMenu from PyQt6.QtCore import QFileSystemWatcher # Create logger @@ -22,6 +23,30 @@ log.error("Statefile does not exist: %s", STATE_FILE) sys.exit(1) +# Find translations +paths = [] +if 'XDG_DATA_DIRS' in os.environ: + paths.extend(os.environ['XDG_DATA_DIRS'].split(":")) +if 'XDG_DATA_HOME' in os.environ: + paths.extend(os.environ['XDG_DATA_HOME'].split(":")) +if 'HOME' in os.environ: + paths.append(os.path.join( + os.environ['HOME'], '.local', 'share')) +paths.extend(['/usr/share', '/usr/local/share']) +_ = None +for path in paths: + french_translation_file = os.path.join( + path, "locale", "fr", "LC_MESSAGES", "Arch-Update.mo") + if os.path.isfile(french_translation_file): + path = os.path.join(path, 'locale') + t = gettext.translation('Arch-Update', localedir=path, fallback=True) + _ = t.gettext + break +if not _: + t = gettext.translation('Arch-Update', fallback=True) + _ = t.gettext + log.error("No translations found") + def arch_update(): """ Launch with desktop file """ @@ -63,10 +88,14 @@ def file_changed(self): icon = QIcon.fromTheme(contents) self.tray.setIcon(icon) - def update(self): + def run(self): """ Start arch-update """ arch_update() + def exit(self): + """ Close systray process """ + sys.exit(0) + def __init__(self, statefile): """ Start Qt6 System Tray """ @@ -81,7 +110,19 @@ def __init__(self, statefile): self.tray = QSystemTrayIcon() self.file_changed() self.tray.setVisible(True) - self.tray.activated.connect(self.update) + self.tray.activated.connect(self.run) + + # Menu + menu = QMenu() + menu_launch = QAction(_("Run Arch-Update")) + menu_exit = QAction(_("Exit")) + menu.addAction(menu_launch) + menu.addAction(menu_exit) + + menu_launch.triggered.connect(self.run) + menu_exit.triggered.connect(self.exit) + + self.tray.setContextMenu(menu) # File Watcher self.watcher = QFileSystemWatcher([self.statefile])