10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 from PyQt5.QtCore import pyqtSlot, QUuid |
12 from PyQt5.QtCore import pyqtSlot, QUuid |
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton |
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton |
14 |
14 |
|
15 from E5Gui import E5MessageBox |
|
16 from E5Gui.E5PathPicker import E5PathPickerModes |
|
17 |
15 from .Ui_MqttConnectionOptionsDialog import Ui_MqttConnectionOptionsDialog |
18 from .Ui_MqttConnectionOptionsDialog import Ui_MqttConnectionOptionsDialog |
|
19 |
|
20 from Utilities.crypto import pwConvert |
16 |
21 |
17 |
22 |
18 class MqttConnectionOptionsDialog(QDialog, Ui_MqttConnectionOptionsDialog): |
23 class MqttConnectionOptionsDialog(QDialog, Ui_MqttConnectionOptionsDialog): |
19 """ |
24 """ |
20 Class implementing a dialog to enter MQTT connection options. |
25 Class implementing a dialog to enter MQTT connection options. |
26 @param client reference to the MQTT client object |
31 @param client reference to the MQTT client object |
27 @type MqttClient |
32 @type MqttClient |
28 @param options dictionary containing the connection options to |
33 @param options dictionary containing the connection options to |
29 populate the dialog with. It must have the keys "ClientId", |
34 populate the dialog with. It must have the keys "ClientId", |
30 "Keepalive", "CleanSession", "Username", "Password", "WillTopic", |
35 "Keepalive", "CleanSession", "Username", "Password", "WillTopic", |
31 "WillMessage", "WillQos", "WillRetain". |
36 "WillMessage", "WillQos", "WillRetain", "TlsEnable", "TlsCaCert". |
32 @@type dict |
37 @type dict |
33 @param parent reference to the parent widget |
38 @param parent reference to the parent widget |
34 @type QWidget |
39 @type QWidget |
35 """ |
40 """ |
36 super(MqttConnectionOptionsDialog, self).__init__(parent) |
41 super(MqttConnectionOptionsDialog, self).__init__(parent) |
37 self.setupUi(self) |
42 self.setupUi(self) |
38 |
43 |
39 self.__client = client |
44 self.__client = client |
40 |
45 |
|
46 self.tlsCertsFilePicker.setMode(E5PathPickerModes.OpenFileMode) |
|
47 self.tlsCertsFilePicker.setFilters( |
|
48 self.tr("Certificate Files (*.crt *.pem);;All Files (*)")) |
|
49 |
41 self.__populateDefaults(options=options) |
50 self.__populateDefaults(options=options) |
|
51 |
|
52 self.__updateOkButton() |
|
53 |
|
54 def __updateOkButton(self): |
|
55 """ |
|
56 Private method to update the enabled state of the OK button. |
|
57 """ |
|
58 if self.clientIdEdit.text() == "" and \ |
|
59 not self.cleanSessionCheckBox.isChecked(): |
|
60 enable = False |
|
61 E5MessageBox.critical( |
|
62 self, |
|
63 self.tr("Invalid Connection Parameters"), |
|
64 self.tr("""An empty Client ID requires a clean session.""")) |
|
65 else: |
|
66 enable = True |
|
67 |
|
68 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable) |
42 |
69 |
43 @pyqtSlot() |
70 @pyqtSlot() |
44 def on_generateIdButton_clicked(self): |
71 def on_generateIdButton_clicked(self): |
45 """ |
72 """ |
46 Private slot to generate a client ID. |
73 Private slot to generate a client ID. |
67 default values. |
94 default values. |
68 |
95 |
69 @param options dictionary containing the connection options to populate |
96 @param options dictionary containing the connection options to populate |
70 the dialog with. It must have the keys "ClientId", "Keepalive", |
97 the dialog with. It must have the keys "ClientId", "Keepalive", |
71 "CleanSession", "Username", "Password", "WillTopic", "WillMessage", |
98 "CleanSession", "Username", "Password", "WillTopic", "WillMessage", |
72 "WillQos", "WillRetain". |
99 "WillQos", "WillRetain", "TlsEnable", "TlsCaCert". |
73 @type dict |
100 @type dict |
74 """ |
101 """ |
75 if options is None: |
102 if options is None: |
76 options = self.__client.defaultConnectionOptions() |
103 options = self.__client.defaultConnectionOptions() |
77 |
104 |
80 self.keepaliveSpinBox.setValue(options["Keepalive"]) |
107 self.keepaliveSpinBox.setValue(options["Keepalive"]) |
81 self.cleanSessionCheckBox.setChecked(options["CleanSession"]) |
108 self.cleanSessionCheckBox.setChecked(options["CleanSession"]) |
82 |
109 |
83 # user credentials |
110 # user credentials |
84 self.usernameEdit.setText(options["Username"]) |
111 self.usernameEdit.setText(options["Username"]) |
85 self.passwordEdit.setText(options["Password"]) |
112 self.passwordEdit.setText(pwConvert(options["Password"], encode=False)) |
86 |
113 |
87 # last will and testament |
114 # last will and testament |
88 self.willQosSpinBox.setValue(options["WillQos"]) |
115 self.willQosSpinBox.setValue(options["WillQos"]) |
89 self.willRetainCheckBox.setChecked(options["WillRetain"]) |
116 self.willRetainCheckBox.setChecked(options["WillRetain"]) |
90 self.willTopicEdit.setText(options["WillTopic"]) |
117 self.willTopicEdit.setText(options["WillTopic"]) |
91 self.willMessageEdit.setPlainText(options["WillMessage"]) |
118 self.willMessageEdit.setPlainText(options["WillMessage"]) |
|
119 |
|
120 # TLS parameters |
|
121 self.tlsEnableCheckBox.setChecked(options["TlsEnable"]) |
|
122 self.tlsCertsFilePicker.setText(options["TlsCaCert"]) |
92 |
123 |
93 def getConnectionOptions(self): |
124 def getConnectionOptions(self): |
94 """ |
125 """ |
95 Public method get the entered connection options. |
126 Public method get the entered connection options. |
96 |
127 |
97 @return dictionary containing the connection options. It has the keys |
128 @return dictionary containing the connection options. It has the keys |
98 "ClientId", "Keepalive", "CleanSession", "Username", "Password", |
129 "ClientId", "Keepalive", "CleanSession", "Username", "Password", |
99 "WillTopic", "WillMessage", "WillQos", "WillRetain". |
130 "WillTopic", "WillMessage", "WillQos", "WillRetain", "TlsEnable", |
|
131 "TlsCaCert". |
100 @rtype tuple of (int, dict) |
132 @rtype tuple of (int, dict) |
101 """ |
133 """ |
102 return { |
134 return { |
103 "ClientId": self.clientIdEdit.text(), |
135 "ClientId": self.clientIdEdit.text(), |
104 "Keepalive": self.keepaliveSpinBox.value(), |
136 "Keepalive": self.keepaliveSpinBox.value(), |
105 "CleanSession": self.cleanSessionCheckBox.isChecked(), |
137 "CleanSession": self.cleanSessionCheckBox.isChecked(), |
106 "Username": self.usernameEdit.text(), |
138 "Username": self.usernameEdit.text(), |
107 "Password": self.passwordEdit.text(), |
139 "Password": pwConvert(self.passwordEdit.text(), encode=True), |
108 "WillTopic": self.willTopicEdit.text(), |
140 "WillTopic": self.willTopicEdit.text(), |
109 "WillMessage": self.willMessageEdit.toPlainText(), |
141 "WillMessage": self.willMessageEdit.toPlainText(), |
110 "WillQos": self.willQosSpinBox.value(), |
142 "WillQos": self.willQosSpinBox.value(), |
111 "WillRetain": self.willRetainCheckBox.isChecked(), |
143 "WillRetain": self.willRetainCheckBox.isChecked(), |
|
144 "TlsEnable": self.tlsEnableCheckBox.isChecked(), |
|
145 "TlsCaCert": self.tlsCertsFilePicker.text() |
112 } |
146 } |
|
147 |
|
148 @pyqtSlot(str) |
|
149 def on_clientIdEdit_textChanged(self, clientId): |
|
150 """ |
|
151 Private slot handling a change of the client ID string. |
|
152 |
|
153 @param clientId client ID |
|
154 @type str |
|
155 """ |
|
156 self.__updateOkButton() |
|
157 |
|
158 @pyqtSlot(bool) |
|
159 def on_cleanSessionCheckBox_clicked(self, checked): |
|
160 """ |
|
161 Private slot to handle a change of the clean session selection. |
|
162 |
|
163 @param checked current state of the clean session selection |
|
164 @type bool |
|
165 """ |
|
166 self.__updateOkButton() |