## $Id: theming.py 7459 2012-01-12 16:19:57Z henrik $
##
## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
## 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
##
"""
Components to support themes.
"""
import grok
from zope.component import getUtilitiesFor
from zope.interface import Interface
from zope.schema.interfaces import IVocabularyFactory
from waeup.sirp.interfaces import SimpleSIRPVocabulary
from waeup.sirp.browser.interfaces import ITheme
from waeup.sirp.browser.resources import (
    waeuptheme_empty, waeup_base_css,
    )

class SIRPThemeBase(grok.GlobalUtility):
    grok.implements(ITheme)
    grok.name('base waeup theme')

    description = u"Base Theme"

    def getResources(self):
        return [waeup_base_css]

#class SIRPThemeRed1(grok.GlobalUtility):
#    grok.implements(ITheme)
#    grok.name('red waeup theme')

#    description = u"Uli's Red Theme"

#    def getResources(self):
#        return [waeuptheme_red1]

#class SIRPThemeGray1(grok.GlobalUtility):
#    grok.implements(ITheme)
#    grok.name('gray waeup theme')

#    description = u"Henrik's Gray Theme"

#    def getResources(self):
#        return [waeuptheme_gray1]

class SIRPThemeEmpty(grok.GlobalUtility):
    """A theme based on jQuery only.

    XXX: crappy yet.
    """
    grok.implements(ITheme)
    grok.name('empty theme')
    description = u'Empty Theme'

    def getResources(self):
        return [waeuptheme_empty]

def get_all_themes():
    """Return all themes registered as `ITheme` somewhere.

    Returns a list of ``(<NAME>, <THEME>)`` tuples where ``<NAME>`` is
    the internal name under which a theme was registered and
    ``<THEME>`` is the real theme implementing `ITheme`.
    """
    for name, theme in getUtilitiesFor(ITheme):
        yield name, theme

class ThemesVocabulary(grok.GlobalUtility):
    """A vocabulary that provides all themes available.

    A named global utility implementing

      :class:`zope.schema.interfaces.IVocabularyFactory`

    and registered under the name

      'waeup.sirp.browser.theming.ThemesVocabulary'

    Interface fields that wish to provide a list of available themes
    can require a 'named vocabulary', i.e. set:

      vocabulary = 'waeup.sirp.browser.theming.ThemesVocabulary'

    and the vocabulary will deliver themes and their descriptive text
    for instance in select boxes of forms.

    The name is chosen so that the location of this vocabulary can be
    found easily when used in an interface definition.
    """
    grok.implements(IVocabularyFactory)
    grok.name('waeup.sirp.browser.theming.ThemesVocabulary')

    def __call__(self, context):
        """Deliver a vocabulary of available themes.

        The `description`s of available themes are used as 'title' and
        the utility names delivering the different themes as 'value'
        and 'token'.
        """
        terms = [(theme.description, name)
                 for name, theme in get_all_themes()]
        vocab = SimpleSIRPVocabulary(*terms)
        return vocab
