15976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/*
25976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * libjingle
35976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Copyright 2011, Google Inc.
45976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
55976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Redistribution and use in source and binary forms, with or without
65976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * modification, are permitted provided that the following conditions are met:
75976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
85976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
95976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer.
105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer in the documentation
125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     and/or other materials provided with the distribution.
135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  3. The name of the author may not be used to endorse or promote products
145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     derived from this software without specific prior written permission.
155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */
275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/xmpp/pubsubclient.h"
295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <string>
315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <vector>
325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/xmpp/constants.h"
345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/xmpp/jid.h"
355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/xmpp/pubsubtasks.h"
365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgnamespace buzz {
385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid PubSubClient::RequestItems() {
405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  PubSubRequestTask* request_task =
415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      new PubSubRequestTask(parent_, pubsubjid_, node_);
425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  request_task->SignalResult.connect(this, &PubSubClient::OnRequestResult);
435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  request_task->SignalError.connect(this, &PubSubClient::OnRequestError);
445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  PubSubReceiveTask* receive_task =
465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      new PubSubReceiveTask(parent_, pubsubjid_, node_);
475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  receive_task->SignalUpdate.connect(this, &PubSubClient::OnReceiveUpdate);
485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
495976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  receive_task->Start();
505976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  request_task->Start();
515976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
525976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
535976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid PubSubClient::PublishItem(
545976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    const std::string& itemid, XmlElement* payload, std::string* task_id_out) {
555976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  std::vector<XmlElement*> children;
565976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  children.push_back(payload);
575976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  PublishItem(itemid, children, task_id_out);
585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid PubSubClient::PublishItem(
615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    const std::string& itemid, const std::vector<XmlElement*>& children,
625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    std::string* task_id_out) {
635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  PubSubPublishTask* publish_task =
645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      new PubSubPublishTask(parent_, pubsubjid_, node_, itemid, children);
655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  publish_task->SignalError.connect(this, &PubSubClient::OnPublishError);
665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  publish_task->SignalResult.connect(this, &PubSubClient::OnPublishResult);
675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  publish_task->Start();
685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  if (task_id_out) {
695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    *task_id_out = publish_task->task_id();
705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid PubSubClient::RetractItem(
745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    const std::string& itemid, std::string* task_id_out) {
755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  PubSubRetractTask* retract_task =
765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      new PubSubRetractTask(parent_, pubsubjid_, node_, itemid);
775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  retract_task->SignalError.connect(this, &PubSubClient::OnRetractError);
785976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  retract_task->SignalResult.connect(this, &PubSubClient::OnRetractResult);
795976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  retract_task->Start();
805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  if (task_id_out) {
815976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    *task_id_out = retract_task->task_id();
825976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
835976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
855976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid PubSubClient::OnRequestResult(PubSubRequestTask* task,
865976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                   const std::vector<PubSubItem>& items) {
875976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  SignalItems(this, items);
885976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
895976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
905976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid PubSubClient::OnRequestError(IqTask* task,
915976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                  const XmlElement* stanza) {
925976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  SignalRequestError(this, stanza);
935976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
945976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
955976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid PubSubClient::OnReceiveUpdate(PubSubReceiveTask* task,
965976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                   const std::vector<PubSubItem>& items) {
975976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  SignalItems(this, items);
985976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
995976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1005976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgconst XmlElement* GetItemFromStanza(const XmlElement* stanza) {
1015976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  if (stanza != NULL) {
1025976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    const XmlElement* pubsub = stanza->FirstNamed(QN_PUBSUB);
1035976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    if (pubsub != NULL) {
1045976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      const XmlElement* publish = pubsub->FirstNamed(QN_PUBSUB_PUBLISH);
1055976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      if (publish != NULL) {
1065976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        return publish->FirstNamed(QN_PUBSUB_ITEM);
1075976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      }
1085976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    }
1095976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
1105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  return NULL;
1115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
1125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid PubSubClient::OnPublishResult(PubSubPublishTask* task) {
1145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  const XmlElement* item = GetItemFromStanza(task->stanza());
1155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  SignalPublishResult(this, task->task_id(), item);
1165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
1175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid PubSubClient::OnPublishError(IqTask* task,
1195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                  const XmlElement* error_stanza) {
1205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  PubSubPublishTask* publish_task =
1215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      static_cast<PubSubPublishTask*>(task);
1225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  const XmlElement* item = GetItemFromStanza(publish_task->stanza());
1235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  SignalPublishError(this, publish_task->task_id(), item, error_stanza);
1245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
1255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid PubSubClient::OnRetractResult(PubSubRetractTask* task) {
1275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  SignalRetractResult(this, task->task_id());
1285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
1295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgvoid PubSubClient::OnRetractError(IqTask* task,
1315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                  const XmlElement* stanza) {
1325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  PubSubRetractTask* retract_task =
1335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      static_cast<PubSubRetractTask*>(task);
1345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  SignalRetractError(this, retract_task->task_id(), stanza);
1355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
1365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}  // namespace buzz
138