Add in a few extra plugins
This commit is contained in:
269
plugins/IrcClientPlugin/IrcClientPlugin.cpp
Normal file
269
plugins/IrcClientPlugin/IrcClientPlugin.cpp
Normal file
@@ -0,0 +1,269 @@
|
||||
#include "IrcClientPlugin.h"
|
||||
#include "pingmessagehandler.h"
|
||||
#include "privmsgmessagehandler.h"
|
||||
#include "testhandler.h"
|
||||
#include "topichandler.h"
|
||||
#include "joinmessagehandler.h"
|
||||
#include "quitmessagehandler.h"
|
||||
#include <QVBoxLayout>
|
||||
#include <QtWidgets>
|
||||
|
||||
|
||||
QString IrcClientPlugin::pname()
|
||||
{
|
||||
return "Irc Client Plugin";
|
||||
}
|
||||
|
||||
QString IrcClientPlugin::pdesc()
|
||||
{
|
||||
return "Simple Irc Client Plugin";
|
||||
}
|
||||
|
||||
QWidget* IrcClientPlugin::pcontent()
|
||||
{
|
||||
connectButton = new QPushButton(tr("Connect"));
|
||||
connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToServer()));
|
||||
sendButton = new QPushButton(tr("Send"));
|
||||
connect(sendButton, SIGNAL(clicked()), this, SLOT(sendMsg()));
|
||||
joinButton = new QPushButton(tr("Join"));
|
||||
connect(joinButton, SIGNAL(clicked()), this, SLOT(joinRoom()));
|
||||
smallEditor = new QLineEdit;
|
||||
smallEditor->setText("");
|
||||
topic = new QLineEdit;
|
||||
topic->setText("");
|
||||
msgs = new QTextBrowser;
|
||||
nicks = new QTextEdit;
|
||||
nicks->setMinimumWidth(100);
|
||||
nicks->setMaximumWidth(100);
|
||||
msgs->setOpenExternalLinks(true);
|
||||
connect(msgs, SIGNAL(textChanged()), this, SLOT(changePixmap())) ;
|
||||
test = new QWidget();
|
||||
test->setWindowTitle(tr("Irc Client Plugin"));
|
||||
test->resize(400, 300);
|
||||
|
||||
QHBoxLayout *topLayout = new QHBoxLayout;
|
||||
topLayout->addWidget(connectButton);
|
||||
topLayout->addWidget(joinButton);
|
||||
|
||||
QHBoxLayout *midleLayout = new QHBoxLayout;
|
||||
midleLayout->addWidget(msgs);
|
||||
midleLayout->addWidget(nicks);
|
||||
|
||||
|
||||
QHBoxLayout *bottomLayout = new QHBoxLayout;
|
||||
// topLayout->addWidget(ftpServerLabel);
|
||||
bottomLayout->addWidget(smallEditor);
|
||||
//topLayout->addWidget(cdToParentButton);
|
||||
bottomLayout->addWidget(sendButton);
|
||||
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
||||
mainLayout->addLayout(topLayout);
|
||||
mainLayout->addWidget(topic);
|
||||
mainLayout->addLayout(midleLayout);
|
||||
// mainLayout->addWidget(msgs);
|
||||
//mainLayout->addWidget(statusLabel);
|
||||
mainLayout->addLayout(bottomLayout);
|
||||
test->setLayout(mainLayout);
|
||||
QIcon icon = QIcon(":/images/monkey.png");
|
||||
test->setWindowIcon(icon);
|
||||
return test;
|
||||
}
|
||||
|
||||
void IrcClientPlugin::changePixmap()
|
||||
{
|
||||
QRegularExpression reg1(":\\)");
|
||||
QTextCursor cursor1(msgs->document()->find(reg1));
|
||||
|
||||
if (!cursor1.isNull()) {
|
||||
cursor1.insertHtml("<img src=\":/images/smile.png\">");
|
||||
}
|
||||
|
||||
//auto text = msgs->toHtml();
|
||||
// if(text != ""){
|
||||
// QRegExp exp("<\sa\s+[^>]href\s=\s\"([^\"])\"\s>(.)<\/\sa\s*>");
|
||||
// text.replace(exp, "{{\1}}");
|
||||
// msgs->setHtml(text);
|
||||
//}
|
||||
|
||||
|
||||
//QRegExp reg(":\\("); // you can improve regular expression
|
||||
QRegularExpression reg("https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)");
|
||||
QTextCursor cursor(msgs->document()->find(reg));
|
||||
|
||||
if (!cursor.isNull()) {
|
||||
|
||||
QString nlink = "<a href=\"http://barosonix.com\">http://barosonix.com</a>";
|
||||
|
||||
cursor.insertHtml(nlink);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void IrcClientPlugin::connectToServer()
|
||||
{
|
||||
_client = new Client(test);
|
||||
connect(_client, SIGNAL(PrivmsgReceived(Hostmask,QString,QString)), SLOT(onPrivmsgReceived(Hostmask,QString,QString)));
|
||||
connect(_client, SIGNAL(NamesListReceived(QString)), SLOT(onNamesListReceived(QString)));
|
||||
connect(_client, SIGNAL(JoinMsgReceived(Hostmask,QString,QString)), SLOT(onJoinMsgReceived(Hostmask,QString,QString)));
|
||||
connect(_client, SIGNAL(QuitMsgReceived(Hostmask,QString,QString)), SLOT(onQuitMsgReceived(Hostmask,QString,QString)));
|
||||
connect(_client, SIGNAL(TopicReceived(Hostmask,QString,QString)), SLOT(onTopicReceived(Hostmask,QString,QString)));
|
||||
|
||||
_client->setNickname("ahf_");
|
||||
_client->setUsername("ahf");
|
||||
_client->setRealname(QString::fromUtf8("Alexander Færøy"));
|
||||
|
||||
_client->setServerHostname("127.0.0.1");
|
||||
_client->setServerPort(6667);
|
||||
|
||||
_client->registerMessageHandler("PING", new PingMessageHandler);
|
||||
_client->registerMessageHandler("PRIVMSG", new PrivmsgMessageHandler);
|
||||
_client->registerMessageHandler("353", new testhandler);
|
||||
_client->registerMessageHandler("332", new topichandler);
|
||||
_client->registerMessageHandler("JOIN", new joinmessagehandler);
|
||||
_client->registerMessageHandler("PART", new quitmessagehandler);
|
||||
|
||||
_client->connectToServer();
|
||||
}
|
||||
|
||||
void IrcClientPlugin::sendMsg()
|
||||
{
|
||||
QString input;
|
||||
|
||||
input = smallEditor->text();
|
||||
|
||||
if (input.isEmpty())
|
||||
return;
|
||||
|
||||
if (input.at(0) == '/')
|
||||
{
|
||||
QString command = input.mid(1);
|
||||
_client->sendRaw(command);
|
||||
}
|
||||
else
|
||||
{
|
||||
_client->sendPrivmsg("#test", input);
|
||||
|
||||
QString format;
|
||||
format = "<font style='color: blue'>" + _client->getNickname() + "</font> " + input;
|
||||
append(format);
|
||||
}
|
||||
|
||||
smallEditor->clear();
|
||||
|
||||
}
|
||||
|
||||
void IrcClientPlugin::joinRoom()
|
||||
{
|
||||
_client->sendJoin("#test");
|
||||
}
|
||||
|
||||
void IrcClientPlugin::onPrivmsgReceived(const Hostmask &hostmask, const QString &target, const QString &text)
|
||||
{
|
||||
QString format;
|
||||
|
||||
format = "<" + hostmask.getNickname() + "> " + text;
|
||||
|
||||
append(format);
|
||||
}
|
||||
|
||||
void IrcClientPlugin::onNamesListReceived(const QString &text)
|
||||
{
|
||||
//QString format;
|
||||
int marker = 0;
|
||||
// qDebug() << "GOT:" << text;
|
||||
QStringList list1 = text.split(QLatin1Char(' '));
|
||||
for (QString data : list1) // iterate over array fibonacci
|
||||
{
|
||||
marker = marker +1;
|
||||
if(marker > 2)
|
||||
{
|
||||
nicks->append(data);
|
||||
// qDebug() << "GOT:" << data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//format = "<" + hostmask.getNickname() + "> " + text;
|
||||
|
||||
//append(format);
|
||||
}
|
||||
|
||||
void IrcClientPlugin::onJoinMsgReceived(const Hostmask& hostmask, const QString& target, const QString& text)
|
||||
{
|
||||
if(hostmask.getNickname() != _client->getNickname())
|
||||
{
|
||||
QString format;
|
||||
qDebug() << "GOT:" << text;
|
||||
format = "<" + hostmask.getNickname() + "> Has Joined.";
|
||||
nicks->append(hostmask.getNickname());
|
||||
append(format);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void IrcClientPlugin::onTopicReceived(const Hostmask& hostmask, const QString& target, const QString& text)
|
||||
{
|
||||
int marker = 0;
|
||||
|
||||
QString ftext = "";
|
||||
QStringList list1 = text.split(QLatin1Char(' '));
|
||||
for (QString data : list1) // iterate over array fibonacci
|
||||
{
|
||||
marker = marker +1;
|
||||
if(marker > 1)
|
||||
{
|
||||
if(ftext != "")
|
||||
{
|
||||
ftext += " " + data;
|
||||
}
|
||||
else
|
||||
{
|
||||
ftext += data;
|
||||
}
|
||||
|
||||
// qDebug() << "GOT:" << data;
|
||||
}
|
||||
|
||||
}
|
||||
// if(hostmask.getNickname() != _client->getNickname())
|
||||
//{
|
||||
// QString format;
|
||||
qDebug() << "GOT:" << ftext;
|
||||
topic->setText(ftext);
|
||||
// format = "<" + hostmask.getNickname() + "> Has Joined.";
|
||||
// nicks->append(hostmask.getNickname());
|
||||
// append(format);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
void IrcClientPlugin::onQuitMsgReceived(const Hostmask& hostmask, const QString& target, const QString& text)
|
||||
{
|
||||
QString format;
|
||||
qDebug() << "GOT:" << text;
|
||||
format = "<" + hostmask.getNickname() + "> Has Quit.";
|
||||
QStringList lines = nicks->toPlainText().split("\n");
|
||||
|
||||
int i = 0;
|
||||
while(i < lines.size())
|
||||
{
|
||||
QString line = lines.at(i);
|
||||
if(line.contains(hostmask.getNickname()))
|
||||
{
|
||||
lines.removeAt(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
nicks->setPlainText(lines.join("\n"));
|
||||
append(format);
|
||||
}
|
||||
|
||||
void IrcClientPlugin::append(const QString &line)
|
||||
{
|
||||
msgs->append(line);
|
||||
}
|
||||
Reference in New Issue
Block a user