main.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import sys
  2. import os
  3. from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QProgressBar, QMessageBox, QInputDialog, QLineEdit
  4. from PyQt5.QtGui import QIcon
  5. from helpers.config import Config
  6. from threads.Sign import Sign
  7. from gui.SettingsDialog import SettingsDialog
  8. from gui.AboutDialog import AboutDialog
  9. from ui.mainwindow import Ui_MainWindow
  10. # Program dependent variables
  11. app_root = os.path.dirname(os.path.abspath(__file__))
  12. config_file = 'ordersguru_signer.ini'
  13. # Main window class
  14. class MainWindow(QMainWindow):
  15. def __init__(self, parent=None):
  16. QMainWindow.__init__(self, parent)
  17. self.ui = Ui_MainWindow()
  18. self.ui.setupUi(self)
  19. self.setWindowIcon(QIcon(os.path.join(app_root, 'resources', 'icon.ico')))
  20. self.progressBar = QProgressBar(self)
  21. self.progressBar.setRange(0, 1)
  22. self.ui.statusbar.addPermanentWidget(self.progressBar)
  23. self.config = Config(config_file)
  24. self.set_connections()
  25. self.check_config()
  26. self.ui.statusbar.showMessage('Ready for signing')
  27. def set_connections(self):
  28. self.ui.action_Settings.triggered.connect(lambda: self.show_settings(self.config.get_data()))
  29. self.ui.action_About.triggered.connect(lambda: AboutDialog.show_(self))
  30. self.ui.action_Quit.triggered.connect(self.close)
  31. self.ui.btnDirectoryBrowse.clicked.connect(self.browse_directory)
  32. self.ui.btnFileBrowse.clicked.connect(self.browse_file)
  33. self.ui.btnDirectorySignArrange.clicked.connect(self.sign_directory)
  34. self.ui.btnFileSign.clicked.connect(self.sign_file)
  35. def check_config(self):
  36. if not self.config.exists():
  37. self.show_settings(initial=True)
  38. elif not self.config.verify_data():
  39. self.show_settings(self.config.get_data(), False, self.config.get_errors())
  40. def show_settings(self, config=None, initial=False, errors=None):
  41. config, success = SettingsDialog.get_data(self, config, initial, errors)
  42. if success:
  43. try:
  44. self.config.save_data(config)
  45. except BaseException as e:
  46. self.show_message((str(e), False))
  47. sys.exit()
  48. elif initial or errors:
  49. sys.exit()
  50. def browse_directory(self):
  51. fbrowser = QFileDialog(self)
  52. fbrowser.selectFile(self.ui.leDirectory.displayText())
  53. fbrowser.setFileMode(QFileDialog.DirectoryOnly)
  54. if fbrowser.exec_():
  55. self.ui.leDirectory.setText(fbrowser.selectedFiles()[0])
  56. self.ui.btnDirectorySignArrange.setEnabled(True)
  57. def sign_directory(self):
  58. password = self.get_password()
  59. if password:
  60. self.ui.btnDirectorySignArrange.setEnabled(False)
  61. self.ui.statusbar.showMessage('Signing & arranging ...')
  62. self.progressBar.setRange(0, 0)
  63. sign = Sign(self, {
  64. 'directory': self.ui.leDirectory.displayText(),
  65. 'config': self.config.get_data(),
  66. 'password': password
  67. })
  68. sign.status_bar_signal.connect(self.ui.statusbar.showMessage)
  69. sign.sign_button_signal.connect(self.ui.btnDirectorySignArrange.setEnabled)
  70. sign.progress_bar_signal.connect(self.set_progress)
  71. sign.message_signal.connect(self.show_message)
  72. sign.start()
  73. def browse_file(self):
  74. fbrowser = QFileDialog(self)
  75. fbrowser.selectFile(self.ui.leFile.displayText())
  76. fbrowser.setFileMode(QFileDialog.ExistingFile)
  77. fbrowser.setNameFilter('XML files (*.xml)')
  78. if fbrowser.exec_():
  79. self.ui.leFile.setText(fbrowser.selectedFiles()[0])
  80. self.ui.btnFileSign.setEnabled(True)
  81. def sign_file(self):
  82. password = self.get_password()
  83. if password:
  84. self.ui.btnFileSign.setEnabled(False)
  85. self.ui.statusbar.showMessage('Signing ...')
  86. self.progressBar.setRange(0, 0)
  87. sign = Sign(self, {
  88. 'file': self.ui.leFile.displayText(),
  89. 'config': self.config.get_data(),
  90. 'password': password
  91. })
  92. sign.status_bar_signal.connect(self.ui.statusbar.showMessage)
  93. sign.sign_button_signal.connect(self.ui.btnFileSign.setEnabled)
  94. sign.progress_bar_signal.connect(self.set_progress)
  95. sign.message_signal.connect(self.show_message)
  96. sign.start()
  97. def set_progress(self, in_progress):
  98. self.progressBar.setRange(0, 0 if in_progress else 1)
  99. def show_message(self, message):
  100. msg, success = message
  101. error = QMessageBox(self)
  102. error.setIcon(QMessageBox.Information if success else QMessageBox.Critical)
  103. error.setWindowTitle('Success' if success else 'Error')
  104. error.setText(msg)
  105. error.setStandardButtons(QMessageBox.Ok)
  106. error.exec_()
  107. self.ui.statusbar.showMessage('Ready for signing')
  108. def get_password(self):
  109. password, success = QInputDialog.getText(
  110. self,
  111. 'Certificate password',
  112. 'Please enter certificate password:',
  113. echo=QLineEdit.Password
  114. )
  115. if success:
  116. return password
  117. return False
  118. # Run program by default
  119. if __name__ == '__main__':
  120. app = QApplication(sys.argv)
  121. og_signer = MainWindow()
  122. og_signer.show()
  123. sys.exit(app.exec_())