Wt examples 4.5.2
Public Types | Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
ChatEvent Class Reference

Encapsulate a chat event. More...

#include <SimpleChatServer.h>

Public Types

enum  Type { Login , Logout , Rename , Message }
 Enumeration for the event type. More...
 

Public Member Functions

Type type () const
 Get the event type. More...
 
const Wt::WString & user () const
 Get the user who caused the event. More...
 
const Wt::WString & message () const
 Get the message of the event. More...
 
const Wt::WString & data () const
 Get the extra data for this event. More...
 
const Wt::WString formattedHTML (const Wt::WString &user, Wt::TextFormat format) const
 Get the message formatted as HTML, rendered for the given user. More...
 

Private Member Functions

 ChatEvent (const Wt::WString &user, const Wt::WString &message)
 
 ChatEvent (Type type, const Wt::WString &user, const Wt::WString &data=Wt::WString::Empty)
 

Private Attributes

Type type_
 
Wt::WString user_
 
Wt::WString data_
 
Wt::WString message_
 

Friends

class SimpleChatServer
 

Detailed Description

Encapsulate a chat event.

Definition at line 29 of file SimpleChatServer.h.

Member Enumeration Documentation

◆ Type

Enumeration for the event type.

Enumerator
Login 
Logout 
Rename 
Message 

Definition at line 34 of file SimpleChatServer.h.

Constructor & Destructor Documentation

◆ ChatEvent() [1/2]

ChatEvent::ChatEvent ( const Wt::WString &  user,
const Wt::WString &  message 
)
inlineprivate

Definition at line 68 of file SimpleChatServer.h.

70 { }
Wt::WString user_
const Wt::WString & message() const
Get the message of the event.
Wt::WString message_
const Wt::WString & user() const
Get the user who caused the event.

◆ ChatEvent() [2/2]

ChatEvent::ChatEvent ( Type  type,
const Wt::WString &  user,
const Wt::WString &  data = Wt::WString::Empty 
)
inlineprivate

Definition at line 72 of file SimpleChatServer.h.

75 { }
Type type() const
Get the event type.
const Wt::WString & data() const
Get the extra data for this event.
Wt::WString data_

Member Function Documentation

◆ data()

const Wt::WString & ChatEvent::data ( ) const
inline

Get the extra data for this event.

Definition at line 50 of file SimpleChatServer.h.

50{ return data_; }

◆ formattedHTML()

const Wt::WString ChatEvent::formattedHTML ( const Wt::WString &  user,
Wt::TextFormat  format 
) const

Get the message formatted as HTML, rendered for the given user.

The format indicates how the message should be formatted.

Definition at line 13 of file SimpleChatServer.C.

15{
16 switch (type_) {
17 case Login:
18 return Wt::WString("<span class='chat-info'>")
19 + Wt::WWebWidget::escapeText(user_) + " joined.</span>";
20 case Logout:
21 return Wt::WString("<span class='chat-info'>")
22 + ((user == user_) ?
23 Wt::WString("You") :
24 Wt::WWebWidget::escapeText(user_))
25 + " logged out.</span>";
26 case Rename:
27 return "<span class='chat-info'>"
28 + ((user == data_ || user == user_) ?
29 "You are" :
30 (Wt::WWebWidget::escapeText(user_) + " is"))
31 + " now known as " + Wt::WWebWidget::escapeText(data_) + ".</span>";
32 case Message:{
33 Wt::WString result;
34
35 result = Wt::WString("<span class='")
36 + ((user == user_) ?
37 "chat-self" :
38 "chat-user")
39 + "'>" + Wt::WWebWidget::escapeText(user_) + ":</span>";
40
41 Wt::WString msg
42 = (format == Wt::TextFormat::XHTML ? message_ : Wt::WWebWidget::escapeText(message_));
43
44 if (message_.toUTF8().find(user.toUTF8()) != std::string::npos)
45 return result + "<span class='chat-highlight'>" + msg + "</span>";
46 else
47 return result + msg;
48 }
49 default:
50 return "";
51 }
52}

◆ message()

const Wt::WString & ChatEvent::message ( ) const
inline

Get the message of the event.

Definition at line 46 of file SimpleChatServer.h.

46{ return message_; }

◆ type()

Type ChatEvent::type ( ) const
inline

Get the event type.

Definition at line 38 of file SimpleChatServer.h.

38{ return type_; }

◆ user()

const Wt::WString & ChatEvent::user ( ) const
inline

Get the user who caused the event.

Definition at line 42 of file SimpleChatServer.h.

42{ return user_; }

Friends And Related Function Documentation

◆ SimpleChatServer

friend class SimpleChatServer
friend

Definition at line 77 of file SimpleChatServer.h.

Member Data Documentation

◆ data_

Wt::WString ChatEvent::data_
private

Definition at line 62 of file SimpleChatServer.h.

◆ message_

Wt::WString ChatEvent::message_
private

Definition at line 63 of file SimpleChatServer.h.

◆ type_

Type ChatEvent::type_
private

Definition at line 60 of file SimpleChatServer.h.

◆ user_

Wt::WString ChatEvent::user_
private

Definition at line 61 of file SimpleChatServer.h.


The documentation for this class was generated from the following files:

Generated on Wed Jan 12 2022 for the C++ Web Toolkit (Wt) by doxygen 1.9.3