backend, frontends: callbacks refactoring:

- launchAction is now async, and return a dictionary for its result
    - no more action_id, actionResult* are deprecated
    - callback system is about to be unified
This commit is contained in:
Goffi 2013-12-24 15:19:08 +01:00
commit 069a250b04
6 changed files with 151 additions and 78 deletions

View file

@ -190,8 +190,8 @@ class DBusBridgeFrontend(BridgeFrontend):
def isConnected(self, profile_key="@DEFAULT@"):
return self.db_core_iface.isConnected(profile_key)
def launchAction(self, action_type, data, profile_key="@DEFAULT@"):
return unicode(self.db_core_iface.launchAction(action_type, data, profile_key))
def launchAction(self, callback_id, data, profile_key="@DEFAULT@", callback=None, errback=None):
return self.db_core_iface.launchAction(callback_id, data, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
def registerNewAccount(self, login, password, email, host, port=5222):
return unicode(self.db_core_iface.registerNewAccount(login, password, email, host, port))

View file

@ -282,7 +282,6 @@ class PrimitivusApp(QuickApp, InputHistory):
return input
def _dynamicMenuCb(self, xmlui):
misc = {}
ui = XMLUI(self, xml_data = xmlui)
ui.show('popup')
@ -448,6 +447,29 @@ class PrimitivusApp(QuickApp, InputHistory):
#No notification left, we can hide the bar
self.main_widget.footer = self.editBar
def launchAction(self, callback_id, data=None, profile_key="@NONE@"):
""" Launch a dynamic action
@param callback_id: id of the action to launch
@param data: data needed only for certain actions
@param profile_key: %(doc_profile_key)s
"""
if data is None:
data = dict()
def action_cb(data):
if not data:
# action was a one shot, nothing to do
pass
elif "xmlui" in data:
ui = XMLUI(self, xml_data = data['xmlui'])
ui.show('popup')
else:
self.showPopUp(sat_widgets.Alert(_("Error"), _(u"Unmanaged action result"), ok_cb=self.removePopUp))
def action_eb(failure):
self.showPopUp(sat_widgets.Alert(_("Error"), unicode(failure), ok_cb=self.removePopUp))
self.bridge.launchAction(callback_id, data, profile_key, callback=action_cb, errback=action_eb)
def askConfirmation(self, confirmation_id, confirmation_type, data, profile):
if not self.check_profile(profile):
return

View file

@ -109,7 +109,7 @@ class XMLUI(urwid.WidgetWrap):
ctrl.selectValue(elem.getAttribute("value"))
self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
elif type_=="button":
callback_id = elem.getAttribute("callback_id")
callback_id = elem.getAttribute("callback")
ctrl = sat_widgets.CustomButton(value, on_press=self.onButtonPress)
ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")])
elif type_=="advanced_list":
@ -178,6 +178,7 @@ class XMLUI(urwid.WidgetWrap):
top=cat_dom.documentElement
self.type = top.getAttribute("type")
self.title = top.getAttribute("title") or self.title
self.submit_id = top.getAttribute("submit") or None
if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']:
raise InvalidXMLUI
@ -234,7 +235,7 @@ class XMLUI(urwid.WidgetWrap):
def onButtonPress(self, button):
callback_id, fields = button.param_id
data = {"callback_id":callback_id}
data = {}
for field in fields:
ctrl = self.ctrl_list[field]
if isinstance(ctrl['control'],sat_widgets.List):
@ -242,8 +243,7 @@ class XMLUI(urwid.WidgetWrap):
else:
data[field] = ctrl['control'].getValue()
id = self.host.bridge.launchAction("button", data, profile_key = self.host.profile)
self.host.current_action_ids.add(id)
self.host.launchAction(callback_id, data, profile_key = self.host.profile)
def onParamChange(self, widget, extra=None):
"""Called when type is param and a widget to save is modified"""
@ -262,12 +262,13 @@ class XMLUI(urwid.WidgetWrap):
data.append((ctrl_name, ctrl['control'].get_edit_text()))
if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned
raise NotImplementedError
self.host.debug()
elif 'callback' in self.misc:
elif 'callback' in self.misc: # FIXME: this part is not needed anymore
try:
self.misc['callback'](data, *self.misc['callback_args'])
self.misc['callback'](data, submit_id=self.submit_id, *self.misc['callback_args'])
except KeyError:
self.misc['callback'](data)
self.misc['callback'](data, submit_id=self.submit_id)
elif self.submit_id is not None:
self.host.launchAction(self.submit_id, dict(data), profile_key=self.host.profile)
else:
warning (_("The form data is not sent back, the type is not managed properly"))

View file

@ -238,6 +238,39 @@ class MainWindow(wx.Frame, QuickApp):
self.tools.Disable()
return
def launchAction(self, callback_id, data=None, profile_key="@NONE@"):
""" Launch a dynamic action
@param callback_id: id of the action to launch
@param data: data needed only for certain actions
@param profile_key: %(doc_profile_key)s
"""
if data is None:
data = dict()
def action_cb(data):
if not data:
# action was a one shot, nothing to do
pass
elif "xmlui" in data:
debug (_("XML user interface received"))
XMLUI(self, xml_data = data['xmlui'])
else:
dlg = wx.MessageDialog(self, _(u"Unmanaged action result"),
_('Error'),
wx.OK | wx.ICON_ERROR
)
dlg.ShowModal()
dlg.Destroy()
def action_eb(failure):
dlg = wx.MessageDialog(self, unicode(failure),
_('Error'),
wx.OK | wx.ICON_ERROR
)
dlg.ShowModal()
dlg.Destroy()
self.bridge.launchAction(callback_id, data, profile_key, callback=action_cb, errback=action_eb)
def askConfirmation(self, confirmation_id, confirmation_type, data, profile):
#TODO: refactor this in QuickApp
if not self.check_profile(profile):

View file

@ -129,9 +129,7 @@ class Param(wx.Frame):
self.__save_parameters()
name, category = event.GetEventObject().param_id
callback_id = event.GetEventObject().callback_id
data = {"name":name, "category":category, "callback_id":callback_id}
id = self.host.bridge.launchAction("button", data, profile_key = self.host.profile)
self.host.current_action_ids.add(id)
self.host.launchAction(callback_id, None, profile_key = self.host.profile)
event.Skip()
def __save_parameters(self):

View file

@ -30,11 +30,13 @@ class XMLUI(wx.Frame):
"""Create an user interface from a SàT xml"""
def __init__(self, host, xml_data='', title="Form", options = None, misc = None):
style = wx.DEFAULT_FRAME_STYLE & ~wx.CLOSE_BOX if 'NO_CANCEL' in options else wx.DEFAULT_FRAME_STYLE #FIXME: gof: Q&D tmp hack
if options is None:
options = []
style = wx.DEFAULT_FRAME_STYLE & ~wx.CLOSE_BOX if 'NO_CANCEL' in options else wx.DEFAULT_FRAME_STYLE #FIXME: Q&D tmp hack
super(XMLUI, self).__init__(None, title=title, style=style)
self.host = host
self.options = options or []
self.options = options
self.misc = misc or {}
self.ctrl_list = {} # usefull to access ctrl
@ -165,6 +167,7 @@ class XMLUI(wx.Frame):
top= cat_dom.documentElement
self.type = top.getAttribute("type")
self.title = top .getAttribute("title")
self.submit_id = top.getAttribute("submit") or None
if self.title:
self.SetTitle(self.title)
if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']:
@ -195,7 +198,6 @@ class XMLUI(wx.Frame):
def onButtonClicked(self, event):
"""Called when a button is pushed"""
callback_id, fields = event.GetEventObject().param_id
data = {"callback_id":callback_id}
for field in fields:
ctrl = self.ctrl_list[field]
if isinstance(ctrl['control'], wx.ListBox):
@ -203,8 +205,7 @@ class XMLUI(wx.Frame):
else:
data[field] = ctrl['control'].GetValue()
id = self.host.bridge.launchAction("button", data, profile_key = self.host.profile)
self.host.current_action_ids.add(id)
self.host.launchAction(callback_id, None, profile_key = self.host.profile)
event.Skip()
def onFormSubmitted(self, event):
@ -224,6 +225,10 @@ class XMLUI(wx.Frame):
self.host.current_action_ids.add(id)
elif self.misc.has_key('callback'):
self.misc['callback'](data)
elif self.submit_id is not None:
data = dict(selected_values)
self.host.launchAction(self.submit_id, data, profile_key=self.host.profile)
else:
warning (_("The form data is not sent back, the type is not managed properly"))
self.MakeModal(False)