mambatray/mambatray.py

251 lines
10 KiB
Python
Raw Normal View History

#!/usr/bin/python3
# mambatray 2
# Copyright (C) 2011-2024 by Silvan Calarco
2011-06-30 06:25:36 +02:00
# Copyright (C) 2011 by michiamophil
2011-04-26 21:25:03 +02:00
# Distributed under the terms of the GPL version 3 FLOSS License
#
import sys
2011-07-01 17:57:47 +02:00
import os
2011-04-26 21:25:03 +02:00
import gettext
import distro
from PyQt5.QtCore import Qt
2019-04-02 19:51:25 +02:00
from PyQt5.QtGui import QIcon, QCursor
from PyQt5.QtWidgets import QSystemTrayIcon, QApplication, QMenu, QMessageBox
2019-04-02 19:51:25 +02:00
from PyQt5.QtCore import QTimer, QProcess
2011-04-26 21:25:03 +02:00
DATADIR = os.path.dirname(os.path.realpath((__file__))) + "/"
2019-04-02 19:51:25 +02:00
class SystemTrayIcon(QSystemTrayIcon):
2011-04-26 21:25:03 +02:00
menu = None
2011-04-26 21:25:03 +02:00
def __init__(self, icon, parent=None):
2019-04-02 19:51:25 +02:00
QSystemTrayIcon.__init__(self, icon, parent)
global w, contatore, srpm, lista, NetsrpmsArg, nOre, desktop_session
2019-03-31 19:43:46 +02:00
nOre = 2
2019-04-02 19:51:25 +02:00
srpm = [
"skype", "win32codecs", "msttcorefonts", "b43-firmware",
"virtualbox-extension-pack", "chromium-widevine", "spotify"
2019-04-02 19:51:25 +02:00
]
2019-03-31 19:43:46 +02:00
NetsrpmsArg = "-c"
lista = []
contatore = 0
2011-04-26 21:25:03 +02:00
w = parent
self.menu = QMenu(parent)
2019-03-31 19:43:46 +02:00
try:
desktop_session = os.path.basename(os.getenv('DESKTOP_SESSION'))
except:
desktop_session = 'default'
if desktop_session == 'default':
2019-03-31 19:43:46 +02:00
desktop_session = 'plasma'
elif desktop_session[:3] == 'kde':
2019-03-31 19:43:46 +02:00
desktop_session = 'kde'
# refresh packages cache timer
2019-04-02 19:51:25 +02:00
global refreshPackagesTimer, refreshPackagesCacheProcess,\
checkSRPMSUpdatesTimer, SRPMUpdateProcess, networkOnlineProcess
refreshPackagesTimer = QTimer(self)
checkSRPMSUpdatesTimer = QTimer(self)
refreshPackagesCacheProcess = QProcess(self)
SRPMUpdateProcess = QProcess(self)
networkOnlineProcess = QProcess(self)
refreshPackagesTimer.timeout.connect(self.refreshPackagesCache)
refreshPackagesCacheProcess.finished.connect(self.restartRefreshPackagesTimer)
2019-03-31 19:43:46 +02:00
checkSRPMSUpdatesTimer.timeout.connect(self.SRPMSCheckUpgradeList)
2011-04-26 21:25:03 +02:00
# software menu
2019-04-02 19:51:25 +02:00
global softwareManagerProcess, softwareCheckUpgradesProcess,\
softwareBaseInstallsProcess
2019-04-02 19:51:25 +02:00
softwareManagerProcess = QProcess(self)
softwareCheckUpgradesProcess = QProcess(self)
softwareBaseInstallsProcess = QProcess(self)
2011-04-26 21:25:03 +02:00
softwareMenu = self.menu.addMenu(QIcon.fromTheme("applications-system"),_("Software"))
softwareManageAction = softwareMenu.addAction(QIcon.fromTheme("applications-other"),_("Add/Remove software packages..."))
softwareManageAction.triggered.connect(self.softwareManager)
2011-04-26 21:25:03 +02:00
softwareCheckUpgradesAction = softwareMenu.addAction(QIcon.fromTheme("system-software-update"),_("Check for updates now..."))
softwareCheckUpgradesAction.triggered.connect(self.softwareCheckUpgrades)
2011-04-26 21:25:03 +02:00
softwareBaseInstallsAction = softwareMenu.addAction(QIcon.fromTheme("mambabase"),_("{} components...").format(distro.name()))
softwareBaseInstallsAction.triggered.connect(self.softwareBaseInstalls)
2019-03-31 19:43:46 +02:00
2011-04-26 21:25:03 +02:00
# network menu
2019-04-02 19:51:25 +02:00
global networkFirewallDisableProcess, networkFirewallEnableProcess,\
networkFirewallConfigureProcess, networkProxyProcess
networkFirewallDisableProcess = QProcess(self)
networkFirewallEnableProcess = QProcess(self)
networkFirewallConfigureProcess = QProcess(self)
networkProxyProcess = QProcess(self)
networkMenu = self.menu.addMenu(QIcon.fromTheme("preferences-system-network"),
2019-04-02 19:51:25 +02:00
_("Network"))
networkFirewallMenu = networkMenu.addMenu(QIcon.fromTheme("security-medium"),_("Firewall"))
networkFirewallDisableAction = networkFirewallMenu.addAction(QIcon.fromTheme("security-low"),_("Disable"))
networkFirewallDisableAction.triggered.connect(self.networkFirewallDisable)
networkFirewallDisableProcess.finished.connect(self.networkFirewallDisableMessage)
networkFirewallEnableAction = networkFirewallMenu.addAction(QIcon.fromTheme("security-high"),_("Enable"))
networkFirewallEnableAction.triggered.connect(self.networkFirewallEnable)
networkFirewallEnableProcess.finished.connect(self.networkFirewallEnableMessage)
networkFirewallConfigureAction = networkFirewallMenu.addAction(QIcon.fromTheme("security-medium"),_("Configure..."))
networkFirewallConfigureAction.triggered.connect(self.networkFirewallConfigure)
networkProxyAction = networkMenu.addAction(QIcon.fromTheme("network-server"),_("Proxy..."))
networkProxyAction.triggered.connect(self.networkProxy)
SRPMUpdateProcess.finished.connect(self.SRPMSCheckUpgrade)
2011-06-30 06:25:36 +02:00
2011-04-26 21:25:03 +02:00
# about box
aboutAction = self.menu.addSeparator()
aboutAction = self.menu.addAction(QIcon.fromTheme("system-logo"),_("About"))
aboutAction.triggered.connect(self.about)
2019-03-31 19:43:46 +02:00
self.setToolTip(_("{} control center").format(distro.name()))
2011-04-26 21:25:03 +02:00
self.activated.connect(self.iconActivated)
self.setContextMenu(self.menu)
2019-04-02 19:51:25 +02:00
self.show()
# the first time refresh packages cache after 5 minutes
refreshPackagesTimer.start( 5 * 60000 )
# the first time check for SRPMS updates after 4 minutes
2019-03-31 19:43:46 +02:00
checkSRPMSUpdatesTimer.start( 4 * 60000 )
2011-04-26 21:25:03 +02:00
def iconActivated(self, reason):
2019-04-02 19:51:25 +02:00
if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.DoubleClick, QSystemTrayIcon.Context):
self.contextMenu().popup(QCursor.pos())
2011-04-26 21:25:03 +02:00
def about(self):
aboutBox = QMessageBox(self.menu)
aboutBox.setWindowIcon(QIcon.fromTheme("mambatray"))
aboutBox.setWindowTitle(_("About"))
aboutBox.setText(
"<center><b>"+_("{} control center").format(distro.name()+"</b>"
"<br/><br/>"+distro.name(pretty=True)+
"<br/><br/><a href=\""+distro.os_release_info()['home_url']+
"\">" + distro.os_release_info()['home_url'] + "</a>"
"</center>"))
aboutBox.setTextFormat(Qt.RichText)
aboutBox.setIconPixmap(QIcon.fromTheme("system-logo").pixmap(32));
aboutBox.setWindowModality(Qt.NonModal);
aboutBox.exec()
2011-04-26 21:25:03 +02:00
def networkOnline(self):
2019-03-31 19:43:46 +02:00
return networkOnlineProcess.execute("/usr/bin/nm-online",['-q','-x'])
2011-04-26 21:25:03 +02:00
def softwareManager(self):
softwareManagerProcess.execute("/usr/bin/plasma-discover")
2011-04-26 21:25:03 +02:00
def softwareCheckUpgrades(self):
softwareCheckUpgradesProcess.start("/usr/bin/pkcon", ['refresh', 'force'])
def refreshPackagesCache(self):
refreshPackagesTimer.stop()
refreshPackagesCacheProcess.start("/usr/bin/pkcon", ['refresh', 'force'])
def restartRefreshPackagesTimer(self,status):
# check every 4 hours and 31 minutes */
refreshPackagesTimer.start( (4 * 60 + 31) * 60000)
2011-04-26 21:25:03 +02:00
def softwareBaseInstalls(self):
softwareBaseInstallsProcess.start("/usr/bin/mambabase")
2011-04-26 21:25:03 +02:00
def networkFirewallDisable(self):
networkFirewallDisableProcess.start("sudo",['/usr/sbin/ufw','disable'])
2011-04-26 21:25:03 +02:00
def networkFirewallDisableMessage(self,status):
2019-04-02 19:51:25 +02:00
if (status == QProcess.NormalExit):
2011-04-26 21:25:03 +02:00
self.showMessage("Firewall", _("Firewall disabled in current session"))
else:
self.showMessage("Firewall", _("Could not disable firewall"))
2011-04-26 21:25:03 +02:00
def networkFirewallEnable(self):
networkFirewallEnableProcess.start("sudo",['/usr/sbin/ufw','enable'])
2011-04-26 21:25:03 +02:00
def networkFirewallEnableMessage(self,status):
2019-04-02 19:51:25 +02:00
if (status == QProcess.NormalExit):
2011-04-26 21:25:03 +02:00
self.showMessage("Firewall", _("Firewall enabled in current session"))
else:
self.showMessage("Firewall", _("Could not enable firewall"))
2011-04-26 21:25:03 +02:00
def networkFirewallConfigure(self):
if desktop_session == 'kde' or desktop_session.startswith('plasma'):
networkFirewallConfigureProcess.execute("/usr/bin/kcmshell5", list(set(['firewall'])))
2019-03-31 19:43:46 +02:00
else:
networkFirewallConfigureProcess.execute("/usr/bin/gufw")
2011-04-26 21:25:03 +02:00
def networkProxy(self):
if desktop_session.startswith('plasma'):
2019-03-31 19:43:46 +02:00
networkProxyProcess.execute("/usr/bin/kcmshell5", list(set(['proxy'])))
elif desktop_session == 'kde':
networkProxyProcess.execute("/opt/kde/bin/kcmshell4", list(set(['proxy'])))
elif desktop_session == 'gnome':
networkProxyProcess.execute("/usr/bin/gnome-control-center", "network")
2011-04-26 21:25:03 +02:00
2011-06-30 06:25:36 +02:00
def SRPMSCheckUpgradeList(self):
2019-03-31 19:43:46 +02:00
checkSRPMSUpdatesTimer.stop()
global contatore, lista, NetsrpmsArg
if (self.networkOnline() == 0):
try:
SRPMUpdateProcess.start("/usr/bin/openmamba-netsrpms",[NetsrpmsArg,srpm[contatore]])
except:
2019-03-31 19:43:46 +02:00
contatore = 0
if NetsrpmsArg == '-c':
self.SRPMSAskAndInstall()
else:
lista = []
NetsrpmsArg = '-c'
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000)
# run for each netsrpm at the end of SRPMUpdateProcess
2011-06-30 06:25:36 +02:00
def SRPMSCheckUpgrade(self, status):
2019-03-31 19:43:46 +02:00
global contatore, lista
if status == 1:
lista.append(srpm[contatore])
contatore += 1
self.SRPMSCheckUpgradeList()
2011-06-30 06:25:36 +02:00
# netsrpm updates ask and install
2011-06-30 06:25:36 +02:00
def SRPMSAskAndInstall(self):
2019-03-31 19:43:46 +02:00
global NetsrpmsArg, lista
if not lista == []:
Lista = self.Lista()
msg = QMessageBox.question(self.menu,
(_("Not Open Source {} components").format(distro.name())),
(_("The following components are available for update: ") +
"\n\n" + Lista + "\n\n" + _("Ok to proceed?")),
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if msg == QMessageBox.Yes:
2019-03-31 19:43:46 +02:00
NetsrpmsArg = '-u'
self.SRPMSCheckUpgradeList()
else:
lista = []
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000)
else:
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000)
# starting from the list "lista" returns the list in readable format
# FIXME: make this code more clear and clean
2011-06-30 06:25:36 +02:00
def Lista(self):
2019-03-31 19:43:46 +02:00
testo = ''
for a in lista:
testo = testo + a + ', '
2019-03-31 19:43:46 +02:00
return testo[0:len(testo)-2]
2011-06-30 06:25:36 +02:00
2011-04-26 21:25:03 +02:00
def main():
app = QApplication(sys.argv)
2019-04-02 19:51:25 +02:00
if app.isSessionRestored():
app.exit(1)
else:
app.setQuitOnLastWindowClosed(0)
2019-03-31 19:43:46 +02:00
gettext.install('mambatray', '/usr/share/locale')
trayIcon = SystemTrayIcon(QIcon.fromTheme("mambatray")
2019-04-02 19:51:25 +02:00
)
sys.exit(app.exec_())
2011-04-26 21:25:03 +02:00
2011-04-26 21:25:03 +02:00
if __name__ == '__main__':
main()