license-dialog
3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/python3
# openmamba license-dialog
# Copyright (C) 2011 by michiamophil
# Copyright (C) 2011-2020 by Silvan Calarco
# Distributed under the terms of the GPL version 3 FLOSS License
#--Uscite possibili:
#---: 0 -> licenza accettata
#---: 1 -> licenza rifiutata
#---: 2 -> percorso licenza sbagliato o non presente o se l'argomento digitato è -h o --help
import os
import sys
import gettext
from PyQt5.QtWidgets import QDesktopWidget, QWidget, QApplication, QToolTip, QPushButton,\
QTextEdit, QGridLayout
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import pyqtSignal
# Decisione presa in run-time
imgAccetto = "object-select-symbolic"
imgRifiuto = "window-close-symbolic"
imgForm = "document"
if (os.getenv('DESKTOP_SESSION')):
desktop_session = os.getenv('DESKTOP_SESSION') # Codice preso da mambatray
if desktop_session == 'default' or desktop_session[:3] == 'kde': # Scelgo le icone di kde
imgAccetto = "dialog-ok-apply"
imgRifiuto = "dialog-close"
imgForm = "text-rtf"
def usage():
print(_("Usage: license-dialog /license/path"))
print(_("License-dialog is a simple PyQt5 based license accept/refuse dialog"))
gettext.install('license-dialog', '/usr/share/locale')
try:
path = sys.argv[1]
except:
# si verifica se non si fornisce alcun argomento
usage()
print(_("Error: path not defined"))
sys.exit(2)
# controlla se la licenza esiste e la assegna a txt
if path == "-h" or path == "--help":
usage()
sys.exit(2)
elif not os.path.exists(path):
# esce con 2 se è una cartella
usage()
print(_("Error: Wrong path"))
sys.exit(2)
elif os.path.isdir(path):
usage()
print(_("Error: path cannot be a directory"))
sys.exit(2)
licenza = open(path, "r")
txt = licenza.read()
licenza.close()
#centra il form sullo schermo
def center(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)
class Form(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setWindowTitle(_("Licenze"))
self.setWindowIcon(QIcon.fromTheme(imgForm))
self.resize(500, 400)
center(self)
QToolTip.setFont(QFont('sans', 10))
btnAccetto = QPushButton(QIcon.fromTheme(imgAccetto), _("I agree"))
btnAccetto.setToolTip(_("Click here if you want to accept the license"))
btnAccetto.clicked.connect(self.evtAccetto)
btnRifiuto = QPushButton(QIcon.fromTheme(imgRifiuto), _("I do not agree"))
btnRifiuto.setToolTip(_("Click here if you do <b>not</b> want to accept the license"))
btnRifiuto.clicked.connect(self.evtRifiuto)
licenza = QTextEdit()
licenza.setReadOnly(True)
licenza.setPlainText(txt)
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget(licenza, 1, 0, 1, 2)
grid.addWidget(btnRifiuto, 2, 0)
grid.addWidget(btnAccetto, 2, 1)
self.setLayout(grid)
# annulla la chiusura con la x del window manager
def closeEvent(form, event):
event.ignore()
def evtAccetto(self):
sys.exit(0)
def evtRifiuto(self):
#mostra un messaggio:
#msg = QtGui.QMessageBox.question(form, _("Message"), _("Are you sure to refuse?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
#if msg == QtGui.QMessageBox.Yes:
sys.exit(1)
#crea la finestra di dialogo
app = QApplication(sys.argv)
form = Form()
form.show()
sys.exit(app.exec_())