MqttMonitor/MqttConnectionProfilesDialog.py

branch
eric7
changeset 104
9a4c9b7f078c
parent 103
5fe4f179975f
child 105
36ec7431ad04
--- a/MqttMonitor/MqttConnectionProfilesDialog.py	Thu Jul 22 19:02:32 2021 +0200
+++ b/MqttMonitor/MqttConnectionProfilesDialog.py	Fri Jul 23 17:48:22 2021 +0200
@@ -31,7 +31,7 @@
     """
     Class implementing a dialog to edit the MQTT connection profiles.
     """
-    def __init__(self, profiles, parent=None):
+    def __init__(self, profiles, currentProfile="", parent=None):
         """
         Constructor
         
@@ -40,9 +40,11 @@
             "BrokerAddress", "BrokerPort", "ClientId", "Protocol",
             "ConnectionTimeout", "Keepalive", "CleanSession", "Username",
             "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain",
-            "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey",
-            "UserProperties".
+            "WillProperties", "TlsEnable", "TlsCaCert", "TlsClientCert",
+            "TlsClientKey", "UserProperties".
         @type dict
+        @param currentProfile name of the currently selected profile
+        @type str
         @param parent reference to the parent widget
         @type QWidget
         """
@@ -50,13 +52,15 @@
         self.setupUi(self)
         
         self.__profiles = collections.defaultdict(self.__defaultProfile)
-        self.__profiles.update(profiles)
+        self.__profiles.update(copy.deepcopy(profiles))
         self.__profilesChanged = False
         
         self.plusButton.setIcon(UI.PixmapCache.getIcon("plus"))
         self.copyButton.setIcon(UI.PixmapCache.getIcon("editCopy"))
         self.minusButton.setIcon(UI.PixmapCache.getIcon("minus"))
         self.showPasswordButton.setIcon(UI.PixmapCache.getIcon("showPassword"))
+        self.willPropertiesButton.setIcon(
+            UI.PixmapCache.getIcon("listSelection"))
         
         self.tlsCertsFilePicker.setMode(EricPathPickerModes.OPEN_FILE_MODE)
         self.tlsCertsFilePicker.setFilters(
@@ -89,7 +93,7 @@
         self.__populatingProfile = False
         self.__deletingProfile = False
         
-        self.__populateProfilesList()
+        self.__populateProfilesList(currentProfile=currentProfile)
     
     @pyqtSlot(str)
     def on_profileEdit_textChanged(self, name):
@@ -113,7 +117,7 @@
             QDialogButtonBox.StandardButton.Apply
         ):
             currentProfile = self.__applyProfile()
-            self.__populateProfilesList(currentProfile)
+            self.__populateProfilesList(currentProfile=currentProfile)
         
         elif button == self.profileButtonBox.button(
             QDialogButtonBox.StandardButton.Reset
@@ -206,9 +210,11 @@
                                 """ already. Aborting...</p>""").format(
                             newProfileName))
                 else:
-                    profile = self.__defaultProfile()
-                    profile.update(self.__profiles[profileName])
-                    self.__profiles[newProfileName] = profile
+                    connectionProfile = self.__defaultProfile()
+                    connectionProfile.update(
+                        copy.deepcopy(self.__profiles[profileName]))
+                    self.__profiles[newProfileName] = connectionProfile
+                    self.__profilesChanged = True
                     
                     itm = QListWidgetItem(newProfileName, self.profilesList)
                     self.profilesList.setCurrentItem(itm)
@@ -246,12 +252,13 @@
             connection profiles. Each entry has the keys "BrokerAddress",
             "BrokerPort", "ClientId", "Protocol", "ConnectionTimeout",
             "Keepalive", "CleanSession", "Username", "Password", "WillTopic",
-            "WillMessage", "WillQos", "WillRetain", "TlsEnable", "TlsCaCert",
-            "TlsClientCert", "TlsClientKey", "UserProperties".
+            "WillMessage", "WillQos", "WillRetain", "WillProperties",
+            "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey",
+            "UserProperties".
         @rtype dict
         """
         profilesDict = {}
-        profilesDict.update(self.__profiles)
+        profilesDict.update(copy.deepcopy(dict(self.__profiles)))
         return profilesDict
     
     def __applyProfile(self):
@@ -281,6 +288,7 @@
                 self.samePropertiesCheckBox.isChecked())
         else:
             self.__userProperties = {}
+            self.__willProperties = []
         
         profileName = self.profileEdit.text()
         connectionProfile = {
@@ -297,6 +305,7 @@
             "WillMessage": self.willMessageEdit.toPlainText(),
             "WillQos": self.willQosSpinBox.value(),
             "WillRetain": self.willRetainCheckBox.isChecked(),
+            "WillProperties": copy.deepcopy(self.__willProperties),
             "TlsEnable": self.tlsGroupBox.isChecked(),
             "TlsCaCert": "",
             "TlsClientCert": "",
@@ -400,6 +409,8 @@
         self.willMessageEdit.setPlainText(connectionProfile["WillMessage"])
         self.willQosSpinBox.setValue(connectionProfile["WillQos"])
         self.willRetainCheckBox.setChecked(connectionProfile["WillRetain"])
+        self.__willProperties = copy.deepcopy(
+            connectionProfile.get("WillProperties", []))
         
         # SSL/TLS tab
         self.tlsGroupBox.setChecked(connectionProfile["TlsEnable"])
@@ -467,6 +478,7 @@
         self.willMessageEdit.setPlainText("")
         self.willQosSpinBox.setValue(0)
         self.willRetainCheckBox.setChecked(False)
+        self.__willProperties = []
         self.tlsGroupBox.setChecked(False)
         self.tlsDefaultCertsButton.setChecked(True)
         self.tlsCertsFileButton.setChecked(True)
@@ -556,6 +568,12 @@
                 connectionProfile["WillRetain"] or
                 self.tlsGroupBox.isChecked() != connectionProfile["TlsEnable"]
             )
+            # check will properties only, ig not yet changed
+            if not changed and protocol == MqttProtocols.MQTTv5:
+                changed |= (
+                    sorted(self.__willProperties) !=
+                    sorted(connectionProfile["WillProperties"])
+                )
             # check TLS stuff only, if not yet changed
             if not changed:
                 if self.tlsCertsFileButton.isChecked():
@@ -683,7 +701,8 @@
             self.profileTabWidget.indexOf(self.propertiesTab),
             checked
         )
-        # TODO: add code to enable the WILL properties button
+        self.willPropertiesButton.setEnabled(checked)
+        self.willPropertiesButton.setVisible(checked)
     
     @pyqtSlot(bool)
     def on_showPasswordButton_toggled(self, checked):
@@ -699,6 +718,18 @@
             self.passwordEdit.setEchoMode(QLineEdit.EchoMode.Password)
         )
     
+    @pyqtSlot()
+    def on_willPropertiesButton_clicked(self):
+        """
+        Private slot to edit the last will user properties.
+        """
+        from .MqttUserPropertiesEditor import MqttUserPropertiesEditorDialog
+        
+        dlg = MqttUserPropertiesEditorDialog(
+            self.tr("Last Will User Properties"), self.__willProperties, self)
+        if dlg.exec() == QDialog.DialogCode.Accepted:
+            self.__willProperties = dlg.getProperties()
+    
     @pyqtSlot(str)
     def on_tlsCertsFilePicker_textChanged(self, path):
         """

eric ide

mercurial