from Products.CMFCore.utils import getToolByName

topicmanagement_actions = [
  { 'id'           : 'search'
  , 'no_name'      : 'S\xf8k'
  , 'en_name'      : 'Search'
  , 'action'       : 'string:${folder_url}/content_search'
  , 'condition'    : 'member'
  , 'permission'   : 'Search ZCatalog'
  , 'category'     : 'system'
  , 'visible'      : True
  },
  { 'id'           : 'login'
  , 'no_name'      : 'Logg p\xe5'
  , 'en_name'      : 'Log on'
  , 'action'       : 'string:${portal_url}/logout'
  , 'condition'    : 'not:member'
  , 'permission'   : ''
  , 'category'     : 'shortcuts'
  , 'visible'      : True
  },
  { 'id'           : 'preferences'
  , 'no_name'      : 'Innstillinger'
  , 'en_name'      : 'Preferences'
  , 'action'       : 'string:${portal_url}/personalize_form'
  , 'condition'    : 'member'
  , 'permission'   : 'View'
  , 'category'     : 'user'
  , 'visible'      : True
  },
  { 'id'           : 'homepage'
  , 'no_name'      : 'Min hjemmeside'
  , 'en_name'      : 'My Homepage'
  , 'action'       : 'python:portal.portal_membership.getHomeUrl()'
  , 'condition'    : 'python:portal.portal_membership.getHomeFolder() is not None'
  , 'permission'   : 'View'
  , 'category'     : 'user'
  , 'visible'      : True
  },
  { 'id'           : 'undo'
  , 'no_name'      : 'Angre'
  , 'en_name'      : 'Undo'
  , 'action'       : 'string:${object_url}/undo_form?camefrom=${request/URL}'
  , 'condition'    : 'python:member and request.has_key("URL")'
  , 'permission'   : 'List undoable changes'
  , 'category'     : 'system'
  , 'visible'      : True
  },
  # Homepage actions...
  { 'id'           : 'newarticle'
  , 'no_name'      : 'Skrive ny artikkel'
  , 'en_name'      : 'Write new article'
  , 'action'       : 'string:${portal_url}/createNewContent?type_name=artikkel'
  , 'condition'    : 'member'
  , 'permission'   : 'Add portal content'
  , 'category'     : 'homepage_contributor'
  , 'visible'      : False
  },
  { 'id'           : 'newcontent'
  , 'no_name'      : 'Opprette annet ny innhold'
  , 'en_name'      : 'Create other new content'
  , 'action'       : 'string:${portal_url}/folder_factories'
  , 'condition'    : 'member'
  , 'permission'   : 'Add portal content'
  , 'category'     : 'homepage_contributor'
  , 'visible'      : True
  },
  { 'id'           : 'searchcontent'
  , 'no_name'      : 'S\xf8ke etter nytt innhold'
  , 'en_name'      : 'Search for new content'
  , 'action'       : 'string:${portal_url}/content_search'
  , 'condition'    : 'member'
  , 'permission'   : 'Search ZCatalog'
  , 'category'     : 'homepage_contributor'
  , 'visible'      : True
  },
  { 'id'           : 'frontpage'
  , 'no_name'      : 'G\xe5 til forsiden'
  , 'en_name'      : 'Go to the frontpage'
  , 'action'       : 'python:portal_url'
  , 'condition'    : 'member'
  , 'permission'   : 'View'
  , 'category'     : 'homepage_contributor'
  , 'visible'      : True
  },
  { 'id'           : 'folder_contents'
  , 'no_name'      : 'G\xe5 til mappestrukturen'
  , 'en_name'      : 'List folder contents'
  , 'action'       : 'string:${portal_url}/folder_contents'
  , 'condition'    : 'member'
  , 'permission'   : 'List folder contents'
  , 'category'     : 'homepage_editor'
  , 'visible'      : True
  },
  { 'id'           : 'ontology_editong'
  , 'no_name'      : 'Utvide ontologien'
  , 'en_name'      : 'Modify the ontology'
  , 'action'       : 'python:portal.portal_topicmaps.getTopicMap().absolute_url()+"/topicmap_edit_form"'
  , 'condition'    : 'member'
  , 'permission'   : 'Add portal content'
  , 'category'     : 'homepage_manager'
  , 'visible'      : True
  },
  ]

def install(self):
    """ Install and set up the necessarry actions. """
    actiontool = getToolByName(self, 'portal_actions', None)
    typestool = getToolByName(self, 'portal_types', None)
    if actiontool is not None:
        actiontool.addActionProvider(self.id)
    
    language = self._language
    for action in topicmanagement_actions:
        self.addAction( id=action['id']
                      , name = action['%s_name'%language]
                      , action = action['action']
                      , condition = action['condition']
                      , permission = action['permission']
                      , category = action['category']
                      , visible = action['visible']
                      )
    
    # Add folderContents to TopicTypeInformation as an optimization...
    folderactions = [counter for counter, action in enumerate(actiontool.listActions()) if action.getId()=='folderContents']
    actiontool.deleteActions(folderactions)
    if language == 'en':
        name = 'Folder Contents'
    elif language == 'no':
        name = 'Mappevisning'
    typestool.Folder.addAction( id='folderContents'
                              , name=name
                              , action='string:${object_url}/folder_contents'
                              , condition=""
                              , permission='List folder contents'
                              , category='object'
                              , visible=1)
    for action in typestool.Folder.listActions():
        action.category = 'object'
    
    btreefolderTI = getattr(typestool, 'CMF BTree Folder')
    for action in btreefolderTI.listActions():
        action.category = 'object'
    btreefolderTI.addAction( id='folderContents'
                           , name=name
                           , action='string:${object_url}/folder_contents'
                           , condition=""
                           , permission='List folder contents'
                           , category='object'
                           , visible=1)
    
    #TODO: Attempt to set action on any Portal BTreeFolders as well.
    
    # Disable duplicate or unused actions in portal_membership
    pms = getToolByName(self, 'portal_membership')
    action_ids = ['login', 'preferences', 'logout', 'addFavorite', 'mystuff', 'favorites']
    actions = range(len([action for action in pms.listActions() if action.getId() in action_ids]))
    pms.deleteActions(actions)
    
    pu = getToolByName(self, 'portal_undo')
    action_ids = ['undo']
    actions = range(len([action for action in pu.listActions() if action.getId() in action_ids]))
    pu.deleteActions(actions)

    pp = getToolByName(self, 'portal_properties')
    action_ids = ['configPortal']
    actions = range(len([action for action in pp.listActions() if action.getId() in action_ids]))
    pp.deleteActions(actions)
