"""A document type which may contain directly uploaded images and attachments"""

__author__  = 'Martin Aspeli <optilude@gmx.net>'
__docformat__ = 'plaintext'

from AccessControl import ClassSecurityInfo

try:
  from Products.LinguaPlone.public import *
except ImportError:
  # No multilingual support
  from Products.Archetypes.public import *

from Products.RichDocument.interfaces import IRichDocument

from Products.RichDocument.widget import AttachmentsManagerWidget
from Products.RichDocument.widget import ImagesManagerWidget

from Products.ATContentTypes.content.document import ATDocument
from Products.ATContentTypes.content.document import finalizeATCTSchema

from Products.CMFPlone.interfaces.NonStructuralFolder import INonStructuralFolder

# Copy the ATDocument schema (to avoid modifying the original) and append our
# own fields for the images and attachments manager widgets.
RichDocumentSchema = ATDocument.schema.copy() + Schema((

        BooleanField('displayImages',
            default=False,
            languageIndependent=0,
            widget=ImagesManagerWidget(
                description="If selected, a list of uploaded images will be "
                             "presented at the bottom of the document to allow "
                             "them to be easily downloaded.",
                description_msgid='RichDocument_help_displayImages',
                i18n_domain='richdocument',
                label="""Display images download box""",
                label_msgid='RichDocument_label_displayImages',
            ),
        ),

        BooleanField('displayAttachments',
            default=True,
            languageIndependent=0,
            widget=AttachmentsManagerWidget(
                description="If selected, a list of uploaded attachments will be "
                             "presented at the bottom of the document to allow "
                             "them to be easily downloaded",
                description_msgid='RichDocument_help_displayAttachments',
                i18n_domain='richdocument',
                label="""Display attachments download box""",
                label_msgid='RichDocument_label_displayAttachments',
            ),
        ),

    ),)

# Finalise the schema according to ATContentTypes standards. This basically
# moves the Related items and Allow discussion fields to the bottom of the
# form. See ATContentTypes.content.schemata for details.
finalizeATCTSchema(RichDocumentSchema)

# Declare our class to be first a folderish object, and second get all the
# fields from ATDocument.

## class RichDocument(ATDocument, OrderedBaseFolder):
class RichDocument(OrderedBaseFolder, ATDocument):
    """A document which may contain directly uploaded images and attachments."""

    # Standard content type setup
    portal_type = meta_type = 'RichDocument'
    archetype_name = 'Rich document'
    content_icon = 'RichDocument.gif'
    schema = RichDocumentSchema
    typeDescription = 'A document which can contain rich text, images and attachments'
    typeDescMsgId = 'RichDocument_description_edit'

    # Allow images and files to be uploaded into this containerish object
    allowed_content_types = ['ImageAttachment', 'FileAttachment']

    # Set up our views - these are available from the 'display' menu
    default_view = 'richdocument_view'
    immediate_view = 'richdocument_view'
    suppl_views = ('richdocument_view_preview', 'richdocument_view_float')

    # Make sure we get title-to-id generation when an object is created
    _at_rename_after_creation = True

    # Make sure we get all the interface declarations from ATDocument,
    # which includes support for ISelectableBrowserDefault to get the
    # 'display' menu to work, IHistoryAware and other standard interfaces.
    # In addition, add INonStructuralFolder to tell Plone that even though
    # this type is technically a folder, it should be treated as a standard
    # content type. This ensures the user doesn't perceive a RichDocument as
    # a folder.
    __implements__ = OrderedBaseFolder.__implements__ + \
                     ATDocument.__implements__ + \
                     (IRichDocument, INonStructuralFolder,)

    # Get the standard actions (tabs)
    actions = ATDocument.actions

    # This method, from ISelectableBrowserDefault, is used to check whether
    # the "Choose content item to use as deafult view" option will be
    # presented. This makes sense for folders, but not for RichDocument, so
    # always disallow
    def canSetDefaultPage(self):
        return False

    # enable FTP/WebDAV and friends
    PUT = ATDocument.PUT

registerType(RichDocument)
