From 0e926185415c6deb2943c2aea4626189a3f462b7 Mon Sep 17 00:00:00 2001
From: Yusei Tahara <yusei@nexedi.com>
Date: Tue, 8 Jan 2008 18:57:15 +0000
Subject: [PATCH] Temporary dirty fix, need refactoring and cleaning up.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@18635 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/EmailDocument.py | 46 +++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 8 deletions(-)

diff --git a/product/ERP5/Document/EmailDocument.py b/product/ERP5/Document/EmailDocument.py
index d7ad74d050..4d968d6bb7 100644
--- a/product/ERP5/Document/EmailDocument.py
+++ b/product/ERP5/Document/EmailDocument.py
@@ -40,6 +40,15 @@ from Products.ERP5.Document.TextDocument import TextDocument
 from Products.ERP5.Document.File import File
 from Products.ERP5.Document.Document import ConversionError
 from Products.CMFDefault.utils import isHTMLSafe
+try:
+  from Products.MimetypesRegistry.common import MimeTypeException
+except ImportError:
+  class MimeTypeException(Exception):
+    """
+    A dummy exception class which is used when MimetypesRegistry product is
+    not installed yet.
+    """
+  
 
 from email import message_from_string
 from email.Header import decode_header
@@ -440,15 +449,36 @@ class EmailDocument(File, TextDocument):
       # Attach files
       document_type_list = self.getPortalDocumentTypeList()
       for attachment in self.getAggregateValueList():
+        mime_type = None
+        attached_data = None
         if attachment.getPortalType() in document_type_list:
-          # If this is a document, use 
-          mime_type = attachment.getContentType() # WARNING - this could fail since getContentType
-                                                  # is not (yet) part of Document API
-          try:
-            mime_type, attached_data = attachment.convert(mime_type)
-          except ConversionError:
-            mime_type = attachment.getBaseContentType()
-            attached_data = attachment.getBaseData()
+          # If this is a document, use
+
+          # WARNING - this could fail since getContentType
+          # is not (yet) part of Document API
+          if getattr(attachment, 'getContentType', None) is not None:
+            mime_type = attachment.getContentType()
+          elif getattr(attachment, 'getTextFormat', None) is not None:
+            mime_type = attachment.getTextFormat()
+          else:
+            raise ValueError, "Cannot find mimetype of the document."
+
+          if mime_type is not None:
+            try:
+              mime_type, attached_data = attachment.convert(mime_type)
+            except ConversionError:
+              mime_type = attachment.getBaseContentType()
+              attached_data = attachment.getBaseData()
+            except (NotImplementedError, MimeTypeException):
+              pass
+
+          if attached_data is None:
+            if getattr(attachment, 'getTextContent', None) is not None:
+              attached_data = attachment.getTextContent()
+            elif getattr(attachment, 'getData', None) is not None:
+              attached_data = attachment.getData()
+            elif getattr(attachment, 'getBaseData', None) is not None:
+              attached_data = attachment.getBaseData()
         else:
           mime_type = 'application/pdf'
           attached_data = attachment.asPDF() # XXX - Not implemented yet
-- 
2.30.9