mambatray/mambatray.py

268 lines
11 KiB
Python
Executable File

#!/usr/bin/python3
# mambatray 2
# Copyright (C) 2011-2022 by Silvan Calarco
# Copyright (C) 2011 by michiamophil
# Distributed under the terms of the GPL version 3 FLOSS License
#
import sys
import os
import gettext
import libproxy
import distro
from PyQt5 import uic
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QIcon, QCursor
from PyQt5.QtWidgets import QSystemTrayIcon, QApplication, QMenu, QMainWindow,\
QMessageBox, QWidget
from PyQt5.QtCore import QTimer, QProcess
DATADIR = os.path.dirname(os.path.realpath((__file__))) + "/"
class SystemTrayIcon(QSystemTrayIcon):
menu = None
def __init__(self, icon, parent=None):
QSystemTrayIcon.__init__(self, icon, parent)
global w, contatore, srpm, lista, NetsrpmsArg, nOre, desktop_session
nOre = 2
srpm = [
"skype", "win32codecs", "msttcorefonts", "b43-firmware",
"virtualbox-extension-pack", "chromium-widevine", "spotify"
]
NetsrpmsArg = "-c"
lista = []
contatore = 0
w = parent
self.menu = QMenu(parent)
try:
desktop_session = os.path.basename(os.getenv('DESKTOP_SESSION'))
except:
desktop_session = 'default'
if desktop_session == 'default':
desktop_session = 'plasma'
elif desktop_session[:3] == 'kde':
desktop_session = 'kde'
# refresh packages cache timer
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)
checkSRPMSUpdatesTimer.timeout.connect(self.SRPMSCheckUpgradeList)
# software menu
global softwareManagerProcess, softwareCheckUpgradesProcess,\
softwareBaseInstallsProcess
softwareManagerProcess = QProcess(self)
softwareCheckUpgradesProcess = QProcess(self)
softwareBaseInstallsProcess = QProcess(self)
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)
softwareCheckUpgradesAction = softwareMenu.addAction(QIcon.fromTheme("system-software-update"),_("Check for updates now..."))
softwareCheckUpgradesAction.triggered.connect(self.softwareCheckUpgrades)
softwareBaseInstallsAction = softwareMenu.addAction(QIcon.fromTheme("mambabase"),_("{} components...").format(distro.name()))
softwareBaseInstallsAction.triggered.connect(self.softwareBaseInstalls)
# network menu
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"),
_("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)
# about box
aboutAction = self.menu.addSeparator()
aboutAction = self.menu.addAction(QIcon.fromTheme("system-logo"),_("About"))
aboutAction.triggered.connect(self.about)
self.setToolTip(_("{} control center").format(distro.name()))
self.activated.connect(self.iconActivated)
self.setContextMenu(self.menu)
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
checkSRPMSUpdatesTimer.start( 4 * 60000 )
def setProxyForSmart(self):
setProxyProcess = QProcess(self)
pf = libproxy.ProxyFactory()
for protocol in ["http","https","ftp"]:
proxies = pf.getProxies("%s://openmamba.org" % protocol)
for proxy in proxies:
if proxy == "direct://":
setProxyProcess.execute("smart",['config','--remove','%s_proxy' % protocol])
break
else:
setProxyProcess.execute("smart",['config','--set','%s_proxy=%s' %(protocol,proxy)])
break
def iconActivated(self, reason):
if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.DoubleClick, QSystemTrayIcon.Context):
self.contextMenu().popup(QCursor.pos())
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()
def networkOnline(self):
return networkOnlineProcess.execute("/usr/bin/nm-online",['-q','-x'])
def softwareManager(self):
#self.setProxyForSmart()
softwareManagerProcess.execute("/usr/bin/plasma-discover")
def softwareCheckUpgrades(self):
#self.setProxyForSmart()
softwareCheckUpgradesProcess.start("/usr/bin/pkcon", ['refresh', 'force'])
def refreshPackagesCache(self):
#self.setProxyForSmart()
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)
def softwareBaseInstalls(self):
softwareBaseInstallsProcess.start("/usr/bin/mambabase")
def networkFirewallDisable(self):
networkFirewallDisableProcess.start("sudo",['/usr/sbin/ufw','disable'])
def networkFirewallDisableMessage(self,status):
if (status == QProcess.NormalExit):
self.showMessage("Firewall", _("Firewall disabled in current session"))
else:
self.showMessage("Firewall", _("Could not disable firewall"))
def networkFirewallEnable(self):
networkFirewallEnableProcess.start("sudo",['/usr/sbin/ufw','enable'])
def networkFirewallEnableMessage(self,status):
if (status == QProcess.NormalExit):
self.showMessage("Firewall", _("Firewall enabled in current session"))
else:
self.showMessage("Firewall", _("Could not enable firewall"))
def networkFirewallConfigure(self):
if desktop_session == 'kde' or desktop_session == 'plasma':
networkFirewallConfigureProcess.execute("/usr/bin/kcmshell5", list(set(['firewall'])))
else:
networkFirewallConfigureProcess.execute("/usr/bin/gufw")
def networkProxy(self):
if desktop_session == 'plasma':
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")
#self.setProxyForSmart()
def SRPMSCheckUpgradeList(self):
checkSRPMSUpdatesTimer.stop()
global contatore, lista, NetsrpmsArg
if (self.networkOnline() == 0):
try:
SRPMUpdateProcess.start("/usr/bin/openmamba-netsrpms",[NetsrpmsArg,srpm[contatore]])
except: #supera il limite dell'indice nella lista
contatore = 0
if NetsrpmsArg == '-c':
self.SRPMSAskAndInstall()
else:
lista = []
NetsrpmsArg = '-c'
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000)
#viene eseguita per ogni SRPM al termine del processo SRPMUpdateProcess
def SRPMSCheckUpgrade(self, status):
global contatore, lista
if status == 1:
lista.append(srpm[contatore])
contatore += 1
self.SRPMSCheckUpgradeList()
#viene eseguita una volta terminata la fase di controllo degli aggiornamenti
def SRPMSAskAndInstall(self):
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:
NetsrpmsArg = '-u'
self.SRPMSCheckUpgradeList()
else:
lista = []
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000)
else:
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000)
#partendo dalla lista lista (scusate il gioco di parole :) ) restituisce la lista in modo 'leggibile'
def Lista(self):
testo = ''
for a in lista:
testo = testo + a + ', '
return testo[0:len(testo)-2]
def main():
app = QApplication(sys.argv)
if app.isSessionRestored():
app.exit(1)
else:
app.setQuitOnLastWindowClosed(0)
gettext.install('mambatray', '/usr/share/locale')
trayIcon = SystemTrayIcon(QIcon.fromTheme("mambatray")
)
sys.exit(app.exec_())
if __name__ == '__main__':
main()