UI/EmailDialog.py

changeset 5769
944c04cec861
parent 5758
e9af479d5c48
child 5786
6a1b25cff5fb
diff -r 6f4d7d02cb1e -r 944c04cec861 UI/EmailDialog.py
--- a/UI/EmailDialog.py	Sun Jun 25 16:17:00 2017 +0200
+++ b/UI/EmailDialog.py	Mon Jun 26 19:31:49 2017 +0200
@@ -4,7 +4,7 @@
 #
 
 """
-Module implementing a dialog to send bug reports.
+Module implementing a dialog to send bug reports or feature requests.
 """
 
 from __future__ import unicode_literals
@@ -64,7 +64,7 @@
 
 class EmailDialog(QDialog, Ui_EmailDialog):
     """
-    Class implementing a dialog to send bug reports.
+    Class implementing a dialog to send bug reports or feature requests.
     """
     def __init__(self, mode="bug", parent=None):
         """
@@ -96,6 +96,9 @@
         self.sendButton.setEnabled(False)
         self.sendButton.setDefault(True)
         
+        self.googleHelpButton = self.buttonBox.addButton(
+            self.tr("Google Mail API Help"), QDialogButtonBox.HelpRole)
+        
         height = self.height()
         self.mainSplitter.setSizes([int(0.7 * height), int(0.3 * height)])
         
@@ -117,6 +120,8 @@
         
         self.__deleteFiles = []
         
+        self.__helpDialog = None
+    
     def keyPressEvent(self, ev):
         """
         Protected method to handle the user pressing the escape key.
@@ -139,6 +144,8 @@
         """
         if button == self.sendButton:
             self.on_sendButton_clicked()
+        elif button == self.googleHelpButton:
+            self.on_googleHelpButton_clicked()
         
     def on_buttonBox_rejected(self):
         """
@@ -150,7 +157,29 @@
             self.tr("""Do you really want to close the dialog?"""))
         if res:
             self.reject()
+    
+    @pyqtSlot()
+    def on_googleHelpButton_clicked(self):
+        """
+        Private slot to show some help text "how to turn on the Gmail API".
+        """
+        if self.__helpDialog is None:
+            try:
+                from E5Network.E5GoogleMail import GoogleMailHelp
+                help = GoogleMailHelp()
+            except ImportError:
+                help = self.tr(
+                    "<p>The Google Mail Client API is not installed."
+                    " Use <code>pip install --upgrade google-api-python-client"
+                    "</code> to install it.</p>")
+            
+            from E5Gui.E5SimpleHelpDialog import E5SimpleHelpDialog
+            self.__helpDialog = E5SimpleHelpDialog(
+                title=self.tr("Gmail API Help"),
+                help=help, parent=self)
         
+        self.__helpDialog.show()
+    
     @pyqtSlot()
     def on_sendButton_clicked(self):
         """
@@ -160,8 +189,11 @@
             msg = self.__createMultipartMail()
         else:
             msg = self.__createSimpleMail()
-            
-        ok = self.__sendmail(msg)
+        
+        if Preferences.getUser("UseGoogleMailOAuth2"):
+            ok = self.__sendmailGoogle(msg)
+        else:
+            ok = self.__sendmail(msg.as_string())
         
         if ok:
             for f in self.__deleteFiles:
@@ -203,7 +235,8 @@
         """
         Private method to create a simple mail message.
         
-        @return string containing the mail message
+        @return prepared mail message
+        @rtype email.mime.text.MIMEText
         """
         msgtext = "{0}\r\n----\r\n{1}----\r\n{2}----\r\n{3}".format(
             self.message.toPlainText(),
@@ -217,13 +250,14 @@
         subject = '[eric6] {0}'.format(self.subject.text())
         msg['Subject'] = self.__encodedHeader(subject)
         
-        return msg.as_string()
+        return msg
         
     def __createMultipartMail(self):
         """
         Private method to create a multipart mail message.
         
-        @return string containing the mail message
+        @return prepared mail message
+        @rtype email.mime.text.MIMEMultipart
         """
         mpPreamble = ("This is a MIME-encoded message with attachments. "
                       "If you see this message, your mail client is not "
@@ -273,7 +307,7 @@
             att.add_header('Content-Disposition', 'attachment', filename=name)
             msg.attach(att)
             
-        return msg.as_string()
+        return msg
 
     def __sendmail(self, msg):
         """
@@ -320,7 +354,7 @@
                         errorStr = str(e)
                     res = E5MessageBox.retryAbort(
                         self,
-                        self.tr("Send bug report"),
+                        self.tr("Send Message"),
                         self.tr(
                             """<p>Authentication failed.<br>Reason: {0}</p>""")
                         .format(errorStr),
@@ -348,7 +382,7 @@
                 errorStr = str(e)
             res = E5MessageBox.retryAbort(
                 self,
-                self.tr("Send bug report"),
+                self.tr("Send Message"),
                 self.tr(
                     """<p>Message could not be sent.<br>Reason: {0}</p>""")
                 .format(errorStr),
@@ -358,7 +392,34 @@
             else:
                 return False
         return True
+    
+
+    def __sendmailGoogle(self, msg):
+        """
+        Private method to actually send the message via Google Mail.
         
+        @param message email message to be sent
+        @type email.mime.text.MIMEBase
+        @return flag indicating success
+        @rtype bool
+        """
+        from E5Network.E5GoogleMail import GoogleMailSendMessage
+        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
+        QApplication.processEvents()
+        ok, result = GoogleMailSendMessage(msg)
+        QApplication.restoreOverrideCursor()
+        if not ok:
+            # we got an error
+            E5MessageBox.critical(
+                self,
+                self.tr("Send Message"),
+                self.tr(
+                    """<p>Message could not be sent.<br>Reason: {0}</p>""")
+                .format(result)
+            )
+        
+        return ok
+    
     @pyqtSlot()
     def on_addButton_clicked(self):
         """

eric ide

mercurial