123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- from PyQt5.QtWidgets import QDialog, QMessageBox, QFileDialog
- from PyQt5.QtCore import Qt, QSize
- from ui.settings import Ui_Settings
- import os
- class SettingsDialog(QDialog):
- def __init__(self, parent=None, config=None, initial=False, errors=None):
- super(SettingsDialog, self).__init__(
- parent, Qt.WindowSystemMenuHint | Qt.WindowTitleHint | Qt.WindowCloseButtonHint
- )
- self.ui = Ui_Settings()
- self.ui.setupUi(self)
- self.set_values(config)
- self.set_mode(initial, errors)
- self.set_connections()
- def set_values(self, config):
- if config:
- self.ui.leXMLSchema.setText(config['xmlschema'])
- self.ui.leCertificate.setText(config['certificate'])
- def set_mode(self, initial, errors):
- if initial:
- self.setWindowTitle('OrdersGuru Signer Initial Settings')
- elif errors:
- self.setWindowTitle('OrdersGuru Signer Settings')
- self.ui.lblWelcomeErrorsHeader.setText('<center><b>OrdersGuru Signer detected a problem!</b></center>')
- self.ui.lblWelcomeErrors.setText(
- 'There is a problem with following ...<br /><span style="color:red">' +
- '<br />'.join(' ' * 4 + '- ' + err for err in errors) + '</span>'
- )
- self.toggle_buttons()
- else:
- self.ui.lblWelcomeErrorsHeader.setVisible(False)
- self.ui.lblWelcomeErrors.setText('Settings used for validating and signing XML files.')
- self.resize(455, 290)
- self.setMinimumSize(QSize(455, 290))
- self.setMaximumSize(QSize(455, 290))
- self.toggle_buttons()
- def set_connections(self):
- self.ui.btnXMLSchemaBrowse.clicked.connect(self.browse_schema)
- self.ui.btnCertificateBrowse.clicked.connect(self.browse_certificate)
- self.ui.btnCancel.clicked.connect(self.close)
- self.ui.btnOk.clicked.connect(self.validate_fields)
- self.ui.btnReset.clicked.connect(self.reset_settings)
- def browse_schema(self):
- fbrowser = QFileDialog(self)
- fbrowser.selectFile(self.ui.leXMLSchema.displayText())
- fbrowser.setFileMode(QFileDialog.ExistingFile)
- fbrowser.setNameFilter('XML Schema Definition files (*.xsd)')
- if fbrowser.exec_():
- self.ui.leXMLSchema.setText(fbrowser.selectedFiles()[0])
- self.toggle_buttons_fields()
- def browse_certificate(self):
- fbrowser = QFileDialog(self)
- fbrowser.selectFile(self.ui.leCertificate.displayText())
- fbrowser.setFileMode(QFileDialog.ExistingFile)
- fbrowser.setNameFilter('PKCS #12 files (*.p12)')
- if fbrowser.exec_():
- self.ui.leCertificate.setText(fbrowser.selectedFiles()[0])
- self.toggle_buttons_fields()
- def toggle_buttons(self, toggle=True):
- self.ui.btnReset.setEnabled(toggle)
- self.ui.btnOk.setEnabled(toggle)
- def toggle_buttons_fields(self):
- if self.ui.leXMLSchema.displayText() and self.ui.leCertificate.displayText():
- self.toggle_buttons()
- elif self.ui.leXMLSchema.displayText() or self.ui.leCertificate.displayText():
- self.ui.btnReset.setEnabled(True)
- def reset_settings(self):
- self.ui.leXMLSchema.setText('')
- self.ui.leCertificate.setText('')
- self.toggle_buttons(False)
- def validate_fields(self):
- errors = []
- if len(self.ui.leXMLSchema.displayText()) == 0:
- errors.append('XML Schema file path is not set.')
- elif not os.path.isfile(self.ui.leXMLSchema.displayText()):
- errors.append('XML Schema file doesn\'t exist.')
- if len(self.ui.leCertificate.displayText()) == 0:
- errors.append('Certificate path is not set.')
- elif not os.path.isfile(self.ui.leCertificate.displayText()):
- errors.append('Certificate file doesn\'t exist.')
- if errors:
- error = QMessageBox(self)
- error.setIcon(QMessageBox.Critical)
- error.setWindowTitle('Error')
- error.setText('Something went wrong with chosen files.')
- error.setInformativeText('Please correct errors if you want to proceed.')
- error.setStandardButtons(QMessageBox.Ok)
- error.setDetailedText('\n'.join(errors))
- error.exec_()
- else:
- self.accept()
- @staticmethod
- def get_data(parent=None, config=None, initial=False, errors=None):
- settings = SettingsDialog(parent, config, initial, errors)
- result = settings.exec_()
- return ({
- 'xmlschema': settings.ui.leXMLSchema.displayText(),
- 'certificate': settings.ui.leCertificate.displayText()
- }, result == QDialog.Accepted)
|