diff src/tools/xml_tools.py @ 1504:a2e4b976e707

core (xmlui): added helper method to easily manage dialogs with a deferred
author Goffi <goffi@goffi.org>
date Thu, 27 Aug 2015 17:59:22 +0200
parents c7c872a40e56
children 84250128e425
line wrap: on
line diff
--- a/src/tools/xml_tools.py	Thu Aug 27 17:59:15 2015 +0200
+++ b/src/tools/xml_tools.py	Thu Aug 27 17:59:22 2015 +0200
@@ -26,6 +26,7 @@
 from wokkel import data_form
 from twisted.words.xish import domish
 from twisted.words.protocols.jabber import jid
+from twisted.internet import defer
 from sat.core import exceptions
 from collections import OrderedDict
 
@@ -1048,7 +1049,7 @@
         top_element.setAttribute("type", panel_type)
         if title:
             top_element.setAttribute("title", title)
-        self.submit_id = submit_id
+        self._submit_id = submit_id
         self.session_id = session_id
         if panel_type == C.XMLUI_DIALOG:
             if dialog_opt is None:
@@ -1134,7 +1135,7 @@
 
     def _createDialog(self, dialog_opt):
         dialog_type = dialog_opt.setdefault(C.XMLUI_DATA_TYPE, C.XMLUI_DIALOG_MESSAGE)
-        if dialog_type in [C.XMLUI_DIALOG_CONFIRM, C.XMLUI_DIALOG_FILE] and self.submit_id is None:
+        if dialog_type in [C.XMLUI_DIALOG_CONFIRM, C.XMLUI_DIALOG_FILE] and self._submit_id is None:
             raise exceptions.InternalError(_("Submit ID must be filled for this kind of dialog"))
         top_element = TopElement(self)
         level = dialog_opt.get(C.XMLUI_DATA_LVL)
@@ -1198,9 +1199,10 @@
 def note(message, title='', level=C.XMLUI_DATA_LVL_INFO):
     """Sugar to easily create a Note Dialog
 
-    @param message: body of the note
-    @param title: title of the note
-    @param level: one of C.XMLUI_DATA_LVL_*
+    @param message(unicode): body of the note
+    @param title(unicode): title of the note
+    @param level(unicode): one of C.XMLUI_DATA_LVL_*
+    @return(XMLUI): instance of XMLUI
     """
     note_xmlui = XMLUI(C.XMLUI_DIALOG, dialog_opt={
                        C.XMLUI_DATA_TYPE: C.XMLUI_DIALOG_NOTE,
@@ -1210,9 +1212,46 @@
                        )
     return note_xmlui
 
+def deferXMLUI(host, xmlui, profile):
+    """Create a deferred linked to XMLUI
+
+    @param xmlui(XMLUI): instance of the XMLUI
+        Must be an XMLUI that you can submit, with submit_id set to ''
+    @param profile: %(doc_profile)s
+    @return (data): a deferred which fire the data
+    """
+    assert xmlui._submit_id == '' # xmlui.submit_id can't be the empty string, but xmlui._submit_id must here
+    xmlui_d = defer.Deferred()
+
+    def onSubmit(data, profile):
+        xmlui_d.callback(data)
+        return {}
+
+    xmlui.submit_id = host.registerCallback(onSubmit, with_data=True, one_shot=True)
+    host.actionNew({'xmlui': xmlui.toXml()}, profile)
+    return xmlui_d
+
+def deferDialog(host, message, title=u'Please confirm', type_=C.XMLUI_DIALOG_CONFIRM, profile=None):
+    """Create a submitable dialog and manage it with a deferred
+
+    @param message(unicode): message to display
+    @param title(unicode): title of the dialog
+    @param type(unicode): dialog type (C.XMLUI_DIALOG_*)
+    @param profile: %(doc_profile)s
+    @return (dict): Deferred dict
+    """
+    assert profile is not None
+    dialog = XMLUI(C.XMLUI_DIALOG, title=title, dialog_opt = {'type': type_, 'message': message}, submit_id='')
+    return deferXMLUI(host, dialog, profile)
+
+def deferConfirm(*args, **kwargs):
+    """call deferDialog and return a boolean instead of the whole data dict"""
+    d = deferDialog(*args, **kwargs)
+    d.addCallback(lambda data: C.bool(data['answer']))
+    return d
+
 # Misc other funtions
 
-
 class ElementParser(object):
     """callable class to parse XML string into Element