#format python
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - Visio Macro

    PURPOSE:
        This macro is used to enable an attached Visio file to be displayed
        in a browser. Optionally, the size of the image can be adjusted.

    CALLING SEQUENCE:
        [[visio(attachment,[width=width],[height=height])]]

    INPUTS:
        attachment:file name of attachment

    KEYWORD PARAMETERS:
        width: width of the embedded image (default is 100%)
	      height: height of the embedded image (default is 100%)

    EXAMPLE:
      [[visio(organogram.vsd]]
      [[visio(organogram.vsd,width=800,height=600)]]
      [[visio(organogram.vsd,width=50%)]]

      This macro must be put in "[yourwiki]/data/plugin/macro/"

    CREDITS:
      This macro was built using the SVG macro as a template,
      which was built using the ImageLink macro as a template.
      Please feel free to post improved versions of it.

    MODIFICATION HISTORY:
        @license: GNU GPL, see COPYING for details.


"""

from MoinMoin.action  import AttachFile
from MoinMoin         import wikiutil, config
import os,string


def execute(macro, text):

  kw = {'width': '95%', 'height': '100%'}

  if text:
    args = text.split(',')
  else:
    args = []

  argCount = len(args)
  kwCount = 0
  for a in args:
    if (a.find('=') > -1):
      kwCount = kwCount + 1
      key = a.split('=')
      kw[str(key[0])] = wikiutil.escape(string.join(key[1],''), quote=1)

  argCount = argCount - kwCount

  if (argCount < 1):
    msg = 'Not enough arguments to visio macro! Try [[visio(attachment[,width=n][,height=n])]]'
    return macro.formatter.sysmsg(1) + macro.formatter.text(msg) + macro.formatter.sysmsg(0)

  attachmentName = wikiutil.taintfilename(macro.formatter.text(args[0]))
  currentPageName = macro.formatter.page.page_name

  kw['src'] = AttachFile.getAttachUrl(currentPageName,attachmentName,macro.request)
  attachmentPath = os.path.join(AttachFile.getAttachDir(macro.request,currentPageName), attachmentName)

  if not os.path.exists(attachmentPath):
    import urllib
    linktext = macro.request.getText('Upload new attachment "%(filename)s"')
    return wikiutil.link_tag(macro.request,
                             '%s?action=AttachFile&amp;rename=%s' %
                             #(wikiutil.quoteWikinameFS(currentPageName),
                              (wikiutil.quoteWikinameURL(currentPageName),
                              urllib.quote_plus(attachmentName)),
                             linktext % {'filename': attachmentName})

  if (argCount == 1):
    src = kw['src'].split(u'/')[-1]
    
    objectTag = '<object classid="CLSID:279D6C9A-652E-4833-BEFC-312CA8887857" codebase="http://download.microsoft.com/download/visiostandard2003/vviewer/2003/w2kxp/en-us/vviewer.exe" id="viewer1" width="%s" height="%s"> <param name="CurrentPageIndex" value="0"> <param name="Zoom" value="-1"> <param name = "SRC" value = "%s"/>Your browser cannot display Visio</object>' \
                % ( kw['width'], kw['height'], wikiutil.escape(src))
    return macro.formatter.rawHTML(objectTag)


