src/eric7/Preferences/ConfigurationPages/EmailPage.py

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

eric ide

mercurial