presence.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #ifndef __PJSUA2_PRESENCE_HPP__
  19. #define __PJSUA2_PRESENCE_HPP__
  20. /**
  21. * @file pjsua2/presence.hpp
  22. * @brief PJSUA2 Presence Operations
  23. */
  24. #include <pjsua2/persistent.hpp>
  25. #include <pjsua2/siptypes.hpp>
  26. /** PJSUA2 API is inside pj namespace */
  27. namespace pj
  28. {
  29. /**
  30. * @defgroup PJSUA2_PRES Presence
  31. * @ingroup PJSUA2_Ref
  32. * @{
  33. */
  34. using std::string;
  35. using std::vector;
  36. /**
  37. * This describes presence status.
  38. */
  39. struct PresenceStatus
  40. {
  41. /**
  42. * Buddy's online status.
  43. */
  44. pjsua_buddy_status status;
  45. /**
  46. * Text to describe buddy's online status.
  47. */
  48. string statusText;
  49. /**
  50. * Activity type.
  51. */
  52. pjrpid_activity activity;
  53. /**
  54. * Optional text describing the person/element.
  55. */
  56. string note;
  57. /**
  58. * Optional RPID ID string.
  59. */
  60. string rpidId;
  61. public:
  62. /**
  63. * Constructor.
  64. */
  65. PresenceStatus();
  66. };
  67. /**
  68. * This structure describes buddy configuration when adding a buddy to
  69. * the buddy list with Buddy::create().
  70. */
  71. struct BuddyConfig : public PersistentObject
  72. {
  73. /**
  74. * Buddy URL or name address.
  75. */
  76. string uri;
  77. /**
  78. * Specify whether presence subscription should start immediately.
  79. */
  80. bool subscribe;
  81. public:
  82. /**
  83. * Read this object from a container node.
  84. *
  85. * @param node Container to read values from.
  86. */
  87. virtual void readObject(const ContainerNode &node) PJSUA2_THROW(Error);
  88. /**
  89. * Write this object to a container node.
  90. *
  91. * @param node Container to write values to.
  92. */
  93. virtual void writeObject(ContainerNode &node) const PJSUA2_THROW(Error);
  94. };
  95. /**
  96. * This structure describes buddy info, which can be retrieved by via
  97. * Buddy::getInfo().
  98. */
  99. struct BuddyInfo
  100. {
  101. /**
  102. * The full URI of the buddy, as specified in the configuration.
  103. */
  104. string uri;
  105. /**
  106. * Buddy's Contact, only available when presence subscription has
  107. * been established to the buddy.
  108. */
  109. string contact;
  110. /**
  111. * Flag to indicate that we should monitor the presence information for
  112. * this buddy (normally yes, unless explicitly disabled).
  113. */
  114. bool presMonitorEnabled;
  115. /**
  116. * If \a presMonitorEnabled is true, this specifies the last state of
  117. * the presence subscription. If presence subscription session is currently
  118. * active, the value will be PJSIP_EVSUB_STATE_ACTIVE. If presence
  119. * subscription request has been rejected, the value will be
  120. * PJSIP_EVSUB_STATE_TERMINATED, and the termination reason will be
  121. * specified in \a subTermReason.
  122. */
  123. pjsip_evsub_state subState;
  124. /**
  125. * String representation of subscription state.
  126. */
  127. string subStateName;
  128. /**
  129. * Specifies the last presence subscription termination code. This would
  130. * return the last status of the SUBSCRIBE request. If the subscription
  131. * is terminated with NOTIFY by the server, this value will be set to
  132. * 200, and subscription termination reason will be given in the
  133. * \a subTermReason field.
  134. */
  135. pjsip_status_code subTermCode;
  136. /**
  137. * Specifies the last presence subscription termination reason. If
  138. * presence subscription is currently active, the value will be empty.
  139. */
  140. string subTermReason;
  141. /**
  142. * Presence status.
  143. */
  144. PresenceStatus presStatus;
  145. public:
  146. /**
  147. * Default constructor
  148. */
  149. BuddyInfo()
  150. : presMonitorEnabled(true),
  151. subState(PJSIP_EVSUB_STATE_UNKNOWN),
  152. subTermCode(PJSIP_SC_NULL)
  153. {}
  154. /** Import from pjsip structure */
  155. void fromPj(const pjsua_buddy_info &pbi);
  156. };
  157. /**
  158. * This structure contains parameters for Buddy::onBuddyEvSubState() callback.
  159. */
  160. struct OnBuddyEvSubStateParam
  161. {
  162. /**
  163. * * The event which triggers state change event.
  164. */
  165. SipEvent e;
  166. };
  167. /**
  168. * Buddy. This is a lite wrapper class for PJSUA-LIB buddy, i.e: this class
  169. * only maintains one data member, PJSUA-LIB buddy ID, and the methods are
  170. * simply proxies for PJSUA-LIB buddy operations.
  171. *
  172. * Application can use create() to register a buddy to PJSUA-LIB, and
  173. * the destructor of the original instance (i.e: the instance that calls
  174. * create()) will unregister and delete the buddy from PJSUA-LIB. Application
  175. * must delete all Buddy instances belong to an account before shutting down
  176. * the account (via Account::shutdown()).
  177. *
  178. * The library will not keep a list of Buddy instances, so any Buddy (or
  179. * descendant) instances instantiated by application must be maintained
  180. * and destroyed by the application itself. Any PJSUA2 APIs that return Buddy
  181. * instance(s) such as Account::enumBuddies2() or Account::findBuddy2() will
  182. * just return generated copy. All Buddy methods should work normally on this
  183. * generated copy instance.
  184. */
  185. class Buddy
  186. {
  187. public:
  188. /**
  189. * Constructor.
  190. */
  191. Buddy();
  192. /**
  193. * Destructor. Note that if the Buddy original instance (i.e: the instance
  194. * that calls Buddy::create()) is destroyed, it will also delete the
  195. * corresponding buddy in the PJSUA-LIB. While the destructor of
  196. * a generated copy (i.e: Buddy instance returned by PJSUA2 APIs such as
  197. * Account::enumBuddies2() or Account::findBuddy2()) will do nothing.
  198. */
  199. virtual ~Buddy();
  200. /**
  201. * Create buddy and register the buddy to PJSUA-LIB.
  202. *
  203. * Note that application should maintain the Buddy original instance, i.e:
  204. * the instance that calls this create() method as it is only the original
  205. * instance destructor that will delete the underlying Buddy in PJSUA-LIB.
  206. *
  207. * @param acc The account for this buddy.
  208. * @param cfg The buddy config.
  209. */
  210. void create(Account &acc, const BuddyConfig &cfg) PJSUA2_THROW(Error);
  211. /**
  212. * Check if this buddy is valid.
  213. *
  214. * @return True if it is.
  215. */
  216. bool isValid() const;
  217. /**
  218. * Get PJSUA-LIB buddy ID or index associated with this buddy.
  219. *
  220. * @return Integer greater than or equal to zero.
  221. */
  222. int getId() const;
  223. /**
  224. * Get detailed buddy info.
  225. *
  226. * @return Buddy info.
  227. */
  228. BuddyInfo getInfo() const PJSUA2_THROW(Error);
  229. /**
  230. * Enable/disable buddy's presence monitoring. Once buddy's presence is
  231. * subscribed, application will be informed about buddy's presence status
  232. * changed via \a onBuddyState() callback.
  233. *
  234. * @param subscribe Specify true to activate presence
  235. * subscription.
  236. */
  237. void subscribePresence(bool subscribe) PJSUA2_THROW(Error);
  238. /**
  239. * Update the presence information for the buddy. Although the library
  240. * periodically refreshes the presence subscription for all buddies,
  241. * some application may want to refresh the buddy's presence subscription
  242. * immediately, and in this case it can use this function to accomplish
  243. * this.
  244. *
  245. * Note that the buddy's presence subscription will only be initiated
  246. * if presence monitoring is enabled for the buddy. See
  247. * subscribePresence() for more info. Also if presence subscription for
  248. * the buddy is already active, this function will not do anything.
  249. *
  250. * Once the presence subscription is activated successfully for the buddy,
  251. * application will be notified about the buddy's presence status in the
  252. * \a onBuddyState() callback.
  253. */
  254. void updatePresence(void) PJSUA2_THROW(Error);
  255. /**
  256. * Send instant messaging outside dialog, using this buddy's specified
  257. * account for route set and authentication.
  258. *
  259. * @param prm Sending instant message parameter.
  260. */
  261. void sendInstantMessage(const SendInstantMessageParam &prm)
  262. PJSUA2_THROW(Error);
  263. /**
  264. * Send typing indication outside dialog.
  265. *
  266. * @param prm Sending instant message parameter.
  267. */
  268. void sendTypingIndication(const SendTypingIndicationParam &prm)
  269. PJSUA2_THROW(Error);
  270. public:
  271. /*
  272. * Callbacks
  273. */
  274. /**
  275. * Notify application when the buddy state has changed.
  276. * Application may then query the buddy info to get the details.
  277. */
  278. virtual void onBuddyState()
  279. {}
  280. /**
  281. * Notify application when the state of client subscription session
  282. * associated with a buddy has changed. Application may use this
  283. * callback to retrieve more detailed information about the state
  284. * changed event.
  285. *
  286. * @param prm Callback parameter.
  287. */
  288. virtual void onBuddyEvSubState(OnBuddyEvSubStateParam &prm)
  289. { PJ_UNUSED_ARG(prm); }
  290. private:
  291. /**
  292. * Buddy ID.
  293. */
  294. pjsua_buddy_id id;
  295. private:
  296. friend class Endpoint;
  297. friend class Account;
  298. /* Internal constructor/methods used by Endpoint and Account */
  299. Buddy(pjsua_buddy_id buddy_id);
  300. Buddy *getOriginalInstance();
  301. };
  302. /**
  303. * Warning: deprecated, use BuddyVector2 instead.
  304. *
  305. * Array of buddies.
  306. */
  307. typedef std::vector<Buddy*> BuddyVector;
  308. /** Array of buddies */
  309. typedef std::vector<Buddy> BuddyVector2;
  310. /**
  311. * @} // PJSUA2_PRES
  312. */
  313. } // namespace pj
  314. #endif /* __PJSUA2_PRESENCE_HPP__ */