added send and receive for irc messages

This commit is contained in:
fourtf 2019-09-10 14:46:43 +02:00
parent 25f75f7580
commit 82f63d2f7e
8 changed files with 137 additions and 41 deletions

View file

@ -13,7 +13,7 @@ namespace {
{
public:
Model(QObject *parent)
: SignalVectorModel<IrcConnection_>(6, parent)
: SignalVectorModel<IrcConnection_>(7, parent)
{
}
@ -21,14 +21,17 @@ namespace {
IrcConnection_ getItemFromRow(std::vector<QStandardItem *> &row,
const IrcConnection_ &original)
{
qDebug() << row[2]->data(Qt::CheckStateRole).toBool();
return IrcConnection_{
row[0]->data(Qt::EditRole).toString(), // host
row[1]->data(Qt::EditRole).toInt(), // port
row[2]->data(Qt::Checked).toBool(), // ssl
row[3]->data(Qt::EditRole).toString(), // user
row[4]->data(Qt::EditRole).toString(), // nick
row[5]->data(Qt::EditRole).toString(), // password
original.id, // id
row[0]->data(Qt::EditRole).toString(), // host
row[1]->data(Qt::EditRole).toInt(), // port
row[2]->data(Qt::CheckStateRole).toBool(), // ssl
row[3]->data(Qt::EditRole).toString(), // user
row[4]->data(Qt::EditRole).toString(), // nick
row[5]->data(Qt::EditRole).toString(), // real
row[6]->data(Qt::EditRole).toString(), // password
original.id, // id
};
}
@ -41,7 +44,8 @@ namespace {
setBoolItem(row[2], item.ssl);
setStringItem(row[3], item.user);
setStringItem(row[4], item.nick);
setStringItem(row[5], item.password);
setStringItem(row[5], item.real);
setStringItem(row[6], item.password);
}
};
} // namespace
@ -77,9 +81,17 @@ Irc::Irc()
if (auto ab = this->abandonedChannels_.find(args.item.id);
ab != this->abandonedChannels_.end())
{
auto server = std::make_unique<IrcServer>(args.item, ab->second);
// set server of abandoned channels
for (auto weak : ab->second)
if (auto shared = weak.lock())
if (auto ircChannel =
dynamic_cast<IrcChannel *>(shared.get()))
ircChannel->setServer(server.get());
// add new server with abandoned channels
this->servers_.emplace(args.item.id, std::make_unique<IrcServer>(
args.item, ab->second));
this->servers_.emplace(args.item.id, std::move(server));
this->abandonedChannels_.erase(ab);
}
else
@ -95,8 +107,16 @@ Irc::Irc()
if (auto server = this->servers_.find(args.item.id);
server != this->servers_.end())
{
this->abandonedChannels_[args.item.id] =
server->second->getChannels();
auto abandoned = server->second->getChannels();
// set server of abandoned servers to nullptr
for (auto weak : abandoned)
if (auto shared = weak.lock())
if (auto ircChannel =
dynamic_cast<IrcChannel *>(shared.get()))
ircChannel->setServer(nullptr);
this->abandonedChannels_[args.item.id] = abandoned;
this->servers_.erase(server);
}
});
@ -107,9 +127,11 @@ IrcConnection_ IrcConnection_::unique()
IrcConnection_ c;
c.host = "localhost";
c.id = currentId++;
c.port = 6697;
c.port = 6667;
c.ssl = false;
c.user = "xD";
c.nick = "xD";
c.real = "xD";
return c;
}
@ -120,17 +142,6 @@ QAbstractTableModel *Irc::newConnectionModel(QObject *parent)
return model;
}
IrcServer *Irc::getServerOfChannel(Channel *channel)
{
for (auto &&server : this->servers_)
for (auto weak : server.second->getChannels())
if (auto shared = weak.lock())
if (shared.get() == channel)
return server.second.get();
return nullptr;
}
ChannelPtr Irc::getOrAddChannel(int id, QString name)
{
if (auto server = this->servers_.find(id); server != this->servers_.end())

View file

@ -17,6 +17,8 @@ struct IrcConnection_ {
QString user;
QString nick;
QString real;
QString password;
int id;
@ -35,7 +37,6 @@ public:
UnsortedSignalVector<IrcConnection_> connections;
QAbstractTableModel *newConnectionModel(QObject *parent);
IrcServer *getServerOfChannel(Channel *channel);
ChannelPtr getOrAddChannel(int serverId, QString name);
signals:

View file

@ -1,10 +1,43 @@
#include "IrcChannel2.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/irc/IrcServer.hpp"
namespace chatterino {
IrcChannel::IrcChannel(const QString &name)
IrcChannel::IrcChannel(const QString &name, IrcServer *server)
: Channel(name, Channel::Type::Irc)
, server_(server)
{
}
void IrcChannel::sendMessage(const QString &message)
{
assertInGuiThread();
if (this->server())
this->server()->sendMessage(this->getName(), message);
MessageBuilder builder;
builder.emplace<TextElement>(this->server()->nick() + ":",
MessageElementFlag::Username);
builder.emplace<TextElement>(message, MessageElementFlag::Text);
this->addMessage(builder.release());
}
IrcServer *IrcChannel::server()
{
assertInGuiThread();
return this->server_;
}
void IrcChannel::setServer(IrcServer *server)
{
assertInGuiThread();
this->server_ = server;
}
} // namespace chatterino

View file

@ -4,10 +4,24 @@
namespace chatterino {
class Irc;
class IrcServer;
class IrcChannel : public Channel
{
public:
explicit IrcChannel(const QString &name);
explicit IrcChannel(const QString &name, IrcServer *server);
void sendMessage(const QString &message) override;
IrcServer *server();
private:
void setServer(IrcServer *server);
IrcServer *server_;
friend class Irc;
};
} // namespace chatterino

View file

@ -2,6 +2,7 @@
#include <cassert>
#include "messages/MessageBuilder.hpp"
#include "providers/irc/Irc2.hpp"
#include "providers/irc/IrcChannel2.hpp"
@ -31,11 +32,21 @@ IrcServer::~IrcServer()
delete this->data_;
}
int IrcServer::getId()
int IrcServer::id()
{
return this->data_->id;
}
const QString &IrcServer::user()
{
return this->data_->user;
}
const QString &IrcServer::nick()
{
return this->data_->nick;
}
void IrcServer::initializeConnection(IrcConnection *connection, bool isRead,
bool isWrite)
{
@ -47,13 +58,13 @@ void IrcServer::initializeConnection(IrcConnection *connection, bool isRead,
connection->setUserName(this->data_->user);
connection->setNickName(this->data_->nick);
connection->setRealName(this->data_->nick);
connection->setRealName(this->data_->real);
connection->setPassword(this->data_->password);
}
std::shared_ptr<Channel> IrcServer::createChannel(const QString &channelName)
{
return std::make_shared<IrcChannel>(channelName);
return std::make_shared<IrcChannel>(channelName, this);
}
bool IrcServer::hasSeparateWriteConnection() const
@ -61,4 +72,27 @@ bool IrcServer::hasSeparateWriteConnection() const
return false;
}
void IrcServer::privateMessageReceived(Communi::IrcPrivateMessage *message)
{
auto target = message->target();
target = target.startsWith('#') ? target.mid(1) : target;
if (auto channel = this->getChannelOrEmpty(target); !channel->isEmpty())
{
MessageBuilder builder;
builder.emplace<TextElement>(message->nick() + ":",
MessageElementFlag::Username);
builder.emplace<TextElement>(message->content(),
MessageElementFlag::Text);
channel->addMessage(builder.release());
}
}
void IrcServer::readConnectionMessageReceived(Communi::IrcMessage *message)
{
qDebug() << QString(message->toData());
}
} // namespace chatterino

View file

@ -15,7 +15,9 @@ public:
const std::vector<std::weak_ptr<Channel>> &restoreChannels);
~IrcServer() override;
int getId();
int id();
const QString &user();
const QString &nick();
// AbstractIrcServer interface
protected:
@ -23,6 +25,8 @@ protected:
bool isWrite) override;
std::shared_ptr<Channel> createChannel(const QString &channelName) override;
bool hasSeparateWriteConnection() const override;
void privateMessageReceived(Communi::IrcPrivateMessage *message) override;
void readConnectionMessageReceived(Communi::IrcMessage *message) override;
private:
// pointer so we don't have to circle include Irc2.hpp

View file

@ -7,7 +7,7 @@ namespace chatterino {
static void setBoolItem(QStandardItem *item, bool value,
bool userCheckable = true, bool selectable = true)
{
item->setFlags((Qt::ItemFlags)(
item->setFlags(Qt::ItemFlags(
Qt::ItemIsEnabled | (selectable ? Qt::ItemIsSelectable : 0) |
(userCheckable ? Qt::ItemIsUserCheckable : 0)));
item->setCheckState(value ? Qt::Checked : Qt::Unchecked);
@ -17,15 +17,15 @@ static void setStringItem(QStandardItem *item, const QString &value,
bool editable = true, bool selectable = true)
{
item->setData(value, Qt::EditRole);
item->setFlags((Qt::ItemFlags)(Qt::ItemIsEnabled |
(selectable ? Qt::ItemIsSelectable : 0) |
(editable ? (Qt::ItemIsEditable) : 0)));
item->setFlags(Qt::ItemFlags(Qt::ItemIsEnabled |
(selectable ? Qt::ItemIsSelectable : 0) |
(editable ? (Qt::ItemIsEditable) : 0)));
}
static QStandardItem *emptyItem()
{
auto *item = new QStandardItem();
item->setFlags((Qt::ItemFlags)0);
item->setFlags(Qt::ItemFlags(0));
return item;
}

View file

@ -139,7 +139,7 @@ SelectChannelDialog::SelectChannelDialog(QWidget *parent)
Irc::getInstance().newConnectionModel(this));
view->setTitles(
{"host", "port", "ssl", "user", "nick", "password"});
{"real", "port", "ssl", "user", "nick", "real", "password"});
view->getTableView()->horizontalHeader()->resizeSection(0, 140);
view->getTableView()->horizontalHeader()->resizeSection(1, 30);
view->getTableView()->horizontalHeader()->resizeSection(2, 30);
@ -270,13 +270,12 @@ void SelectChannelDialog::setSelectedChannel(IndirectChannel _channel)
if (auto ircChannel =
dynamic_cast<IrcChannel *>(_channel.get().get()))
{
if (auto server =
Irc::getInstance().getServerOfChannel(ircChannel))
if (auto server = ircChannel->server())
{
int i = 0;
for (auto &&conn : Irc::getInstance().connections)
{
if (conn.id == server->getId())
if (conn.id == server->id())
{
this->ui_.irc.servers->getTableView()->selectRow(i);
break;