##############################################################################
#
# Copyright (c) 2002 Ingeniweb SARL
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################

"""
AttachmentField product
"""

import AttachmentHandler

import os
import os.path
import string
import re
import sys

if sys.platform == "win32":
    program = "bin\\wvware.exe"
else:
    program = "wvWare"

class MSWordAttachment(AttachmentHandler.AbstractHandler):
    """
    MSWord file abstraction
    """
    icon_file = "word.gif"
    small_icon_file = "word_small.gif"
    content_types = ('application/msword', )
    converter_type = "MSWord"

    index_path = program
    index_arguments = r"%s"          # XXX SHOULD BE "-d DIR %s"
    index_encoding = ("CP1252", "utf8", "latin1", )
    
    preview_path = program
    preview_arguments = r"%s"
    preview_format = "html"
    preview_encoding = ("CP1252", "utf8", "latin1", )


# XXX TODO: BETTER PREVIEW

##def cleanHTMLcode(self, htmlcode): 
##        """ Clean style attributes from HTML """ 
##        retidy=re.compile('style\s*=\s*([\'\"])[^\"\']*\\1',re.IGNORECASE) 
##        return re.sub(retidy,'',htmlcode)

    

## XXX TODO: IMAGES MANAGEMENT
##
##    def convertPreview(self,):
##        """
##        Let's convert things into pretty HTML !
##        """
##        # Perform conversion and retreive files as well
##        fn = self.writeAttachmentFile()
##        try:
##            # Actual HTML conversion
##            ret = self.callConverter(arguments = "-d %s %s" % (self.getAttachmentFileDir(), fn, ))

##            # Retreive images
##            Log(LOG_DEBUG, "Temporary files:", os.listdir(self.getAttachmentFileDir()))
##            for fn in os.listdir(self.getAttachmentFileDir()):
##                ext = string.lower(string.split(fn, ".")[-1])
##                if ext in ('png', 'jpg', 'gif', 'wmz', 'wmf', 'tif', 'tiff', 'jpeg', ):
##                    filename = os.path.join(self.getAttachmentFileDir(), fn,)
##                    img = open(filename, "rb")
##                    self.addRelatedFile(fn, img.read(), "image/%s" % ext)
##                    Log(LOG_DEBUG, "Filename to delete", filename)

##                    # Close and delete file
##                    # We ignore temp file deletion problems
##                    img.close()
##                    try:        os.unlink(filename)
##                    except:     LogException()
            
##        finally:
##            self.deleteAttachmentFile()

##        return self.stripBody(ret)


AttachmentHandler.registerHandler(MSWordAttachment)
