Skip to content

Commit

Permalink
- Remove Gtk3
Browse files Browse the repository at this point in the history
- Remove exo open
- Remove kioclient
- Remove gettext
  • Loading branch information
trigg committed May 7, 2024
1 parent e879653 commit b376cb3
Showing 1 changed file with 6 additions and 133 deletions.
139 changes: 6 additions & 133 deletions src/script/arch-update-tray.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
#!/usr/bin/env python3
"""Arch-Update System Tray."""
import gettext
import logging
import os
import sys
import shutil
import subprocess

# Import toolkits and list availability
available_tk_list = []

# Test for GTK3 availability
try:
# pylint: disable=import-outside-toplevel
import gi
gi.require_version('AppIndicator3', '0.1')
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gio
from gi.repository import AppIndicator3 as ai
available_tk_list.append("gtk3")
except ImportError:
pass

# Test for QT6 availability
try:
# pylint: disable=import-outside-toplevel
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QApplication, QSystemTrayIcon
from PyQt6.QtCore import QFileSystemWatcher
available_tk_list.append("qt6")
except ImportError:
pass
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QApplication, QSystemTrayIcon
from PyQt6.QtCore import QFileSystemWatcher

# Create logger
log = logging.getLogger(__name__)
Expand All @@ -46,93 +22,11 @@
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(":"))

_ = 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):
t = gettext.translation('default', '.', fallback=True)
_ = t.gettext
break
if not _:
log.error("No translations found")
sys.exit(1)

# Find user preference
TOOLKIT_PREFERENCE = "qt6"
if 'ARCH_UPDATE_TK' in os.environ:
match os.environ['ARCH_UPDATE_TK']:
case 'gtk3':
TOOLKIT_PREFERENCE = "gtk3"
case 'qt6':
TOOLKIT_PREFERENCE = 'qt6'
case _:
log.error("Unknown toolkit preference: %s",
os.environ['ARCH_UPDATE_TK'])


def arch_update():
""" Find a way to launch with terminal """
""" Launch with terminal """
update = "/usr/share/applications/arch-update.desktop"
if shutil.which("kioclient"):
subprocess.run(["kioclient", "exec", update], check=False)
elif shutil.which("gio"):
subprocess.run(["gio", "launch", update], check=False)
elif shutil.which("exo-open"):
subprocess.run(["exo-open", update], check=False)
else:
log.error("Unable to start updater")
sys.exit(1)


class ArchUpdateGtk3:
""" System Tray using GTK3 library """

def file_changed(self, _a=None, _b=None, _c=None, _d=None):
""" Called when statefile contents change """
try:
with open(self.statefile, encoding="utf-8") as f:
contents = f.readline().strip()
except FileNotFoundError:
log.error("Statefile Missing")
sys.exit(1)
if contents.startswith("arch-update"):
self.ind.set_icon(contents)

def update(self, _button=None):
""" Start arch-update """
arch_update()

def __init__(self, statefile):
""" Start Gtk3 System Tray """
self.statefile = statefile

self.ind = ai.Indicator.new(
'arch-update', 'arch-update', ai.IndicatorCategory.SYSTEM_SERVICES)
self.ind.set_status(ai.IndicatorStatus.ACTIVE)

# Gtk won't allow indicators without a menu
menu = Gtk.Menu()
item = Gtk.MenuItem.new_with_label(_("Update now"))
item.show()
menu.append(item)
self.ind.set_menu(menu)

item.connect('activate', self.update)

# File monitor
gtkstatefile = Gio.File.new_for_path(self.statefile)
monitor = gtkstatefile.monitor_file(0, None)
monitor.connect("changed", self.file_changed)

Gtk.main()
subprocess.run(["gio", "launch", update], check=False)


class ArchUpdateQt6:
Expand Down Expand Up @@ -181,26 +75,5 @@ def __init__(self, statefile):
app.exec()


def start(toolkit):
""" Start one of the System trays """
match toolkit:
case 'qt6':
ArchUpdateQt6(STATE_FILE)
case 'gtk3':
ArchUpdateGtk3(STATE_FILE)
case _:
log.error("Unknown toolkit selected: %s", toolkit)


if __name__ == "__main__":
# Starting point

if TOOLKIT_PREFERENCE in available_tk_list:
# User preference first
start(TOOLKIT_PREFERENCE)
elif len(available_tk_list) > 0:
# Use any other, if preference isn't available
start(available_tk_list[0])
else:
# Uh oh
log.error("No available toolkits")
ArchUpdateQt6(STATE_FILE)

0 comments on commit b376cb3

Please sign in to comment.