#!/usr/bin/python3
# openmamba license-dialog
# Copyright (C) 2011 by michiamophil
# Copyright (C) 2011-2025 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 PyQt6.QtWidgets import (
    QWidget, QApplication, QToolTip, QPushButton,
    QTextEdit, QGridLayout
)
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import Qt

# Decisione presa in run-time
imgAccetto = "object-select-symbolic"
imgRifiuto = "window-close-symbolic"
imgForm = "openmamba"

if (os.getenv('DESKTOP_SESSION')):
    desktop_session = os.getenv('DESKTOP_SESSION')
    if desktop_session == 'default' or desktop_session[:3] == '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 license accept/refuse dialog"))

# Localizzazione
gettext.install('license-dialog', '/usr/share/locale')

try:
    path = sys.argv[1]
except IndexError:
    usage()
    print(_("Error: path not defined"))
    sys.exit(2)

if path == "-h" or path == "--help":
    usage()
    sys.exit(2)
elif not os.path.exists(path):
    usage()
    print(_("Error: Wrong path"))
    sys.exit(2)
elif os.path.isdir(path):
    usage()
    print(_("Error: path cannot be a directory"))
    sys.exit(2)

with open(path, "r") as licenza:
    txt = licenza.read()

# Funzione per centrare la finestra
def center(widget):
    screen = QApplication.primaryScreen().availableGeometry()
    size = widget.frameGeometry()
    widget.move(
        int((screen.width() - size.width()) / 2),
        int((screen.height() - size.height()) / 2)
    )

class Form(QWidget):
    def __init__(self, parent=None):
        super().__init__(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_view = QTextEdit()
        licenza_view.setReadOnly(True)
        licenza_view.setPlainText(txt)

        grid = QGridLayout()
        grid.setSpacing(10)

        grid.addWidget(licenza_view, 1, 0, 1, 2)
        grid.addWidget(btnRifiuto, 2, 0)
        grid.addWidget(btnAccetto, 2, 1)

        self.setLayout(grid)

    # Blocca la chiusura con la X
    def closeEvent(self, event):
        event.ignore()

    def evtAccetto(self):
        sys.exit(0)

    def evtRifiuto(self):
        sys.exit(1)

# Avvia l'app
app = QApplication(sys.argv)
form = Form()
form.show()
sys.exit(app.exec())
