# -*- 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
        
"""    


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

def execute(macro, text):
    kw = {} 
    if text:
        args=text.split(',')
    else:
        args=[]
      
    number_args = len(args)
    count = 0
    kw["width"] = "100%"
    kw["height"] = "100%"
    kw["play"] = "true"
    kw["loop"] = "true"
    kw["quality"] = "high"

    for a in args:
        if (a.find('=') > -1):
           count += 1
           key = a.split('=')
           kw[str(key[0])] = wikiutil.escape(string.join(key[1],''), quote=1)

    number_args = number_args - count
 
    if number_args < 1:
       msg='Not enough arguments to EO macro! Try [[EO(attachment [,width=width] [,height=heigt])]]'
       return macro.formatter.sysmsg(1) + macro.formatter.text(msg) + macro.formatter.sysmsg(0)
    else:    
       filename = args[0]
       
    if filename.find('/') > -1 and filename.find(':') == -1:
        pagename,filename = string.split(filename,'/')
    else:
        pagename = macro.formatter.page.page_name     
    
    
    attachment_file = os.path.join(AttachFile.getAttachDir(macro.request,pagename), filename)
    
    if not os.path.exists(attachment_file):
            import urllib
            linktext = macro.request.getText('Upload new attachment "%(filename)s"'%{
            "filename":filename})
            return wikiutil.link_tag(macro.request,
                             "%(pagename)s?action=AttachFile&amp;rename=%(newname)s" % {
                             "pagename":pagename,
                             "newname": filename},linktext)

    url = AttachFile.getAttachUrl(pagename,filename,macro.request)
    
    mime_type, enc = mimetypes.guess_type(filename)
    
    if mime_type == "application/x-shockwave-flash" :
    
        txt = '''
<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}
        
        return txt
        
    elif mime_type == "image/svg+xml": 
        txt = '''
<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}
        
        return txt

    elif mime_type == "application/pdf":
       txt = '''
<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}

       return txt

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