changeset 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 8f5479f8709a
children e0770d977d58
files frontends/src/primitivus/primitivus src/plugins/plugin_xep_0055.py
diffstat 2 files changed, 55 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/primitivus	Tue Feb 04 18:02:40 2014 +0100
+++ b/frontends/src/primitivus/primitivus	Tue Feb 04 18:03:53 2014 +0100
@@ -295,7 +295,6 @@
         communication = _("Communication")
         menu.addMenu(communication, _("Join room"), self.onJoinRoomRequest, 'meta j')
         menu.addMenu(communication, _("Find Gateways"), self.onFindGatewaysRequest, 'meta g')
-        menu.addMenu(communication, _("Search directory"), self.onSearchDirectory)
         #additionals menus
         #FIXME: do this in a more generic way (in quickapp)
         add_menus = self.bridge.getMenus('', Const.NO_SECURITY_LIMIT)
@@ -595,24 +594,6 @@
         self.current_action_ids.add(id)
         self.current_action_ids_cb[id] = self.onGatewaysFound
 
-    def onSearchDirectory(self, e):
-        debug(_("Search directory request"))
-
-        def requestSearchUi(button, edit):
-            self.removePopUp()
-            search_jid = edit.get_edit_text()
-            if search_jid:
-                def success(xml):
-                    self.addWindow(XMLUI(self,xml_data=xml, misc={'callback': self._onSearchRequest, 'callback_args': [search_jid,]}))
-                def failure(error):
-                    self.showPopUp(sat_widgets.Alert(_("Error"), _("Can't get search UI"), ok_cb=self.removePopUp))
-                self.bridge.getSearchUI(search_jid, self.profile, callback=success, errback=failure)
-
-
-        # TODO: replace users.jabberfr.org by any XEP-0055 compatible service discovered on current server
-        pop_up_widget = sat_widgets.InputDialog(_("Search directory"), _("Please enter the search jid: "), default_txt="users.jabberfr.org", cancel_cb=lambda ignore: self.removePopUp(), ok_cb=requestSearchUi)
-        self.showPopUp(pop_up_widget)
-
     def onAddContactRequest(self, menu):
         pop_up_widget = sat_widgets.InputDialog(_("Adding a contact"), _("Please enter new contact JID"), default_txt = 'name@server.tld', cancel_cb=self.removePopUp, ok_cb=self.onAddContact)
         self.showPopUp(pop_up_widget)
@@ -637,14 +618,6 @@
         gatewayManager = GatewaysManager(self, data, server=target)
         self.addWindow(gatewayManager)
 
-    def _onSearchRequest(self, data, search_jid):
-        def success(xml):
-            ui = XMLUI(self, title=_(u"Search result"), xml_data = xml)
-            ui.show('window')
-        def failure(error):
-            self.showPopUp(sat_widgets.Alert(_("Error"), _("Can't get search UI"), ok_cb=self.removePopUp))
-        self.bridge.searchRequest(search_jid, dict(data), self.profile, callback=success, errback=failure)
-
     def chatStateReceived(self, from_jid_s, state, profile):
         """Signal observer to display a contact chat state
         @param from_jid_s: contact who sent his new state
--- 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 """