MqttMonitor/MqttConnectionOptionsDialog.py

branch
eric7
changeset 104
9a4c9b7f078c
parent 103
5fe4f179975f
child 105
36ec7431ad04
equal deleted inserted replaced
103:5fe4f179975f 104:9a4c9b7f078c
18 from .Ui_MqttConnectionOptionsDialog import Ui_MqttConnectionOptionsDialog 18 from .Ui_MqttConnectionOptionsDialog import Ui_MqttConnectionOptionsDialog
19 19
20 from .MqttClient import MqttClient, MqttProtocols 20 from .MqttClient import MqttClient, MqttProtocols
21 21
22 from Utilities.crypto import pwConvert 22 from Utilities.crypto import pwConvert
23 import UI.PixmapCache
23 24
24 25
25 class MqttConnectionOptionsDialog(QDialog, Ui_MqttConnectionOptionsDialog): 26 class MqttConnectionOptionsDialog(QDialog, Ui_MqttConnectionOptionsDialog):
26 """ 27 """
27 Class implementing a dialog to enter MQTT connection options. 28 Class implementing a dialog to enter MQTT connection options.
28 """ 29 """
29 # TODO: add WILL user properties
30 def __init__(self, options=None, parent=None): 30 def __init__(self, options=None, parent=None):
31 """ 31 """
32 Constructor 32 Constructor
33 33
34 @param options dictionary containing the connection options to 34 @param options dictionary containing the connection options to
35 populate the dialog with. It must have the keys "ClientId", 35 populate the dialog with. It must have the keys "ClientId",
36 "Protocol", "ConnectionTimeout", "Keepalive", "CleanSession", 36 "Protocol", "ConnectionTimeout", "Keepalive", "CleanSession",
37 "Username", "Password", "WillTopic", "WillMessage", "WillQos", 37 "Username", "Password", "WillTopic", "WillMessage", "WillQos",
38 "WillRetain", "TlsEnable", "TlsCaCert", "UserProperties". 38 "WillRetain", "WillProperties", "TlsEnable", "TlsCaCert",
39 "UserProperties".
39 @type dict 40 @type dict
40 @param parent reference to the parent widget 41 @param parent reference to the parent widget
41 @type QWidget 42 @type QWidget
42 """ 43 """
43 super().__init__(parent) 44 super().__init__(parent)
44 self.setupUi(self) 45 self.setupUi(self)
45 46
46 self.tlsCertsFilePicker.setMode(EricPathPickerModes.OPEN_FILE_MODE) 47 self.tlsCertsFilePicker.setMode(EricPathPickerModes.OPEN_FILE_MODE)
47 self.tlsCertsFilePicker.setFilters( 48 self.tlsCertsFilePicker.setFilters(
48 self.tr("Certificate Files (*.crt *.pem);;All Files (*)")) 49 self.tr("Certificate Files (*.crt *.pem);;All Files (*)"))
50
51 self.willPropertiesButton.setIcon(
52 UI.PixmapCache.getIcon("listSelection"))
53
54 self.optionsWidget.setCurrentIndex(0)
49 55
50 # initialize MQTTv5 related stuff 56 # initialize MQTTv5 related stuff
51 self.on_mqttv5Button_toggled(False) 57 self.on_mqttv5Button_toggled(False)
52 58
53 self.__populateDefaults(options=options) 59 self.__populateDefaults(options=options)
117 """ 123 """
118 self.optionsWidget.setTabEnabled( 124 self.optionsWidget.setTabEnabled(
119 self.optionsWidget.indexOf(self.propertiesTab), 125 self.optionsWidget.indexOf(self.propertiesTab),
120 checked 126 checked
121 ) 127 )
122 # TODO: add code to enable the WILL properties button 128 self.willPropertiesButton.setEnabled(checked)
129 self.willPropertiesButton.setVisible(checked)
123 130
124 @pyqtSlot(QAbstractButton) 131 @pyqtSlot(QAbstractButton)
125 def on_buttonBox_clicked(self, button): 132 def on_buttonBox_clicked(self, button):
126 """ 133 """
127 Private slot to handle the press of a button box button. 134 Private slot to handle the press of a button box button.
166 self.__userProperties["connect"] = ( 173 self.__userProperties["connect"] = (
167 self.propertiesWidget.getProperties()) 174 self.propertiesWidget.getProperties())
168 self.propertiesWidget.setProperties( 175 self.propertiesWidget.setProperties(
169 self.__userProperties["disconnect"]) 176 self.__userProperties["disconnect"])
170 177
178 @pyqtSlot()
179 def on_willPropertiesButton_clicked(self):
180 """
181 Private slot to edit the last will user properties.
182 """
183 from .MqttUserPropertiesEditor import MqttUserPropertiesEditorDialog
184
185 dlg = MqttUserPropertiesEditorDialog(
186 self.tr("Last Will User Properties"), self.__willProperties, self)
187 if dlg.exec() == QDialog.DialogCode.Accepted:
188 self.__willProperties = dlg.getProperties()
189
171 def __populateDefaults(self, options=None): 190 def __populateDefaults(self, options=None):
172 """ 191 """
173 Private method to populate the dialog. 192 Private method to populate the dialog.
174 193
175 If no options dictionary is given, the dialog will be populated with 194 If no options dictionary is given, the dialog will be populated with
177 196
178 @param options dictionary containing the connection options to populate 197 @param options dictionary containing the connection options to populate
179 the dialog with. It must have the keys "ClientId", "Protocol", 198 the dialog with. It must have the keys "ClientId", "Protocol",
180 "ConnectionTimeout", "Keepalive", "CleanSession", "Username", 199 "ConnectionTimeout", "Keepalive", "CleanSession", "Username",
181 "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain", 200 "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain",
182 "TlsEnable", "TlsCaCert", "UserProperties". 201 "WillProperties", "TlsEnable", "TlsCaCert", "UserProperties".
183 @type dict 202 @type dict
184 """ 203 """
185 if options is None: 204 if options is None:
186 options = MqttClient.defaultConnectionOptions() 205 options = MqttClient.defaultConnectionOptions()
187 206
204 # last will and testament 223 # last will and testament
205 self.willQosSpinBox.setValue(options["WillQos"]) 224 self.willQosSpinBox.setValue(options["WillQos"])
206 self.willRetainCheckBox.setChecked(options["WillRetain"]) 225 self.willRetainCheckBox.setChecked(options["WillRetain"])
207 self.willTopicEdit.setText(options["WillTopic"]) 226 self.willTopicEdit.setText(options["WillTopic"])
208 self.willMessageEdit.setPlainText(options["WillMessage"]) 227 self.willMessageEdit.setPlainText(options["WillMessage"])
228 self.__willProperties = copy.deepcopy(
229 options.get("WillProperties", []))
209 230
210 # TLS parameters 231 # TLS parameters
211 self.tlsEnableCheckBox.setChecked(options["TlsEnable"]) 232 self.tlsEnableCheckBox.setChecked(options["TlsEnable"])
212 self.tlsCertsFilePicker.setText(options["TlsCaCert"]) 233 self.tlsCertsFilePicker.setText(options["TlsCaCert"])
213 234
237 Public method get the entered connection options. 258 Public method get the entered connection options.
238 259
239 @return dictionary containing the connection options. It has the keys 260 @return dictionary containing the connection options. It has the keys
240 "ClientId", "Protocol", "ConnectionTimeout", "Keepalive", 261 "ClientId", "Protocol", "ConnectionTimeout", "Keepalive",
241 "CleanSession", "Username", "Password", "WillTopic", "WillMessage", 262 "CleanSession", "Username", "Password", "WillTopic", "WillMessage",
242 "WillQos", "WillRetain", "TlsEnable", "TlsCaCert", 263 "WillQos", "WillRetain", "WillProperties", "TlsEnable",
243 "UserProperties". 264 "TlsCaCert", "UserProperties".
244 @rtype dict 265 @rtype dict
245 """ 266 """
246 if self.mqttv31Button.isChecked(): 267 if self.mqttv31Button.isChecked():
247 protocol = MqttProtocols.MQTTv31 268 protocol = MqttProtocols.MQTTv31
248 elif self.mqttv311Button.isChecked(): 269 elif self.mqttv311Button.isChecked():
261 self.propertiesWidget.getProperties()) 282 self.propertiesWidget.getProperties())
262 self.__userProperties["use_connect"] = ( 283 self.__userProperties["use_connect"] = (
263 self.samePropertiesCheckBox.isChecked()) 284 self.samePropertiesCheckBox.isChecked())
264 else: 285 else:
265 self.__userProperties = {} 286 self.__userProperties = {}
287 self.__willProperties = []
266 288
267 return { 289 return {
268 "ClientId": self.clientIdEdit.text(), 290 "ClientId": self.clientIdEdit.text(),
269 "Protocol": protocol, 291 "Protocol": protocol,
270 "ConnectionTimeout": self.connectionTimeoutSpinBox.value(), 292 "ConnectionTimeout": self.connectionTimeoutSpinBox.value(),
274 "Password": pwConvert(self.passwordEdit.text(), encode=True), 296 "Password": pwConvert(self.passwordEdit.text(), encode=True),
275 "WillTopic": self.willTopicEdit.text(), 297 "WillTopic": self.willTopicEdit.text(),
276 "WillMessage": self.willMessageEdit.toPlainText(), 298 "WillMessage": self.willMessageEdit.toPlainText(),
277 "WillQos": self.willQosSpinBox.value(), 299 "WillQos": self.willQosSpinBox.value(),
278 "WillRetain": self.willRetainCheckBox.isChecked(), 300 "WillRetain": self.willRetainCheckBox.isChecked(),
301 "WillProperties": copy.deepcopy(self.__willProperties),
279 "TlsEnable": self.tlsEnableCheckBox.isChecked(), 302 "TlsEnable": self.tlsEnableCheckBox.isChecked(),
280 "TlsCaCert": self.tlsCertsFilePicker.text(), 303 "TlsCaCert": self.tlsCertsFilePicker.text(),
281 "UserProperties": copy.deepcopy(self.__userProperties), 304 "UserProperties": copy.deepcopy(self.__userProperties),
282 } 305 }

eric ide

mercurial