added new function and classes for irc support

This commit is contained in:
fourtf 2019-09-09 22:26:14 +02:00
parent 9f1a5b900e
commit fd0c11964e
7 changed files with 187 additions and 28 deletions

View file

@ -28,6 +28,7 @@ public:
// channels // channels
ChannelPtr getOrAddChannel(const QString &dirtyChannelName); ChannelPtr getOrAddChannel(const QString &dirtyChannelName);
ChannelPtr getChannelOrEmpty(const QString &dirtyChannelName); ChannelPtr getChannelOrEmpty(const QString &dirtyChannelName);
std::vector<std::weak_ptr<Channel>> getChannels();
// signals // signals
pajlada::Signals::NoArgSignal connected; pajlada::Signals::NoArgSignal connected;

View file

@ -1,6 +1,9 @@
#include "Irc2.hpp" #include "Irc2.hpp"
#include <pajlada/serialize.hpp>
#include "common/Credentials.hpp"
#include "common/SignalVectorModel.hpp" #include "common/SignalVectorModel.hpp"
#include "util/RapidjsonHelpers.hpp"
#include "util/StandardItemHelper.hpp" #include "util/StandardItemHelper.hpp"
namespace chatterino { namespace chatterino {
@ -45,15 +48,68 @@ namespace {
static std::atomic_int currentId; static std::atomic_int currentId;
//inline QString escape(QString str)
//{
// return str.replace(":", "::");
//}
//inline QString getCredentialName(const IrcConnection_ &conn)
//{
// //return escape(conn.host) + escape(conn.
//}
//QString IrcConnection_::getPassword()
//{
// return
//}
//void IrcConnection_::setPassword(const QString &str)
//{
// //Credentials::set("irc",
//}
Irc::Irc() Irc::Irc()
{ {
this->connections.itemInserted.connect([this](auto &&args) {
// make sure only one id can only exist for one server
assert(this->servers_.find(args.item.id) == this->servers_.end());
if (auto ab = this->abandonedChannels_.find(args.item.id);
ab != this->abandonedChannels_.end())
{
// add new server with abandoned channels
this->servers_.emplace(args.item.id, std::make_unique<IrcServer>(
args.item, ab->second));
this->abandonedChannels_.erase(ab);
}
else
{
// add new server
this->servers_.emplace(args.item.id,
std::make_unique<IrcServer>(args.item));
}
});
this->connections.itemRemoved.connect([this](auto &&args) {
// restore
if (auto server = this->servers_.find(args.item.id);
server != this->servers_.end())
{
this->abandonedChannels_[args.item.id] =
server->second->getChannels();
this->servers_.erase(server);
}
});
} }
IrcConnection_ IrcConnection_::unique() IrcConnection_ IrcConnection_::unique()
{ {
IrcConnection_ c; IrcConnection_ c;
c.host = "localhost";
c.id = currentId++; c.id = currentId++;
c.port = 6697; c.port = 6697;
c.user = "xD";
c.nick = "xD";
return c; return c;
} }
@ -64,6 +120,29 @@ QAbstractTableModel *Irc::newConnectionModel(QObject *parent)
return model; 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())
{
return server->second->getOrAddChannel(name);
}
else
{
return Channel::getEmpty();
}
}
Irc &Irc::getInstance() Irc &Irc::getInstance()
{ {
static Irc irc; static Irc irc;

View file

@ -1,7 +1,11 @@
#pragma once #pragma once
#include <rapidjson/rapidjson.h>
#include <common/SignalVector.hpp> #include <common/SignalVector.hpp>
#include "providers/irc/IrcChannel2.hpp"
#include "providers/irc/IrcServer.hpp"
class QAbstractTableModel; class QAbstractTableModel;
namespace chatterino { namespace chatterino {
@ -31,8 +35,20 @@ public:
UnsortedSignalVector<IrcConnection_> connections; UnsortedSignalVector<IrcConnection_> connections;
QAbstractTableModel *newConnectionModel(QObject *parent); QAbstractTableModel *newConnectionModel(QObject *parent);
IrcServer *getServerOfChannel(Channel *channel);
ChannelPtr getOrAddChannel(int serverId, QString name);
signals: signals:
void connectionUpdated(int id); void connectionUpdated(int id);
private:
// Servers have a unique id.
// When a server gets changed it gets removed and then added again.
// So we store the channels of that server in abandonedChannels_ temporarily.
// Or if the server got removed permanently then it's still stored there.
std::unordered_map<int, std::unique_ptr<IrcServer>> servers_;
std::unordered_map<int, std::vector<std::weak_ptr<Channel>>>
abandonedChannels_;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -2,8 +2,9 @@
namespace chatterino { namespace chatterino {
// IrcChannel::IrcChannel() IrcChannel::IrcChannel(const QString &name)
//{ : Channel(name, Channel::Type::Irc)
//} {
// }
} // namespace chatterino } // namespace chatterino

View file

@ -1,11 +1,13 @@
#pragma once #pragma once
#include "common/Channel.hpp"
namespace chatterino { namespace chatterino {
// class IrcChannel class IrcChannel : public Channel
//{ {
// public: public:
// IrcChannel(); explicit IrcChannel(const QString &name);
//}; };
//
} // namespace chatterino } // namespace chatterino

View file

@ -2,11 +2,63 @@
#include <cassert> #include <cassert>
#include "providers/irc/Irc2.hpp"
#include "providers/irc/IrcChannel2.hpp"
namespace chatterino { namespace chatterino {
// IrcServer::IrcServer(const QString &hostname, int port) IrcServer::IrcServer(const IrcConnection_ &data)
//{ : data_(new IrcConnection_(data))
// this->initConnection(); {
//} this->connect();
// }
IrcServer::IrcServer(const IrcConnection_ &data,
const std::vector<std::weak_ptr<Channel>> &restoreChannels)
: IrcServer(data)
{
for (auto &&weak : restoreChannels)
{
if (auto shared = weak.lock())
{
this->channels[shared->getName()] = weak;
}
}
}
IrcServer::~IrcServer()
{
delete this->data_;
}
int IrcServer::getId()
{
return this->data_->id;
}
void IrcServer::initializeConnection(IrcConnection *connection, bool isRead,
bool isWrite)
{
assert(isRead && isWrite);
connection->setSecure(this->data_->ssl);
connection->setHost(this->data_->host);
connection->setPort(this->data_->port);
connection->setUserName(this->data_->user);
connection->setNickName(this->data_->nick);
connection->setRealName(this->data_->nick);
connection->setPassword(this->data_->password);
}
std::shared_ptr<Channel> IrcServer::createChannel(const QString &channelName)
{
return std::make_shared<IrcChannel>(channelName);
}
bool IrcServer::hasSeparateWriteConnection() const
{
return false;
}
} // namespace chatterino } // namespace chatterino

View file

@ -5,20 +5,28 @@
namespace chatterino { namespace chatterino {
// class IrcServer struct IrcConnection_;
//{
// public:
// IrcServer(const QString &hostname, int port);
// void setAccount(std::shared_ptr<IrcAccount> newAccount); class IrcServer : public AbstractIrcServer
// std::shared_ptr<IrcAccount> getAccount() const; {
public:
explicit IrcServer(const IrcConnection_ &data);
IrcServer(const IrcConnection_ &data,
const std::vector<std::weak_ptr<Channel>> &restoreChannels);
~IrcServer() override;
// protected: int getId();
// virtual void initializeConnection(Communi::IrcConnection *connection, bool
// isReadConnection); // AbstractIrcServer interface
protected:
void initializeConnection(IrcConnection *connection, bool isRead,
bool isWrite) override;
std::shared_ptr<Channel> createChannel(const QString &channelName) override;
bool hasSeparateWriteConnection() const override;
private:
// pointer so we don't have to circle include Irc2.hpp
IrcConnection_ *data_;
};
// virtual void privateMessageReceived(Communi::IrcPrivateMessage *message);
// virtual void messageReceived(Communi::IrcMessage *message);
//};
//
} // namespace chatterino } // namespace chatterino