# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - EO Macro

    PURPOSE:
        This macro is used to Embed an Object into a wiki page. Optional size of the object
        could be adjusted. Further keywords are dependent on the kind of application

    CALLING SEQUENCE:
        [[EO(attachment[,width=width][,height=heigt])]]

    INPUTS:
        attachment:image name of attachment

    KEYWORD PARAMETERS:
        width: width of the embedded image, default is 100% of window
        height: height of the embedded image, default is 100% of window

        application/x-shockwave-flash:
          play: true is default
          loop: true is default
          quality: high is default (medium,low)

    EXAMPLE:
        [[EO]]
        [[EO(example.swf)]]
        [[EO(example.swf,width=637,height=392)]]
        [[EO(SlideShow/example.swf,width=637,height=392)]]
        [[EO(SlideShow/example.swf,width=637,height=392,play=false)]]
        [[EO(SlideShow/example.swf,width=637,height=392,play=false,loop=false)]]
        [[EO(SlideShow/example.swf,width=637,height=392,play=false,loop=low)]]

    PROCEDURE:
        This routine requires attachment enabled. If the attachment isn't downloaded at all
        the attachment line is shown.
        If you give only one size argument e.g. width only the other one is calculated

        By the swftools it is possible to get the swf size returned. I don't know if it is 
        possible to get sizes for svg, pdf and others detected too, that's the reason why 
        I haven't added it by now.

        Please add needed mimetypes as objects.

        It must be in "MoinMoin/macro"

        Please remove the version number from the file name!

    MODIFICATION HISTORY:
        @copyright: 2006 by Reimar Bauer (R.Bauer@fz-juelich.de)      
        initial version: 1.5.0-1
        2006-05-04 TomSi: added mp3 support
        2006-05-09 RB code refactored, fixed a taintfilename bug

"""
import os, mimetypes
from MoinMoin import wikiutil
from MoinMoin.action import AttachFile

def execute(macro, args):
    request = macro.request
    _ = request.getText
    formatter = macro.formatter
    if args:
        args = args.split(',')
        args = [arg.strip() for arg in args]
    else:
        args = []
        
    argc = len(args)
    kw_count = 0
    kw = {} # create a dictionary 
    kw["width"] = "100%"
    kw["height"] = "100%"
    kw["play"] = "true"
    kw["loop"] = "true"
    kw["quality"] = "high"
    
    for arg in args :
        if '=' in arg:
            kw_count += 1
            key, value = arg.split('=', 1)
            kw[str(key)] = wikiutil.escape(value, quote=1)

    argc -= kw_count
   
    if not argc:
       msg='Not enough arguments to EO macro! Try [[EO(attachment [,width=width] [,height=heigt])]]'
       return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
    else:    
        target = args[0]

    target = wikiutil.taintfilename(target)
    pagename, attname = AttachFile.absoluteName(target, formatter.page.page_name)
    attachment_fname = AttachFile.getFilename(request, pagename, attname)

    if not os.path.exists(attachment_fname):
        linktext = _('Upload new attachment "%(filename)s"')
        return wikiutil.link_tag(request,
                                 ('%s?action=AttachFile&rename=%s' % (
                                  wikiutil.quoteWikinameURL(pagename),
                                  wikiutil.url_quote_plus(attname))),
                                  linktext % {'filename': attname})   

    url = AttachFile.getAttachUrl(pagename,attname,request)

    mime_type, enc = mimetypes.guess_type(attname)

    if mime_type == "application/x-shockwave-flash":
        return '''
<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
WIDTH="%(width)s"
HEIGHT="%(height)s"
CODEBASE="http://active.macromedia.com/flash5/cabs/swflash.cab#version=6,0,23,0">
<PARAM NAME="MOVIE" VALUE="%(file)s">
<PARAM NAME="PLAY" VALUE="%(play)s">
<PARAM NAME="LOOP" VALUE="%(loop)s">
<PARAM NAME="QUALITY" VALUE="%(quality)s">
<EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s"
PLAY="%(play)s" ALIGN="" LOOP="%(loop)s" QUALITY="%(quality)s"
TYPE="application/x-shockwave-flash"
PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
</EMBED>
</OBJECT>''' % {
"width": kw["width"],
"height": kw["height"],
"play": kw["play"],
"loop": kw["loop"],
"quality": kw["quality"],
"file": url}

    elif mime_type == "image/svg+xml": 
        return '''
<OBJECT CLASSID="" 
WIDTH="%(width)s"
HEIGHT="%(height)s"
CODEBASE="http://purl.org/dc/dcmitype/StillImage">
<EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s"
TYPE="image/svg+xml">
</EMBED>
</OBJECT>''' % {
"width": kw["width"],
"height": kw["height"],
"file": url}

    elif mime_type == "application/pdf":
        return '''
<OBJECT CLASSID=""
WIDTH="%(width)s"
HEIGHT="%(height)s"
CODEBASE="http://www.adobe.com">
<EMBED SRC="%(file)s" WIDTH="%(width)s" HEIGHT="%(height)s"
TYPE="application/pdf">
</EMBED>
</OBJECT>''' % {
"width": kw["width"],
"height": kw["height"],
"file": url}

    elif mime_type == "audio/mpeg":
        return '''
<OBJECT CLASSID=""
WIDTH="%(width)s"
HEIGHT="%(height)s"
<EMBED SRC="%(file)s" HEIGHT="0" REPEAT="TRUE" AUTOSTART="TRUE" WIDTH="0" OP="TRUE"
TYPE="audio/mpeg">
</EMBED>
</OBJECT>''' % {
"width": kw["width"],
"height": kw["height"],
"file": url}

    else: 
        msg = 'Not supported mimetype %(mimetype)s ' % {"mimetype":mime_type}
        return "%s%s%s" % (macro.formatter.sysmsg(1),
                           macro.formatter.text(msg),
                           macro.formatter.sysmsg(0))