Package elisa :: Package core :: Module frontend
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.frontend

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17   
 18  __maintainer__ = 'Lionel Martin <lionel@fluendo.com>' 
 19   
 20   
 21  from elisa.core.utils import classinit, signal 
 22  from elisa.core import log 
 23  from elisa.base_components.theme import Theme 
 24  from elisa.core import common 
 25  from elisa.extern.translation import Translatable 
 26   
 27  from twisted.internet import defer 
 28   
 29   
30 -class Frontend(log.Loggable):
31 """ 32 A frontend can be connected to a L{elisa.core.backend.Backend} in order to 33 render its content. It does so by holding the root of a tree 34 L{elisa.base_components.view.View}s that render the 35 L{elisa.base_components.controller.Controller}s hold by the backend. The 36 rendering is done in a context which is unique to a frontend. 37 38 A L{elisa.base_components.theme.Theme} can be used in order to change the 39 set of icons used by the views for their rendering. 40 41 @ivar view: root view associated to the frontend 42 @type view: L{elisa.base_components.view.View} 43 @ivar context: context in which the root view is rendered 44 @type context: L{elisa.base_components.context.Context} 45 @ivar theme: theme used by the frontend at rendering 46 @type theme: L{elisa.base_components.theme.Theme} 47 @ivar languages: the translation to use 48 @type languages: list 49 @ivar mvc_config: MVC associations config 50 @type mvc_config: L{elisa.core.config.Config} 51 52 DOCME: ivar name; see below. 53 """ 54 55 __metaclass__ = classinit.ClassInitMeta 56 __classinit__ = classinit.build_properties 57 58 theme_changed = signal.Signal('theme_changed', Theme) 59 60 # FIXME: what is name used for? It needs to be documented. 61 name = None 62
63 - def __init__(self, mvc_config):
64 log.Loggable.__init__(self) 65 self.view = None 66 self.context = None 67 self._theme = None 68 self._languages = [] 69 self.mvc_config = mvc_config
70
71 - def backend__set(self, backend):
72 backend.frontends.append(self) 73 self._backend = backend
74
75 - def backend__get(self):
76 return self._backend
77
78 - def theme__get(self):
79 return self._theme
80
81 - def theme__set(self, new_theme):
82 old_theme = self._theme 83 self._theme = new_theme 84 85 self.context.theme_changed(old_theme, new_theme) 86 self.theme_changed.emit(new_theme) 87 88 # save theme path in config 89 application = common.application 90 application.config.set_option('theme', new_theme.path, section=self.name)
91
92 - def languages__get(self):
93 return self._languages
94
95 - def languages__set(self, languages):
96 self.debug("Setting languages to %r", languages) 97 if isinstance(languages, list): 98 self._languages = languages 99 elif isinstance(languages, base_string): 100 self._languages = [languages] 101 else: 102 self.warning("Could not set the languages to %r. Please use a" 103 " list of strings instead", languages)
104
105 - def translate_text(self, text):
106 """ 107 Convert a translatable text to a text in the languages 108 supported by the frontend. 109 110 @param text: text to translate 111 @type text: string or L{elisa.extern.translator.Translatable} 112 @rtype: unicode 113 """ 114 self.info("Translating text %r", text) 115 translated = text 116 trans = common.application.translator 117 if isinstance(text, Translatable): 118 translated = trans.translateTranslatable(text, self._languages) 119 else: 120 self.debug("%r is not a Translatable", text) 121 return translated
122
123 - def clean(self):
124 """ 125 Cleanup the frontend. Once done, no rendering will be possible. 126 127 @rtype: L{twisted.internet.defer.Deferred} 128 """ 129 # the cleanups have to be chained (in a sequence) 130 dfr = defer.Deferred() 131 dfr.addCallback(lambda x: self.view.clean()) 132 dfr.addCallback(lambda x: self._theme.clean()) 133 dfr.addCallback(lambda x: self.context.clean()) 134 dfr.callback(None) 135 return dfr
136
137 - def get_view_path(self, model_path, content_type=None):
138 """ 139 Retrieve the path of the view associated with given model 140 141 @param model_path: path of the Model we're looking a View for 142 @type model_path: string 143 @keyword content_type: content-type stored in the model 144 @type content_type: None or string 145 @raises UndefinedMVCAssociation: if the frontend's MVC mappings don't 146 define any association for the given 147 model 148 """ 149 config = self.mvc_config 150 self.debug("Looking in %r for a view for %r with content_type %r", 151 config.get_filename(), model_path, content_type) 152 153 view_path = None 154 if content_type: 155 section = "%s/%s" % (model_path, content_type) 156 view_path = config.get_option('view', section=section) 157 158 if view_path == None: 159 section = model_path 160 view_path = config.get_option('view', section=section) 161 162 if view_path == None: 163 from elisa.core.interface_controller import UndefinedMVCAssociation 164 msg = "No view path for %r" % model_path 165 raise UndefinedMVCAssociation(msg) 166 else: 167 self.debug("Found %r", view_path) 168 return view_path
169