#------------------------------------------------------------------------------
# $Id$
# $URL$
# Copyright (c) 2006, Ilias Lazaridis - All Rights Reserved
#------------------------------------------------------------------------------

import re

from trac.core import *
from trac.web.chrome import INavigationContributor
from trac.util.html import html

#------------------------------------------------------------------------------

class ProjectNavigation(Component):
    implements(INavigationContributor)

    # INavigationContributor methods
    def get_active_navigation_item(self, req):
        return ''
                
    def get_navigation_items(self, req):
        yield 'projnav', 'project', html.a( 'Project', href=req.href.wiki('ProjectGuide') )
        yield 'projnav', 'product', html.a( 'Product', href=req.href.wiki('ProductGuide') )
        yield 'projnav', 'license', html.a( 'License', href=req.href.wiki('ProjectLicense') )
        yield 'projnav', 'services', html.a( 'Services', href=req.href.wiki('CommercialServices') )
        yield 'mainnav', 'plans', html.a( 'Plans', href=req.href.wiki('WorkingPlans') )

#------------------------------------------------------------------------------

class NavigationOrder(Component):
    """ implements a generic way to define the order of navigation items
    
    [navigation-order]
    projnav = guide, license, plans, services,
    usernav = x, y, z
    ... (define your own navigation_sets)   
    
    """
    
    implements(INavigationContributor)

    def _get_navigation_orders(self):
        
        # retrieve all available categories from *.ini section
        config = self.config['navigation-order']           
        navorders = {}
        for category, value in config.options():
            navorders[category] = config.getlist(category, sep=',')

        # retrieve the chrome component                  
        from trac.web.chrome import Chrome
        chrome = Chrome(self.env)
        
        # insert the categeries as attributes
        # further processing from within the chrome component
        for category, order in navorders.iteritems():       
            setattr(chrome, category + '_order', order)
            
            self.log.debug("loaded navigation category: %s", category)
            
    def get_active_navigation_item(self, req):
        return 'dummy'

    def get_navigation_items(self, req):
        yield 'dummynav', 'dummy', 'dummy'
        #WORKAROUND: cannot use __init__ alone, see trac:#4190
        #verification: remove INavigationContributor, activate call in __init__
        self._get_navigation_orders()

    def __init__(self):
        #self._get_navigation_orders()        
        pass
   
#------------------------------------------------------------------------------        

class WikiBacklinks(Component):
    implements(INavigationContributor)

    # INavigationContributor methods
    def get_active_navigation_item(self, req):
        return ''
 
    def get_navigation_items(self, req):
        import AutoNav                  
        if ( req.path_info.startswith('/wiki') ):
            # wiki page name is not yet within hdf, thus extract
            match = re.match(r'^/wiki(?:/(.*))?', req.path_info)
            if match:
                if match.group(1):
                    req.hdf['wiki.page_name'] = match.group(1)
                else:
                    req.hdf['wiki.page_name'] = '--Empty--'
                     
            backlinks = AutoNav.execute(req.hdf, '', self.env)
            
            for name in backlinks:
                yield 'backlinks', name, html.a( name, href=req.href.wiki(name) )

#------------------------------------------------------------------------------

class CustomCss(Component):
    from trac.web.chrome import ITemplateProvider, add_stylesheet
    from trac.web.api    import IRequestFilter

    implements(ITemplateProvider, IRequestFilter)
    
    def __init__(self):
        import patch       
    
    # ITemplateProvider methods
    def get_templates_dirs(self):
        """
        Return the absolute path of the directory containing the provided
        ClearSilver templates.
        """
        from pkg_resources import resource_filename
#        return [resource_filename(__name__, 'templates')]
        return ''

    def get_htdocs_dirs(self):
        """Return the absolute path of a directory containing additional
        static resources (such as images, style sheets, etc).
        """
        from pkg_resources import resource_filename
        return [('tracx', resource_filename(__name__, 'htdocs'))]

    def pre_process_request(self, req, handler):
        """Do any pre-processing the request might need; typically adding
        values to req.hdf, or redirecting.
        
        Always returns the request handler, even if unchanged.
        """
 
        from trac.web.chrome import add_stylesheet
        add_stylesheet(req, 'tracx/css/trac.css')
        add_stylesheet(req, 'tracx/css/size.css')
               
        return(handler)

    def post_process_request(self, req, template, content_type):
        """Do any post-processing the request might need; typically adding
        values to req.hdf, or changing template or mime type.
        
        Always returns a tuple of (template, content_type), even if
        unchanged.
        """

        return(template, content_type)

#------------------------------------------------------------------------------

