1 /* 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/libjingle/xmpp/constants.h" 12 #include "webrtc/libjingle/xmpp/discoitemsquerytask.h" 13 #include "webrtc/libjingle/xmpp/xmpptask.h" 14 #include "webrtc/base/scoped_ptr.h" 15 16 namespace buzz { 17 18 DiscoItemsQueryTask::DiscoItemsQueryTask(XmppTaskParentInterface* parent, 19 const Jid& to, 20 const std::string& node) 21 : IqTask(parent, STR_GET, to, MakeRequest(node)) { 22 } 23 24 XmlElement* DiscoItemsQueryTask::MakeRequest(const std::string& node) { 25 XmlElement* element = new XmlElement(QN_DISCO_ITEMS_QUERY, true); 26 if (!node.empty()) { 27 element->AddAttr(QN_NODE, node); 28 } 29 return element; 30 } 31 32 void DiscoItemsQueryTask::HandleResult(const XmlElement* stanza) { 33 const XmlElement* query = stanza->FirstNamed(QN_DISCO_ITEMS_QUERY); 34 if (query) { 35 std::vector<DiscoItem> items; 36 for (const buzz::XmlChild* child = query->FirstChild(); child; 37 child = child->NextChild()) { 38 DiscoItem item; 39 const buzz::XmlElement* child_element = child->AsElement(); 40 if (ParseItem(child_element, &item)) { 41 items.push_back(item); 42 } 43 } 44 SignalResult(items); 45 } else { 46 SignalError(this, NULL); 47 } 48 } 49 50 bool DiscoItemsQueryTask::ParseItem(const XmlElement* element, 51 DiscoItem* item) { 52 if (element->HasAttr(QN_JID)) { 53 return false; 54 } 55 56 item->jid = element->Attr(QN_JID); 57 item->name = element->Attr(QN_NAME); 58 item->node = element->Attr(QN_NODE); 59 return true; 60 } 61 62 } // namespace buzz 63