# -*- 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 Products.CMFCore.permissions import View, ModifyPortalContent

factory_type_information = ( {'id'             : 'String Slot',
                              'meta_type'      : 'String Slot',
                              'description'    : "String Slot for Mosaic Document",
                              'icon'           : 'mosaic_tool/str_icon.gif',
                              'product'        : 'MosaicDocument',
                              'factory'        : 'addStringSlot',
                              'immediate_view' : 'view',
                              'actions'        :
                                ({'id'            : 'view',
                                  'name'          : 'View',
                                  'action'        : 'slot_string_view',
                                  'permissions'   : (View, )
                                  },
                                 
                                 {'id'            : 'edit',
                                  'name'          : 'Edit',
                                  'action'        : 'slot_string_form',
                                  'permissions'   : (ModifyPortalContent, )
                                  },
                                 )
                               },
                               
                             {'id'             : 'Text Slot',
                              'meta_type'      : 'Text Slot',
                              'description'    : "Text Slot for Mosaic Document",
                              'icon'           : 'mosaic_tool/txt_icon.gif',
                              'product'        : 'MosaicDocument',
                              'factory'        : 'addStringSlot',
                              'immediate_view' : 'view',
                              'actions'        :
                                ({'id'            : 'view',
                                  'name'          : 'View',
                                  'action'        : 'slot_text_view',
                                  'permissions'   : (View, )
                                  },
                                 
                                 {'id'            : 'edit',
                                  'name'          : 'Edit',
                                  'action'        : 'slot_text_form',
                                  'permissions'   : (ModifyPortalContent, )
                                  },
                                 )
                               }, 
                                
                             {'id'             : 'List Slot',
                              'meta_type'      : 'List Slot',
                              'description'    : "List Slot for Mosaic Document",
                              'icon'           : 'mosaic_tool/str_icon.gif',
                              'product'        : 'MosaicDocument',
                              'factory'        : 'addStringSlot',
                              'immediate_view' : 'view',
                              'actions'        :
                                ({'id'            : 'view',
                                  'name'          : 'View',
                                  'action'        : 'slot_list_view',
                                  'permissions'   : (View, )
                                  },
                                 
                                 {'id'            : 'edit',
                                  'name'          : 'Edit',
                                  'action'        : 'slot_text_form',
                                  'permissions'   : (ModifyPortalContent, )
                                  },
                                 )
                               },
                             )

from AccessControl import ClassSecurityInfo
from Globals import InitializeClass
from Products.CMFCore.Expression import Expression
from Products.CMFDefault.Document import Document
from OFS.PropertyManager import PropertyManager
from BaseSlot import BaseSlot

class StringSlot(BaseSlot, Document, PropertyManager) :
    """String Slot"""
    meta_type = 'String Slot'
    _editableFields = ('text', 'text_format', 'size', 'cols', 'rows')
    _indexableFields = ('text',)
    
    manage_options = Document.manage_options[:2] + PropertyManager.manage_options + Document.manage_options[2:]
    
    _properties = ({'id' : 'choices',       'type' : 'lines',   'mode' : 'w'},
                   {'id' : 'castOperator',  'type' : 'string',  'mode' : 'w'},
                   {'id' : 'size',          'type' : 'int',     'mode' : 'w'},
                   {'id' : 'cols',          'type' : 'int',     'mode' : 'w'},
                   {'id' : 'rows',          'type' : 'int',     'mode' : 'w'},
                   {'id' : 'url_expression', 'type': 'expr',    'mode' : 'w'})
    
    security = ClassSecurityInfo()
    
    
    def __init__(self, id, text='', text_format='plain',
                 choices = [], castOperator = 'string', size = 60,
                 cols=50, rows=50, url_expression = None) :
                 
        Document.__init__(self, id, text_format = text_format, text = text)
        
        self.choices = choices
        self.castOperator = castOperator
        self.size = size
        self.cols = cols
        self.rows = rows
        if url_expression is None :
            self.url_expression = Expression("python:None")
        else :
            self.url_expression = url_expression
    
    security.declareProtected(ModifyPortalContent, 'setFormat')
    def setFormat(self, format):
        """ Set text format and Dublin Core resource format.
        """
        value = str(format)
        if value == 'text/html' or value == 'html':
            self.text_format = 'html'
        elif value == 'text/plain':
            if self.text_format not in ('structured-text', 'plain'):
                self.text_format = 'structured-text'
        elif value == 'plain':
            self.text_format = 'plain'
        else:
            self.text_format = 'structured-text'

        
InitializeClass(StringSlot)


def addStringSlot(dispatcher, id, text='', text_format='plain',
                  choices = [], castOperator = 'string', size=60,
                  cols=50, rows=50, url_expression = None) :
    """
    Add a new TextSlot object.
    """
    stringSlot = StringSlot(id,
                            text=text,
                            text_format=text_format,
                            choices=choices,
                            castOperator=castOperator,
                            size=size,
                            cols=cols,
                            rows=rows,
                            url_expression=url_expression)
    parentContainer = dispatcher.Destination()
    parentContainer._setObject(id, stringSlot)
