1 /*
2  * libjingle
3  * Copyright 2011, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef TALK_XMPP_PUBSUBTASKS_H_
29 #define TALK_XMPP_PUBSUBTASKS_H_
30 
31 #include <vector>
32 
33 #include "talk/base/sigslot.h"
34 #include "talk/xmpp/iqtask.h"
35 #include "talk/xmpp/receivetask.h"
36 
37 namespace buzz {
38 
39 // A PubSub itemid + payload.  Useful for signaling items.
40 struct PubSubItem {
41   std::string itemid;
42   // The entire <item>, owned by the stanza handler.  To keep a
43   // reference after handling, make a copy.
44   const XmlElement* elem;
45 };
46 
47 // An IqTask which gets a <pubsub><items> for a particular jid and
48 // node, parses the items in the response and signals the items.
49 class PubSubRequestTask : public IqTask {
50  public:
51   PubSubRequestTask(XmppTaskParentInterface* parent,
52                     const Jid& pubsubjid,
53                     const std::string& node);
54 
55   sigslot::signal2<PubSubRequestTask*,
56                    const std::vector<PubSubItem>&> SignalResult;
57   // SignalError inherited by IqTask.
58  private:
59   virtual void HandleResult(const XmlElement* stanza);
60 };
61 
62 // A ReceiveTask which listens for <event><items> of a particular
63 // pubsub JID and node and then signals them items.
64 class PubSubReceiveTask : public ReceiveTask {
65  public:
PubSubReceiveTask(XmppTaskParentInterface * parent,const Jid & pubsubjid,const std::string & node)66   PubSubReceiveTask(XmppTaskParentInterface* parent,
67                     const Jid& pubsubjid,
68                     const std::string& node)
69       : ReceiveTask(parent),
70         pubsubjid_(pubsubjid),
71         node_(node) {
72   }
73 
74   sigslot::signal2<PubSubReceiveTask*,
75                    const std::vector<PubSubItem>&> SignalUpdate;
76 
77  protected:
78   virtual bool WantsStanza(const XmlElement* stanza);
79   virtual void ReceiveStanza(const XmlElement* stanza);
80 
81  private:
82   Jid pubsubjid_;
83   std::string node_;
84 };
85 
86 // An IqTask which publishes a <pubsub><publish><item> to a particular
87 // pubsub jid and node.
88 class PubSubPublishTask : public IqTask {
89  public:
90   // Takes ownership of children
91   PubSubPublishTask(XmppTaskParentInterface* parent,
92                     const Jid& pubsubjid,
93                     const std::string& node,
94                     const std::string& itemid,
95                     const std::vector<XmlElement*>& children);
96 
itemid()97   const std::string& itemid() const { return itemid_; }
98 
99   sigslot::signal1<PubSubPublishTask*> SignalResult;
100 
101  private:
102   // SignalError inherited by IqTask.
103   virtual void HandleResult(const XmlElement* stanza);
104 
105   std::string itemid_;
106 };
107 
108 // An IqTask which publishes a <pubsub><publish><retract> to a particular
109 // pubsub jid and node.
110 class PubSubRetractTask : public IqTask {
111  public:
112   PubSubRetractTask(XmppTaskParentInterface* parent,
113                     const Jid& pubsubjid,
114                     const std::string& node,
115                     const std::string& itemid);
116 
itemid()117   const std::string& itemid() const { return itemid_; }
118 
119   sigslot::signal1<PubSubRetractTask*> SignalResult;
120 
121  private:
122   // SignalError inherited by IqTask.
123   virtual void HandleResult(const XmlElement* stanza);
124 
125   std::string itemid_;
126 };
127 
128 }  // namespace buzz
129 
130 #endif  // TALK_XMPP_PUBSUBTASKS_H_
131