import sys import os from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QProgressBar, QMessageBox, QInputDialog, QLineEdit from PyQt5.QtGui import QIcon from helpers.config import Config from threads.Sign import Sign from gui.SettingsDialog import SettingsDialog from gui.AboutDialog import AboutDialog from ui.mainwindow import Ui_MainWindow # Program dependent variables app_root = os.path.dirname(os.path.abspath(__file__)) config_file = 'ordersguru_signer.ini' # Main window class class MainWindow(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.setWindowIcon(QIcon(os.path.join(app_root, 'resources', 'icon.ico'))) self.progressBar = QProgressBar(self) self.progressBar.setRange(0, 1) self.ui.statusbar.addPermanentWidget(self.progressBar) self.config = Config(config_file) self.set_connections() self.check_config() self.ui.statusbar.showMessage('Ready for signing') def set_connections(self): self.ui.action_Settings.triggered.connect(lambda: self.show_settings(self.config.get_data())) self.ui.action_About.triggered.connect(lambda: AboutDialog.show_(self)) self.ui.action_Quit.triggered.connect(self.close) self.ui.btnDirectoryBrowse.clicked.connect(self.browse_directory) self.ui.btnFileBrowse.clicked.connect(self.browse_file) self.ui.btnDirectorySignArrange.clicked.connect(self.sign_directory) self.ui.btnFileSign.clicked.connect(self.sign_file) def check_config(self): if not self.config.exists(): self.show_settings(initial=True) elif not self.config.verify_data(): self.show_settings(self.config.get_data(), False, self.config.get_errors()) def show_settings(self, config=None, initial=False, errors=None): config, success = SettingsDialog.get_data(self, config, initial, errors) if success: try: self.config.save_data(config) except BaseException as e: self.show_message((str(e), False)) sys.exit() elif initial or errors: sys.exit() def browse_directory(self): fbrowser = QFileDialog(self) fbrowser.selectFile(self.ui.leDirectory.displayText()) fbrowser.setFileMode(QFileDialog.DirectoryOnly) if fbrowser.exec_(): self.ui.leDirectory.setText(fbrowser.selectedFiles()[0]) self.ui.btnDirectorySignArrange.setEnabled(True) def sign_directory(self): password = self.get_password() if password: self.ui.btnDirectorySignArrange.setEnabled(False) self.ui.statusbar.showMessage('Signing & arranging ...') self.progressBar.setRange(0, 0) sign = Sign(self, { 'directory': self.ui.leDirectory.displayText(), 'config': self.config.get_data(), 'password': password }) sign.status_bar_signal.connect(self.ui.statusbar.showMessage) sign.sign_button_signal.connect(self.ui.btnDirectorySignArrange.setEnabled) sign.progress_bar_signal.connect(self.set_progress) sign.message_signal.connect(self.show_message) sign.start() def browse_file(self): fbrowser = QFileDialog(self) fbrowser.selectFile(self.ui.leFile.displayText()) fbrowser.setFileMode(QFileDialog.ExistingFile) fbrowser.setNameFilter('XML files (*.xml)') if fbrowser.exec_(): self.ui.leFile.setText(fbrowser.selectedFiles()[0]) self.ui.btnFileSign.setEnabled(True) def sign_file(self): password = self.get_password() if password: self.ui.btnFileSign.setEnabled(False) self.ui.statusbar.showMessage('Signing ...') self.progressBar.setRange(0, 0) sign = Sign(self, { 'file': self.ui.leFile.displayText(), 'config': self.config.get_data(), 'password': password }) sign.status_bar_signal.connect(self.ui.statusbar.showMessage) sign.sign_button_signal.connect(self.ui.btnFileSign.setEnabled) sign.progress_bar_signal.connect(self.set_progress) sign.message_signal.connect(self.show_message) sign.start() def set_progress(self, in_progress): self.progressBar.setRange(0, 0 if in_progress else 1) def show_message(self, message): msg, success = message error = QMessageBox(self) error.setIcon(QMessageBox.Information if success else QMessageBox.Critical) error.setWindowTitle('Success' if success else 'Error') error.setText(msg) error.setStandardButtons(QMessageBox.Ok) error.exec_() self.ui.statusbar.showMessage('Ready for signing') def get_password(self): password, success = QInputDialog.getText( self, 'Certificate password', 'Please enter certificate password:', echo=QLineEdit.Password ) if success: return password return False # Run program by default if __name__ == '__main__': app = QApplication(sys.argv) og_signer = MainWindow() og_signer.show() sys.exit(app.exec_())