# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - embbed jgraphpad macro
    more documentation available at: http://wiki.visualmodeller.org

    @copyright: 2006 by Raphael Valyi
    @license: GNU GPL, see COPYING for details.
"""

from MoinMoin.Page import Page
import re, os


# README:
# make sure the following settings match with your own MoinMoin configuration
host = "http://wiki.visualmodeller.org"    #the url before the page name
javascript_helper_path = "/applets/JGraphpadCE/lib/javascript/firepad.js"  #relative path to the javascript helper
webstart_launchers_dir = "/home/rvalyi/public_html/wiki/applets/JGraphpadCE"   #the place were you want the jnlp webstart launchers to be written out


javascript_helper_url = host + javascript_helper_path
codebase = host + "/applets/JGraphpadCE"   #relative url to the jars (or to the the correct server redirectors) 
host_without_protocol = host.replace('http://', '')


#generic parameters you might also want to change; see firepad.js javascript file for more details
archive = "jgraphpad.jar,jgraphpad_lazy.jar,library.jar,translations.jar,gpgraph.jar,layouts.jar,console.jar,codecs.jar" #you shoud also change the jnlp generation if you change it!
default_mainClass = "org.jgraph.pad.jgraphpad.JGraphpad.class"
default_plugins = "no"		 #add the plugins if you want
mode = "both"    #you can also use 'applet' or 'webstart'
appletAutoPopup = "true"



def execute(macro, args):

#first we take some parameters from the wiki macro. May be a more generic system (like a map) could allow to pass more parameters
    args = re.split (r'[;\s]+', args)  #WARNING: we param separator is ";"

    try:
        basename = args[0]    #diagram name param
    except IndexError:
        basename = "diagram1"

    try:
        plugins = args[1]     #plugins if any, see JGraphpad examples; plugins should be separated by ","
    except IndexError:
        plugins = default_plugins

    try:
        appletMain = args[2]  #the main class
    except IndexError:
        appletMain = default_mainClass


#common parameters for the applet code and the jnlp webstart code
    pagename = macro.formatter.page.page_name
    request = macro.request
    savelink = Page(request, pagename).url(request) + "?action=AttachFile"
    drawpath = savelink + """&do=get&target=%(basename)s.xml""" % {'basename' : basename}
    pngpath = savelink + """&do=get&target=%(basename)s.png""" % {'basename' : basename}
    fullpng = host + pngpath
    

#check if the user may upload the modified diagram or not
    readOnly="false"
    if not request.user.may.write(pagename):
        readOnly="true"
    elif not macro.formatter.page.isWritable():
        readOnly="true"



#jnlp webstart launcher generation: this file should can't be generated by javascript, it should be available in a given URL of the server
#comment this section if you don't want webstart support
    jnlpfile = webstart_launchers_dir + "/wspad_" + pagename + "_" + basename +".jnlp"
    jnlpurl = codebase + "/wspad_" + pagename + "_" + basename + ".jnlp"
    code = """<?xml version="1.0" encoding="utf-8"?>
<jnlp
  spec="1.0+"
  codebase="%(codebase)s"   href="%(jnlpurl)s">
  <information>
    <title>JGraphpad Community Edition</title>
    <vendor>the JGraph Team</vendor>
    <description>JGraphpad Community Edition Diagram Editor</description>
    <offline-allowed/>
  </information>  
  <resources>
    <j2se version="1.4+"/>
    <jar href="jgraphpad.jar"/>
    <jar href="jgraphpad_lazy.jar"/>
    <jar href="library.jar"/>
    <jar href="translations.jar"/>
    <jar href="gpgraph.jar"/>
    <jar href="library.jar"/>
    <jar href="console.jar"/>
    <jar href="codecs.jar"/>
  </resources>
  <application-desc>
  <argument>-h</argument>
  <argument>%(host_without_protocol)s</argument>
  <argument>-p</argument>
  <argument>80</argument>
  <argument>-t</argument>
  <argument>http</argument>
  <argument>-d</argument>
  <argument>%(drawpath)s</argument>
  <argument>-v</argument>
  <argument>%(pngpath)s</argument>
  <argument>-f</argument>
  <argument>%(basename)s</argument>
  <argument>-u</argument>
  <argument>%(savelink)s</argument>
  <argument>-n</argument>
  <argument>no</argument>
  </application-desc>
</jnlp>""" % {
    'pngpath': pngpath, 'drawpath': drawpath, 'jnlpurl': jnlpurl,
    'savelink': savelink, 'basename': basename, 'codebase': codebase, 'host_without_protocol': host_without_protocol}
    file = open(jnlpfile,'w')
    file.write(code)
    file.close()
    os.chmod(jnlpfile, 0755) #todo: make sure this also work on windows based servers
#end of the webstart section


	
#firepad applet settings
    code = """<script>id='%(basename)s'
    archive = '%(archive)s'
    basename='%(basename)s'
    mode = '%(mode)s'
    xmlDownloadPath='%(drawpath)s'
    xmlUploadLPath='%(savelink)s'
    imageDownloadPath='%(fullpng)s'
    plugins='%(plugins)s'
    appletMain='%(appletMain)s'
    readOnly='%(readOnly)s'
    webstartLink='%(jnlpurl)s'
    </script><script type='text/javascript' src='%(javascript_helper_url)s'></script>""" % {
    'pngpath': pngpath, 'drawpath': drawpath, 'fullpng' : fullpng,
    'savelink': savelink, 'basename': basename, 'plugins': plugins, 'archive': archive, 'mode': mode, 'appletAutoPopup': appletAutoPopup,
    'appletMain': appletMain, 'readOnly':readOnly, 'jnlpurl':jnlpurl, 'javascript_helper_url':javascript_helper_url}
    return code
    

