#     Calendaring is a simple CMF/Plone calendaring implementation.
#     Copyright (C) 2004 Enfold Systems
#
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with this program; if not, write to the Free Software
#     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from Products.Archetypes.public import listTypes
from Products.Archetypes.Extensions.utils import installTypes, install_subskin
from Products.CMFCore.utils import getToolByName
from Products.Calendaring.config import *
from StringIO import StringIO

def install_ctr(self, out):
    ctr = getToolByName(self, 'content_type_registry')
    if 'calendar_mime' not in ctr.predicate_ids:
        ctr.addPredicate('calendar_mime', 'major_minor' )
        ctr.getPredicate('calendar_mime').edit('text', 'calendar')
        ctr.assignTypeName('calendar_mime', 'Calendar')
        ctr.reorderPredicate('calendar_mime', 0)
        out.write('Installed CTR Predicate for text/calendar -> Calendar.\n')
    else:
        out.write('Predicate for text/calendar -> Calendar was already installed.\n')

    if 'calendar_ext' not in ctr.predicate_ids:
        ctr.addPredicate('calendar_ext', 'extension' )
        ctr.getPredicate('calendar_ext').edit('ics')
        ctr.assignTypeName('calendar_ext', 'Calendar')
        ctr.reorderPredicate('calendar_ext', 1)
        out.write('Installed CTR Predicate for .ics -> Calendar.\n')
    else:
        out.write('Predicate for .ics -> Calendar was already installed.\n')

def install_actions(self, out):
    ai = getToolByName(self, 'portal_actionicons')
    for category, config in ACTION_ICONS.items():
        for icon_id, info in config.items():
            if ai.queryActionIcon(category, icon_id, None) is None:
                ai.addActionIcon(category, icon_id,
                                 info[0], info[1])
                out.write("Installed action icon "
                          "for %s.\n" % info[1])
            else:
                out.write(("Action Icon for %s "
                           "was already Installed.\n"
                           % info[1]))

def install_tools(self, product, tools, out):
    addTool = self.manage_addProduct[product].manage_addTool
    for id in TOOLIDS:
        if hasattr(self, id):
            self.manage_delObjects(ids=[id])
    for tool in tools:
        addTool(tool)
        out.write("Created tool: %s.\n" % tool)

def install_types(self, out, types, project):
    installTypes(self, out, types, project)

def install(self):
    from Products.Calendaring.tools.calendar import CalendarTool
    out = StringIO()

    install_types(self, out, listTypes(PROJECTNAME), PROJECTNAME)
    install_subskin(self, out, GLOBALS)
    install_tools(self, PROJECTNAME, [CalendarTool.meta_type], out)
    install_actions(self, out)
    install_ctr(self, out)

    out.write("Successfully installed %s." % PROJECTNAME)
    return out.getvalue()
