##############################################################################
# Copyright (c) 2005 The Open Planning Project and Rob Miller
# All Rights Reserved.
#
# This software is subject to the provisions of the GNU General Public
# License, Version 2.0 (GPL).  A copy of the GPL 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.
##############################################################################

"""
Install Anonymailer

$Id: Install.py 15510 2006-01-05 21:55:41Z rafrombrc $
"""

from StringIO import StringIO

from Products.CMFCore.utils import getToolByName
from Products.Archetypes.Extensions.utils import installTypes, install_subskin
from Products.Archetypes import listTypes

from Products.Anonymailer.config import PROJECT_NAME, GLOBALS
from Products.Anonymailer.tools.anonymailer import AnonymailerTool
from Products.Anonymailer.permissions import DEFAULT_PERMISSIONS_DATA

def addTool(portal, out):
    if getToolByName(portal, AnonymailerTool.id, None) is None:
        print >> out, 'Adding Anonymailer Tool'
        addTool = portal.manage_addProduct[PROJECT_NAME].manage_addTool
        addTool('AnonymailerTool')

def securityTweaks(portal, out):
    """ tweak site-wide security settings """
    for roles, perms in DEFAULT_PERMISSIONS_DATA:
        for perm in perms:
            print >> out, '-> Specifying "%s" permission access' % perm
            portal.manage_permission(perm, roles=roles)

def install(self):
    out = StringIO()
    portal = getToolByName(self, 'portal_url').getPortalObject()

    # Install types
    installTypes(self, out, listTypes(PROJECT_NAME), PROJECT_NAME)

    # Install skins
    install_subskin(self, out, GLOBALS, 'skins')

    addTool(portal, out)

    print >> out, "Successfully installed %s." % PROJECT_NAME
    return out.getvalue()
