annotate src/plugins/plugin_xep_0055.py @ 1507:84250128e425

tools (xml_tools): split dataFormResult2AdvancedList in dataFormResult2XMLUIData and XMLUIData2AdvancedList
author souliane <souliane@mailoo.org>
date Wed, 02 Sep 2015 13:56:34 +0200
parents 8405d622bde0
children ec60682a9d17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
3
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Jabber Search (xep-0055)
1396
069ad98b360d 2015 copyright dates update
Goffi <goffi@goffi.org>
parents: 1219
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
6
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
11
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
16
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
19
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
Goffi <goffi@goffi.org>
parents: 771
diff changeset
20 from sat.core.i18n import _, D_
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
21 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
22 log = getLogger(__name__)
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
23
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.protocols.jabber.xmlstream import IQ
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import jid
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
26 from twisted.internet import defer
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from wokkel import data_form
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
28 from sat.core.constants import Const as C
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from sat.core.exceptions import DataError
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
Goffi <goffi@goffi.org>
parents: 771
diff changeset
30 from sat.memory.memory import Sessions
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
31 from sat.tools import xml_tools
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
32
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
33 from wokkel import disco, iwokkel
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
34 try:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
35 from twisted.words.protocols.xmlstream import XMPPHandler
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
36 except ImportError:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
37 from wokkel.subprotocols import XMPPHandler
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
38 from zope.interface import implements
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
39
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
40
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
41 NS_SEARCH = 'jabber:iq:search'
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
42
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
43 PLUGIN_INFO = {
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
44 "name": "Jabber Search",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "import_name": "XEP-0055",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "type": "XEP",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "protocols": ["XEP-0055"],
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents: 993
diff changeset
48 "dependencies": [],
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents: 993
diff changeset
49 "recommendations": ["XEP-0059"],
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "main": "XEP_0055",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "handler": "no",
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "description": _("""Implementation of Jabber Search""")
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
53 }
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
54
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
55 # config file parameters
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
56 CONFIG_SECTION = "plugin search"
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
57 CONFIG_SERVICE_LIST = "service_list"
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
58
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
59 DEFAULT_SERVICE_LIST = ["salut.libervia.org"]
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
60
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
61 FIELD_SINGLE = "field_single" # single text field for the simple search
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
62 FIELD_CURRENT_SERVICE = "current_service_jid" # read-only text field for the advanced search
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
63
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
64 class XEP_0055(object):
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
65
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
67 log.info(_("Jabber search plugin initialization"))
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.host = host
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
69
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
70 # default search services (config file + hard-coded lists)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
71 self.services = [jid.JID(entry) for entry in host.memory.getConfig(CONFIG_SECTION, CONFIG_SERVICE_LIST, DEFAULT_SERVICE_LIST)]
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
72
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
73 host.bridge.addMethod("searchGetFieldsUI", ".plugin", in_sign='ss', out_sign='s',
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
74 method=self._getFieldsUI,
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
75 async=True)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
76 host.bridge.addMethod("searchRequest", ".plugin", in_sign='sa{ss}s', out_sign='s',
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
77 method=self._searchRequest,
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
78 async=True)
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
Goffi <goffi@goffi.org>
parents: 771
diff changeset
79
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
80 self.__search_menu_id = host.registerCallback(self._getMainUI, with_data=True)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
81 host.importMenu((D_("Communication"), D_("Search directory")), self._getMainUI, security_limit=1, help_string=D_("Search use directory"))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
82
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
83
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
84 ## Main search UI (menu item callback) ##
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
Goffi <goffi@goffi.org>
parents: 771
diff changeset
85
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
86
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
87 def _getMainUI(self, raw_data, profile):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
88 """Get the XMLUI for selecting a service and searching the directory.
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
89
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
90 @param raw_data (dict): data received from the frontend
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
91 @param profile (unicode): %(doc_profile)s
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
92 @return: a deferred XMLUI string representation
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
93 """
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
94 # check if the user's server offers some search services
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
95 d = self.host.findFeaturesSet([NS_SEARCH], profile_key=profile)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
96 return d.addCallback(lambda services: self.getMainUI(list(services), raw_data, profile))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
97
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
98 def getMainUI(self, services, raw_data, profile):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
99 """Get the XMLUI for selecting a service and searching the directory.
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
Goffi <goffi@goffi.org>
parents: 771
diff changeset
100
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
101 @param services (list[jid.JID]): search services offered by the user server
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
102 @param raw_data (dict): data received from the frontend
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
103 @param profile (unicode): %(doc_profile)s
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
104 @return: a deferred XMLUI string representation
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
105 """
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
106 # extend services offered by user's server with the default services
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
107 services.extend([service for service in self.services if service not in services])
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
108 data = xml_tools.XMLUIResult2DataFormResult(raw_data)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
109 main_ui = xml_tools.XMLUI(C.XMLUI_WINDOW, container="tabs", title=_("Search users"), submit_id=self.__search_menu_id)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
110
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
111 d = self._addSimpleSearchUI(services, main_ui, data, profile)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
112 d.addCallback(lambda dummy: self._addAdvancedSearchUI(services, main_ui, data, profile))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
113 return d.addCallback(lambda dummy: {'xmlui': main_ui.toXml()})
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
114
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
115 def _addSimpleSearchUI(self, services, main_ui, data, profile):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
116 """Add to the main UI a tab for the simple search.
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
117
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
118 Display a single input field and search on the main service (it actually does one search per search field and then compile the results).
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
119
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
120 @param services (list[jid.JID]): search services offered by the user server
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
121 @param main_ui (XMLUI): the main XMLUI instance
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
122 @param data (dict): form data without SAT_FORM_PREFIX
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
123 @param profile (unicode): %(doc_profile)s
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
124
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
125 @return: a dummy Deferred
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
Goffi <goffi@goffi.org>
parents: 771
diff changeset
126 """
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
127 service_jid = services[0] # TODO: search on all the given services, not only the first one
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
128
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
129 form = data_form.Form('form', formNamespace=NS_SEARCH)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
130 form.addField(data_form.Field('text-single', FIELD_SINGLE, label=_('Search for'), value=data.get(FIELD_SINGLE, '')))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
131
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
132 sub_cont = main_ui.main_container.addTab("simple_search", label=_("Simple search"), container=xml_tools.VerticalContainer)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
133 main_ui.changeContainer(sub_cont.append(xml_tools.PairsContainer(main_ui)))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
134 xml_tools.dataForm2Widgets(main_ui, form)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
135
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
136 # FIXME: add colspan attribute to divider? (we are in a PairsContainer)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
137 main_ui.addDivider('blank')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
138 main_ui.addDivider('blank') # here we added a blank line before the button
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
139 main_ui.addDivider('blank')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
140 main_ui.addButton(self.__search_menu_id, _("Search"), (FIELD_SINGLE,))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
141 main_ui.addDivider('blank')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
142 main_ui.addDivider('blank') # a blank line again after the button
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
143
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
144 simple_data = {key: value for key, value in data.iteritems() if key in (FIELD_SINGLE,)}
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
145 if simple_data:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
146 log.debug("Simple search with %s on %s" % (simple_data, service_jid))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
147 sub_cont.parent.setSelected(True)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
148 main_ui.changeContainer(sub_cont.append(xml_tools.VerticalContainer(main_ui)))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
149 main_ui.addDivider('dash')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
150 d = self.searchRequest(service_jid, simple_data, profile)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
151 d.addCallbacks(lambda elt: self._displaySearchResult(main_ui, elt),
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
152 lambda failure: main_ui.addText(failure.getErrorMessage()))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
153 return d
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
154
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
155 return defer.succeed(None)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
156
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
157 def _addAdvancedSearchUI(self, services, main_ui, data, profile):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
158 """Add to the main UI a tab for the advanced search.
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
159
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
160 Display a service selector and allow to search on all the fields that are implemented by the selected service.
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
161
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
162 @param services (list[jid.JID]): search services offered by the user server
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
163 @param main_ui (XMLUI): the main XMLUI instance
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
164 @param data (dict): form data without SAT_FORM_PREFIX
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
165 @param profile (unicode): %(doc_profile)s
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
166
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
167 @return: a dummy Deferred
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
168 """
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
169 sub_cont = main_ui.main_container.addTab("advanced_search", label=_("Advanced search"), container=xml_tools.VerticalContainer)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
170 service_selection_fields = ['service_jid', 'service_jid_extra']
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
171
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
172 if 'service_jid_extra' in data:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
173 # refresh button has been pushed, select the tab
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
174 sub_cont.parent.setSelected(True)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
175 # get the selected service
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
176 service_jid_s = data.get('service_jid_extra', '')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
177 if not service_jid_s:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
178 service_jid_s = data.get('service_jid', unicode(services[0]))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
179 log.debug("Refreshing search fields for %s" % service_jid_s)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
180 else:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
181 service_jid_s = data.get(FIELD_CURRENT_SERVICE, unicode(services[0]))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
182 services_s = [unicode(service) for service in services]
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
183 if service_jid_s not in services_s:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
184 services_s.append(service_jid_s)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
185
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
186 main_ui.changeContainer(sub_cont.append(xml_tools.PairsContainer(main_ui)))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
187 main_ui.addLabel(_("Search on"))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
188 main_ui.addList('service_jid', options=services_s, selected=service_jid_s)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
189 main_ui.addLabel(_("Other service"))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
190 main_ui.addString(name='service_jid_extra')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
191
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
192 # FIXME: add colspan attribute to divider? (we are in a PairsContainer)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
193 main_ui.addDivider('blank')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
194 main_ui.addDivider('blank') # here we added a blank line before the button
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
195 main_ui.addDivider('blank')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
196 main_ui.addButton(self.__search_menu_id, _("Refresh fields"), service_selection_fields)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
197 main_ui.addDivider('blank')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
198 main_ui.addDivider('blank') # a blank line again after the button
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
199 main_ui.addLabel(_("Displaying the search form for"))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
200 main_ui.addString(name=FIELD_CURRENT_SERVICE, value=service_jid_s, read_only=True)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
201 main_ui.addDivider('dash')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
202 main_ui.addDivider('dash')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
203
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
204 main_ui.changeContainer(sub_cont.append(xml_tools.VerticalContainer(main_ui)))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
205 service_jid = jid.JID(service_jid_s)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
206 d = self.getFieldsUI(service_jid, profile)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
207 d.addCallbacks(self._addAdvancedForm, lambda failure: main_ui.addText(failure.getErrorMessage()),
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
208 [service_jid, main_ui, sub_cont, data, profile])
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
Goffi <goffi@goffi.org>
parents: 771
diff changeset
209 return d
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
210
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
211 def _addAdvancedForm(self, form_elt, service_jid, main_ui, sub_cont, data, profile):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
212 """Add the search form and the search results (if there is some to display).
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
213
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
214 @param form_elt (domish.Element): form element listing the fields
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
215 @param service_jid (jid.JID): current search service
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
216 @param main_ui (XMLUI): the main XMLUI instance
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
217 @param sub_cont (Container): the container of the current tab
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
218 @param data (dict): form data without SAT_FORM_PREFIX
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
219 @param profile (unicode): %(doc_profile)s
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
220
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
221 @return: a dummy Deferred
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
222 """
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
223 field_list = data_form.Form.fromElement(form_elt).fieldList
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
224 adv_fields = [field.var for field in field_list if field.var]
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
225 adv_data = {key: value for key, value in data.iteritems() if key in adv_fields}
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
226
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
227 xml_tools.dataForm2Widgets(main_ui, data_form.Form.fromElement(form_elt))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
228
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
229 # refill the submitted values
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
230 # FIXME: wokkel's data_form.Form.fromElement doesn't parse the values, so we do it directly in XMLUI for now
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
231 for widget in main_ui.current_container.elem.childNodes:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
232 name = widget.getAttribute("name")
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
233 if adv_data.get(name):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
234 widget.setAttribute("value", adv_data[name])
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
235
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
236 # FIXME: add colspan attribute to divider? (we are in a PairsContainer)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
237 main_ui.addDivider('blank')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
238 main_ui.addDivider('blank') # here we added a blank line before the button
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
239 main_ui.addDivider('blank')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
240 main_ui.addButton(self.__search_menu_id, _("Search"), adv_fields + [FIELD_CURRENT_SERVICE])
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
241 main_ui.addDivider('blank')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
242 main_ui.addDivider('blank') # a blank line again after the button
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
243
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
244 if adv_data: # display the search results
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
245 log.debug("Advanced search with %s on %s" % (adv_data, service_jid))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
246 sub_cont.parent.setSelected(True)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
247 main_ui.changeContainer(sub_cont.append(xml_tools.VerticalContainer(main_ui)))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
248 main_ui.addDivider('dash')
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
249 d = self.searchRequest(service_jid, adv_data, profile)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
250 d.addCallbacks(lambda elt: self._displaySearchResult(main_ui, elt),
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
251 lambda failure: main_ui.addText(failure.getErrorMessage()))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
252 return d
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
253
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
254 return defer.succeed(None)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
255
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
256
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
257 def _displaySearchResult(self, main_ui, elt):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
258 """Display the search results.
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
259
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
260 @param main_ui (XMLUI): the main XMLUI instance
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
261 @param elt (domish.Element): form result element
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
262 """
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
263 if [child for child in elt.children if child.name == "item"]:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
264 xml_tools.dataFormResult2AdvancedList(main_ui, elt)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
265 else:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
266 main_ui.addText(D_("The search gave no result"))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
267
1506
8405d622bde0 plugin XEP-0055: remove a "debug" break that has been forgotten here
souliane <souliane@mailoo.org>
parents: 1498
diff changeset
268
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
269 ## Retrieve the search fields ##
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
270
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
271
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
272 def _getFieldsUI(self, to_jid_s, profile_key):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
273 """Ask a service to send us the list of the form fields it manages.
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
274
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
275 @param to_jid_s (unicode): XEP-0055 compliant search entity
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
276 @param profile_key (unicode): %(doc_profile_key)s
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
277 @return: a deferred XMLUI instance
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
278 """
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
279 d = self.getFieldsUI(jid.JID(to_jid_s), profile_key)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
280 d.addCallback(lambda form: xml_tools.dataFormResult2XMLUI(form).toXml())
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
281 return d
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
282
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
283 def getFieldsUI(self, to_jid, profile_key):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
284 """Ask a service to send us the list of the form fields it manages.
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
285
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
286 @param to_jid (jid.JID): XEP-0055 compliant search entity
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
287 @param profile_key (unicode): %(doc_profile_key)s
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
288 @return: a deferred domish.Element
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
289 """
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
290 client = self.host.getClient(profile_key)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
291 fields_request = IQ(client.xmlstream, 'get')
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
292 fields_request["from"] = client.jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
293 fields_request["to"] = to_jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
294 fields_request.addElement('query', NS_SEARCH)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
295 d = fields_request.send(to_jid.full())
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
296 d.addCallbacks(self._getFieldsUICb, self._getFieldsUIEb)
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
297 return d
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
298
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
299 def _getFieldsUICb(self, answer):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
300 """Callback for self.getFieldsUI.
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
301
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
302 @param answer (domish.Element): search query element
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
303 @return: domish.Element
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
304 """
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
305 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
306 query_elts = answer.elements('jabber:iq:search', 'query').next()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
307 except StopIteration:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
308 log.info(_("No query element found"))
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
309 raise DataError # FIXME: StanzaError is probably more appropriate, check the RFC
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
310 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
311 form_elt = query_elts.elements(data_form.NS_X_DATA, 'x').next()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
312 except StopIteration:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
313 log.info(_("No data form found"))
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
314 raise NotImplementedError("Only search through data form is implemented so far")
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
315 return form_elt
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
316
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
317 def _getFieldsUIEb(self, failure):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
318 """Errback to self.getFieldsUI.
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
319
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
320 @param failure (defer.failure.Failure): twisted failure
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
321 @raise: the unchanged defer.failure.Failure
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
322 """
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
323 log.info(_("Fields request failure: %s") % unicode(failure.getErrorMessage()))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
324 raise failure
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
325
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
326
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
327 ## Do the search ##
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
328
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
329
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
330 def _searchRequest(self, to_jid_s, search_data, profile_key):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
331 """Actually do a search, according to filled data.
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
Goffi <goffi@goffi.org>
parents: 771
diff changeset
332
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
333 @param to_jid_s (unicode): XEP-0055 compliant search entity
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
334 @param search_data (dict): filled data, corresponding to the form obtained in getFieldsUI
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
335 @param profile_key (unicode): %(doc_profile_key)s
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
336 @return: a deferred XMLUI string representation
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
337 """
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
338 d = self.searchRequest(jid.JID(to_jid_s), search_data, profile_key)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
339 d.addCallback(lambda form: xml_tools.dataFormResult2XMLUI(form).toXml())
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
Goffi <goffi@goffi.org>
parents: 771
diff changeset
340 return d
7f2082b192ed plugin XEP-0055, Primitivus: Directory search dialogs are now entirely done in plugin XEP-0055, specific code in frontend is not needed anymore
Goffi <goffi@goffi.org>
parents: 771
diff changeset
341
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
342 def searchRequest(self, to_jid, search_data, profile_key):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
343 """Actually do a search, according to filled data.
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
344
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
345 @param to_jid (jid.JID): XEP-0055 compliant search entity
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
346 @param search_data (dict): filled data, corresponding to the form obtained in getFieldsUI
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
347 @param profile_key (unicode): %(doc_profile_key)s
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
348 @return: a deferred domish.Element
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
349 """
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
350 if FIELD_SINGLE in search_data:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
351 value = search_data[FIELD_SINGLE]
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
352 d = self.getFieldsUI(to_jid, profile_key)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
353 d.addCallback(lambda elt: self.searchRequestMulti(to_jid, value, elt, profile_key))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
354 return d
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
355
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
356 client = self.host.getClient(profile_key)
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
357 search_request = IQ(client.xmlstream, 'set')
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
358 search_request["from"] = client.jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
359 search_request["to"] = to_jid.full()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
360 query_elt = search_request.addElement('query', NS_SEARCH)
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
361 x_form = data_form.Form('submit', formNamespace=NS_SEARCH)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
362 x_form.makeFields(search_data)
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
363 query_elt.addChild(x_form.toElement())
1219
16484ebb695b plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet
souliane <souliane@mailoo.org>
parents: 993
diff changeset
364 # TODO: XEP-0059 could be used here (with the needed new method attributes)
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
365 d = search_request.send(to_jid.full())
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
366 d.addCallbacks(self._searchOk, self._searchErr)
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
367 return d
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
368
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
369 def searchRequestMulti(self, to_jid, value, form_elt, profile_key):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
370 """Search for a value simultaneously in all fields, returns the results compilation.
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
371
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
372 @param to_jid (jid.JID): XEP-0055 compliant search entity
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
373 @param value (unicode): value to search
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
374 @param form_elt (domish.Element): form element listing the fields
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
375 @param profile_key (unicode): %(doc_profile_key)s
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
376 @return: a deferred domish.Element
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
377 """
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
378 form = data_form.Form.fromElement(form_elt)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
379 d_list = []
1506
8405d622bde0 plugin XEP-0055: remove a "debug" break that has been forgotten here
souliane <souliane@mailoo.org>
parents: 1498
diff changeset
380
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
381 for field in [field.var for field in form.fieldList if field.var]:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
382 d_list.append(self.searchRequest(to_jid, {field: value}, profile_key))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
383
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
384 def cb(result): # return the results compiled in one domish element
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
385 result_elt = None
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
386 for success, form_elt in result:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
387 if not success:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
388 continue
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
389 if result_elt is None: # the result element is built over the first answer
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
390 result_elt = form_elt
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
391 continue
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
392 for item_elt in form_elt.elements('jabber:x:data', 'item'):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
393 result_elt.addChild(item_elt)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
394 if result_elt is None:
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
395 raise defer.failure.Failure(DataError(_("The search could not be performed")))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
396 return result_elt
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
397
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
398 return defer.DeferredList(d_list).addCallback(cb)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
399
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
400 def _searchOk(self, answer):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
401 """Callback for self.searchRequest.
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
402
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
403 @param answer (domish.Element): search query element
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
404 @return: domish.Element
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
405 """
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
406 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
407 query_elts = answer.elements('jabber:iq:search', 'query').next()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
408 except StopIteration:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
409 log.info(_("No query element found"))
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
410 raise DataError # FIXME: StanzaError is probably more appropriate, check the RFC
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
411 try:
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
412 form_elt = query_elts.elements(data_form.NS_X_DATA, 'x').next()
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
413 except StopIteration:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 867
diff changeset
414 log.info(_("No data form found"))
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
415 raise NotImplementedError("Only search through data form is implemented so far")
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
416 return form_elt
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
417
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
418 def _searchErr(self, failure):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
419 """Errback to self.searchRequest.
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
420
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
421 @param failure (defer.failure.Failure): twisted failure
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
422 @raise: the unchanged defer.failure.Failure
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
423 """
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
424 log.info(_("Search request failure: %s") % unicode(failure.getErrorMessage()))
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
425 raise failure
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
426
631
694f118d0cd5 plugin XEP-0055: implementation of Jabber Search
Goffi <goffi@goffi.org>
parents:
diff changeset
427
1498
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
428 class XEP_0059_handler(XMPPHandler):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
429 implements(iwokkel.IDisco)
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
430
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
431 def __init__(self, plugin_parent, profile):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
432 self.plugin_parent = plugin_parent
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
433 self.host = plugin_parent.host
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
434 self.profile = profile
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
435
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
436 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
437 return [disco.DiscoFeature(NS_SEARCH)]
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
438
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
439 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
440 return []
e3330ce65285 plugin XEP-0055: add "simple" and "advanced" modes to Jabber search:
souliane <souliane@mailoo.org>
parents: 1409
diff changeset
441