Package flumotion :: Package component :: Package base :: Module admin_gtk
[hide private]

Source Code for Module flumotion.component.base.admin_gtk

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_feedcomponent010 -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """ 
 23  Base classes for component UI's using GTK+ 
 24  """ 
 25   
 26  import gettext 
 27  import locale 
 28  import os 
 29   
 30  from twisted.python import util 
 31  from twisted.internet import defer 
 32   
 33  from flumotion.common import log 
 34  from flumotion.common.errors import SleepingComponentError 
 35  from flumotion.common.i18n import getLL, gettexter 
 36  from flumotion.component.base.componentnode import ComponentAdminGtkNode 
 37  from flumotion.component.base.eatersnode import EatersAdminGtkNode 
 38  from flumotion.component.base.feedersnode import FeedersAdminGtkNode 
 39  from flumotion.component.base.propertiesnode import PropertiesAdminGtkNode 
 40   
 41  _ = gettext.gettext 
 42  __version__ = "$Rev: 6990 $" 
 43  T_ = gettexter() 
 44   
 45  # stupid pychecker 
 46  dir(locale) 
 47   
48 -class BaseAdminGtk(log.Loggable):
49 """ 50 I am a base class for all GTK+-based Admin views. 51 I am a view on one component's properties. 52 53 @type nodes: L{twisted.python.util.OrderedDict} 54 @ivar nodes: an ordered dict of name -> L{BaseAdminGtkNode} 55 """ 56 57 logCategory = "admingtk" 58 gettextDomain = None 59
60 - def __init__(self, state, admin):
61 """ 62 @param state: state of component this is a UI for 63 @type state: L{flumotion.common.planet.AdminComponentState} 64 @type admin: L{flumotion.admin.admin.AdminModel} 65 @param admin: the admin model that interfaces with the manager for us 66 """ 67 self._debugEnabled = False 68 self.state = state 69 self.name = state.get('name') 70 self.admin = admin 71 self.debug('creating admin gtk for state %r' % state) 72 self.uiState = None 73 self.nodes = util.OrderedDict() 74 75 d = admin.componentCallRemote(state, 'getUIState') 76 d.addCallback(self.setUIState) 77 d.addErrback(lambda failure: failure.trap(SleepingComponentError))
78
79 - def setDebugEnabled(self, enabled):
80 """Set if debug should be enabled. 81 Not all pages are visible unless debugging is set to true 82 83 @param enabled: whether debug should be enabled 84 @type enabled: bool 85 """ 86 self._debugEnabled = enabled 87 for node in self.getNodes().values(): 88 node.setDebugEnabled(enabled)
89
90 - def cleanup(self):
91 if self.uiState: 92 self.uiState.removeListener(self) 93 self.uiState = None 94 for node in self.getNodes().values(): 95 node.cleanup()
96
97 - def setUIState(self, state):
98 self.debug('starting listening to state %r', state) 99 state.addListener(self, set_=self.stateSet, append=self.stateAppend, 100 remove=self.stateRemove) 101 self.uiState = state 102 for node in self.getNodes().values(): 103 node.gotUIState(state) 104 self.uiStateChanged(state)
105
106 - def callRemote(self, methodName, *args, **kwargs):
107 return self.admin.componentCallRemote(self.state, methodName, 108 *args, **kwargs)
109 110 # FIXME: .setup() is subclassable, while .render() on nodes has 111 # haveWidgetTree. choose one of the two patterns in general
112 - def setup(self):
113 """ 114 Set up the admin view so it can display nodes. 115 """ 116 self.debug('BaseAdminGtk.setup()') 117 118 def fetchTranslations(): 119 if not self.gettextDomain: 120 return defer.succeed(None) 121 122 def haveBundle(localedatadir): 123 localeDir = os.path.join(localedatadir, 'locale') 124 self.debug("Loading locales for %s from %s" % ( 125 self.gettextDomain, localeDir)) 126 gettext.bindtextdomain(self.gettextDomain, localeDir) 127 locale.bindtextdomain(self.gettextDomain, localeDir)
128 129 lang = getLL() 130 self.debug("loading bundle for %s locales" % lang) 131 bundleName = '%s-locale-%s' % (self.gettextDomain, lang) 132 d = self.admin.bundleLoader.getBundleByName(bundleName) 133 d.addCallbacks(haveBundle, lambda _: None) 134 return d
135 136 def addPages(_): 137 # FIXME: node order should be fixed somehow, so e.g. Component 138 # always comes last, together with eater/feeder ? 139 140 # add a generic component node 141 self.nodes['Component'] = ComponentAdminGtkNode(self.state, 142 self.admin) 143 144 config = self.state.get('config') 145 146 # add feeder node, if component has feeders 147 if config['feed']: 148 self.debug("Component has feeders, show Feeders node") 149 self.nodes['Feeders'] = FeedersAdminGtkNode( 150 self.state, self.admin) 151 152 # add eater node, if component has eaters 153 if 'eater' in config and config['eater']: 154 self.debug("Component has eaters, show Eaters node") 155 self.nodes['Eaters'] = EatersAdminGtkNode( 156 self.state, self.admin) 157 158 # add a properties node 159 self.nodes['Properties'] = PropertiesAdminGtkNode(self.state, 160 self.admin) 161 162 d = fetchTranslations() 163 d.addCallback(addPages) 164 165 # FIXME: why are we not returning the deferred here ? If there is 166 # a good reason, it should be commented here 167 return 168
169 - def getNodes(self):
170 """ 171 Return a dict of admin UI nodes. 172 173 @rtype: dict of str -> L{BaseAdminGtkNode} 174 @returns: dict of name (untranslated) -> admin node 175 """ 176 return self.nodes
177 178 # FIXME: deprecated
179 - def render(self):
180 """ 181 Render the GTK+ admin view for this component and return the 182 main widget for embedding. 183 """ 184 raise NotImplementedError
185
186 - def uiStateChanged(self, stateObject):
187 # so, this is still here, but I'd prefer people to (1) just use 188 # the nodes and not the global admin; and (2) use the state 189 # listener stuff more than the chunkier 'uistatechanged' 190 pass
191
192 - def stateSet(self, object, key, value):
193 self.uiStateChanged(object)
194
195 - def stateAppend(self, object, key, value):
196 self.uiStateChanged(object)
197
198 - def stateRemove(self, object, key, value):
199 self.uiStateChanged(object)
200