# -*- coding: utf-8 -*-
# (c) 2003 Centre de Recherche en Informatique ENSMP Fontainebleau <http://cri.ensmp.fr>
# (c) 2003 Benoît PIN <mailto:pin@cri.ensmp.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#

from OFS.Image import cookId
from Globals import InitializeClass

from Products.CMFCore.permissions import View, ModifyPortalContent
from AccessControl import ClassSecurityInfo
from Products.Photo import Photo
from BaseSlot import BaseSlot

factory_type_information = ( {'id'             : 'Image Slot',
                              'meta_type'      : 'Image Slot',
                              'description'    : "Image Slot for Mosaic Document",
                              'icon'           : 'mosaic_tool/photo_icon.gif',
                              'product'        : 'MosaicDocument',
                              'factory'        : 'addImageSlot',
                              'immediate_view' : 'view',
                              'actions'        :
                                ({'id'            : 'view',
                                  'name'          : 'View',
                                  'action'        : 'slot_image_view',
                                  'permissions'   : (View, )
                                  },
                                 
                                 {'id'            : 'edit',
                                  'name'          : 'Edit',
                                  'action'        : 'slot_image_form',
                                  'permissions'   : (ModifyPortalContent, )
                                  },
                                 )
                               },
                             )

class ImageSlot(BaseSlot, Photo) :
    """Slot"""
    meta_type = 'Image Slot'
    manage_options = Photo.manage_options

    _indexableFields = ('title',)

    security = ClassSecurityInfo()

            
    def __init__(self, parentContainer, id, title='', file='',
                 content_type='', precondition='', blankThumbnail = '', **kw) :
        blankThumbnailOb = None
        if blankThumbnail :
            blankThumbnailOb = parentContainer.restrictedTraverse(blankThumbnail)
        Photo.__init__(self, id, title=title, file=file,
                       defaultBlankThumbnail = blankThumbnailOb, **kw)


    # This method is overloaded to manage file upload.
    # It's necessary to use manage_upload method from image
    # because manage_upload invoke update_data and the overload of
    # update_data in Photo product update the internal thumbnail image.
    security.declareProtected(ModifyPortalContent, 'edit')
    def edit(self, title='', thumb_width='440', thumb_height='440', file='', REQUEST=None):
        """ Edit image slot"""
        if file :
            self.manage_upload(file=file)
        
        if self.thumb_height != thumb_height or thumb_width != thumb_width :
            if thumb_width <= thumb_height :
                self.manage_editProperties(thumb_width=thumb_width, thumb_height=thumb_height)
            else :
                self.manage_editProperties(thumb_width=thumb_height, thumb_height=thumb_width)
        if title :
            self.manage_editProperties(title = title, no_refresh = 1)
        
    view = index_html = __call__ = Photo.index_html

InitializeClass(ImageSlot)

def addImageSlot(dispatcher, id, file='', title='',
             precondition='', content_type='', REQUEST=None, **kw) :
    """
    Add a new Photo object.
    Creates a new Photo object 'id' with the contents of 'file'.
    """
    id=str(id)
    title=str(title)
    content_type=str(content_type)
    precondition=str(precondition)

    id, title = cookId(id, title, file)
    parentContainer = dispatcher.Destination()

    # First, we create the image without data:
    parentContainer._setObject(id, ImageSlot(parentContainer, id,title,'',content_type, precondition, **kw))

    # Now we "upload" the data.  By doing this in two steps, we
    # can use a database trick to make the upload more efficient.
    if file:
        parentContainer._getOb(id).manage_upload(file)
    if content_type:
        parentContainer._getOb(id).content_type=content_type

    if REQUEST is not None:
        try:    url=dispatcher.DestinationURL()
        except: url=REQUEST['URL1']
        REQUEST.RESPONSE.redirect('%s/manage_main' % url)
    return id
