Communi  3.7.0
A cross-platform IRC framework written with Qt
ircmessageformatter.cpp File Reference

Client example More...

Detailed Description

/*
* Copyright (C) 2008-2020 The Communi Project
*
* This example is free, and not covered by the BSD license. There is no
* restriction applied to their modification, redistribution, using and so on.
* You can study them, modify them, use them in your own program - either
* completely or partially.
*/
#include <IrcTextFormat>
#include <IrcConnection>
#include <QTime>
#include <Irc>
QString IrcMessageFormatter::formatMessage(IrcMessage* message)
{
QString formatted;
switch (message->type()) {
formatted = formatJoinMessage(static_cast<IrcJoinMessage*>(message));
break;
formatted = formatModeMessage(static_cast<IrcModeMessage*>(message));
break;
formatted = formatNamesMessage(static_cast<IrcNamesMessage*>(message));
break;
formatted = formatNickMessage(static_cast<IrcNickMessage*>(message));
break;
formatted = formatPartMessage(static_cast<IrcPartMessage*>(message));
break;
formatted = formatPrivateMessage(static_cast<IrcPrivateMessage*>(message));
break;
formatted = formatQuitMessage(static_cast<IrcQuitMessage*>(message));
break;
default:
break;
}
return formatMessage(formatted);
}
QString IrcMessageFormatter::formatMessage(const QString& message)
{
if (!message.isEmpty()) {
QString formatted = QObject::tr("[%1] %2").arg(QTime::currentTime().toString(), message);
if (message.startsWith("!"))
formatted = QObject::tr("<font color='gray'>%1</font>").arg(formatted);
else if (message.startsWith("*"))
formatted = QObject::tr("<font color='maroon'>%1</font>").arg(formatted);
else if (message.startsWith("["))
formatted = QObject::tr("<font color='indianred'>%1</font>").arg(formatted);
return formatted;
}
return QString();
}
QString IrcMessageFormatter::formatJoinMessage(IrcJoinMessage* message)
{
if (message->flags() & IrcMessage::Own)
return QObject::tr("! You have joined %1 as %2").arg(message->channel(), message->nick());
else
return QObject::tr("! %1 has joined %2").arg(message->nick(), message->channel());
}
QString IrcMessageFormatter::formatModeMessage(IrcModeMessage* message)
{
QString args = message->arguments().join(" ");
if (message->isReply())
return QObject::tr("! %1 mode is %2 %3").arg(message->target(), message->mode(), args);
else
return QObject::tr("! %1 sets mode %2 %3 %4").arg(message->nick(), message->target(), message->mode(), args);
}
QString IrcMessageFormatter::formatNamesMessage(IrcNamesMessage* message)
{
return QObject::tr("! %1 has %2 users").arg(message->channel()).arg(message->names().count());
}
QString IrcMessageFormatter::formatNickMessage(IrcNickMessage* message)
{
return QObject::tr("! %1 has changed nick to %2").arg(message->oldNick(), message->newNick());
}
QString IrcMessageFormatter::formatPartMessage(IrcPartMessage* message)
{
if (message->reason().isEmpty())
return QObject::tr("! %1 has left %2").arg(message->nick(), message->channel());
else
return QObject::tr("! %1 has left %2 (%3)").arg(message->nick(), message->channel(), message->reason());
}
QString IrcMessageFormatter::formatPrivateMessage(IrcPrivateMessage* message)
{
const QString content = IrcTextFormat().toHtml(message->content());
if (message->isAction())
return QObject::tr("* %1 %2").arg(message->nick(), content);
else
return QObject::tr("&lt;%1&gt; %2").arg(message->nick(),content);
}
QString IrcMessageFormatter::formatQuitMessage(IrcQuitMessage* message)
{
if (message->reason().isEmpty())
return QObject::tr("! %1 has quit").arg(message->nick());
else
return QObject::tr("! %1 has quit (%2)").arg(message->nick(), message->reason());
}
Represents a join message.
Definition: ircmessage.h:299
The base class of all messages.
Definition: ircmessage.h:48
@ Own
The message is user's own message.
Definition: ircmessage.h:100
@ Nick
A nick message (IrcNickMessage).
Definition: ircmessage.h:80
@ Mode
A mode message (IrcModeMessage).
Definition: ircmessage.h:77
@ Part
A part message (IrcPartMessage).
Definition: ircmessage.h:83
@ Join
A join message (IrcJoinMessage).
Definition: ircmessage.h:75
@ Private
A private message (IrcPrivateMessage).
Definition: ircmessage.h:86
@ Quit
A quit message (IrcQuitMessage).
Definition: ircmessage.h:87
@ Names
A names message (IrcNamesMessage).
Definition: ircmessage.h:79
Represents a mode message.
Definition: ircmessage.h:339
Represents a names list message.
Definition: ircmessage.h:384
Represents a nick message.
Definition: ircmessage.h:402
Represents a part message.
Definition: ircmessage.h:462
Represents a private message.
Definition: ircmessage.h:512
Represents a quit message.
Definition: ircmessage.h:538
Provides methods for text formatting.
Definition: irctextformat.h:46
Q_INVOKABLE QString toHtml(const QString &text) const
Definition: irctextformat.cpp:446
Client example