diff src/plugins/plugin_xep_0055.py @ 799:7f2082b192ed

plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
author Goffi <goffi@goffi.org>
date Tue, 04 Feb 2014 18:03:53 +0100
parents bfabeedbf32e
children 9007bb133009
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0055.py	Tue Feb 04 18:02:40 2014 +0100
+++ b/src/plugins/plugin_xep_0055.py	Tue Feb 04 18:03:53 2014 +0100
@@ -17,13 +17,14 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from sat.core.i18n import _
+from sat.core.i18n import _, D_
 from logging import debug, info, warning, error
 from twisted.words.protocols.jabber.xmlstream import IQ
 from twisted.words.protocols.jabber import jid
 from wokkel import data_form
 from sat.core.exceptions import DataError
-from sat.tools.xml_tools import dataForm2XMLUI, dataFormResult2XMLUI
+from sat.memory.memory import Sessions
+from sat.tools import xml_tools
 
 NS_SEARCH = 'jabber:iq:search'
 
@@ -49,9 +50,40 @@
         host.bridge.addMethod("searchRequest", ".plugin", in_sign='sa{ss}s', out_sign='s',
                               method=self._searchRequest,
                               async=True)
+        self._sessions = Sessions()
+        self.__menu_cb_id = host.registerCallback(self._menuCb, with_data=True)
+        self.__search_request_id = host.registerCallback(self._xmluiSearchRequest, with_data=True)
+        host.importMenu((D_("Communication"), D_("Search directory")), self._searchMenu, help_string=D_("Search use directory"))
+
+    def _menuCb(self, data, profile):
+        entity = jid.JID(data[xml_tools.SAT_FORM_PREFIX+'jid'])
+        d = self.getSearchUI(entity, profile)
+        def gotXMLUI(xmlui):
+            session_id, session_data = self._sessions.newSession(profile=profile)
+            session_data['jid'] = entity
+            xmlui.session_id = session_id # we need to keep track of the session
+            xmlui.submit_id = self.__search_request_id
+            return {'xmlui': xmlui.toXml()}
+        d.addCallback(gotXMLUI)
+        return d
+
+
+    def _searchMenu(self, menu_data, profile):
+        """ First XMLUI activated by menu: ask for target jid
+        @param profile: %(doc_profile)s
+
+        """
+        form_ui = xml_tools.XMLUI("form", title=_("Search directory"), submit_id=self.__menu_cb_id)
+        form_ui.addText(_("Please enter the search jid"), 'instructions')
+        form_ui.changeLayout("pairs")
+        form_ui.addLabel("jid")
+        form_ui.addString("jid", value="users.jabberfr.org") # TODO: replace users.jabberfr.org by any XEP-0055 compatible service discovered on current server
+        return {'xmlui': form_ui.toXml()}
 
     def _getSearchUI(self, to_jid_s, profile_key):
-        return self.getSearchUI(jid.JID(to_jid_s), profile_key)
+        d = self.getSearchUI(jid.JID(to_jid_s), profile_key)
+        d.addCallback(lambda xmlui: xmlui.toXml())
+        return d
 
     def getSearchUI(self, to_jid, profile_key):
         """ Ask for a search interface
@@ -80,15 +112,32 @@
             info(_("No data form found"))
             raise NotImplementedError("Only search through data form is implemented so far")
         parsed_form = data_form.Form.fromElement(form_elt)
-        return dataForm2XMLUI(parsed_form, "").toXml()
+        return xml_tools.dataForm2XMLUI(parsed_form, "")
 
     def _fieldsErr(self, failure, profile):
         """ Called when something is wrong with fields request """
         info(_("Fields request failure: %s") % str(failure.value))
         return failure
 
+    def _xmluiSearchRequest(self, raw_data, profile):
+        try:
+            session_data = self._sessions.profileGet(raw_data["session_id"], profile)
+        except KeyError:
+            warning ("session id doesn't exist, session has probably expired")
+            # TODO: send error dialog
+            return defer.succeed({})
+
+        data = xml_tools.XMLUIResult2DataFormResult(raw_data)
+        entity =session_data['jid']
+        d = self.searchRequest(entity, data, profile)
+        d.addCallback(lambda xmlui: {'xmlui':xmlui.toXml()})
+        del self._sessions[raw_data["session_id"]]
+        return d
+
     def _searchRequest(self, to_jid_s, search_dict, profile_key):
-        return self.searchRequest(jid.JID(to_jid_s), search_dict, profile_key)
+        d = self.searchRequest(jid.JID(to_jid_s), search_dict, profile_key)
+        d.addCallback(lambda xmlui: xmlui.toXml())
+        return d
 
     def searchRequest(self, to_jid, search_dict, profile_key):
         """ Actually do a search, according to filled data
@@ -120,10 +169,7 @@
         except StopIteration:
             info(_("No data form found"))
             raise NotImplementedError("Only search through data form is implemented so far")
-        xmlui = dataFormResult2XMLUI(form_elt).toXml()
-        print "=== XMLUI ===\n%s\n\n" %  xmlui
-        return xmlui
-
+        return xml_tools.dataFormResult2XMLUI(form_elt)
 
     def _searchErr(self, failure, profile):
         """ Called when something is wrong with search request """