"""Install for Silva Mailing
"""

# zope3
from zope.app import zapi

# Silva
from Products.Silva.install import add_fss_directory_view

import os
here = os.path.abspath(os.path.dirname(__file__))

def install(root):
    # create the core views from filesystem
    add_fss_directory_view(root.service_views,
                           'SilvaMailing', __file__, 'views')

    # security
    root.manage_permission('Add Silva Mailing Folders',
                           ['Manager', 'ChiefEditor', 'Editor', 'Author'])
    root.manage_permission('Add Silva Mailing Documents',
                           ['Manager', 'ChiefEditor', 'Editor', 'Author'])
    root.manage_permission('Add Silva Mailing Document Versions',
                           ['Manager', 'ChiefEditor', 'Editor', 'Author'])

    # register metadata sets
    collection = root.service_metadata.getCollection()
    if 'silva-mailing-subscriptions' in collection.objectIds():
        collection.manage_delObjects(['silva-mailing-subscriptions'])
    xml_file = os.path.join(here, 'silva-mailing-subscriptions.xml')
    fh = open(xml_file, 'r')
    try:
        collection.importSet(fh)
    finally:
        fh.close()

    root.service_metadata.addTypesMapping(('Silva Mailing Folder',),
        ('silva-content', 'silva-extra', 'silva-mailing-subscriptions'))
    root.service_metadata.addTypesMapping(
        ('Silva Mailing Document Version',),
        ('silva-content', 'silva-extra'))

    # register views
    reg = root.service_view_registry
    reg.register('add', 'Silva Mailing Folder',
                 ['add', 'MailingFolder'])
    reg.register('edit', 'Silva Mailing Folder',
                 ['edit', 'Container', 'Folder', 'MailingFolder'])
    reg.register('public', 'Silva Mailing Folder',
                 ['public', 'MailingFolder', 'view'])
    reg.register('preview', 'Silva Mailing Folder',
                 ['public', 'MailingFolder', 'preview'])

    reg.register('add', 'Silva Mailing Document',
                 ['add', 'MailingDocument'])
    reg.register('edit', 'Silva Mailing Document',
                 ['edit', 'VersionedContent', 'Document',
                  'MailingDocument'])
    reg.register('public', 'Silva Mailing Document',
                 ['public', 'Document'])
    reg.register('public', 'Silva Mailing Document Version',
                 ['public', 'Document', 'view'])
    reg.register('preview', 'Silva Mailing Document Version',
                 ['public', 'Document', 'preview'])

    configureAddables(root)

def uninstall(root):
    reg = root.service_view_registry
    reg.unregister('add', 'Silva Mailing Folder')
    reg.unregister('edit', 'Silva Mailing Folder')
    reg.unregister('public', 'Silva Mailing Folder')
    reg.unregister('preview', 'Silva Mailing Folder')
    reg.unregister('add', 'Silva Mailing Document')
    reg.unregister('edit', 'Silva Mailing Document')
    reg.unregister('public', 'Silva Mailing Document')
    reg.unregister('public', 'Silva Mailing Document Version')
    reg.unregister('preview', 'Silva Mailing Document Version')
    root.service_views.manage_delObjects(['SilvaMailing'])
    
def is_installed(root):
    return hasattr(root.service_views, 'SilvaMailing')

def configureAddables(root):
    """Configure what can and cannot be added where."""
    asset_addables = root.get_silva_asset_types()
    current_addables = root.get_silva_addables_allowed_in_publication()
    root_addables = ['Silva Mailing Folder']
    root_non_addables = ['Silva Mailing Document']

    new_addables = []
    for a in current_addables:
        if a not in root_non_addables:
            new_addables.append(a)
    for a in root_addables:
        if a not in new_addables:
            new_addables.append(a)

    root.set_silva_addables_allowed_in_publication(new_addables)

