Basic PyQt5 + Python 3.6 support

This commit is contained in:
Silvan Calarco 2019-03-31 19:43:46 +02:00
parent 2038c0f763
commit c3e25efaef

View File

@ -1,6 +1,6 @@
#!/usr/bin/python #!/usr/bin/python3.6
# mambatray 2 # mambatray 2
# Copyright (C) 2011-2018 by Silvan Calarco # Copyright (C) 2011-2019 by Silvan Calarco
# Copyright (C) 2011 by michiamophil # Copyright (C) 2011 by michiamophil
# Distributed under the terms of the GPL version 3 FLOSS License # Distributed under the terms of the GPL version 3 FLOSS License
# #
@ -17,32 +17,35 @@ class SystemTrayIcon(QtWidgets.QSystemTrayIcon):
def __init__(self, icon, parent=None): def __init__(self, icon, parent=None):
global w, menu, contatore, srpm, lista, NetsrpmsArg, nOre, desktop_session global w, menu, contatore, srpm, lista, NetsrpmsArg, nOre, desktop_session
nOre = 2 nOre = 2
srpm = ["skype", "flashplugin", "win32codecs", "msttcorefonts", "b43-firmware", "sun-java7", "sun-java8", "virtualbox-extension-pack", "chromium-pepper-flash", "spotify"] srpm = ["skype", "flashplugin", "win32codecs", "msttcorefonts", "b43-firmware", "sun-java7", "sun-java8", "virtualbox-extension-pack", "chromium-pepper-flash", "spotify"]
NetsrpmsArg = "-c" NetsrpmsArg = "-c"
lista = [] lista = []
contatore = 0 contatore = 0
w = parent w = parent
QtWidgets.QSystemTrayIcon.__init__(self, icon, parent) QtWidgets.QSystemTrayIcon.__init__(self, icon, parent)
menu = QtWidgets.QMenu(parent) menu = QtWidgets.QMenu(parent)
desktop_session = os.path.basename(os.getenv('DESKTOP_SESSION')) try:
desktop_session = os.path.basename(os.getenv('DESKTOP_SESSION'))
except:
desktop_session = 'default'
if desktop_session == 'default': if desktop_session == 'default':
desktop_session = 'plasma' desktop_session = 'plasma'
elif desktop_session[:3] == 'kde': elif desktop_session[:3] == 'kde':
desktop_session = 'kde' desktop_session = 'kde'
# refresh packages cache timer # refresh packages cache timer
global refreshPackagesTimer, refreshPackagesCacheProcess, checkSRPMSUpdatesTimer, SRPMUpdateProcess, networkOnlineProcess global refreshPackagesTimer, refreshPackagesCacheProcess, checkSRPMSUpdatesTimer, SRPMUpdateProcess, networkOnlineProcess
refreshPackagesTimer = QtCore.QTimer(self) refreshPackagesTimer = QtCore.QTimer(self)
checkSRPMSUpdatesTimer = QtCore.QTimer(self) checkSRPMSUpdatesTimer = QtCore.QTimer(self)
refreshPackagesCacheProcess = QtCore.QProcess(self) refreshPackagesCacheProcess = QtCore.QProcess(self)
SRPMUpdateProcess = QtCore.QProcess(self) SRPMUpdateProcess = QtCore.QProcess(self)
networkOnlineProcess = QtCore.QProcess(self) networkOnlineProcess = QtCore.QProcess(self)
refreshPackagesTimer.timeout.connect(self.refreshPackagesCache) refreshPackagesTimer.timeout.connect(self.refreshPackagesCache)
refreshPackagesCacheProcess.finished.connect(self.restartRefreshPackagesTimer) refreshPackagesCacheProcess.finished.connect(self.restartRefreshPackagesTimer)
checkSRPMSUpdatesTimer.timeout.connect(self.SRPMSCheckUpgradeList) checkSRPMSUpdatesTimer.timeout.connect(self.SRPMSCheckUpgradeList)
# software menu # software menu
global softwareManagerProcess, softwareCheckUpgradesProcess, softwareBaseInstallsProcess, dcopServerProcess global softwareManagerProcess, softwareCheckUpgradesProcess, softwareBaseInstallsProcess, dcopServerProcess
@ -95,76 +98,76 @@ class SystemTrayIcon(QtWidgets.QSystemTrayIcon):
# the first time refresh packages cache after 5 minutes # the first time refresh packages cache after 5 minutes
refreshPackagesTimer.start( 5 * 60000 ) 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 ) checkSRPMSUpdatesTimer.start( 4 * 60000 )
def setProxyForSmart(self): def setProxyForSmart(self):
setProxyProcess = QtCore.QProcess(self) setProxyProcess = QtCore.QProcess(self)
pf = libproxy.ProxyFactory() pf = libproxy.ProxyFactory()
for protocol in ["http","https","ftp"]: for protocol in ["http","https","ftp"]:
proxies = pf.getProxies("%s://openmamba.org" % protocol) proxies = pf.getProxies("%s://openmamba.org" % protocol)
for proxy in proxies: for proxy in proxies:
if proxy == "direct://": if proxy == "direct://":
setProxyProcess.execute("smart",['config','--remove','%s_proxy' % protocol]) setProxyProcess.execute("smart",['config','--remove','%s_proxy' % protocol])
break break
else: else:
setProxyProcess.execute("smart",['config','--set','%s_proxy=%s' %(protocol,proxy)]) setProxyProcess.execute("smart",['config','--set','%s_proxy=%s' %(protocol,proxy)])
break break
def iconActivated(self, reason): def iconActivated(self, reason):
if reason in (QtWidgets.QSystemTrayIcon.Trigger, QtWidgets.QSystemTrayIcon.DoubleClick): if reason in (QtWidgets.QSystemTrayIcon.Trigger, QtWidgets.QSystemTrayIcon.DoubleClick):
menu.popup(self.geometry().center()) menu.popup(self.geometry().center())
def about(self): def about(self):
global version global version
path = '/etc/openmamba-release' path = '/etc/openmamba-release'
if os.path.exists(path): if os.path.exists(path):
v = open(path, 'r') v = open(path, 'r')
version = v.read() version = v.read()
a = self.FormattaInfo() a = self.FormattaInfo()
version = "<ul><li>"+_("<font color=\"green\">open</font>mamba release:")+" <i>"+a[0]+"</i><li>"+_("Platform: ")+"<i>"+a[1]+"</i></ul>" version = "<ul><li>"+_("<font color=\"green\">open</font>mamba release:")+" <i>"+a[0]+"</i><li>"+_("Platform: ")+"<i>"+a[1]+"</i></ul>"
v.close() v.close()
else: else:
version = _("Error: Unable to find /etc/openmamba-release") version = _("Error: Unable to find /etc/openmamba-release")
QtWidgets.QMessageBox.about(menu, _("About"), QtWidgets.QMessageBox.about(menu, _("About"),
"<center><b>"+_("openmamba control center")+"</b>" "<center><b>"+_("openmamba control center")+"</b>"
"<br><br>Copyright(c) 2011-2018 by Silvan Calarco" "<br><br>Copyright(c) 2011-2019 by Silvan Calarco"
"<br>Copyright(c) 2011 by michiamophil" "<br>Copyright(c) 2011 by michiamophil"
"<br><br><b>"+_("System info:")+"</b></center>" "<br><br><b>"+_("System info:")+"</b></center>"
""+_(version)+"<center><a href=\"http://openmamba.org\">http://openmamba.org</a></center>") ""+_(version)+"<center><a href=\"http://openmamba.org\">http://openmamba.org</a></center>")
def FormattaInfo(self): def FormattaInfo(self):
result = [] result = []
v = version v = version
#result[0]=versione #result[0]=versione
result.append(v[ v.find("openmamba release ")+18 : v.find(" for") ] + " " + v[ v.find("(") : v.find(")")+1 ]) result.append(v[ v.find("openmamba release ")+18 : v.find(" for") ] + " " + v[ v.find("(") : v.find(")")+1 ])
#result[1] = piattaforma #result[1] = piattaforma
result.append(v[ v.find("for")+4 : v.find("(") ]) result.append(v[ v.find("for")+4 : v.find("(") ])
return result return result
def networkOnline(self): def networkOnline(self):
return networkOnlineProcess.execute("/usr/bin/nm-online",['-q','-x']) return networkOnlineProcess.execute("/usr/bin/nm-online",['-q','-x'])
def softwareManager(self): def softwareManager(self):
self.setProxyForSmart() self.setProxyForSmart()
if desktop_session == 'plasma': if desktop_session == 'plasma':
softwareManagerProcess.execute("/usr/bin/plasma-discover", ['--mode', 'Browsing']) softwareManagerProcess.execute("/usr/bin/plasma-discover", ['--mode', 'Browsing'])
elif desktop_session == 'kde': elif desktop_session == 'kde':
softwareManagerProcess.execute("/opt/kde/bin/apper") softwareManagerProcess.execute("/opt/kde/bin/apper")
else: else:
softwareManagerProcess.execute("/usr/bin/gpk-application") softwareManagerProcess.execute("/usr/bin/gpk-application")
def softwareCheckUpgrades(self): def softwareCheckUpgrades(self):
self.setProxyForSmart() self.setProxyForSmart()
if desktop_session == 'plasma': if desktop_session == 'plasma':
softwareCheckUpgradesProcess.start("/usr/bin/plasma-discover", ['--mode', 'Update']) softwareCheckUpgradesProcess.start("/usr/bin/plasma-discover", ['--mode', 'Update'])
elif desktop_session == 'kde': elif desktop_session == 'kde':
softwareCheckUpgradesProcess.start("/opt/kde/bin/apper", ['--updates']) softwareCheckUpgradesProcess.start("/opt/kde/bin/apper", ['--updates'])
else: else:
softwareCheckUpgradesProcess.start("/usr/bin/gpk-update-viewer") softwareCheckUpgradesProcess.start("/usr/bin/gpk-update-viewer")
def refreshPackagesCache(self): def refreshPackagesCache(self):
self.setProxyForSmart() self.setProxyForSmart()
refreshPackagesTimer.stop() refreshPackagesTimer.stop()
refreshPackagesCacheProcess.start("/usr/bin/pkcon", ['refresh']) refreshPackagesCacheProcess.start("/usr/bin/pkcon", ['refresh'])
@ -195,66 +198,64 @@ class SystemTrayIcon(QtWidgets.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 == 'plasma': if desktop_session == 'kde' or desktop_session == 'plasma':
networkFirewallConfigureProcess.execute("/opt/kde/bin/kcmshell4", list(set(['ufw']))) networkFirewallConfigureProcess.execute("/opt/kde/bin/kcmshell4", list(set(['ufw'])))
else: else:
networkFirewallConfigureProcess.execute("/usr/bin/gufw") networkFirewallConfigureProcess.execute("/usr/bin/gufw")
def networkProxy(self): def networkProxy(self):
if desktop_session == 'plasma': if desktop_session == 'plasma':
networkProxyProcess.execute("/usr/bin/kcmshell5", list(set(['proxy']))) networkProxyProcess.execute("/usr/bin/kcmshell5", list(set(['proxy'])))
elif desktop_session == 'kde': elif desktop_session == 'kde':
networkProxyProcess.execute("/opt/kde/bin/kcmshell4", list(set(['proxy']))) networkProxyProcess.execute("/opt/kde/bin/kcmshell4", list(set(['proxy'])))
elif desktop_session == 'gnome': elif desktop_session == 'gnome':
networkProxyProcess.execute("/usr/bin/gnome-control-center", "network") networkProxyProcess.execute("/usr/bin/gnome-control-center", "network")
self.setProxyForSmart() self.setProxyForSmart()
def SRPMSCheckUpgradeList(self): def SRPMSCheckUpgradeList(self):
checkSRPMSUpdatesTimer.stop() checkSRPMSUpdatesTimer.stop()
global contatore, lista, NetsrpmsArg global contatore, lista, NetsrpmsArg
if (self.networkOnline() == 0): if (self.networkOnline() == 0):
try: try:
SRPMUpdateProcess.start("/usr/bin/openmamba-netsrpms",[NetsrpmsArg,srpm[contatore]]) SRPMUpdateProcess.start("/usr/bin/openmamba-netsrpms",[NetsrpmsArg,srpm[contatore]])
except: #supera il limite dell'indice nella lista except: #supera il limite dell'indice nella lista
contatore = 0 contatore = 0
if NetsrpmsArg == '-c': if NetsrpmsArg == '-c':
self.SRPMSAskAndInstall() self.SRPMSAskAndInstall()
else: else:
lista = [] lista = []
NetsrpmsArg = '-c' NetsrpmsArg = '-c'
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000) checkSRPMSUpdatesTimer.start((nOre * 60) * 60000)
#viene eseguita per ogni SRPM al termine del processo SRPMUpdateProcess #viene eseguita per ogni SRPM al termine del processo SRPMUpdateProcess
def SRPMSCheckUpgrade(self, status): def SRPMSCheckUpgrade(self, status):
global contatore, lista global contatore, lista
if status == 1: if status == 1:
lista.append(srpm[contatore]) lista.append(srpm[contatore])
contatore += 1 contatore += 1
self.SRPMSCheckUpgradeList() self.SRPMSCheckUpgradeList()
#viene eseguita una volta terminata la fase di controllo degli aggiornamenti #viene eseguita una volta terminata la fase di controllo degli aggiornamenti
def SRPMSAskAndInstall(self): def SRPMSAskAndInstall(self):
global NetsrpmsArg, lista global NetsrpmsArg, lista
if not lista == []: if not lista == []:
Lista = self.Lista() Lista = self.Lista()
msg = QtGui.QMessageBox.question(menu, (_("Not Open Source openmamba components")), (_("The following components are available for update: ") + "\n\n" + Lista + "\n\n" + _("Ok to proceed?")), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.Yes) msg = QtGui.QMessageBox.question(menu, (_("Not Open Source openmamba components")), (_("The following components are available for update: ") + "\n\n" + Lista + "\n\n" + _("Ok to proceed?")), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.Yes)
if msg == QtGui.QMessageBox.Yes: if msg == QtGui.QMessageBox.Yes:
NetsrpmsArg = '-u' NetsrpmsArg = '-u'
self.SRPMSCheckUpgradeList() self.SRPMSCheckUpgradeList()
else: else:
lista = [] lista = []
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000) checkSRPMSUpdatesTimer.start((nOre * 60) * 60000)
else: else:
checkSRPMSUpdatesTimer.start((nOre * 60) * 60000) checkSRPMSUpdatesTimer.start((nOre * 60) * 60000)
#partendo dalla lista lista (scusate il gioco di parole :) ) restituisce la lista in modo 'leggibile' #partendo dalla lista lista (scusate il gioco di parole :) ) restituisce la lista in modo 'leggibile'
def Lista(self): def Lista(self):
testo = '' testo = ''
for a in lista: for a in lista:
testo = testo + a + ', ' testo = testo + a + ', '
return testo[0:len(testo)-2] return testo[0:len(testo)-2]
def main(): def main():
app = QtWidgets.QApplication(sys.argv) app = QtWidgets.QApplication(sys.argv)
@ -262,7 +263,7 @@ def main():
app.exit(1) app.exit(1)
else: else:
app.setQuitOnLastWindowClosed(0) app.setQuitOnLastWindowClosed(0)
gettext.install('mambatray', '/usr/share/locale', unicode=1) gettext.install('mambatray', '/usr/share/locale')
w = QtWidgets.QWidget() w = QtWidgets.QWidget()
w.setWindowIcon(QtGui.QIcon.fromTheme("mambatray",QtGui.QIcon("/usr/share/icons/hicolor/64x64/apps/mambatray.png"))) w.setWindowIcon(QtGui.QIcon.fromTheme("mambatray",QtGui.QIcon("/usr/share/icons/hicolor/64x64/apps/mambatray.png")))
trayIcon = SystemTrayIcon(QtGui.QIcon.fromTheme("mambatray", QtGui.QIcon("/usr/share/icons/hicolor/32x32/apps/mambatray.png")), w) trayIcon = SystemTrayIcon(QtGui.QIcon.fromTheme("mambatray", QtGui.QIcon("/usr/share/icons/hicolor/32x32/apps/mambatray.png")), w)