Debian Codegraph @

Xpjproject/pjsip/src/pjsua2/presence.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2013 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pjsua2/presence.hpp>
19 #include <pjsua2/account.hpp>
20 #include "util.hpp"
21
22 using namespace pj;
23 using namespace std;
24
25 #define THIS_FILE "presence.cpp"
26
27
28 ///////////////////////////////////////////////////////////////////////////////
29
30 PresenceStatus::PresenceStatus()
31 : status(PJSUA_BUDDY_STATUS_UNKNOWN), activity(PJRPID_ACTIVITY_UNKNOWN)
32 {
33 }
34
35
36 ///////////////////////////////////////////////////////////////////////////////
37
38 void BuddyConfig::readObject(const ContainerNode &node) PJSUA2_THROW(Error)
39 {
40 ContainerNode this_node = node.readContainer("BuddyConfig");
41
42 NODE_READ_STRING ( this_node, uri);
43 NODE_READ_BOOL ( this_node, subscribe);
44 NODE_READ_BOOL ( this_node, subscribe_dlg_event);
45 }
46
47 void BuddyConfig::writeObject(ContainerNode &node) const PJSUA2_THROW(Error)
48 {
49 ContainerNode this_node = node.writeNewContainer("BuddyConfig");
50
51 NODE_WRITE_STRING ( this_node, uri);
52 NODE_WRITE_BOOL ( this_node, subscribe);
53 NODE_WRITE_BOOL ( this_node, subscribe_dlg_event);
54 }
55
56 //////////////////////////////////////////////////////////////////////////////
57
58 void BuddyInfo::fromPj(const pjsua_buddy_info &pbi)
59 {
60 uri = pj2Str(pbi.uri);
61 contact = pj2Str(pbi.contact);
62 accId = pbi.acc_id;
63 presMonitorEnabled = PJ2BOOL(pbi.monitor_pres);
64 subState = pbi.sub_state;
65 subStateName = string(pbi.sub_state_name);
66 subTermCode = (pjsip_status_code)pbi.sub_term_code;
67 subTermReason = pj2Str(pbi.sub_term_reason);
68
69 /* Presence status */
70 presStatus.status = pbi.status;
71 presStatus.statusText = pj2Str(pbi.status_text);
72 presStatus.activity = pbi.rpid.activity;
73 presStatus.note = pj2Str(pbi.rpid.note);
74 presStatus.rpidId = pj2Str(pbi.rpid.id);
75 }
76
77 //////////////////////////////////////////////////////////////////////////////
78 typedef struct BuddyUserData
79 {
80 Buddy *self;
81 Account *acc;
82 } BuddyUserData;
83
84 /*
85 * Constructor.
86 */
87 Buddy::Buddy()
88 : id(PJSUA_INVALID_ID)
89 {
90 }
91
92 Buddy::Buddy(pjsua_buddy_id buddy_id)
93 : id(buddy_id)
94 {
95 }
96
97 Buddy *Buddy::getOriginalInstance()
98 {
99 BuddyUserData *bud = (BuddyUserData*)pjsua_buddy_get_user_data(id);
100 return (bud? bud->self : NULL);
101 }
102
103 /*
104 * Destructor.
105 */
106 Buddy::~Buddy()
107 {
108 if (isValid() && getOriginalInstance()==this) {
109 Account *acc = NULL;
110 BuddyUserData *bud = (BuddyUserData*)pjsua_buddy_get_user_data(id);
111 pjsua_buddy_set_user_data(id, NULL);
112 if (bud) {
113 acc = bud->acc;
114 delete bud;
115 }
116
117 pj_status_t status = pjsua_buddy_del(id);
118 if (status != PJ_SUCCESS) {
119 // TODO: schedule retry to delete buddy
120 PJ_PERROR(1,(THIS_FILE, status, "Failed to delete buddy %d", id));
121 }
122
123 #if !DEPRECATED_FOR_TICKET_2232
124 /* Remove from account buddy list */
125 if (acc)
126 acc->removeBuddy(this);
127 #else
128 PJ_UNUSED_ARG(acc);
129 #endif
130 }
131 }
132
133 /*
134 * Create buddy and register the buddy to PJSUA-LIB.
135 */
136 void Buddy::create(Account &account, const BuddyConfig &cfg)
137 PJSUA2_THROW(Error)
138 {
139 pjsua_buddy_config pj_cfg;
140 pjsua_buddy_config_default(&pj_cfg);
141
142 if (!account.isValid())
143 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "Buddy::create()", "Invalid account");
144
145 BuddyUserData *bud = new BuddyUserData();
146 bud->self = this;
147 bud->acc = &account;
148
149 pj_cfg.uri = str2Pj(cfg.uri);
150 pj_cfg.subscribe = cfg.subscribe;
151 pj_cfg.subscribe_dlg_event = cfg.subscribe_dlg_event;
152 pj_cfg.user_data = (void*)bud;
153 pj_cfg.acc_id = account.getId();
154 PJSUA2_CHECK_EXPR( pjsua_buddy_add(&pj_cfg, &id) );
155
156 account.addBuddy(this);
157 }
158
159 int Buddy::getId() const
160 {
161 return id;
162 }
163
164 /*
165 * Check if this buddy is valid.
166 */
167 bool Buddy::isValid() const
168 {
169 return PJ2BOOL( pjsua_buddy_is_valid(id) );
170 }
171
172 /*
173 * Get detailed buddy info.
174 */
175 BuddyInfo Buddy::getInfo() const PJSUA2_THROW(Error)
176 {
177 pjsua_buddy_info pj_bi;
178 BuddyInfo bi;
179
180 PJSUA2_CHECK_EXPR( pjsua_buddy_get_info(id, &pj_bi) );
181 bi.fromPj(pj_bi);
182 return bi;
183 }
184
185 /*
186 * Enable/disable buddy's presence monitoring.
187 */
188 void Buddy::subscribePresence(bool subscribe) PJSUA2_THROW(Error)
189 {
190 PJSUA2_CHECK_EXPR( pjsua_buddy_subscribe_pres(id, subscribe) );
191 }
192
193 /*
194 * Enable/disable buddy's dialog event monitoring.
195 */
196 void Buddy::subscribeDlgEvent(bool subscribe) PJSUA2_THROW(Error)
197 {
198 PJSUA2_CHECK_EXPR( pjsua_buddy_subscribe_dlg_event(id, subscribe) );
199 }
200
201 /*
202 * Update the presence information for the buddy.
203 */
204 void Buddy::updatePresence(void) PJSUA2_THROW(Error)
205 {
206 PJSUA2_CHECK_EXPR( pjsua_buddy_update_pres(id) );
207 }
208
209 /*
210 * Update the dialog event information for the buddy.
211 */
212 void Buddy::updateDlgEvent(void) PJSUA2_THROW(Error)
213 {
214 PJSUA2_CHECK_EXPR( pjsua_buddy_update_dlg_event(id) );
215 }
216
217 /*
218 * Send instant messaging outside dialog.
219 */
220 void Buddy::sendInstantMessage(const SendInstantMessageParam &prm)
221 PJSUA2_THROW(Error)
222 {
223 BuddyInfo bi = getInfo();
224 BuddyUserData *bud = (BuddyUserData*)pjsua_buddy_get_user_data(id);
225 Account *acc = bud? bud->acc : NULL;
226
227 if (!bud || bi.accId == PJSUA_INVALID_ID || !acc || !acc->isValid()) {
228 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "sendInstantMessage()",
229 "Invalid Buddy");
230 }
231
232 pj_str_t to = str2Pj(bi.contact.empty()? bi.uri : bi.contact);
233 pj_str_t mime_type = str2Pj(prm.contentType);
234 pj_str_t content = str2Pj(prm.content);
235 void *user_data = (void*)prm.userData;
236 pjsua_msg_data msg_data;
237 prm.txOption.toPj(msg_data);
238
239 PJSUA2_CHECK_EXPR( pjsua_im_send(acc->getId(), &to, &mime_type, &content,
240 &msg_data, user_data) );
241 }
242
243 /*
244 * Send typing indication outside dialog.
245 */
246 void Buddy::sendTypingIndication(const SendTypingIndicationParam &prm)
247 PJSUA2_THROW(Error)
248 {
249 BuddyInfo bi = getInfo();
250 BuddyUserData *bud = (BuddyUserData*)pjsua_buddy_get_user_data(id);
251 Account *acc = bud? bud->acc : NULL;
252
253 if (!bud || bi.accId == PJSUA_INVALID_ID || !acc || !acc->isValid()) {
254 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "sendInstantMessage()",
255 "Invalid Buddy");
256 }
257
258 pj_str_t to = str2Pj(bi.contact.empty()? bi.uri : bi.contact);
259 pjsua_msg_data msg_data;
260 prm.txOption.toPj(msg_data);
261
262 PJSUA2_CHECK_EXPR( pjsua_im_typing(acc->getId(), &to, prm.isTyping,
263 &msg_data) );
264 }
265