from Products.CMFCore.utils import getToolByName
from Products.CMFFormController.FormAction import FormActionKey
from Products.Archetypes.Extensions.utils import installTypes
from Products.Archetypes.Extensions.utils import install_subskin
from Products.Archetypes.public import listTypes

from StringIO import StringIO

from Products.CMFDynamicViewFTI.migrate import migrateFTIs

from Products.RichDocument.config import *
from Products.RichDocument.Extensions.utils import *

def install(self):
    """Install RichDocument: Install content types, skin layer, install the
    stylesheet, set up global properties, enable the portal factory and
    set up form controller actions for the widget actions
    """

    out = StringIO()

    print >> out, "Installing RichDocument"

    # Install types
    classes = listTypes(PROJECTNAME)
    installTypes(self, out,
                 classes,
                 PROJECTNAME)
    print >> out, "Installed types"

    # Install skin
    install_subskin(self, out, product_globals)
    print >> out, "Installed skin"

    # Migrate FTI, to make sure we get the necessary infrastructure for the
    # 'display' menu to work.
    migrated = migrateFTIs(self, product=PROJECTNAME)
    print >>out, "Switched to DynamicViewFTI: %s" % ', '.join(migrated)

    # Install stylesheet
    portal_css = getToolByName(self, 'portal_css')
    portal_css.manage_addStylesheet(id = 'richdocument.css',
                                    expression = 'python:object.getTypeInfo().getId() == "RichDocument"',
                                    media = 'all',
                                    title = 'RichDocument styles',
                                    enabled = True)

    # Enable portal_factory
    factory = getToolByName(self, 'portal_factory')
    types = factory.getFactoryTypes().keys()
    if 'RichDocument' not in types:
        types.append('RichDocument')
        factory.manage_setPortalFactoryTypes(listOfTypeIds = types)

    print >> out, "Added RichDocument to portal_factory"

    propsTool = getToolByName(self, 'portal_properties')
    siteProperties = getattr(propsTool, 'site_properties')
    navtreeProperties = getattr(propsTool, 'navtree_properties')

    # Remove from use_folder_tabs
    useFolderTabs = list(siteProperties.getProperty('use_folder_tabs'))
    if 'RichDocument' in useFolderTabs:
        useFolderTabs.remove('RichDocument')
    siteProperties.manage_changeProperties(use_folder_tabs = useFolderTabs)

    # Remove from typesLinkToFolderContentsInFC 
    typesLinkToFolderContentsInFC = list(siteProperties.getProperty('typesLinkToFolderContentsInFC'))
    if 'RichDocument' in typesLinkToFolderContentsInFC:
        typesLinkToFolderContentsInFC.remove('RichDocument')
    siteProperties.manage_changeProperties(typesLinkToFolderContentsInFC = typesLinkToFolderContentsInFC)

    # Add to default_page_types
    defaultPageTypes = list(siteProperties.getProperty('default_page_types'))
    if 'RichDocument' not in defaultPageTypes:
        defaultPageTypes.append('RichDocument')
    siteProperties.manage_changeProperties(default_page_types = defaultPageTypes)

    # Add to parentMetaTypesNotToQuery
    parentMetaTypesNotToQuery = list(navtreeProperties.getProperty('parentMetaTypesNotToQuery'))
    if 'RichDocument' not in parentMetaTypesNotToQuery:
        parentMetaTypesNotToQuery.append('RichDocument')
    navtreeProperties.manage_changeProperties(parentMetaTypesNotToQuery = parentMetaTypesNotToQuery)

    # Set up form controller actions for the widgets to work
    registerAttachmentsFormControllerActions(self, contentType = 'RichDocument', template = 'atct_edit')
    registerImagesFormControllerActions(self, contentType = 'RichDocument', template = 'atct_edit')
    print >> out, "Added actions for the image and attachment controls to the atct_edit form controller."

    # Register form controller actions for LinguaPlone translate_item
    registerAttachmentsFormControllerActions(self, contentType = 'RichDocument', template = 'translate_item')
    registerImagesFormControllerActions(self, contentType = 'RichDocument', template = 'translate_item')
    print >> out, "Added actions for the image and attachment controls to the translate_item form controller."

    # Set up the workflow for the widget types
    setupFileAttachmentWorkflow(self)
    setupImageAttachmentWorkflow(self)
    print >> out, "Set up FileAttachment and ImageAttachment workflows."

    # Make the widget types use the /view action when linked to
    setupFileAttachmentView(self)
    setupImageAttachmentView(self)
    print >> out, "Set up FileAttachment and ImageAttachment to use /view."

    # Add the FileAttachment and ImageAttachment types to types_not_searched
    # (this is configurable via the Search settings control panel)
    typesNotSearched = list(siteProperties.getProperty('types_not_searched'))
    if 'FileAttachment' not in typesNotSearched:
        typesNotSearched.append('FileAttachment')
    if 'ImageAttachment' not in typesNotSearched:
        typesNotSearched.append('ImageAttachment')
    siteProperties.manage_changeProperties(types_not_searched = typesNotSearched)
    print >> out, "Added FileAttachment and ImageAttachment to types_not_searched"

    # Add FileAttachment and ImageAttachment to kupu's linkable and media
    # types
    kupuTool = getToolByName(self, 'kupu_library_tool')
    linkable = list(kupuTool.getPortalTypesForResourceType('linkable'))
    mediaobject = list(kupuTool.getPortalTypesForResourceType('mediaobject'))
    if 'FileAttachment' not in linkable:
        linkable.append('FileAttachment')
    if 'ImageAttachment' not in linkable:
        linkable.append('ImageAttachment')
    if 'RichDocument' not in linkable:
        linkable.append('RichDocument')
    if 'ImageAttachment' not in mediaobject:
        mediaobject.append('ImageAttachment')
    # kupu_library_tool has an idiotic interface, basically written purely to
    # work with its configuration page. :-(
    kupuTool.updateResourceTypes(({'resource_type' : 'linkable',
                                   'old_type'      : 'linkable',
                                   'portal_types'  :  linkable},
                                  {'resource_type' : 'mediaobject',
                                   'old_type'      : 'mediaobject',
                                   'portal_types'  :  mediaobject},))
    print >> out, "Added FileAttachment and ImageAttachment to kupu's linkable and mediaobject types"

    return out.getvalue()