#!/usr/bin/python3 -su

## Copyright (C) 2014 troubadour <trobador@riseup.net>
## Copyright (C) 2014 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

"""
This script creates a one-time popup message dialog using PyQt5. The popup will not be shown again if the user selects "Do not show this message again."

Usage:
/usr/libexec/msgcollector/one-time-popup <status_file> <title> <message>

Arguments:
1. status_file: Path to the status file used to track if the message should be shown again
2. title: The window title
3. message: The main message text

Example:
/usr/libexec/msgcollector/one-time-popup ~/testfolder/status-file "Test Title" "Test Message"

For more information, visit: https://forums.whonix.org/t/do-not-show-this-message-again-generic-one-time-popup/8066
"""

import sys
import signal
import os
from pathlib import Path
import argparse
from PyQt5 import QtCore, QtWidgets

def signal_handler(sig, frame):
    sys.exit(128 + sig)

class Notice(QtWidgets.QDialog):
    def __init__(self, status_file, title, message):
        super(Notice, self).__init__()
        self.status_file = status_file
        self.title = title
        self.message = message
        self.initUI()

    def initUI(self):
        cb = QtWidgets.QCheckBox('Do not show this message again.', self)
        cb.move(25, 230)
        cb.stateChanged.connect(self.checkState)

        OKbtn = QtWidgets.QPushButton('OK', self)
        OKbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        OKbtn.resize(OKbtn.sizeHint())
        OKbtn.move(270, 260)

        lb = QtWidgets.QLabel(self.message, self)
        lb.setGeometry(0, 0, 550, 190)  # Window size -110.
        lb.setWordWrap(True)
        lb.move(25, 20)

        self.resize(600, 300)
        self.setWindowTitle(self.title)
        self.show()

    def checkState(self, state):
        status_folder = os.path.dirname(self.status_file)
        Path(status_folder).mkdir(parents=True, exist_ok=True)
        with open(self.status_file, "w") as f:
            f.write(str(state))

    def center(self):
        qr = self.frameGeometry()
        cp = QtWidgets.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

def main():
    parser = argparse.ArgumentParser(description="Create a one-time popup message dialog using PyQt5.")
    parser.add_argument('status_file', help="Path to the status file used to track if the message should be shown again")
    parser.add_argument('title', help="The window title")
    parser.add_argument('message', help="The main message text")

    args = parser.parse_args()

    # Check if the message has already been dismissed
    if os.path.exists(args.status_file):
        sys.exit()

    app = QtWidgets.QApplication(sys.argv)
    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)

    timer = QtCore.QTimer()
    timer.start(500)
    timer.timeout.connect(lambda: None)

    ex = Notice(args.status_file, args.title, args.message)
    app.exec_()

if __name__ == '__main__':
    main()
