##############################################################################
#
# Copyright (c) 2002 Ingeniweb SARL
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################

"""AttachmentField"""
import Globals
from AccessControl import ModuleSecurityInfo, allow_module

import AccessControl.Permissions
import AttachmentField
import AttachmentService

from global_symbols import *

from AccessControl.Permissions import *

from Products.CMFCore.DirectoryView import registerDirectory
from Products.CMFCore import utils, CMFCorePermissions

PROJECTNAME = "AttachmentField"

registerDirectory('skins', globals())
registerDirectory('skins/attachmentfield', globals())

product_globals = globals()          # Used only in the Extensions/Install.py script

# Service management
misc_ = {
    'AttachmentService.jpg':
    Globals.ImageFile('www/AttachmentService.jpg', globals()),
    }

cp_id = "AttachmentService"

security = ModuleSecurityInfo('Products.AttachmentField')
attachment_service = None

security.declarePublic("getAttachmentService")
def getAttachmentService():
    """Return as. singleton"""
    return attachment_service

def make_attachment_service(cp):
    """Control_Panel attachment service
    """
    service = AttachmentService.AttachmentService()
    service.id = cp_id
    cp._setObject(cp_id, service)
    return getattr(cp, cp_id)

def initialize(context):
    # hook into the Control Panel
    global attachment_service
    cp = context._ProductContext__app.Control_Panel # argh
    if not cp_id in cp.objectIds():
        attachment_service = make_attachment_service(cp)
    else:
        attachment_service = getattr(cp, cp_id)

    # Import tool
    from Products.AttachmentField.AttachmentFieldTool import AttachmentFieldTool
    utils.ToolInit(
        PROJECTNAME + ' Tool',
        tools=(AttachmentFieldTool,),
        product_name=PROJECTNAME,
        icon='tool.gif').initialize(context)


# Plugins MUST BE IMPORTED LAST !
# XXX TODO : we should provide a way to automatically import services
# (like Zope's 'Products' dir)
import AudioAttachment
import AutocadAttachment
import DummyAttachment
import HTMLAttachment
import ImageAttachment
import MSAccessAttachment
import MSExcelAttachment
import MSPowerpointAttachment
import MSProjectAttachment
import MSWordAttachment
import PDFAttachment
import TextAttachment
import VisioAttachment
import ZipAttachment
import VideoAttachment
import CompressedAttachment
import TarGzAttachment
import PortalTransformsAttachment
