diff --git a/src/providers/irc/Irc2.cpp b/src/providers/irc/Irc2.cpp index 551f9ef13..5f92ad590 100644 --- a/src/providers/irc/Irc2.cpp +++ b/src/providers/irc/Irc2.cpp @@ -13,7 +13,7 @@ namespace { { public: Model(QObject *parent) - : SignalVectorModel(6, parent) + : SignalVectorModel(7, parent) { } @@ -21,14 +21,17 @@ namespace { IrcConnection_ getItemFromRow(std::vector &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(args.item, ab->second); + + // set server of abandoned channels + for (auto weak : ab->second) + if (auto shared = weak.lock()) + if (auto ircChannel = + dynamic_cast(shared.get())) + ircChannel->setServer(server.get()); + // add new server with abandoned channels - this->servers_.emplace(args.item.id, std::make_unique( - 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(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()) diff --git a/src/providers/irc/Irc2.hpp b/src/providers/irc/Irc2.hpp index f50c6b73f..ec99c8511 100644 --- a/src/providers/irc/Irc2.hpp +++ b/src/providers/irc/Irc2.hpp @@ -17,6 +17,8 @@ struct IrcConnection_ { QString user; QString nick; + QString real; + QString password; int id; @@ -35,7 +37,6 @@ public: UnsortedSignalVector connections; QAbstractTableModel *newConnectionModel(QObject *parent); - IrcServer *getServerOfChannel(Channel *channel); ChannelPtr getOrAddChannel(int serverId, QString name); signals: diff --git a/src/providers/irc/IrcChannel2.cpp b/src/providers/irc/IrcChannel2.cpp index b938841d9..6ccf681e1 100644 --- a/src/providers/irc/IrcChannel2.cpp +++ b/src/providers/irc/IrcChannel2.cpp @@ -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(this->server()->nick() + ":", + MessageElementFlag::Username); + builder.emplace(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 diff --git a/src/providers/irc/IrcChannel2.hpp b/src/providers/irc/IrcChannel2.hpp index 53da3e23a..4b07be155 100644 --- a/src/providers/irc/IrcChannel2.hpp +++ b/src/providers/irc/IrcChannel2.hpp @@ -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 diff --git a/src/providers/irc/IrcServer.cpp b/src/providers/irc/IrcServer.cpp index ff256a2b7..832c67f50 100644 --- a/src/providers/irc/IrcServer.cpp +++ b/src/providers/irc/IrcServer.cpp @@ -2,6 +2,7 @@ #include +#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 IrcServer::createChannel(const QString &channelName) { - return std::make_shared(channelName); + return std::make_shared(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(message->nick() + ":", + MessageElementFlag::Username); + builder.emplace(message->content(), + MessageElementFlag::Text); + + channel->addMessage(builder.release()); + } +} + +void IrcServer::readConnectionMessageReceived(Communi::IrcMessage *message) +{ + qDebug() << QString(message->toData()); +} + } // namespace chatterino diff --git a/src/providers/irc/IrcServer.hpp b/src/providers/irc/IrcServer.hpp index bb9218312..9a7310a57 100644 --- a/src/providers/irc/IrcServer.hpp +++ b/src/providers/irc/IrcServer.hpp @@ -15,7 +15,9 @@ public: const std::vector> &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 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 diff --git a/src/util/StandardItemHelper.hpp b/src/util/StandardItemHelper.hpp index 4638e7277..fb5eda108 100644 --- a/src/util/StandardItemHelper.hpp +++ b/src/util/StandardItemHelper.hpp @@ -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; } diff --git a/src/widgets/dialogs/SelectChannelDialog.cpp b/src/widgets/dialogs/SelectChannelDialog.cpp index be0779190..6a8f027f4 100644 --- a/src/widgets/dialogs/SelectChannelDialog.cpp +++ b/src/widgets/dialogs/SelectChannelDialog.cpp @@ -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(_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;