summaryrefslogtreecommitdiff
path: root/chromium/third_party/libjingle/source/talk/examples/plus/libjingleplus.h
blob: a2898f51a3fab1e7d8529b8b4646f3bdf0e1707c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * libjingle
 * Copyright 2006, Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// LibjinglePlus is a class that connects to Google Talk, creates
// some common tasks, and emits signals when things change

#ifndef LIBJINGLEPLUS_H__
#define LIBJINGLEPLUS_H__

#include "talk/base/basicdefs.h"
#include "talk/app/rosteritem.h"
#include "talk/app/message.h"
#include "talk/app/status.h"
#include "talk/xmpp/xmppengine.h"
#include "talk/base/scoped_ptr.h"


class LibjinglePlusWorker;

class LibjinglePlusNotify {
 public:
  virtual ~LibjinglePlusNotify() {}

  /* Libjingle+ works on its own thread. It will call WakeupMainThread
   * when it has something to report. The main thread should then wake up,
   * and call DoCallbacks on the LibjinglePlus object.
   *
   * This function gets called from libjingle+'s worker thread. All other
   * methods in LibjinglePlusNotify get called from the thread you call
   * DoCallbacks() on.
   *
   * If running on Windows, libjingle+ will use Windows messages to generate
   * callbacks from the main thread, and you don't need to do anything here.
   */
  virtual void WakeupMainThread() = 0;

  /* Connection */
  /* Called when the connection state changes */
  virtual void OnStateChange(buzz::XmppEngine::State) = 0;

  /* Called when the socket closes */
  virtual void OnSocketClose(int error_code) = 0;

  /* Called when XMPP is being sent or received. Used for debugging */
  virtual void OnXmppOutput(const std::string &output) = 0;
  virtual void OnXmppInput(const std::string &input) = 0;

  /* Presence */
  /* Called when someone's Status is updated */
  virtual void OnStatusUpdate(const buzz::Status &status) = 0;

  /* Called when a status update results in an error */
  virtual void OnStatusError(const buzz::XmlElement &stanza) = 0;

  /* Called with an IQ return code */
  virtual void OnIqDone(bool success, const buzz::XmlElement &stanza) = 0;

  /* Message */
  /* Called when a message comes in. */
  virtual void OnMessage(const buzz::XmppMessage &message) = 0;

  /* Roster */

  /* Called when we start refreshing the roster */
  virtual void OnRosterRefreshStarted() = 0;
  /* Called when we have the entire roster */
  virtual void OnRosterRefreshFinished() = 0;
  /* Called when an item on the roster is created or updated */
  virtual void OnRosterItemUpdated(const buzz::RosterItem &ri) = 0;
  /* Called when an item on the roster is removed */
  virtual void OnRosterItemRemoved(const buzz::RosterItem &ri) = 0;

  /* Subscriptions */
  virtual void OnRosterSubscribe(const buzz::Jid &jid) = 0;
  virtual void OnRosterUnsubscribe(const buzz::Jid &jid) = 0;
  virtual void OnRosterSubscribed(const buzz::Jid &jid) = 0;
  virtual void OnRosterUnsubscribed(const buzz::Jid &jid) = 0;

};

class LibjinglePlus 
{
 public:
  /* Provide the constructor with your interface. */
  LibjinglePlus(LibjinglePlusNotify *notify);
  ~LibjinglePlus();
 
  /* Logs in and starts doing stuff 
   *
   * If cookie_auth is true, password must be a Gaia SID. Otherwise,
   * it should be the user's password
   */
  void Login(const std::string &username, const std::string &password,
	     const std::string &machine_address, bool is_test, bool cookie_auth);

  /* Set Presence */
  void SendPresence(const buzz::Status & s);
  void SendDirectedPresence(const buzz::Jid & j, const buzz::Status & s);
  void SendDirectedMUCPresence(const buzz::Jid & j, const buzz::Status & s, 
		       const std::string &user_nick, const std::string &api_capability,
		       const std::string &api_message, const std::string &role);

  /* Send Message */
  void SendXmppMessage(const buzz::XmppMessage & m);

  /* Send IQ */
  void SendXmppIq(const buzz::Jid &to_jid, bool is_get,
                  const buzz::XmlElement *iq_element);

  /* Set Roster */
  void UpdateRosterItem(const buzz::Jid & jid, const std::string & name, 
			const std::vector<std::string> & groups, buzz::GrType grt);
  void RemoveRosterItem(const buzz::Jid &jid);

  /* Call this from the thread you want to receive callbacks on. Typically, this will be called
   * after your WakeupMainThread() notify function is called.
   *
   * On Windows, libjingle+ will trigger its callback from the Windows message loop, and
   * you needn't call this yourself.
   */
  void DoCallbacks();

 private:
  void LoginInternal(const std::string &jid, const std::string &password,
		     const std::string &machine_address, bool is_test);

  LibjinglePlusWorker *worker_;
};

#endif  // LIBJINGLE_PLUS_H__