comparison frontends/src/quick_frontend/quick_widgets.py @ 1312:9e904f8a094e frontends_multi_profiles

quick_frontend: getOrCreateWidget callbacks can return another widget
author souliane <souliane@mailoo.org>
date Mon, 09 Feb 2015 09:19:30 +0100
parents 9ca93ecdeea5
children e9888db0eb0c
comparison
equal deleted inserted replaced
1311:4895e1e092fb 1312:9e904f8a094e
98 98
99 If the widget is new, self.host.newWidget will be called with it. 99 If the widget is new, self.host.newWidget will be called with it.
100 @param class_(class): class of the widget to create 100 @param class_(class): class of the widget to create
101 @param target: target depending of the widget, usually a JID instance 101 @param target: target depending of the widget, usually a JID instance
102 @param args(list): optional args to create a new instance of class_ 102 @param args(list): optional args to create a new instance of class_
103 @param kwargs(list): optional kwargs to create anew instance of class_ 103 @param kwargs(dict): optional kwargs to create a new instance of class_
104 if 'profile' key is present, it will be popped and put in 'profiles' 104 if 'profile' key is present, it will be popped and put in 'profiles'
105 if there is neither 'profile' nor 'profiles', None will be used for 'profiles' 105 if there is neither 'profile' nor 'profiles', None will be used for 'profiles'
106 if 'on_new_widget' is present it can have the following values: 106 if 'on_new_widget' is present it can have the following values:
107 C.WIDGET_NEW [default]: self.host.newWidget will be called on widget creation 107 C.WIDGET_NEW [default]: self.host.newWidget will be called on widget creation
108 [callable]: this method will be called instead of self.host.newWidget 108 [callable]: this method will be called instead of self.host.newWidget,
109 and can return another widget to modify the result of the present method
109 None: do nothing 110 None: do nothing
110 if 'on_existing_widget' is present it can have the following values: 111 if 'on_existing_widget' is present it can have the following values:
111 C.WIDGET_KEEP [default]: return the existing widget 112 C.WIDGET_KEEP [default]: return the existing widget
112 C.WIDGET_RAISE: raise WidgetAlreadyExistsError 113 C.WIDGET_RAISE: raise WidgetAlreadyExistsError
113 C.WIDGET_RECREATE: create a new widget *WITH A NEW HASH* 114 C.WIDGET_RECREATE: create a new widget *WITH A NEW HASH*
114 [callable]: this method will be called with existing widget as argument 115 [callable]: this method will be called with existing widget as argument,
116 and can return another widget to modify the result of the present method
115 if 'force_hash' is present, the hash given in value will be used instead of the one returned by class_.getWidgetHash 117 if 'force_hash' is present, the hash given in value will be used instead of the one returned by class_.getWidgetHash
116 other keys will be used to instanciate class_ if the case happen (e.g. if type_ is present and class_ is a QuickChat subclass, 118 other keys will be used to instanciate class_ if the case happen (e.g. if type_ is present and class_ is a QuickChat subclass,
117 it will be used to create a new QuickChat instance). 119 it will be used to create a new QuickChat instance).
118 @return: a class_ instance, either new or already existing 120 @return: a class_ instance, either new or already existing
119 """ 121 """
167 widgets_map[hash_] = widget 169 widgets_map[hash_] = widget
168 170
169 if on_new_widget == C.WIDGET_NEW: 171 if on_new_widget == C.WIDGET_NEW:
170 self.host.newWidget(widget) 172 self.host.newWidget(widget)
171 elif callable(on_new_widget): 173 elif callable(on_new_widget):
172 on_new_widget(widget) 174 result = on_new_widget(widget)
175 if isinstance(result, QuickWidget):
176 widget = result
173 else: 177 else:
174 assert on_new_widget is None 178 assert on_new_widget is None
175 else: 179 else:
176 # the widget already exists 180 # the widget already exists
177 if on_existing_widget == C.WIDGET_KEEP: 181 if on_existing_widget == C.WIDGET_KEEP:
202 hash_idx += 1 206 hash_idx += 1
203 else: 207 else:
204 log.debug(u"Widget already exists, a new one has been recreated with hash {}".format(new_kwargs['force_hash'])) 208 log.debug(u"Widget already exists, a new one has been recreated with hash {}".format(new_kwargs['force_hash']))
205 break 209 break
206 elif callable(on_existing_widget): 210 elif callable(on_existing_widget):
207 on_existing_widget(widget) 211 result = on_existing_widget(widget)
212 if isinstance(result, QuickWidget):
213 widget = result
208 else: 214 else:
209 raise exceptions.InternalError("Unexpected on_existing_widget value ({})".format(on_existing_widget)) 215 raise exceptions.InternalError("Unexpected on_existing_widget value ({})".format(on_existing_widget))
210 216
211 return widget 217 return widget
212 218