Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Default class - contains the complete message (untouched)
- class Message
- {
- public:
- QString virtual getRawMessage() { return dataRawMessage; }
- protected:
- QString dataRawMessage;
- };
- // Join class - cointains the name of the joined user and the channel
- class MessageJoin : public Message
- {
- public:
- MessageJoin(const QString &rawmessage, const QString &channel, const QString &user)
- {
- dataRawMessage = rawmessage;
- dataChannel = channel;
- dataUser = user;
- }
- QString getChannel() { return dataChannel; }
- QString getUser(){ return dataUser; }
- private:
- QString dataChannel;
- QString dataUser;
- };
- // Notice class - contains a message
- class MessageNotice : public Message
- {
- public:
- MessageNotice(const QString &rawmessage, const QString &text)
- {
- dataRawMessage = rawmessage;
- dataText = text;
- }
- QString getText() { return dataText;}
- private:
- QString dataText;
- };
- // Client code - print message and update UI
- void Client::messageReceived(Message *message)
- {
- if(message)
- {
- MessageJoin *messagejoin;
- MessagePart *messagepart;
- MessageNotice *messagenotice;
- if((messagejoin = dynamic_cast<MessageJoin *>(message)) != 0)
- {
- qDebug() << messagejoin->getUser() << " joined " << messagejoin->getChannel();
- // Update UI: Add user
- }
- else if((messagenotice = dynamic_cast<MessageNotice *>(message)) != 0)
- {
- qDebug() << messagenotice->getText();
- // Update UI: Display message
- }
- else
- {
- qDebug() << "Cannot cast message object";
- }
- delete message; // Message was allocated in the library and is not used anymore
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement