src/eric7/Preferences/ConfigurationPages/EmailPage.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
14 14
15 from EricWidgets import EricMessageBox 15 from EricWidgets import EricMessageBox
16 from EricWidgets.EricApplication import ericApp 16 from EricWidgets.EricApplication import ericApp
17 from EricGui.EricOverrideCursor import EricOverrideCursor 17 from EricGui.EricOverrideCursor import EricOverrideCursor
18 18
19 from EricNetwork.EricGoogleMailHelpers import ( 19 from EricNetwork.EricGoogleMailHelpers import getInstallCommand, RequiredPackages
20 getInstallCommand, RequiredPackages
21 )
22 20
23 from .ConfigurationPageBase import ConfigurationPageBase 21 from .ConfigurationPageBase import ConfigurationPageBase
24 from .Ui_EmailPage import Ui_EmailPage 22 from .Ui_EmailPage import Ui_EmailPage
25 23
26 import Globals 24 import Globals
29 27
30 class EmailPage(ConfigurationPageBase, Ui_EmailPage): 28 class EmailPage(ConfigurationPageBase, Ui_EmailPage):
31 """ 29 """
32 Class implementing the Email configuration page. 30 Class implementing the Email configuration page.
33 """ 31 """
32
34 def __init__(self): 33 def __init__(self):
35 """ 34 """
36 Constructor 35 Constructor
37 """ 36 """
38 super().__init__() 37 super().__init__()
39 self.setupUi(self) 38 self.setupUi(self)
40 self.setObjectName("EmailPage") 39 self.setObjectName("EmailPage")
41 40
42 self.__helpDialog = None 41 self.__helpDialog = None
43 42
44 pipPackages = [ 43 pipPackages = [
45 "google-api-python-client", 44 "google-api-python-client",
46 "google-auth-oauthlib", 45 "google-auth-oauthlib",
47 ] 46 ]
48 self.__pipCommand = "pip install --upgrade {0}".format( 47 self.__pipCommand = "pip install --upgrade {0}".format(" ".join(pipPackages))
49 " ".join(pipPackages)) 48
50
51 # set initial values 49 # set initial values
52 self.__checkGoogleMail() 50 self.__checkGoogleMail()
53 51
54 self.mailServerEdit.setText(Preferences.getUser("MailServer")) 52 self.mailServerEdit.setText(Preferences.getUser("MailServer"))
55 self.portSpin.setValue(Preferences.getUser("MailServerPort")) 53 self.portSpin.setValue(Preferences.getUser("MailServerPort"))
56 self.emailEdit.setText(Preferences.getUser("Email")) 54 self.emailEdit.setText(Preferences.getUser("Email"))
57 self.signatureEdit.setPlainText(Preferences.getUser("Signature")) 55 self.signatureEdit.setPlainText(Preferences.getUser("Signature"))
58 self.mailAuthenticationGroup.setChecked( 56 self.mailAuthenticationGroup.setChecked(
59 Preferences.getUser("MailServerAuthentication")) 57 Preferences.getUser("MailServerAuthentication")
58 )
60 self.mailUserEdit.setText(Preferences.getUser("MailServerUser")) 59 self.mailUserEdit.setText(Preferences.getUser("MailServerUser"))
61 self.mailPasswordEdit.setText( 60 self.mailPasswordEdit.setText(Preferences.getUser("MailServerPassword"))
62 Preferences.getUser("MailServerPassword"))
63 encryption = Preferences.getUser("MailServerEncryption") 61 encryption = Preferences.getUser("MailServerEncryption")
64 if encryption == "TLS": 62 if encryption == "TLS":
65 self.useTlsButton.setChecked(True) 63 self.useTlsButton.setChecked(True)
66 elif encryption == "SSL": 64 elif encryption == "SSL":
67 self.useSslButton.setChecked(True) 65 self.useSslButton.setChecked(True)
68 else: 66 else:
69 self.noEncryptionButton.setChecked(True) 67 self.noEncryptionButton.setChecked(True)
70 68
71 def save(self): 69 def save(self):
72 """ 70 """
73 Public slot to save the Email configuration. 71 Public slot to save the Email configuration.
74 """ 72 """
73 Preferences.setUser("UseGoogleMailOAuth2", self.googleMailCheckBox.isChecked())
74 Preferences.setUser("MailServer", self.mailServerEdit.text())
75 Preferences.setUser("MailServerPort", self.portSpin.value())
76 Preferences.setUser("Email", self.emailEdit.text())
77 Preferences.setUser("Signature", self.signatureEdit.toPlainText())
75 Preferences.setUser( 78 Preferences.setUser(
76 "UseGoogleMailOAuth2", 79 "MailServerAuthentication", self.mailAuthenticationGroup.isChecked()
77 self.googleMailCheckBox.isChecked()) 80 )
78 Preferences.setUser( 81 Preferences.setUser("MailServerUser", self.mailUserEdit.text())
79 "MailServer", 82 Preferences.setUser("MailServerPassword", self.mailPasswordEdit.text())
80 self.mailServerEdit.text())
81 Preferences.setUser(
82 "MailServerPort",
83 self.portSpin.value())
84 Preferences.setUser(
85 "Email",
86 self.emailEdit.text())
87 Preferences.setUser(
88 "Signature",
89 self.signatureEdit.toPlainText())
90 Preferences.setUser(
91 "MailServerAuthentication",
92 self.mailAuthenticationGroup.isChecked())
93 Preferences.setUser(
94 "MailServerUser",
95 self.mailUserEdit.text())
96 Preferences.setUser(
97 "MailServerPassword",
98 self.mailPasswordEdit.text())
99 if self.useTlsButton.isChecked(): 83 if self.useTlsButton.isChecked():
100 encryption = "TLS" 84 encryption = "TLS"
101 elif self.useSslButton.isChecked(): 85 elif self.useSslButton.isChecked():
102 encryption = "SSL" 86 encryption = "SSL"
103 else: 87 else:
104 encryption = "No" 88 encryption = "No"
105 Preferences.setUser("MailServerEncryption", encryption) 89 Preferences.setUser("MailServerEncryption", encryption)
106 90
107 def __updatePortSpin(self): 91 def __updatePortSpin(self):
108 """ 92 """
109 Private slot to set the value of the port spin box depending upon 93 Private slot to set the value of the port spin box depending upon
110 the selected encryption method. 94 the selected encryption method.
111 """ 95 """
113 self.portSpin.setValue(465) 97 self.portSpin.setValue(465)
114 elif self.useTlsButton.isChecked(): 98 elif self.useTlsButton.isChecked():
115 self.portSpin.setValue(587) 99 self.portSpin.setValue(587)
116 else: 100 else:
117 self.portSpin.setValue(25) 101 self.portSpin.setValue(25)
118 102
119 @pyqtSlot(bool) 103 @pyqtSlot(bool)
120 def on_noEncryptionButton_toggled(self, checked): 104 def on_noEncryptionButton_toggled(self, checked):
121 """ 105 """
122 Private slot handling a change of no encryption button. 106 Private slot handling a change of no encryption button.
123 107
124 @param checked current state of the button 108 @param checked current state of the button
125 @type bool 109 @type bool
126 """ 110 """
127 self.__updatePortSpin() 111 self.__updatePortSpin()
128 112
129 @pyqtSlot(bool) 113 @pyqtSlot(bool)
130 def on_useSslButton_toggled(self, checked): 114 def on_useSslButton_toggled(self, checked):
131 """ 115 """
132 Private slot handling a change of SSL encryption button. 116 Private slot handling a change of SSL encryption button.
133 117
134 @param checked current state of the button 118 @param checked current state of the button
135 @type bool 119 @type bool
136 """ 120 """
137 self.__updatePortSpin() 121 self.__updatePortSpin()
138 122
139 @pyqtSlot(bool) 123 @pyqtSlot(bool)
140 def on_useTlsButton_toggled(self, checked): 124 def on_useTlsButton_toggled(self, checked):
141 """ 125 """
142 Private slot handling a change of TLS encryption button. 126 Private slot handling a change of TLS encryption button.
143 127
144 @param checked current state of the button 128 @param checked current state of the button
145 @type bool 129 @type bool
146 """ 130 """
147 self.__updatePortSpin() 131 self.__updatePortSpin()
148 132
149 def __updateTestButton(self): 133 def __updateTestButton(self):
150 """ 134 """
151 Private slot to update the enabled state of the test button. 135 Private slot to update the enabled state of the test button.
152 """ 136 """
153 self.testButton.setEnabled( 137 self.testButton.setEnabled(
154 self.mailAuthenticationGroup.isChecked() and 138 self.mailAuthenticationGroup.isChecked()
155 self.mailUserEdit.text() != "" and 139 and self.mailUserEdit.text() != ""
156 self.mailPasswordEdit.text() != "" and 140 and self.mailPasswordEdit.text() != ""
157 self.mailServerEdit.text() != "" 141 and self.mailServerEdit.text() != ""
158 ) 142 )
159 143
160 @pyqtSlot(str) 144 @pyqtSlot(str)
161 def on_mailServerEdit_textChanged(self, txt): 145 def on_mailServerEdit_textChanged(self, txt):
162 """ 146 """
163 Private slot to handle a change of the text of the mail server edit. 147 Private slot to handle a change of the text of the mail server edit.
164 148
165 @param txt current text of the edit (string) 149 @param txt current text of the edit (string)
166 @type str 150 @type str
167 """ 151 """
168 self.__updateTestButton() 152 self.__updateTestButton()
169 153
170 @pyqtSlot(bool) 154 @pyqtSlot(bool)
171 def on_mailAuthenticationGroup_toggled(self, checked): 155 def on_mailAuthenticationGroup_toggled(self, checked):
172 """ 156 """
173 Private slot to handle a change of the state of the authentication 157 Private slot to handle a change of the state of the authentication
174 group. 158 group.
175 159
176 @param checked state of the group (boolean) 160 @param checked state of the group (boolean)
177 """ 161 """
178 self.__updateTestButton() 162 self.__updateTestButton()
179 163
180 @pyqtSlot(str) 164 @pyqtSlot(str)
181 def on_mailUserEdit_textChanged(self, txt): 165 def on_mailUserEdit_textChanged(self, txt):
182 """ 166 """
183 Private slot to handle a change of the text of the user edit. 167 Private slot to handle a change of the text of the user edit.
184 168
185 @param txt current text of the edit (string) 169 @param txt current text of the edit (string)
186 """ 170 """
187 self.__updateTestButton() 171 self.__updateTestButton()
188 172
189 @pyqtSlot(str) 173 @pyqtSlot(str)
190 def on_mailPasswordEdit_textChanged(self, txt): 174 def on_mailPasswordEdit_textChanged(self, txt):
191 """ 175 """
192 Private slot to handle a change of the text of the user edit. 176 Private slot to handle a change of the text of the user edit.
193 177
194 @param txt current text of the edit (string) 178 @param txt current text of the edit (string)
195 """ 179 """
196 self.__updateTestButton() 180 self.__updateTestButton()
197 181
198 @pyqtSlot() 182 @pyqtSlot()
199 def on_testButton_clicked(self): 183 def on_testButton_clicked(self):
200 """ 184 """
201 Private slot to test the mail server login data. 185 Private slot to test the mail server login data.
202 """ 186 """
203 try: 187 try:
204 with EricOverrideCursor(): 188 with EricOverrideCursor():
205 if self.useSslButton.isChecked(): 189 if self.useSslButton.isChecked():
206 server = smtplib.SMTP_SSL(self.mailServerEdit.text(), 190 server = smtplib.SMTP_SSL(
207 self.portSpin.value(), 191 self.mailServerEdit.text(), self.portSpin.value(), timeout=10
208 timeout=10) 192 )
209 else: 193 else:
210 server = smtplib.SMTP(self.mailServerEdit.text(), 194 server = smtplib.SMTP(
211 self.portSpin.value(), 195 self.mailServerEdit.text(), self.portSpin.value(), timeout=10
212 timeout=10) 196 )
213 if self.useTlsButton.isChecked(): 197 if self.useTlsButton.isChecked():
214 server.starttls() 198 server.starttls()
215 server.login(self.mailUserEdit.text(), 199 server.login(self.mailUserEdit.text(), self.mailPasswordEdit.text())
216 self.mailPasswordEdit.text())
217 server.quit() 200 server.quit()
218 EricMessageBox.information( 201 EricMessageBox.information(
219 self, 202 self, self.tr("Login Test"), self.tr("""The login test succeeded.""")
220 self.tr("Login Test"), 203 )
221 self.tr("""The login test succeeded."""))
222 except (smtplib.SMTPException, OSError) as e: 204 except (smtplib.SMTPException, OSError) as e:
223 if isinstance(e, smtplib.SMTPResponseException): 205 if isinstance(e, smtplib.SMTPResponseException):
224 errorStr = e.smtp_error.decode() 206 errorStr = e.smtp_error.decode()
225 elif isinstance(e, socket.timeout): 207 elif isinstance(e, socket.timeout):
226 errorStr = str(e) 208 errorStr = str(e)
232 else: 214 else:
233 errorStr = str(e) 215 errorStr = str(e)
234 EricMessageBox.critical( 216 EricMessageBox.critical(
235 self, 217 self,
236 self.tr("Login Test"), 218 self.tr("Login Test"),
237 self.tr( 219 self.tr("""<p>The login test failed.<br>Reason: {0}</p>""").format(
238 """<p>The login test failed.<br>Reason: {0}</p>""") 220 errorStr
239 .format(errorStr)) 221 ),
240 222 )
223
241 @pyqtSlot() 224 @pyqtSlot()
242 def on_googleHelpButton_clicked(self): 225 def on_googleHelpButton_clicked(self):
243 """ 226 """
244 Private slot to show some help text "how to turn on the Gmail API". 227 Private slot to show some help text "how to turn on the Gmail API".
245 """ 228 """
246 if self.__helpDialog is None: 229 if self.__helpDialog is None:
247 try: 230 try:
248 from EricNetwork.EricGoogleMail import GoogleMailHelp 231 from EricNetwork.EricGoogleMail import GoogleMailHelp
232
249 helpStr = GoogleMailHelp() 233 helpStr = GoogleMailHelp()
250 except ImportError: 234 except ImportError:
251 helpStr = self.tr( 235 helpStr = self.tr(
252 "<p>The Google Mail Client API is not installed." 236 "<p>The Google Mail Client API is not installed."
253 " Use <code>{0}</code> to install it.</p>" 237 " Use <code>{0}</code> to install it.</p>"
254 ).format(getInstallCommand()) 238 ).format(getInstallCommand())
255 239
256 from EricWidgets.EricSimpleHelpDialog import EricSimpleHelpDialog 240 from EricWidgets.EricSimpleHelpDialog import EricSimpleHelpDialog
241
257 self.__helpDialog = EricSimpleHelpDialog( 242 self.__helpDialog = EricSimpleHelpDialog(
258 title=self.tr("Gmail API Help"), 243 title=self.tr("Gmail API Help"), helpStr=helpStr, parent=self
259 helpStr=helpStr, parent=self) 244 )
260 245
261 self.__helpDialog.show() 246 self.__helpDialog.show()
262 247
263 @pyqtSlot() 248 @pyqtSlot()
264 def on_googleInstallButton_clicked(self): 249 def on_googleInstallButton_clicked(self):
265 """ 250 """
266 Private slot to install the required packages for use of Google Mail. 251 Private slot to install the required packages for use of Google Mail.
267 """ 252 """
268 pip = ericApp().getObject("Pip") 253 pip = ericApp().getObject("Pip")
269 pip.installPackages(RequiredPackages, 254 pip.installPackages(RequiredPackages, interpreter=Globals.getPythonExecutable())
270 interpreter=Globals.getPythonExecutable())
271 self.__checkGoogleMail() 255 self.__checkGoogleMail()
272 256
273 @pyqtSlot() 257 @pyqtSlot()
274 def on_googleCheckAgainButton_clicked(self): 258 def on_googleCheckAgainButton_clicked(self):
275 """ 259 """
276 Private slot to check again the availability of Google Mail. 260 Private slot to check again the availability of Google Mail.
277 """ 261 """
278 self.__checkGoogleMail() 262 self.__checkGoogleMail()
279 263
280 def __checkGoogleMail(self): 264 def __checkGoogleMail(self):
281 """ 265 """
282 Private method to check the Google Mail availability and set the 266 Private method to check the Google Mail availability and set the
283 widgets accordingly. 267 widgets accordingly.
284 """ 268 """
285 self.googleMailInfoLabel.hide() 269 self.googleMailInfoLabel.hide()
286 self.googleInstallButton.show() 270 self.googleInstallButton.show()
287 self.googleCheckAgainButton.show() 271 self.googleCheckAgainButton.show()
288 self.googleHelpButton.setEnabled(True) 272 self.googleHelpButton.setEnabled(True)
289 self.googleMailCheckBox.setEnabled(True) 273 self.googleMailCheckBox.setEnabled(True)
290 274
291 try: 275 try:
292 import EricNetwork.EricGoogleMail # __IGNORE_WARNING__ 276 import EricNetwork.EricGoogleMail # __IGNORE_WARNING__
293 from EricNetwork.EricGoogleMailHelpers import ( 277 from EricNetwork.EricGoogleMailHelpers import isClientSecretFileAvailable
294 isClientSecretFileAvailable 278
295 )
296
297 self.googleInstallButton.hide() 279 self.googleInstallButton.hide()
298 if not isClientSecretFileAvailable(): 280 if not isClientSecretFileAvailable():
299 # secrets file is not installed 281 # secrets file is not installed
300 self.googleMailCheckBox.setChecked(False) 282 self.googleMailCheckBox.setChecked(False)
301 self.googleMailCheckBox.setEnabled(False) 283 self.googleMailCheckBox.setEnabled(False)
302 self.googleMailInfoLabel.setText(self.tr( 284 self.googleMailInfoLabel.setText(
303 "<p>The client secrets file is not present." 285 self.tr(
304 " Has the Gmail API been enabled?</p>")) 286 "<p>The client secrets file is not present."
287 " Has the Gmail API been enabled?</p>"
288 )
289 )
305 self.googleMailInfoLabel.show() 290 self.googleMailInfoLabel.show()
306 Preferences.setUser("UseGoogleMailOAuth2", False) 291 Preferences.setUser("UseGoogleMailOAuth2", False)
307 else: 292 else:
308 self.googleMailCheckBox.setChecked( 293 self.googleMailCheckBox.setChecked(
309 Preferences.getUser("UseGoogleMailOAuth2")) 294 Preferences.getUser("UseGoogleMailOAuth2")
295 )
310 self.googleMailInfoLabel.hide() 296 self.googleMailInfoLabel.hide()
311 self.googleCheckAgainButton.hide() 297 self.googleCheckAgainButton.hide()
312 except ImportError: 298 except ImportError:
313 # missing libraries, disable Google Mail 299 # missing libraries, disable Google Mail
314 self.googleMailCheckBox.setChecked(False) 300 self.googleMailCheckBox.setChecked(False)
315 self.googleMailCheckBox.setEnabled(False) 301 self.googleMailCheckBox.setEnabled(False)
316 self.googleMailInfoLabel.setText(self.tr( 302 self.googleMailInfoLabel.setText(
317 "<p>The Google Mail Client API is not installed." 303 self.tr(
318 " Use <code>{0}</code> to install it.</p>" 304 "<p>The Google Mail Client API is not installed."
319 ).format(getInstallCommand())) 305 " Use <code>{0}</code> to install it.</p>"
306 ).format(getInstallCommand())
307 )
320 self.googleMailInfoLabel.show() 308 self.googleMailInfoLabel.show()
321 self.googleHelpButton.setEnabled(False) 309 self.googleHelpButton.setEnabled(False)
322 Preferences.setUser("UseGoogleMailOAuth2", False) 310 Preferences.setUser("UseGoogleMailOAuth2", False)
323 311
324 312
325 def create(dlg): 313 def create(dlg):
326 """ 314 """
327 Module function to create the configuration page. 315 Module function to create the configuration page.
328 316
329 @param dlg reference to the configuration dialog 317 @param dlg reference to the configuration dialog
330 @return reference to the instantiated page (ConfigurationPageBase) 318 @return reference to the instantiated page (ConfigurationPageBase)
331 """ 319 """
332 page = EmailPage() 320 page = EmailPage()
333 return page 321 return page

eric ide

mercurial