# -*- coding: utf-8 -*-
# Elisa - Home multimedia server
# Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com).
# All rights reserved.
#
# This file is available under one of two license agreements.
#
# This file is licensed under the GPL version 3.
# See "LICENSE.GPL" in the root of this distribution including a special
# exception to use Elisa with Fluendo's plugins.
#
# The GPL part of Elisa is also available under a commercial licensing
# agreement from Fluendo.
# See "LICENSE.Elisa" in the root directory of this distribution package
# for details on that license.


__maintainer__ = 'Lionel Martin <lionel@fluendo.com>'


from elisa.core import common
from elisa.core.plugin_registry import PluginNotFound, ComponentNotFound
from elisa.core import plugin_registry

from elisa.extern.translation import gettexter, N_
T_ = gettexter('elisa-service')

MenuActivityClass = plugin_registry.get_component_class('base:menu_activity')

class ServiceActivity(MenuActivityClass):
    """
    This class provides and manages the Service Activities
    """
    config_doc = {'service_activities': 'a list of activites, which should'
                                        'appear in the service_menu'}
    default_config = {'service_activities': []}

    menu_name = T_(N_("Services"))
    menu_icon_name = "service"
    media_types = []
    
    def get_model(self):
        """
        Returns a menu_node_model for the Service Menu
        """
        registry = common.application.plugin_registry
        main_node = MenuActivityClass.get_model(self)
        main_node.children = registry.create_component('base:list_model')
        
        for activity in self.config.get("service_activities" , []):
            try:
                component = registry.create_component(activity)
            except (PluginNotFound, ComponentNotFound), error:
                self.warning(error)
            else:
                menu_model = component.get_model()
                self.log("Component %r with menu model %r", component,
                         menu_model)

                if menu_model != None:
                    self.log("menu model: %r", menu_model)
                    main_node.children.append(menu_model)
                
        main_node.has_children = len(main_node.children) > 0
        
        return main_node

