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

    MODIFICATION HISTORY:
        Version 1.3.3.-1
        @copyright: 2005 by Reimar Bauer (R.Bauer@fz-juelich.de)
        @license: GNU GPL, see COPYING for details.

"""

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=attachment_path+'/'+thumbfile
        webf=attachment_path+'/'+webnail
        infile=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'):
            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()

