# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - gallery2Image Actionmacro

    PURPOSE:
        This action macro is used to rotate the images from Gallery2

    CALLING SEQUENCE:
        called by Gallery2 POST Method

    PROCEDURE:
        see Gallery2

	Please remove the version number from this file
	
    RESTRICTIONS:
        spaces in file names are not supported. I don't know how to escape them right.	

    MODIFICATION HISTORY:
        Version 1.3.3.-1
        @copyright: 2005 by Reimar Bauer (R.Bauer@fz-juelich.de)
        @license: GNU GPL, see COPYING for details.
	2005-06-24: 1.3.3.-2 feature reqeust of CraigJohnson added 
	                     os.path.join used to join platform independent pathes 
			     os.unlink removed to get it more platform independend
	

"""

import os,string,Image
from MoinMoin import config, wikiutil
from MoinMoin.PageEditor import PageEditor
from MoinMoin import user, util
from MoinMoin.Page import Page
from MoinMoin.action import AttachFile


action_name = __name__.split('.')[-1]

def execute(pagename, request):
    """ Main dispatcher for the 'Gallery' action.
    """
    _ = request.getText

    if(request.form['do'][0] == 'PS'):
            images=string.split(request.form['target'][0],',')
            attachment_path = AttachFile.getAttachDir(request,pagename)
           # for img in images:
           #     infile=attachment_path+'/'+img
           #     im = Image.open(infile)
           #     src=im.tostring()

            msg= _('not finished by now')

    elif request.user.may.delete(pagename):
       # only users which are allowed to delete should use this tool

        target=request.form['target'][0]
        file, ext = os.path.splitext(target)

        if (ext == '.gif') or (ext == '.png'):
            img_type='PNG'
            thumbfile='thumbnail_'+file+".png"
            webnail='webnail_'+file+".png"
        else:
            img_type="JPEG"
            thumbfile='thumbnail_'+file+".jpg"
            webnail='webnail_'+file+".jpg"


        attachment_path = AttachFile.getAttachDir(request,pagename)
        thumb=os.path.join(attachment_path,thumbfile)
        webf=os.path.join(attachment_path,webnail)
        infile=os.path.join(attachment_path,target)
	
        msg = None
        if action_name in request.cfg.excluded_actions:
            msg = _('File attachments are not allowed in this wiki!')

        elif (request.form['do'][0] == 'RM'):
	    if os.path.exists(infile+'.bak'):
	       os.unlink(infile+'.bak')
            os.link(infile,infile+'.bak')
            os.unlink(infile)
            os.unlink(webf)
            os.unlink(thumb)

            msg= _('%(target)s deleted, backup in place' % {
                  'target':target})

        elif (request.form['do'][0] == 'RL'):
            im = Image.open(infile)
            #os.unlink(infile)
            im.rotate(90).save(infile,img_type)
            nim = Image.open(infile)
            nim.thumbnail((640,640), Image.ANTIALIAS)
            #os.unlink(webf)
            nim.save(webf, img_type)
            nim.thumbnail((128, 128), Image.ANTIALIAS)
            #os.unlink(thumb)
            nim.save(thumb, img_type)
            msg= _('%(target)s rotated to left 90 degrees' % {
                  'target':target})

        elif (request.form['do'][0] == 'RR'):
            im = Image.open(infile)
            #os.unlink(infile)
            im.rotate(270).save(infile,img_type)


            nim = Image.open(infile)

            nim.thumbnail((640,640), Image.ANTIALIAS)
            #os.unlink(webf)
            nim.save(webf, img_type)
            nim.thumbnail((128, 128), Image.ANTIALIAS)
            #os.unlink(thumb)
            nim.save(thumb, img_type)
            msg= _('%(target)s rotated to right 90 degrees' % {
                  'target':target})

        else:
            msg = _('action not implemented: %s') % (request.form['do'][0],)
    else:
        msg = _('Your are not allowed to change images on this page: %s') % (pagename,)

    if msg:
        AttachFile.error_msg(pagename, request, msg)

    return()


