A self-contained chat widget. More...
#include <SimpleChatWidget.h>

A self-contained chat widget.
Definition at line 33 of file SimpleChatWidget.h.
typedef std::map<Wt::WString, bool> SimpleChatWidget::UserMap [private] |
Definition at line 68 of file SimpleChatWidget.h.
| SimpleChatWidget::SimpleChatWidget | ( | SimpleChatServer & | server, |
| Wt::WContainerWidget * | parent = 0 |
||
| ) |
Create a chat widget that will connect to the given server.
Definition at line 26 of file SimpleChatWidget.C.
: WContainerWidget(parent), server_(server), app_(WApplication::instance()), messageReceived_("sounds/message_received.mp3") { user_ = server_.suggestGuest(); letLogin(); }
| SimpleChatWidget::~SimpleChatWidget | ( | ) |
| virtual void SimpleChatWidget::createLayout | ( | WWidget * | messages, |
| WWidget * | userList, | ||
| WWidget * | messageEdit, | ||
| WWidget * | sendButton, | ||
| WWidget * | logoutButton | ||
| ) | [protected, virtual] |
| void SimpleChatWidget::letLogin | ( | ) |
Show a simple login screen.
Definition at line 42 of file SimpleChatWidget.C.
{
clear();
WVBoxLayout *vLayout = new WVBoxLayout();
setLayout(vLayout, AlignLeft | AlignTop);
WHBoxLayout *hLayout = new WHBoxLayout();
vLayout->addLayout(hLayout);
hLayout->addWidget(new WLabel("User name:"), 0, AlignMiddle);
hLayout->addWidget(userNameEdit_ = new WLineEdit(user_), 0, AlignMiddle);
userNameEdit_->setFocus();
WPushButton *b = new WPushButton("Login");
hLayout->addWidget(b, 0, AlignMiddle);
hLayout->addStretch(1);
b->clicked().connect(this, &SimpleChatWidget::login);
userNameEdit_->enterPressed().connect(this, &SimpleChatWidget::login);
vLayout->addWidget(statusMsg_ = new WText());
statusMsg_->setTextFormat(PlainText);
}
| void SimpleChatWidget::login | ( | ) | [private] |
Definition at line 67 of file SimpleChatWidget.C.
{
WString name = WWebWidget::escapeText(userNameEdit_->text());
if (!startChat(name))
statusMsg_->setText("Sorry, name '" + name + "' is already taken.");
}
| void SimpleChatWidget::logout | ( | ) |
Definition at line 75 of file SimpleChatWidget.C.
{
if (eventConnection_.connected()) {
eventConnection_.disconnect(); // do not listen for more events
server_.logout(user_);
app_->enableUpdates(false);
letLogin();
}
}
| void SimpleChatWidget::processChatEvent | ( | const ChatEvent & | event ) | [private] |
Definition at line 277 of file SimpleChatWidget.C.
{
/*
* This is where the "server-push" happens. This method is called
* when a new event or message needs to be notified to the user. In
* general, it is called from another session.
*/
/*
* First, take the lock to safely manipulate the UI outside of the
* normal event loop, by having exclusive access to the session.
*/
WApplication::UpdateLock lock(app_);
if (lock) {
/*
* Format and append the line to the conversation.
*
* This is also the step where the automatic XSS filtering will kick in:
* - if another user tried to pass on some JavaScript, it is filtered away.
* - if another user did not provide valid XHTML, the text is automatically
* interpreted as PlainText
*/
bool needPush = false;
/*
* If it is not a normal message, also update the user list.
*/
if (event.type() != ChatEvent::Message) {
if (event.type() == ChatEvent::Rename
&& event.user() == user_)
user_ = event.data();
needPush = true;
updateUsers();
}
bool display = event.type() != ChatEvent::Message
|| !userList_
|| (users_.find(event.user()) != users_.end() && users_[event.user()]);
if (display) {
needPush = true;
WText *w = new WText(event.formattedHTML(user_), messages_);
w->setInline(false);
w->setStyleClass("chat-msg");
/*
* Leave not more than 100 messages in the back-log
*/
if (messages_->count() > 100)
delete messages_->children()[0];
/*
* Little javascript trick to make sure we scroll along with new content
*/
app_->doJavaScript(messages_->jsRef() + ".scrollTop += "
+ messages_->jsRef() + ".scrollHeight;");
/* If this message belongs to another user, play a received sound */
if (event.user() != user_)
messageReceived_.play();
}
if (needPush)
app_->triggerUpdate();
}
}
| void SimpleChatWidget::send | ( | ) | [private] |
Definition at line 235 of file SimpleChatWidget.C.
{
if (!messageEdit_->text().empty())
server_.sendMessage(user_, messageEdit_->text());
}
| SimpleChatServer& SimpleChatWidget::server | ( | ) | [inline] |
Definition at line 56 of file SimpleChatWidget.h.
{ return server_; }
| bool SimpleChatWidget::startChat | ( | const Wt::WString & | user ) |
Start a chat for the given user.
Returns false if the user could not login.
Definition at line 149 of file SimpleChatWidget.C.
{
if (server_.login(user)) {
// this widget supports server-side updates its processChatEvent()
// method is connected to a slot that is triggered from outside this
// session's event loop (usually because another user enters text).
app_->enableUpdates(true);
// FIXME, chatEvent() needs to be protected by the server mutex too
eventConnection_
= server_.chatEvent().connect(this, &SimpleChatWidget::processChatEvent);
user_ = user;
clear();
messages_ = new WContainerWidget();
userList_ = new WContainerWidget();
messageEdit_ = new WTextArea();
messageEdit_->setRows(2);
messageEdit_->setFocus();
// Display scroll bars if contents overflows
messages_->setOverflow(WContainerWidget::OverflowAuto);
userList_->setOverflow(WContainerWidget::OverflowAuto);
sendButton_ = new WPushButton("Send");
WPushButton *logoutButton = new WPushButton("Logout");
createLayout(messages_, userList_, messageEdit_, sendButton_, logoutButton);
/*
* Connect event handlers:
* - click on button
* - enter in text area
*
* We will clear the input field using a small custom client-side
* JavaScript invocation.
*/
// Create a JavaScript 'slot' (JSlot). The JavaScript slot always takes
// 2 arguments: the originator of the event (in our case the
// button or text area), and the JavaScript event object.
clearInput_.setJavaScript
("function(o, e) { setTimeout(function() {"
"" + messageEdit_->jsRef() + ".value='';"
"}, 0); }");
// Bind the C++ and JavaScript event handlers.
sendButton_->clicked().connect(this, &SimpleChatWidget::send);
messageEdit_->enterPressed().connect(this, &SimpleChatWidget::send);
sendButton_->clicked().connect(clearInput_);
messageEdit_->enterPressed().connect(clearInput_);
sendButton_->clicked().connect(messageEdit_, &WLineEdit::setFocus);
messageEdit_->enterPressed().connect(messageEdit_, &WLineEdit::setFocus);
// Prevent the enter from generating a new line, which is its default
// action
messageEdit_->enterPressed().preventDefaultAction();
logoutButton->clicked().connect(this, &SimpleChatWidget::logout);
WText *msg = new WText
("<div><span class='chat-info'>You are joining as "
+ user_ + ".</span></div>", messages_);
msg->setStyleClass("chat-msg");
if (!userList_->parent()) {
delete userList_;
userList_ = 0;
}
if (!sendButton_->parent()) {
delete sendButton_;
sendButton_ = 0;
}
if (!logoutButton->parent())
delete logoutButton;
updateUsers();
return true;
} else
return false;
}
| void SimpleChatWidget::updateUser | ( | ) | [private] |
| void SimpleChatWidget::updateUsers | ( | ) | [protected, virtual] |
Reimplemented in PopupChatWidget.
Definition at line 241 of file SimpleChatWidget.C.
{
if (userList_) {
userList_->clear();
SimpleChatServer::UserSet users = server_.users();
UserMap oldUsers = users_;
users_.clear();
for (SimpleChatServer::UserSet::iterator i = users.begin();
i != users.end(); ++i) {
WCheckBox *w = new WCheckBox(*i, userList_);
w->setInline(false);
UserMap::const_iterator j = oldUsers.find(*i);
if (j != oldUsers.end())
w->setChecked(j->second);
else
w->setChecked(true);
users_[*i] = w->isChecked();
w->changed().connect(this, &SimpleChatWidget::updateUser);
if (*i == user_)
w->setStyleClass("chat-self");
}
}
}
| int SimpleChatWidget::userCount | ( | ) | [inline] |
Definition at line 58 of file SimpleChatWidget.h.
{ return users_.size(); }
Wt::WApplication* SimpleChatWidget::app_ [private] |
Definition at line 72 of file SimpleChatWidget.h.
Wt::JSlot SimpleChatWidget::clearInput_ [private] |
Definition at line 74 of file SimpleChatWidget.h.
boost::signals::connection SimpleChatWidget::eventConnection_ [private] |
Definition at line 87 of file SimpleChatWidget.h.
Wt::WTextArea* SimpleChatWidget::messageEdit_ [private] |
Definition at line 83 of file SimpleChatWidget.h.
Definition at line 82 of file SimpleChatWidget.h.
Wt::WSound SimpleChatWidget::messageReceived_ [private] |
Definition at line 89 of file SimpleChatWidget.h.
Wt::WContainerWidget* SimpleChatWidget::messages_ [private] |
Definition at line 81 of file SimpleChatWidget.h.
Wt::WPushButton* SimpleChatWidget::sendButton_ [private] |
Definition at line 84 of file SimpleChatWidget.h.
SimpleChatServer& SimpleChatWidget::server_ [private] |
Definition at line 71 of file SimpleChatWidget.h.
Wt::WText* SimpleChatWidget::statusMsg_ [private] |
Definition at line 79 of file SimpleChatWidget.h.
Wt::WString SimpleChatWidget::user_ [private] |
Definition at line 76 of file SimpleChatWidget.h.
Wt::WContainerWidget* SimpleChatWidget::userList_ [private] |
Definition at line 85 of file SimpleChatWidget.h.
Wt::WLineEdit* SimpleChatWidget::userNameEdit_ [private] |
Definition at line 78 of file SimpleChatWidget.h.
UserMap SimpleChatWidget::users_ [private] |
Definition at line 69 of file SimpleChatWidget.h.
1.7.2