from Globals import InitializeClass, DTMLFile
from AccessControl import ClassSecurityInfo
from OFS.SimpleItem import SimpleItem
from OFS.PropertyManager import PropertyManager
from Acquisition import Implicit, aq_base
from cgi import escape

from Products.CMFCore.utils import UniqueObject, getToolByName
from Products.CMFCore.CMFCorePermissions import ManagePortal
from Products.CMFCore.CMFCorePermissions import ModifyPortalContent

from Products.CMFCore.ActionInformation import ActionInformation
from Products.CMFCore.ActionsTool import ActionsTool
from Products.CMFCore.ActionProviderBase import ActionProviderBase
from Products.CMFCore.Expression import Expression

viewlet_registery = []
def registerViewlets(actions):
    for action in actions:
        viewlet_registery.append(action)

class ContentPanelsTool( UniqueObject, SimpleItem, PropertyManager, ActionsTool ):

    id = 'portal_contentpanels'
    meta_type = 'ContentPanels Tool'
    _actions = tuple()

    action_providers = ('portal_contentpanels',)
    security = ClassSecurityInfo()

    manage_options = (ActionProviderBase.manage_options +
                #     ({ 'label' : 'Overview', 'action' : 'manage_overview' }
                #     , 
                #     ) + 
                     PropertyManager.manage_options +
                     ({ 'label' : 'Install All Viewlets', 'action' : 'manage_installAllViewlets' }, ) + 
                     SimpleItem.manage_options)

    def __init__(self):
        self._setProperty('defaultPortletWrapper', 'Default', 'string')
        self._setProperty('notitlePortletWrapper', 'No Title', 'string')
        self._setProperty('zopezenPortletWrapper', 'ZopeZen', 'string')
        self._setProperty('boxPortletWrapper', 'Default Box', 'string')
        self._setProperty('alertPortletWrapper', 'Alert', 'string')
        self._setProperty('dottedPortletWrapper', 'Dotted', 'string')
        self._setProperty('roundedPortletWrapper', 'Rounded', 'string')

    def valid_property_id(self, id):
        if not id or id[:1]=='_' or (id[:3]=='aq_') \
           or hasattr(aq_base(self), id) or escape(id) != id:
            return 0
        return 1

    security.declarePublic('getPanelSkins')
    def getPanelSkins(self):
        return [ (i[1], i[0]) for i in list(self.propertyItems()) if i[0] != 'title']

    def installActions(self, actions=[]):
        for action in actions:
            self.addAction(action[0],action[1],action[2],
                           action[3],action[4],action[5],action[6])

    security.declareProtected(ManagePortal, 'manage_installAllViewlets')
    def manage_installAllViewlets(self, REQUEST = None):
        """reinstall all registered actions
        """
        self.installActions(viewlet_registery)
        if REQUEST:
            REQUEST.RESPONSE.redirect('manage_editActionsForm')

    security.declarePublic('getViewletAction')
    def getViewletAction(self, viewletId, actions_dict=None):
        """get a name of a viewlet"""
        if actions_dict is None:
            actions_dict = self.listFilteredActions()

        for actions in actions_dict.values():
            for action in actions:
                if action['id'] == viewletId:
                    return action
        return None

    security.declarePublic('getViewletName')
    def getViewletName(self, viewletId, actions_dict=None):
        """get a name of a viewlet"""
        action = self.getViewletAction(viewletId, actions_dict)
        if action:
             return action['name']
        return None

    security.declarePublic('getViewletPath')
    def getViewletPath(self, viewletId, actions_dict=None):
        """get a name of a viewlet"""
        action = self.getViewletAction(viewletId, actions_dict)
        if action:
            return action['url']
        return None

InitializeClass( ContentPanelsTool )
