1 /*
2 * libjingle
3 * Copyright 2004--2005, 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 #include "talk/examples/call/discoitemsquerytask.h"
29 #include "talk/examples/call/voicemailjidrequester.h"
30 #include "talk/base/scoped_ptr.h"
31 #include "talk/xmpp/constants.h"
32
33 namespace buzz {
34
VoicemailJidRequester(talk_base::Task * parent,const Jid & their_jid,const Jid & my_jid)35 VoicemailJidRequester::VoicemailJidRequester(talk_base::Task* parent,
36 const Jid& their_jid,
37 const Jid& my_jid) : Task(parent),
38 their_jid_(their_jid),
39 my_jid_(my_jid),
40 done_with_query_(false) {
41 parent_ = parent;
42 }
43
ProcessStart()44 int VoicemailJidRequester::ProcessStart() {
45 // Start first query to node='voicemail'
46 DiscoItemsQueryTask* disco_items_task = new DiscoItemsQueryTask(this,
47 STR_VOICEMAIL, their_jid_.BareJid());
48 disco_items_task->SignalGotDiscoItems.connect(this,
49 &VoicemailJidRequester::OnFirstVoicemailJidSuccess);
50 disco_items_task->SignalDiscoItemsError.connect(this,
51 &VoicemailJidRequester::OnFirstVoicemailJidError);
52 disco_items_task->Start();
53 return STATE_BLOCKED;
54 }
55
OnFirstVoicemailJidError(buzz::Jid jid,const XmlElement * xml_element)56 void VoicemailJidRequester::OnFirstVoicemailJidError(buzz::Jid jid,
57 const XmlElement* xml_element) {
58 // First query gave us an error - try second query to node='outgoingvoicemail'
59 // and send it to your own jid
60 StartSecondQuery();
61 }
62
OnFirstVoicemailJidSuccess(buzz::Jid jid,const XmlElement * xml_element)63 void VoicemailJidRequester::OnFirstVoicemailJidSuccess(buzz::Jid jid,
64 const XmlElement* xml_element) {
65 // Process the XML and fire the appropriate signals. If the xml was valid,
66 // then we're done with queries. If it wasn't valid, then start the second
67 // query.
68 bool valid_xml = ProcessVoicemailXml(xml_element);
69 if (valid_xml) {
70 done_with_query_ = true;
71 Wake();
72 } else {
73 StartSecondQuery();
74 }
75 }
76
OnSecondVoicemailJidError(buzz::Jid jid,const XmlElement * xml_element)77 void VoicemailJidRequester::OnSecondVoicemailJidError(buzz::Jid jid,
78 const XmlElement* xml_element) {
79 SignalVoicemailJidError(their_jid_);
80 done_with_query_ = true;
81 Wake();
82 }
83
OnSecondVoicemailJidSuccess(buzz::Jid jid,const XmlElement * xml_element)84 void VoicemailJidRequester::OnSecondVoicemailJidSuccess(buzz::Jid jid,
85 const XmlElement* xml_element) {
86 // Whether this is good xml or bad, we're still done with the query
87 bool valid_xml = ProcessVoicemailXml(xml_element);
88 if (!valid_xml) {
89 SignalVoicemailJidError(their_jid_);
90 }
91 done_with_query_ = true;
92 Wake();
93 }
94
95
StartSecondQuery()96 void VoicemailJidRequester::StartSecondQuery() {
97 // Send a query to your own jid to get the voicemail jid
98 DiscoItemsQueryTask* disco_items_task = new DiscoItemsQueryTask(this,
99 STR_OUTGOINGVOICEMAIL, my_jid_.BareJid());
100 disco_items_task->SignalGotDiscoItems.connect(this,
101 &VoicemailJidRequester::OnSecondVoicemailJidSuccess);
102 disco_items_task->SignalDiscoItemsError.connect(this,
103 &VoicemailJidRequester::OnSecondVoicemailJidError);
104 disco_items_task->Start();
105 }
106
Process(int state)107 int VoicemailJidRequester::Process(int state) {
108 if (done_with_query_) {
109 return STATE_DONE;
110 } else {
111 return talk_base::Task::Process(state);
112 }
113 }
114
ProcessVoicemailXml(const XmlElement * xml_element)115 bool VoicemailJidRequester::ProcessVoicemailXml(const XmlElement* xml_element) {
116 if (!xml_element) {
117 return false;
118 }
119 const std::string& node_name = xml_element->Attr(QN_NODE);
120 // Verify that it's one of the two nodes - we don't really care which one
121 if (node_name != "voicemail" &&
122 node_name != "outgoingvoicemail") {
123 return false;
124 }
125
126 const XmlElement* item = xml_element->FirstNamed(QN_DISCO_ITEM);
127 if (item) {
128 const std::string& jid_str = item->Attr(QN_JID);
129 buzz::Jid voicemail_jid(jid_str);
130 SignalGotVoicemailJid(their_jid_, voicemail_jid);
131 return true;
132 }
133 return false;
134 }
135 }
136