MqttMonitor/MqttConnectionOptionsDialog.py

changeset 28
0f02baed8308
parent 25
01d44a4decf5
child 30
17ef10819773
--- a/MqttMonitor/MqttConnectionOptionsDialog.py	Sat Sep 08 16:51:39 2018 +0200
+++ b/MqttMonitor/MqttConnectionOptionsDialog.py	Sat Sep 08 16:55:42 2018 +0200
@@ -12,8 +12,13 @@
 from PyQt5.QtCore import pyqtSlot, QUuid
 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton
 
+from E5Gui import E5MessageBox
+from E5Gui.E5PathPicker import E5PathPickerModes
+
 from .Ui_MqttConnectionOptionsDialog import Ui_MqttConnectionOptionsDialog
 
+from Utilities.crypto import pwConvert
+
 
 class MqttConnectionOptionsDialog(QDialog, Ui_MqttConnectionOptionsDialog):
     """
@@ -28,8 +33,8 @@
         @param options dictionary containing the connection options to
             populate the dialog with. It must have the keys "ClientId",
             "Keepalive", "CleanSession", "Username", "Password", "WillTopic",
-            "WillMessage", "WillQos", "WillRetain".
-        @@type dict
+            "WillMessage", "WillQos", "WillRetain", "TlsEnable", "TlsCaCert".
+        @type dict
         @param parent reference to the parent widget
         @type QWidget
         """
@@ -38,7 +43,29 @@
         
         self.__client = client
         
+        self.tlsCertsFilePicker.setMode(E5PathPickerModes.OpenFileMode)
+        self.tlsCertsFilePicker.setFilters(
+            self.tr("Certificate Files (*.crt *.pem);;All Files (*)"))
+        
         self.__populateDefaults(options=options)
+        
+        self.__updateOkButton()
+    
+    def __updateOkButton(self):
+        """
+        Private method to update the enabled state of the OK button.
+        """
+        if self.clientIdEdit.text() == "" and \
+                not self.cleanSessionCheckBox.isChecked():
+            enable = False
+            E5MessageBox.critical(
+                self,
+                self.tr("Invalid Connection Parameters"),
+                self.tr("""An empty Client ID requires a clean session."""))
+        else:
+            enable = True
+        
+        self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable)
     
     @pyqtSlot()
     def on_generateIdButton_clicked(self):
@@ -69,7 +96,7 @@
         @param options dictionary containing the connection options to populate
             the dialog with. It must have the keys "ClientId", "Keepalive",
             "CleanSession", "Username", "Password", "WillTopic", "WillMessage",
-            "WillQos", "WillRetain".
+            "WillQos", "WillRetain", "TlsEnable", "TlsCaCert".
         @type dict
         """
         if options is None:
@@ -82,13 +109,17 @@
         
         # user credentials
         self.usernameEdit.setText(options["Username"])
-        self.passwordEdit.setText(options["Password"])
+        self.passwordEdit.setText(pwConvert(options["Password"], encode=False))
         
         # last will and testament
         self.willQosSpinBox.setValue(options["WillQos"])
         self.willRetainCheckBox.setChecked(options["WillRetain"])
         self.willTopicEdit.setText(options["WillTopic"])
         self.willMessageEdit.setPlainText(options["WillMessage"])
+        
+        # TLS parameters
+        self.tlsEnableCheckBox.setChecked(options["TlsEnable"])
+        self.tlsCertsFilePicker.setText(options["TlsCaCert"])
     
     def getConnectionOptions(self):
         """
@@ -96,7 +127,8 @@
         
         @return dictionary containing the connection options. It has the keys
             "ClientId", "Keepalive", "CleanSession", "Username", "Password",
-            "WillTopic", "WillMessage", "WillQos", "WillRetain".
+            "WillTopic", "WillMessage", "WillQos", "WillRetain", "TlsEnable",
+            "TlsCaCert".
         @rtype tuple of (int, dict)
         """
         return {
@@ -104,9 +136,31 @@
             "Keepalive": self.keepaliveSpinBox.value(),
             "CleanSession": self.cleanSessionCheckBox.isChecked(),
             "Username": self.usernameEdit.text(),
-            "Password": self.passwordEdit.text(),
+            "Password": pwConvert(self.passwordEdit.text(), encode=True),
             "WillTopic": self.willTopicEdit.text(),
             "WillMessage": self.willMessageEdit.toPlainText(),
             "WillQos": self.willQosSpinBox.value(),
             "WillRetain": self.willRetainCheckBox.isChecked(),
+            "TlsEnable": self.tlsEnableCheckBox.isChecked(),
+            "TlsCaCert": self.tlsCertsFilePicker.text()
         }
+    
+    @pyqtSlot(str)
+    def on_clientIdEdit_textChanged(self, clientId):
+        """
+        Private slot handling a change of the client ID string.
+        
+        @param clientId client ID
+        @type str
+        """
+        self.__updateOkButton()
+    
+    @pyqtSlot(bool)
+    def on_cleanSessionCheckBox_clicked(self, checked):
+        """
+        Private slot to handle a change of the clean session selection.
+        
+        @param checked current state of the clean session selection
+        @type bool
+        """
+        self.__updateOkButton()

eric ide

mercurial