#include <QLineEdit>
#include <QListView>
#if (QT_VERSION) >= (QT_VERSION_CHECK(5, 10, 0))
#include <QRandomGenerator>
#endif
#include <QScrollBar>
#include <QShortcut>
#include <QTextCursor>
#include <QTextDocument>
#include <QTextEdit>
#include <QTime>
#include <QVBoxLayout>
#include <Irc>
#include <IrcUser>
#include <IrcBuffer>
#include <IrcCommand>
#include <IrcMessage>
#include <IrcUserModel>
#include <IrcCompleter>
#include <IrcConnection>
#include <IrcBufferModel>
#include <IrcCommandParser>
static const char* CHANNEL = "#libera";
static const char* SERVER = "irc.libera.chat";
IrcClient::IrcClient(QWidget* parent) : QSplitter(parent)
{
createParser();
createConnection();
createCompleter();
createUserList();
createLayout();
createBufferList();
connection->open();
textEdit->append(IrcMessageFormatter::formatMessage(tr(
"! Welcome to the Communi %1 example client.").arg(
IRC_VERSION_STR)));
textEdit->append(IrcMessageFormatter::formatMessage(tr("! This example connects %1 and joins %2.").arg(SERVER, CHANNEL)));
textEdit->append(IrcMessageFormatter::formatMessage(tr("! PS. Available commands: JOIN, ME, NICK, PART")));
}
IrcClient::~IrcClient()
{
if (connection->isActive()) {
connection->
quit(connection->realName());
}
}
void IrcClient::onConnected()
{
textEdit->append(IrcMessageFormatter::formatMessage(QStringLiteral("! Connected to %1.")).arg(SERVER));
textEdit->append(IrcMessageFormatter::formatMessage(QStringLiteral("! Joining %1...")).arg(CHANNEL));
}
void IrcClient::onConnecting()
{
textEdit->append(IrcMessageFormatter::formatMessage(QStringLiteral("! Connecting %1...")).arg(SERVER));
}
void IrcClient::onDisconnected()
{
textEdit->append(IrcMessageFormatter::formatMessage(QStringLiteral("! Disconnected from %1.")).arg(SERVER));
}
void IrcClient::onTextEdited()
{
lineEdit->setStyleSheet(QString());
}
void IrcClient::onTextEntered()
{
QString input = lineEdit->text();
if (command) {
receiveMessage(msg);
delete msg;
}
lineEdit->clear();
} else if (input.length() > 1) {
QString error;
#if (QT_VERSION) >= (QT_VERSION_CHECK(5, 14, 0))
QString command = lineEdit->text().mid(1).split(QStringLiteral(" "), Qt::SkipEmptyParts).value(0).toUpper();
#else
QString command = lineEdit->text().mid(1).split(" ", QString::SkipEmptyParts).value(0).toUpper();
#endif
if (parser->commands().contains(command))
error = tr(
"[ERROR] Syntax: %1").arg(parser->
syntax(command).replace(QLatin1String(
"<"), QLatin1String(
"<")).replace(QLatin1String(
">"), QLatin1String(
">")));
else
error = tr("[ERROR] Unknown command: %1").arg(command);
textEdit->append(IrcMessageFormatter::formatMessage(error));
lineEdit->setStyleSheet(QStringLiteral("background: salmon"));
}
}
void IrcClient::onCompletion()
{
completer->
complete(lineEdit->text(), lineEdit->cursorPosition());
}
void IrcClient::onCompleted(const QString& text, int cursor)
{
lineEdit->setText(text);
lineEdit->setCursorPosition(cursor);
}
void IrcClient::onBufferAdded(
IrcBuffer* buffer)
{
QTextDocument* document = new QTextDocument(buffer);
documents.insert(buffer, document);
userModels.insert(buffer, userModel);
int idx = bufferModel->buffers().indexOf(buffer);
if (idx != -1)
bufferList->setCurrentIndex(bufferModel->
index(idx));
}
void IrcClient::onBufferRemoved(
IrcBuffer* buffer)
{
delete userModels.take(buffer);
delete documents.take(buffer);
}
void IrcClient::onBufferActivated(const QModelIndex& index)
{
textEdit->setDocument(documents.value(buffer));
textEdit->verticalScrollBar()->triggerAction(QScrollBar::SliderToMaximum);
userList->setModel(userModels.value(buffer));
completer->setBuffer(buffer);
if (buffer)
parser->setTarget(buffer->title());
}
void IrcClient::onUserActivated(const QModelIndex& index)
{
if (user) {
int idx = bufferModel->buffers().indexOf(buffer);
if (idx != -1)
bufferList->setCurrentIndex(bufferModel->
index(idx));
}
}
static void appendHtml(QTextDocument* document, const QString& html)
{
QTextCursor cursor(document);
cursor.beginEditBlock();
cursor.movePosition(QTextCursor::End);
if (!document->isEmpty())
cursor.insertBlock();
cursor.insertHtml(html);
cursor.endEditBlock();
}
void IrcClient::receiveMessage(
IrcMessage* message)
{
IrcBuffer* buffer = qobject_cast<IrcBuffer*>(sender());
if (!buffer)
QTextDocument* document = documents.value(buffer);
if (document) {
QString html = IrcMessageFormatter::formatMessage(message);
if (!html.isEmpty()) {
if (document == textEdit->document())
textEdit->append(html);
else
appendHtml(document, html);
}
}
}
void IrcClient::createLayout()
{
textEdit = new QTextEdit(this);
textEdit->setReadOnly(true);
lineEdit = new QLineEdit(this);
lineEdit->setAttribute(Qt::WA_MacShowFocusRect, false);
textEdit->setFocusProxy(lineEdit);
connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(onTextEntered()));
connect(lineEdit, SIGNAL(textEdited(QString)), this, SLOT(onTextEdited()));
QSplitter* splitter = new QSplitter(this);
splitter->setHandleWidth(1);
splitter->addWidget(textEdit);
splitter->addWidget(userList);
splitter->setStretchFactor(0, 5);
splitter->setStretchFactor(1, 1);
QWidget* container = new QWidget(this);
QVBoxLayout* layout = new QVBoxLayout(container);
layout->setSpacing(0);
layout->setMargin(0);
layout->addWidget(splitter);
layout->addWidget(lineEdit);
addWidget(container);
setHandleWidth(1);
}
void IrcClient::createCompleter()
{
completer->setParser(parser);
connect(completer, SIGNAL(completed(QString,int)), this, SLOT(onCompleted(QString,int)));
QShortcut* shortcut = new QShortcut(Qt::Key_Tab, this);
connect(shortcut, SIGNAL(activated()), this, SLOT(onCompletion()));
}
void IrcClient::createParser()
{
parser->setTolerant(true);
parser->setTriggers(QStringList("/"));
}
void IrcClient::createUserList()
{
userList = new QListView(this);
userList->setFocusPolicy(Qt::NoFocus);
connect(userList, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(onUserActivated(QModelIndex)));
}
void IrcClient::createBufferList()
{
connect(bufferModel, SIGNAL(removed(
IrcBuffer*)),
this, SLOT(onBufferRemoved(
IrcBuffer*)));
bufferList = new QListView(this);
bufferList->setFocusPolicy(Qt::NoFocus);
bufferList->setModel(bufferModel);
connect(bufferModel, SIGNAL(channelsChanged(QStringList)), parser, SLOT(setChannels(QStringList)));
connect(bufferList->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(onBufferActivated(QModelIndex)));
IrcBuffer* serverBuffer = bufferModel->
add(connection->host());
connect(bufferModel, SIGNAL(messageIgnored(
IrcMessage*)), serverBuffer, SLOT(receiveMessage(
IrcMessage*)));
insertWidget(0, bufferList);
setStretchFactor(0, 1);
setStretchFactor(1, 3);
}
void IrcClient::createConnection()
{
connect(connection, SIGNAL(connected()), this, SLOT(onConnected()));
connect(connection, SIGNAL(connecting()), this, SLOT(onConnecting()));
connect(connection, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
connection->setHost(SERVER);
connection->setUserName(QStringLiteral("communi"));
#if (QT_VERSION) >= (QT_VERSION_CHECK(5, 10, 0))
connection->setNickName(tr("Client%1").arg(QRandomGenerator::global()->bounded(1, 10000)));
#else
qsrand(QTime::currentTime().msec());
connection->setNickName(tr("Client%1").arg(qrand() % 9999));
#endif
connection->setRealName(tr(
"Communi %1 example client").arg(
IRC_VERSION_STR));
}
Keeps track of buffers.
Definition ircbuffermodel.h:48
QModelIndex index(IrcBuffer *buffer) const
Definition ircbuffermodel.cpp:923
Q_INVOKABLE IrcBuffer * add(const QString &title)
Definition ircbuffermodel.cpp:830
Keeps track of buffer status.
Definition ircbuffer.h:50
Parses commands from user input.
Definition irccommandparser.h:43
Q_INVOKABLE void addCommand(IrcCommand::Type type, const QString &syntax)
Definition irccommandparser.cpp:398
Q_INVOKABLE QString syntax(const QString &command, Details details=Visual) const
Definition irccommandparser.cpp:370
Q_INVOKABLE IrcCommand * parse(const QString &input) const
Definition irccommandparser.cpp:550
Provides the most common commands.
Definition irccommand.h:45
static Q_INVOKABLE IrcCommand * createJoin(const QString &channel, const QString &key=QString())
Definition irccommand.cpp:572
@ Join
A join command (JOIN) is used to start listening a specific channel.
Definition irccommand.h:65
@ CtcpAction
A CTCP action command is used to send an action message to channels and users.
Definition irccommand.h:59
@ Nick
A nick command (NICK) is used to give user a nickname or change the previous one.
Definition irccommand.h:73
@ Part
A part command (PART) causes the client to be removed from the channel.
Definition irccommand.h:75
@ Message
A message command (PRIVMSG) is used to send private messages to channels and users.
Definition irccommand.h:69
@ Mode
A mode command (MODE) is used to change the mode of users and channels.
Definition irccommand.h:70
Q_INVOKABLE IrcMessage * toMessage(const QString &prefix, IrcConnection *connection) const
Definition irccommand.cpp:468
Provides command and name completion.
Definition irccompleter.h:44
void complete(const QString &text, int cursor, Direction direction=Forward)
Definition irccompleter.cpp:378
Provides means to establish a connection to an IRC server.
Definition ircconnection.h:49
bool sendCommand(IrcCommand *command)
Definition ircconnection.cpp:1442
void close()
Definition ircconnection.cpp:1389
void quit(const QString &reason=QString())
Definition ircconnection.cpp:1415
The base class of all messages.
Definition ircmessage.h:48
Keeps track of channel users.
Definition ircusermodel.h:46
Keeps track of user status on a channel.
Definition ircuser.h:43
@ SortByTitle
Sort by title (Irc::TitleRole)
Definition irc.h:91
@ UserRole
User object (IrcUser*)
Definition irc.h:79
@ BufferRole
Buffer object (IrcBuffer*)
Definition irc.h:80
#define IRC_VERSION_STR
Definition ircglobal.h:95