1/*
2 * libjingle
3 * Copyright 2004--2013, 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_EXAMPLES_CHAT_CHATAPP_H_
29#define TALK_EXAMPLES_CHAT_CHATAPP_H_
30
31#include "talk/base/thread.h"
32#include "talk/base/scoped_ptr.h"
33
34#include "talk/xmpp/jid.h"
35#include "talk/xmpp/xmppclient.h"
36
37namespace buzz {
38class XmppClient;
39class PresenceOutTask;
40class PresenceReceiveTask;
41class TextChatSendTask;
42class TextChatReceiveTask;
43class ConsoleTask;
44class PresenceStatus;
45}
46
47// This is an example chat app for libjingle, showing how to use xmpp tasks,
48// data, callbacks, etc.  It has a simple text-based UI for logging in,
49// sending and receiving messages, and printing the roster.
50class ChatApp: public sigslot::has_slots<> {
51 public:
52  // Arguments:
53  //   xmpp_client  Points to the XmppClient for the communication channel
54  //    (typically created by the XmppPump object).
55  //   main_thread  Wraps the application's main thread.  Subsidiary threads
56  //    for the various tasks will be forked off of this.
57  ChatApp(buzz::XmppClient* xmpp_client, talk_base::Thread* main_thread);
58
59  // Shuts down and releases all of the contained tasks/threads
60  ~ChatApp();
61
62  // Shuts down the current thread and quits
63  void Quit();
64
65 private:
66  //
67  // Initialization
68  //
69  // Called explicitly after the connection to the chat server is established.
70  void OnXmppOpen();
71
72  //
73  // UI Stuff
74  //
75  // Prints the app main menu on the console.
76  // Called when ui_state_ == STATE_BASE.
77  void PrintMenu();
78
79  // Prints a numbered list of the logged-in user's roster on the console.
80  void PrintRoster();
81
82  // Prints a prompt for the user to enter either the index from the
83  // roster list of the user they wish to chat with, or a fully-qualified
84  // (user@server.ext) jid.
85  // Called when when ui_state_ == STATE_PROMPTJID.
86  void PromptJid();
87
88  // Prints a prompt on the console for the user to enter a message to send.
89  // Called when when ui_state_ == STATE_CHATTING.
90  void PromptChatMessage();
91
92  // Sends our presence state to the chat server (and on to your roster list).
93  // Arguments:
94  //  state Specifies the presence state to show.
95  enum PresenceState {online, away};
96  void BroadcastPresence(PresenceState state);
97
98  // Returns the RosterItem associated with the specified index.
99  // Just a helper to select a roster item from a numbered list in the UI.
100  bool GetRosterItem(int index, buzz::PresenceStatus* status);
101
102  //
103  // Input Handling
104  //
105  // Receives input when ui_state_ == STATE_BASE.  Handles choices from the
106  // main menu.
107  void HandleBaseInput(const std::string& message);
108
109  // Receives input when ui_state_ == STATE_PROMPTJID.  Handles selection
110  // of a JID to chat to.
111  void HandleJidInput(const std::string& message);
112
113  // Receives input when ui_state_ == STATE_CHATTING.  Handles text messages.
114  void HandleChatInput(const std::string& message);
115
116  //
117  // signal/slot Callbacks
118  //
119  // Connected to the XmppClient::SignalStateChange slot.  Receives
120  // notifications of state changes of the connection.
121  void OnStateChange(buzz::XmppEngine::State state);
122
123  // Connected to the PresenceReceiveTask::PresenceUpdate slot.
124  // Receives status messages for the logged-in user's roster (i.e.
125  // an initial list from the server and people coming/going).
126  void OnPresenceUpdate(const buzz::PresenceStatus& status);
127
128  // Connected to the TextChatReceiveTask::SignalTextChatReceived slot.
129  // Called when we receive a text chat from someone else.
130  void OnTextMessage(const buzz::Jid& from, const buzz::Jid& to,
131                     const std::string& message);
132
133  // Receives text input from the console task.  This is where any input
134  // from the user comes in.
135  // Arguments:
136  //   message What the user typed.
137  void OnConsoleMessage(const std::string &message);
138
139  // The XmppClient object associated with this chat application instance.
140  buzz::XmppClient* xmpp_client_;
141
142  // We send presence information through this object.
143  talk_base::scoped_ptr<buzz::PresenceOutTask> presence_out_task_;
144
145  // We receive others presence information through this object.
146  talk_base::scoped_ptr<buzz::PresenceReceiveTask> presence_receive_task_;
147
148  // We send text messages though this object.
149  talk_base::scoped_ptr<buzz::TextChatSendTask> message_send_task_;
150
151  // We receive messages through this object.
152  talk_base::scoped_ptr<buzz::TextChatReceiveTask> message_received_task_;
153
154  // UI gets drawn and receives input through this task.
155  talk_base::scoped_ptr< buzz::ConsoleTask> console_task_;
156
157  // The list of JIDs for the people in the logged-in users roster.
158  // RosterList  roster_list_;
159  typedef std::map<std::string, buzz::PresenceStatus> RosterList;
160  RosterList roster_list_;
161
162  // The JID of the user currently being chatted with.
163  buzz::Jid chat_dest_jid_;
164
165  // UI State constants
166  enum UIState { STATE_BASE, STATE_PROMPTJID, STATE_CHATTING };
167  UIState ui_state_;
168};
169
170#endif  // TALK_EXAMPLES_CHAT_CHATAPP_H_
171
172