Fix taskbar icon and left clic on wayland, code improvements

This commit is contained in:
Silvan Calarco 2024-08-06 17:20:15 +02:00
parent caff60b8b6
commit c0d12835dc
3 changed files with 119 additions and 102 deletions

View File

@ -21,6 +21,7 @@ iconsdir = ${datadir}/icons/hicolor
sysconfigdir= ${sysconfdir}/sysconfig sysconfigdir= ${sysconfdir}/sysconfig
localesdir = ${datadir}/locale localesdir = ${datadir}/locale
xdgautostartdir = ${sysconfdir}/xdg/autostart xdgautostartdir = ${sysconfdir}/xdg/autostart
xdgapplicationsdir = ${datadir}/applications
srcdir = . srcdir = .
DESTDIR = DESTDIR =
@ -52,10 +53,12 @@ install-locales: locales
install-dirs: install-dirs:
@$(INSTALL_DIR) $(DESTDIR)$(bindir) @$(INSTALL_DIR) $(DESTDIR)$(bindir)
@$(INSTALL_DIR) $(DESTDIR)$(xdgautostartdir) @$(INSTALL_DIR) $(DESTDIR)$(xdgautostartdir)
@$(INSTALL_DIR) $(DESTDIR)$(xdgapplicationsdir)
install: install-dirs install-locales install: install-dirs install-locales
$(INSTALL_PROGRAM) mambatray.py $(DESTDIR)$(bindir)/mambatray $(INSTALL_PROGRAM) mambatray.py $(DESTDIR)$(bindir)/mambatray
$(INSTALL_DATA) mambatray.desktop $(DESTDIR)$(xdgautostartdir)/mambatray.desktop $(INSTALL_DATA) mambatray.desktop $(DESTDIR)$(xdgautostartdir)/mambatray.desktop
$(INSTALL_DATA) mambatray.desktop $(DESTDIR)$(xdgapplicationsdir)/mambatray.desktop
clean: clean:
rm -f $(pck_catalogs:.po=.mo) rm -f $(pck_catalogs:.po=.mo)

View File

@ -8,3 +8,4 @@ Comment=openmamnba control center
Comment[it]=Centro di controllo openmamba Comment[it]=Centro di controllo openmamba
X-KDE-autostart-after=panel X-KDE-autostart-after=panel
X-KDE-autostart-phase=2 X-KDE-autostart-phase=2
NoDisplay=true

View File

@ -19,104 +19,118 @@ DATADIR = os.path.dirname(os.path.realpath((__file__))) + "/"
class SystemTrayIcon(QSystemTrayIcon): class SystemTrayIcon(QSystemTrayIcon):
menu = None menu = None
contatore = 0
def __init__(self, icon): NetsrpmsArg = "-c"
QSystemTrayIcon.__init__(self, icon) lista = []
global w, contatore, srpm, lista, NetsrpmsArg, nOre, desktop_session
nOre = 2 nOre = 2
srpm = [ srpm = [
"skype", "win32codecs", "msttcorefonts", "b43-firmware", "skype", "win32codecs", "msttcorefonts", "b43-firmware",
"virtualbox-extension-pack", "chromium-widevine", "spotify" "virtualbox-extension-pack", "chromium-widevine", "spotify"
] ]
NetsrpmsArg = "-c" desktop_session = 'default'
lista = [] xdg_session_type = 'x11'
contatore = 0
refreshPackagesTimer = None
checkSRPMSUpdatesTimer = None
refreshPackagesCacheProcess = None
SRPMUpdateProcess = None
softwareManagerProcess = None
softwareCheckUpgradesProcess = None
softwareBaseInstallsProcess = None
networkOnlineProcess = None
networkFirewallDisableProcess = None
networkFirewallEnableProcess = None
networkFirewallConfigureProcess = None
networkProxyProcess = None
def __init__(self, icon):
QSystemTrayIcon.__init__(self, icon)
self.menu = QMenu() self.menu = QMenu()
try: try:
desktop_session = os.path.basename(os.getenv('DESKTOP_SESSION')) self.desktop_session = os.path.basename(os.getenv('DESKTOP_SESSION'))
except: except:
desktop_session = 'default' None
if desktop_session == 'default': try:
desktop_session = 'plasma' self.xdg_session_type = os.path.basename(os.getenv('XDG_SESSION_TYPE'))
elif desktop_session[:3] == 'kde': except:
desktop_session = 'kde' None
if self.desktop_session == 'default':
self.desktop_session = 'plasma'
elif self.desktop_session[:3] == 'kde':
self.desktop_session = 'kde'
# refresh packages cache timer # refresh packages cache timer
global refreshPackagesTimer, refreshPackagesCacheProcess,\ self.refreshPackagesTimer = QTimer(self)
checkSRPMSUpdatesTimer, SRPMUpdateProcess, networkOnlineProcess self.checkSRPMSUpdatesTimer = QTimer(self)
refreshPackagesTimer = QTimer(self) self.refreshPackagesCacheProcess = QProcess(self)
checkSRPMSUpdatesTimer = QTimer(self) self.SRPMUpdateProcess = QProcess(self)
refreshPackagesCacheProcess = QProcess(self) self.networkOnlineProcess = QProcess(self)
SRPMUpdateProcess = QProcess(self) self.refreshPackagesTimer.timeout.connect(self.refreshPackagesCache)
networkOnlineProcess = QProcess(self) self.refreshPackagesCacheProcess.finished.connect(self.restartRefreshPackagesTimer)
refreshPackagesTimer.timeout.connect(self.refreshPackagesCache) self.checkSRPMSUpdatesTimer.timeout.connect(self.SRPMSCheckUpgradeList)
refreshPackagesCacheProcess.finished.connect(self.restartRefreshPackagesTimer)
checkSRPMSUpdatesTimer.timeout.connect(self.SRPMSCheckUpgradeList)
# software menu # software menu
global softwareManagerProcess, softwareCheckUpgradesProcess,\ self.softwareManagerProcess = QProcess(self)
softwareBaseInstallsProcess self.softwareCheckUpgradesProcess = QProcess(self)
softwareManagerProcess = QProcess(self) self.softwareBaseInstallsProcess = QProcess(self)
softwareCheckUpgradesProcess = QProcess(self)
softwareBaseInstallsProcess = QProcess(self)
softwareMenu = self.menu.addMenu(QIcon.fromTheme("applications-system"),_("Software")) self.softwareMenu = self.menu.addMenu(QIcon.fromTheme("applications-system"),_("Software"))
softwareManageAction = softwareMenu.addAction(QIcon.fromTheme("applications-other"),_("Add/Remove software packages...")) self.softwareManageAction = self.softwareMenu.addAction(QIcon.fromTheme("applications-other"),_("Add/Remove software packages..."))
softwareManageAction.triggered.connect(self.softwareManager) self.softwareManageAction.triggered.connect(self.softwareManager)
softwareCheckUpgradesAction = softwareMenu.addAction(QIcon.fromTheme("system-software-update"),_("Check for updates now...")) self.softwareCheckUpgradesAction = self.softwareMenu.addAction(QIcon.fromTheme("system-software-update"),_("Check for updates now..."))
softwareCheckUpgradesAction.triggered.connect(self.softwareCheckUpgrades) self.softwareCheckUpgradesAction.triggered.connect(self.softwareCheckUpgrades)
softwareBaseInstallsAction = softwareMenu.addAction(QIcon.fromTheme("mambabase"),_("{} components...").format(distro.name())) self.softwareBaseInstallsAction = self.softwareMenu.addAction(QIcon.fromTheme("mambabase"),_("{} components...").format(distro.name()))
softwareBaseInstallsAction.triggered.connect(self.softwareBaseInstalls) self.softwareBaseInstallsAction.triggered.connect(self.softwareBaseInstalls)
# network menu # network menu
global networkFirewallDisableProcess, networkFirewallEnableProcess,\ self.networkFirewallDisableProcess = QProcess(self)
networkFirewallConfigureProcess, networkProxyProcess self.networkFirewallEnableProcess = QProcess(self)
networkFirewallDisableProcess = QProcess(self) self.networkFirewallConfigureProcess = QProcess(self)
networkFirewallEnableProcess = QProcess(self) self.networkProxyProcess = QProcess(self)
networkFirewallConfigureProcess = QProcess(self)
networkProxyProcess = QProcess(self)
networkMenu = self.menu.addMenu(QIcon.fromTheme("preferences-system-network"), self.networkMenu = self.menu.addMenu(QIcon.fromTheme("preferences-system-network"), _("Network"))
_("Network"))
networkFirewallMenu = networkMenu.addMenu(QIcon.fromTheme("security-medium"),_("Firewall")) self.networkFirewallMenu = self.networkMenu.addMenu(QIcon.fromTheme("security-medium"),_("Firewall"))
networkFirewallDisableAction = networkFirewallMenu.addAction(QIcon.fromTheme("security-low"),_("Disable")) self.networkFirewallDisableAction = self.networkFirewallMenu.addAction(QIcon.fromTheme("security-low"),_("Disable"))
networkFirewallDisableAction.triggered.connect(self.networkFirewallDisable) self.networkFirewallDisableAction.triggered.connect(self.networkFirewallDisable)
networkFirewallDisableProcess.finished.connect(self.networkFirewallDisableMessage) self.networkFirewallDisableProcess.finished.connect(self.networkFirewallDisableMessage)
networkFirewallEnableAction = networkFirewallMenu.addAction(QIcon.fromTheme("security-high"),_("Enable")) self.networkFirewallEnableAction = self.networkFirewallMenu.addAction(QIcon.fromTheme("security-high"),_("Enable"))
networkFirewallEnableAction.triggered.connect(self.networkFirewallEnable) self.networkFirewallEnableAction.triggered.connect(self.networkFirewallEnable)
networkFirewallEnableProcess.finished.connect(self.networkFirewallEnableMessage) self.networkFirewallEnableProcess.finished.connect(self.networkFirewallEnableMessage)
networkFirewallConfigureAction = networkFirewallMenu.addAction(QIcon.fromTheme("security-medium"),_("Configure...")) self.networkFirewallConfigureAction = self.networkFirewallMenu.addAction(QIcon.fromTheme("security-medium"),_("Configure..."))
networkFirewallConfigureAction.triggered.connect(self.networkFirewallConfigure) self.networkFirewallConfigureAction.triggered.connect(self.networkFirewallConfigure)
networkProxyAction = networkMenu.addAction(QIcon.fromTheme("network-server"),_("Proxy...")) self.networkProxyAction = self.networkMenu.addAction(QIcon.fromTheme("network-server"),_("Proxy..."))
networkProxyAction.triggered.connect(self.networkProxy) self.networkProxyAction.triggered.connect(self.networkProxy)
SRPMUpdateProcess.finished.connect(self.SRPMSCheckUpgrade) self.SRPMUpdateProcess.finished.connect(self.SRPMSCheckUpgrade)
# about box # about box
aboutAction = self.menu.addSeparator() self.aboutAction = self.menu.addSeparator()
aboutAction = self.menu.addAction(QIcon.fromTheme("system-logo"),_("About")) self.aboutAction = self.menu.addAction(QIcon.fromTheme("system-logo"),_("About"))
aboutAction.triggered.connect(self.about) self.aboutAction.triggered.connect(self.about)
self.setToolTip(_("{} control center").format(distro.name())) self.setToolTip(_("{} control center").format(distro.name()))
self.activated.connect(self.iconActivated) self.activated.connect(self.iconActivated)
self.setContextMenu(self.menu) self.setContextMenu(self.menu)
# the first time refresh packages cache after 5 minutes # the first time refresh packages cache after 5 minutes
refreshPackagesTimer.start( 5 * 60000 ) self.refreshPackagesTimer.start( 5 * 60000 )
# the first time check for SRPMS updates after 4 minutes # the first time check for SRPMS updates after 4 minutes
checkSRPMSUpdatesTimer.start( 4 * 60000 ) self.checkSRPMSUpdatesTimer.start( 4 * 60000 )
def iconActivated(self, reason): def iconActivated(self, reason):
if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.DoubleClick, QSystemTrayIcon.Context): if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.DoubleClick, QSystemTrayIcon.Context):
# FIXME: on plasma wayland and left click menu will popup in the center of the screen # FIXME: on plasma wayland and left click menu will popup in the center of the screen
# disabling and only supporting right click. # disabling and only supporting right click.
if desktop_session != 'plasmawayland': if self.xdg_session_type != 'wayland':
self.contextMenu().popup(QCursor.pos()) self.contextMenu().popup(QCursor.pos())
def about(self): def about(self):
@ -135,27 +149,27 @@ class SystemTrayIcon(QSystemTrayIcon):
aboutBox.exec() aboutBox.exec()
def networkOnline(self): def networkOnline(self):
return networkOnlineProcess.execute("/usr/bin/nm-online",['-q','-x']) return self.networkOnlineProcess.execute("/usr/bin/nm-online",['-q','-x'])
def softwareManager(self): def softwareManager(self):
softwareManagerProcess.execute("/usr/bin/plasma-discover") self.softwareManagerProcess.execute("/usr/bin/plasma-discover")
def softwareCheckUpgrades(self): def softwareCheckUpgrades(self):
softwareCheckUpgradesProcess.start("/usr/bin/pkcon", ['refresh', 'force']) self.softwareCheckUpgradesProcess.start("/usr/bin/pkcon", ['refresh', 'force'])
def refreshPackagesCache(self): def refreshPackagesCache(self):
refreshPackagesTimer.stop() self.refreshPackagesTimer.stop()
refreshPackagesCacheProcess.start("/usr/bin/pkcon", ['refresh', 'force']) self.refreshPackagesCacheProcess.start("/usr/bin/pkcon", ['refresh', 'force'])
def restartRefreshPackagesTimer(self,status): def restartRefreshPackagesTimer(self,status):
# check every 4 hours and 31 minutes */ # check every 4 hours and 31 minutes */
refreshPackagesTimer.start( (4 * 60 + 31) * 60000) self.refreshPackagesTimer.start( (4 * 60 + 31) * 60000)
def softwareBaseInstalls(self): def softwareBaseInstalls(self):
softwareBaseInstallsProcess.start("/usr/bin/mambabase") self.softwareBaseInstallsProcess.start("/usr/bin/mambabase")
def networkFirewallDisable(self): def networkFirewallDisable(self):
networkFirewallDisableProcess.start("sudo",['/usr/sbin/ufw','disable']) self.networkFirewallDisableProcess.start("sudo",['/usr/sbin/ufw','disable'])
def networkFirewallDisableMessage(self,status): def networkFirewallDisableMessage(self,status):
if (status == QProcess.NormalExit): if (status == QProcess.NormalExit):
@ -164,7 +178,7 @@ class SystemTrayIcon(QSystemTrayIcon):
self.showMessage("Firewall", _("Could not disable firewall")) self.showMessage("Firewall", _("Could not disable firewall"))
def networkFirewallEnable(self): def networkFirewallEnable(self):
networkFirewallEnableProcess.start("sudo",['/usr/sbin/ufw','enable']) self.networkFirewallEnableProcess.start("sudo",['/usr/sbin/ufw','enable'])
def networkFirewallEnableMessage(self,status): def networkFirewallEnableMessage(self,status):
if (status == QProcess.NormalExit): if (status == QProcess.NormalExit):
@ -173,72 +187,71 @@ class SystemTrayIcon(QSystemTrayIcon):
self.showMessage("Firewall", _("Could not enable firewall")) self.showMessage("Firewall", _("Could not enable firewall"))
def networkFirewallConfigure(self): def networkFirewallConfigure(self):
if desktop_session == 'kde' or desktop_session.startswith('plasma'): if self.desktop_session == 'kde' or self.desktop_session.startswith('plasma'):
networkFirewallConfigureProcess.execute("/usr/bin/kcmshell5", list(set(['firewall']))) self.networkFirewallConfigureProcess.execute("/usr/bin/kcmshell5", list(set(['firewall'])))
else: else:
networkFirewallConfigureProcess.execute("/usr/bin/gufw") self.networkFirewallConfigureProcess.execute("/usr/bin/gufw")
def networkProxy(self): def networkProxy(self):
if desktop_session.startswith('plasma'): if self.desktop_session.startswith('plasma'):
networkProxyProcess.execute("/usr/bin/kcmshell5", list(set(['proxy']))) self.networkProxyProcess.execute("/usr/bin/kcmshell5", list(set(['proxy'])))
elif desktop_session == 'kde': elif self.desktop_session == 'kde':
networkProxyProcess.execute("/opt/kde/bin/kcmshell4", list(set(['proxy']))) self.networkProxyProcess.execute("/opt/kde/bin/kcmshell4", list(set(['proxy'])))
elif desktop_session == 'gnome': elif self.desktop_session == 'gnome':
networkProxyProcess.execute("/usr/bin/gnome-control-center", "network") self.networkProxyProcess.execute("/usr/bin/gnome-control-center", "network")
def SRPMSCheckUpgradeList(self): def SRPMSCheckUpgradeList(self):
checkSRPMSUpdatesTimer.stop() self.checkSRPMSUpdatesTimer.stop()
global contatore, lista, NetsrpmsArg
if (self.networkOnline() == 0): if (self.networkOnline() == 0):
try: try:
SRPMUpdateProcess.start("/usr/bin/openmamba-netsrpms",[NetsrpmsArg,srpm[contatore]]) self.SRPMUpdateProcess.start("/usr/bin/openmamba-netsrpms",[self.NetsrpmsArg,self.srpm[self.contatore]])
except: except:
contatore = 0 self.contatore = 0
if NetsrpmsArg == '-c': if self.NetsrpmsArg == '-c':
self.SRPMSAskAndInstall() self.SRPMSAskAndInstall()
else: else:
lista = [] self.lista = []
NetsrpmsArg = '-c' self.NetsrpmsArg = '-c'
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000) self.checkSRPMSUpdatesTimer.start((self.nOre * 60) * 60000)
# run for each netsrpm at the end of SRPMUpdateProcess # run for each netsrpm at the end of SRPMUpdateProcess
def SRPMSCheckUpgrade(self, status): def SRPMSCheckUpgrade(self, status):
global contatore, lista
if status == 1: if status == 1:
lista.append(srpm[contatore]) self.lista.append(self.srpm[self.contatore])
contatore += 1 self.contatore += 1
self.SRPMSCheckUpgradeList() self.SRPMSCheckUpgradeList()
# netsrpm updates ask and install # netsrpm updates ask and install
def SRPMSAskAndInstall(self): def SRPMSAskAndInstall(self):
global NetsrpmsArg, lista if not self.lista == []:
if not lista == []: self.Lista = self.PrintLista()
Lista = self.Lista()
msg = QMessageBox.question(self.menu, msg = QMessageBox.question(self.menu,
(_("Not Open Source {} components").format(distro.name())), (_("Not Open Source {} components").format(distro.name())),
(_("The following components are available for update: ") + (_("The following components are available for update: ") +
"\n\n" + Lista + "\n\n" + _("Ok to proceed?")), "\n\n" + self.Lista + "\n\n" + _("Ok to proceed?")),
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if msg == QMessageBox.Yes: if msg == QMessageBox.Yes:
NetsrpmsArg = '-u' self.NetsrpmsArg = '-u'
self.SRPMSCheckUpgradeList() self.SRPMSCheckUpgradeList()
else: else:
lista = [] self.lista = []
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000) self.checkSRPMSUpdatesTimer.start((self.nOre * 60) * 60000)
else: else:
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000) self.checkSRPMSUpdatesTimer.start((self.nOre * 60) * 60000)
# starting from the list "lista" returns the list in readable format # starting from the list "lista" returns the list in readable format
# FIXME: make this code more clear and clean # FIXME: make this code more clear and clean
def Lista(self): def PrintLista(self):
testo = '' testo = ''
for a in lista: for a in self.lista:
testo = testo + a + ', ' testo = testo + a + ', '
return testo[0:len(testo)-2] return testo[0:len(testo)-2]
if __name__ == '__main__': if __name__ == '__main__':
app = QApplication(sys.argv) app = QApplication(sys.argv)
app.setDesktopFileName("mambatray")
if app.isSessionRestored(): if app.isSessionRestored():
app.exit(1) app.exit(1)
else: else: