SettingsDialog.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from PyQt5.QtWidgets import QDialog, QMessageBox, QFileDialog
  2. from PyQt5.QtCore import Qt, QSize
  3. from ui.settings import Ui_Settings
  4. import os
  5. class SettingsDialog(QDialog):
  6. def __init__(self, parent=None, config=None, initial=False, errors=None):
  7. super(SettingsDialog, self).__init__(
  8. parent, Qt.WindowSystemMenuHint | Qt.WindowTitleHint | Qt.WindowCloseButtonHint
  9. )
  10. self.ui = Ui_Settings()
  11. self.ui.setupUi(self)
  12. self.set_values(config)
  13. self.set_mode(initial, errors)
  14. self.set_connections()
  15. def set_values(self, config):
  16. if config:
  17. self.ui.leXMLSchema.setText(config['xmlschema'])
  18. self.ui.leCertificate.setText(config['certificate'])
  19. def set_mode(self, initial, errors):
  20. if initial:
  21. self.setWindowTitle('OrdersGuru Signer Initial Settings')
  22. elif errors:
  23. self.setWindowTitle('OrdersGuru Signer Settings')
  24. self.ui.lblWelcomeErrorsHeader.setText('<center><b>OrdersGuru Signer detected a problem!</b></center>')
  25. self.ui.lblWelcomeErrors.setText(
  26. 'There is a problem with following ...<br /><span style="color:red">' +
  27. '<br />'.join('&nbsp;' * 4 + '- ' + err for err in errors) + '</span>'
  28. )
  29. self.toggle_buttons()
  30. else:
  31. self.ui.lblWelcomeErrorsHeader.setVisible(False)
  32. self.ui.lblWelcomeErrors.setText('Settings used for validating and signing XML files.')
  33. self.resize(455, 290)
  34. self.setMinimumSize(QSize(455, 290))
  35. self.setMaximumSize(QSize(455, 290))
  36. self.toggle_buttons()
  37. def set_connections(self):
  38. self.ui.btnXMLSchemaBrowse.clicked.connect(self.browse_schema)
  39. self.ui.btnCertificateBrowse.clicked.connect(self.browse_certificate)
  40. self.ui.btnCancel.clicked.connect(self.close)
  41. self.ui.btnOk.clicked.connect(self.validate_fields)
  42. self.ui.btnReset.clicked.connect(self.reset_settings)
  43. def browse_schema(self):
  44. fbrowser = QFileDialog(self)
  45. fbrowser.selectFile(self.ui.leXMLSchema.displayText())
  46. fbrowser.setFileMode(QFileDialog.ExistingFile)
  47. fbrowser.setNameFilter('XML Schema Definition files (*.xsd)')
  48. if fbrowser.exec_():
  49. self.ui.leXMLSchema.setText(fbrowser.selectedFiles()[0])
  50. self.toggle_buttons_fields()
  51. def browse_certificate(self):
  52. fbrowser = QFileDialog(self)
  53. fbrowser.selectFile(self.ui.leCertificate.displayText())
  54. fbrowser.setFileMode(QFileDialog.ExistingFile)
  55. fbrowser.setNameFilter('PKCS #12 files (*.p12)')
  56. if fbrowser.exec_():
  57. self.ui.leCertificate.setText(fbrowser.selectedFiles()[0])
  58. self.toggle_buttons_fields()
  59. def toggle_buttons(self, toggle=True):
  60. self.ui.btnReset.setEnabled(toggle)
  61. self.ui.btnOk.setEnabled(toggle)
  62. def toggle_buttons_fields(self):
  63. if self.ui.leXMLSchema.displayText() and self.ui.leCertificate.displayText():
  64. self.toggle_buttons()
  65. elif self.ui.leXMLSchema.displayText() or self.ui.leCertificate.displayText():
  66. self.ui.btnReset.setEnabled(True)
  67. def reset_settings(self):
  68. self.ui.leXMLSchema.setText('')
  69. self.ui.leCertificate.setText('')
  70. self.toggle_buttons(False)
  71. def validate_fields(self):
  72. errors = []
  73. if len(self.ui.leXMLSchema.displayText()) == 0:
  74. errors.append('XML Schema file path is not set.')
  75. elif not os.path.isfile(self.ui.leXMLSchema.displayText()):
  76. errors.append('XML Schema file doesn\'t exist.')
  77. if len(self.ui.leCertificate.displayText()) == 0:
  78. errors.append('Certificate path is not set.')
  79. elif not os.path.isfile(self.ui.leCertificate.displayText()):
  80. errors.append('Certificate file doesn\'t exist.')
  81. if errors:
  82. error = QMessageBox(self)
  83. error.setIcon(QMessageBox.Critical)
  84. error.setWindowTitle('Error')
  85. error.setText('Something went wrong with chosen files.')
  86. error.setInformativeText('Please correct errors if you want to proceed.')
  87. error.setStandardButtons(QMessageBox.Ok)
  88. error.setDetailedText('\n'.join(errors))
  89. error.exec_()
  90. else:
  91. self.accept()
  92. @staticmethod
  93. def get_data(parent=None, config=None, initial=False, errors=None):
  94. settings = SettingsDialog(parent, config, initial, errors)
  95. result = settings.exec_()
  96. return ({
  97. 'xmlschema': settings.ui.leXMLSchema.displayText(),
  98. 'certificate': settings.ui.leCertificate.displayText()
  99. }, result == QDialog.Accepted)