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

Source Code for Module elisa.core.backend

  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  __maintainer2__ = 'Florian Boucault <florian@fluendo.com>' 
 20   
 21   
 22  from elisa.core.utils import classinit 
 23  from elisa.core import log 
 24   
25 -class Backend(log.Loggable):
26 """ 27 A backend holds all the data that has to be rendered in order to display a 28 complete Elisa user interface. It does so by storing a tree of 29 L{elisa.base_components.controller.Controller}s and keeping track of the 30 focus amongst them. 31 32 Multiple L{elisa.core.frontend.Frontend}s can be connected to a backend 33 through the L{elisa.core.interface_controller.InterfaceController} and 34 render the content of the tree of controllers. 35 36 L{elisa.base_components.input_provider.InputProvider}s can be associated 37 with a backend so that all the L{elisa.core.input_event.InputEvent}s are 38 passed on to the backend's tree of controllers. 39 Input events are forwarded to the controller that has the focus: 40 L{focused_controller}. 41 42 43 @ivar root_controller: root controller associated to the backend 44 @type root_controller: L{elisa.base_components.controller.Controller} 45 @ivar focused_controller: controller which currently has the focus 46 @type focused_controller: L{elisa.base_components.controller.Controller} 47 @ivar mvc_config: MVC associations config 48 @type mvc_config: L{elisa.core.config.Config} 49 """ 50 51 __metaclass__ = classinit.ClassInitMeta 52 __classinit__ = classinit.build_properties 53
54 - def __init__(self, mvc_config=None):
55 log.Loggable.__init__(self) 56 self.root_controller = None 57 self.mvc_config = mvc_config 58 self.frontends = []
59
60 - def root_controller__set(self, controller):
61 self.debug("Setting controller %s as root controller" % controller) 62 self._root_controller = controller 63 self._focused_controller = controller
64
65 - def root_controller__get(self):
66 return self._root_controller
67
68 - def focused_controller__get(self):
69 return self._focused_controller
70
71 - def focused_controller__set(self, controller):
72 controllers_to_unfocus = self._get_branch(self._focused_controller) 73 controllers_to_focus = self._get_branch(controller) 74 75 self._focused_controller = controller 76 77 # notify focus removal to all the branch of controllers previously 78 # focused 79 for controller in controllers_to_unfocus: 80 if controller not in controllers_to_focus: 81 controller.focused = False 82 83 # notify all the branch of controllers above 'controller' that they 84 # are now focused 85 for controller in controllers_to_focus: 86 controller.focused = True
87
88 - def _get_branch(self, controller):
89 """ 90 Return all the parents of a controller and the controller itself. The 91 list is ordered starting with the deepest controller in the branch and 92 going to the root. 93 """ 94 branch = [] 95 current_controller = controller 96 97 while current_controller != None: 98 branch.append(current_controller) 99 current_controller = current_controller.parent 100 101 return branch
102 103
104 - def dispatch_input(self, input_event):
105 """ 106 Process an input event by passing it to the focused controller. 107 108 @param input_event: input event to be processed 109 @type input_event: L{elisa.core.input_event.InputEvent} 110 """ 111 self.debug("Dispatching %s" % input_event) 112 controller = self.focused_controller 113 event_processed = False 114 115 while controller != None: 116 self.debug("Asking %s to handle %s" % (controller, input_event)) 117 event_processed = controller.handle_input(input_event) 118 if event_processed == True: 119 break 120 controller = controller.parent 121 122 if event_processed: 123 self.debug("%s got processed by %s" % (input_event, controller)) 124 else: 125 self.debug("No controller processed %s" % input_event)
126
127 - def get_controller_path(self, model_path, content_type=None):
128 """ 129 Retrieve the path of the controller associated with given 130 model's path. 131 132 @param model_path: path of the Model for which we need a 133 Controller 134 @type model_path: string 135 @keyword content_type: content-type stored in the model 136 @type content_type: None or string 137 @raises UndefinedMVCAssociation: if the backend's MVC mappings don't 138 define any association for the given 139 model 140 """ 141 config = self.mvc_config 142 self.debug("Looking in %r for a controller for %r with content_type %r", 143 config.get_filename(), model_path, content_type) 144 145 controller_path = None 146 if content_type: 147 section = "%s/%s" % (model_path, content_type) 148 controller_path = config.get_option('controller', section=section) 149 150 if controller_path == None: 151 section = model_path 152 controller_path = config.get_option('controller', section=section) 153 154 if controller_path == None: 155 from elisa.core.interface_controller import UndefinedMVCAssociation 156 msg = "No controller path for %r" % model_path 157 raise UndefinedMVCAssociation(msg) 158 else: 159 self.debug("Found %r", controller_path) 160 return controller_path
161