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: 7162 $" 
 43  T_ = gettexter() 
 44   
 45  # stupid pychecker 
 46  dir(locale) 
 47   
 48   
49 -class BaseAdminGtk(log.Loggable):
50 """ 51 I am a base class for all GTK+-based Admin views. 52 I am a view on one component's properties. 53 54 @type nodes: L{twisted.python.util.OrderedDict} 55 @ivar nodes: an ordered dict of name -> L{BaseAdminGtkNode} 56 """ 57 58 logCategory = "admingtk" 59 gettextDomain = None 60
61 - def __init__(self, state, admin):
62 """ 63 @param state: state of component this is a UI for 64 @type state: L{flumotion.common.planet.AdminComponentState} 65 @type admin: L{flumotion.admin.admin.AdminModel} 66 @param admin: the admin model that interfaces with the manager for us 67 """ 68 self._debugEnabled = False 69 self.state = state 70 self.name = state.get('name') 71 self.admin = admin 72 self.debug('creating admin gtk for state %r' % state) 73 self.uiState = None 74 self.nodes = util.OrderedDict() 75 76 d = admin.componentCallRemote(state, 'getUIState') 77 d.addCallback(self.setUIState) 78 d.addErrback(lambda failure: failure.trap(SleepingComponentError))
79
80 - def setDebugEnabled(self, enabled):
81 """Set if debug should be enabled. 82 Not all pages are visible unless debugging is set to true 83 84 @param enabled: whether debug should be enabled 85 @type enabled: bool 86 """ 87 self._debugEnabled = enabled 88 for node in self.getNodes().values(): 89 node.setDebugEnabled(enabled)
90
91 - def cleanup(self):
92 if self.uiState: 93 self.uiState.removeListener(self) 94 self.uiState = None 95 for node in self.getNodes().values(): 96 node.cleanup()
97
98 - def setUIState(self, state):
99 self.debug('starting listening to state %r', state) 100 state.addListener(self, set_=self.stateSet, append=self.stateAppend, 101 remove=self.stateRemove) 102 self.uiState = state 103 for node in self.getNodes().values(): 104 node.gotUIState(state) 105 self.uiStateChanged(state)
106
107 - def callRemote(self, methodName, *args, **kwargs):
108 return self.admin.componentCallRemote(self.state, methodName, 109 *args, **kwargs)
110 111 # FIXME: .setup() is subclassable, while .render() on nodes has 112 # haveWidgetTree. choose one of the two patterns in general 113
114 - def setup(self):
115 """ 116 Set up the admin view so it can display nodes. 117 """ 118 self.debug('BaseAdminGtk.setup()') 119 120 def fetchTranslations(): 121 if not self.gettextDomain: 122 return defer.succeed(None) 123 124 def haveBundle(localedatadir): 125 localeDir = os.path.join(localedatadir, 'locale') 126 self.debug("Loading locales for %s from %s" % ( 127 self.gettextDomain, localeDir)) 128 gettext.bindtextdomain(self.gettextDomain, localeDir) 129 locale.bindtextdomain(self.gettextDomain, localeDir)
130 131 lang = getLL() 132 self.debug("loading bundle for %s locales" % lang) 133 bundleName = '%s-locale-%s' % (self.gettextDomain, lang) 134 d = self.admin.bundleLoader.getBundleByName(bundleName) 135 d.addCallbacks(haveBundle, lambda _: None) 136 return d
137 138 def addPages(_): 139 # FIXME: node order should be fixed somehow, so e.g. Component 140 # always comes last, together with eater/feeder ? 141 142 # add a generic component node 143 self.nodes['Component'] = ComponentAdminGtkNode(self.state, 144 self.admin) 145 146 config = self.state.get('config') 147 148 # add feeder node, if component has feeders 149 if config['feed']: 150 self.debug("Component has feeders, show Feeders node") 151 self.nodes['Feeders'] = FeedersAdminGtkNode( 152 self.state, self.admin) 153 154 # add eater node, if component has eaters 155 if 'eater' in config and config['eater']: 156 self.debug("Component has eaters, show Eaters node") 157 self.nodes['Eaters'] = EatersAdminGtkNode( 158 self.state, self.admin) 159 160 # add a properties node 161 self.nodes['Properties'] = PropertiesAdminGtkNode(self.state, 162 self.admin) 163 164 d = fetchTranslations() 165 d.addCallback(addPages) 166 167 # FIXME: why are we not returning the deferred here ? If there is 168 # a good reason, it should be commented here 169 return 170
171 - def getNodes(self):
172 """ 173 Return a dict of admin UI nodes. 174 175 @rtype: dict of str -> L{BaseAdminGtkNode} 176 @returns: dict of name (untranslated) -> admin node 177 """ 178 return self.nodes
179 180 # FIXME: deprecated 181
182 - def render(self):
183 """ 184 Render the GTK+ admin view for this component and return the 185 main widget for embedding. 186 """ 187 raise NotImplementedError
188
189 - def uiStateChanged(self, stateObject):
190 # so, this is still here, but I'd prefer people to (1) just use 191 # the nodes and not the global admin; and (2) use the state 192 # listener stuff more than the chunkier 'uistatechanged' 193 pass
194
195 - def stateSet(self, object, key, value):
196 self.uiStateChanged(object)
197
198 - def stateAppend(self, object, key, value):
199 self.uiStateChanged(object)
200
201 - def stateRemove(self, object, key, value):
202 self.uiStateChanged(object)
203