2019-09-08 18:06:43 +02:00
|
|
|
#include "Irc2.hpp"
|
|
|
|
|
2019-09-09 22:26:14 +02:00
|
|
|
#include <pajlada/serialize.hpp>
|
|
|
|
#include "common/Credentials.hpp"
|
2019-09-08 18:06:43 +02:00
|
|
|
#include "common/SignalVectorModel.hpp"
|
2019-09-09 22:26:14 +02:00
|
|
|
#include "util/RapidjsonHelpers.hpp"
|
2019-09-08 18:06:43 +02:00
|
|
|
#include "util/StandardItemHelper.hpp"
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class Model : public SignalVectorModel<IrcConnection_>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Model(QObject *parent)
|
2019-09-10 14:46:43 +02:00
|
|
|
: SignalVectorModel<IrcConnection_>(7, parent)
|
2019-09-08 18:06:43 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// turn a vector item into a model row
|
|
|
|
IrcConnection_ getItemFromRow(std::vector<QStandardItem *> &row,
|
|
|
|
const IrcConnection_ &original)
|
|
|
|
{
|
2019-09-10 14:46:43 +02:00
|
|
|
qDebug() << row[2]->data(Qt::CheckStateRole).toBool();
|
|
|
|
|
2019-09-08 18:06:43 +02:00
|
|
|
return IrcConnection_{
|
2019-09-10 14:46:43 +02:00
|
|
|
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
|
2019-09-08 18:06:43 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// turns a row in the model into a vector item
|
|
|
|
void getRowFromItem(const IrcConnection_ &item,
|
|
|
|
std::vector<QStandardItem *> &row)
|
|
|
|
{
|
|
|
|
setStringItem(row[0], item.host);
|
|
|
|
setStringItem(row[1], QString::number(item.port));
|
|
|
|
setBoolItem(row[2], item.ssl);
|
|
|
|
setStringItem(row[3], item.user);
|
|
|
|
setStringItem(row[4], item.nick);
|
2019-09-10 14:46:43 +02:00
|
|
|
setStringItem(row[5], item.real);
|
|
|
|
setStringItem(row[6], item.password);
|
2019-09-08 18:06:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static std::atomic_int currentId;
|
|
|
|
|
2019-09-09 22:26:14 +02:00
|
|
|
//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",
|
|
|
|
//}
|
|
|
|
|
2019-09-08 18:06:43 +02:00
|
|
|
Irc::Irc()
|
|
|
|
{
|
2019-09-09 22:26:14 +02:00
|
|
|
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())
|
|
|
|
{
|
2019-09-10 14:46:43 +02:00
|
|
|
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());
|
|
|
|
|
2019-09-09 22:26:14 +02:00
|
|
|
// add new server with abandoned channels
|
2019-09-10 14:46:43 +02:00
|
|
|
this->servers_.emplace(args.item.id, std::move(server));
|
2019-09-09 22:26:14 +02:00
|
|
|
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())
|
|
|
|
{
|
2019-09-10 14:46:43 +02:00
|
|
|
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;
|
2019-09-09 22:26:14 +02:00
|
|
|
this->servers_.erase(server);
|
|
|
|
}
|
|
|
|
});
|
2019-09-08 18:06:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IrcConnection_ IrcConnection_::unique()
|
|
|
|
{
|
|
|
|
IrcConnection_ c;
|
2019-09-09 22:26:14 +02:00
|
|
|
c.host = "localhost";
|
2019-09-08 18:06:43 +02:00
|
|
|
c.id = currentId++;
|
2019-09-10 14:46:43 +02:00
|
|
|
c.port = 6667;
|
|
|
|
c.ssl = false;
|
2019-09-09 22:26:14 +02:00
|
|
|
c.user = "xD";
|
|
|
|
c.nick = "xD";
|
2019-09-10 14:46:43 +02:00
|
|
|
c.real = "xD";
|
2019-09-08 18:06:43 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractTableModel *Irc::newConnectionModel(QObject *parent)
|
|
|
|
{
|
|
|
|
auto model = new Model(parent);
|
|
|
|
model->init(&this->connections);
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
2019-09-09 22:26:14 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 18:06:43 +02:00
|
|
|
Irc &Irc::getInstance()
|
|
|
|
{
|
|
|
|
static Irc irc;
|
|
|
|
return irc;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|