added loading and saving of irc servers

This commit is contained in:
fourtf 2019-09-10 23:55:43 +02:00
parent 0219cb0c36
commit c4d0efacff
10 changed files with 292 additions and 80 deletions

View file

@ -13,6 +13,7 @@
#include "providers/bttv/BttvEmotes.hpp"
#include "providers/chatterino/ChatterinoBadges.hpp"
#include "providers/ffz/FfzEmotes.hpp"
#include "providers/irc/Irc2.hpp"
#include "providers/twitch/PubsubClient.hpp"
#include "providers/twitch/TwitchServer.hpp"
#include "singletons/Emotes.hpp"
@ -69,6 +70,8 @@ Application::Application(Settings &_settings, Paths &_paths)
this->fonts->fontChanged.connect(
[this]() { this->windows->layoutChannelViews(); });
Irc::getInstance().load();
this->twitch.server = this->twitch2;
this->twitch.pubsub = this->twitch2->pubsub;
}

View file

@ -185,6 +185,11 @@ public:
Qt::ItemFlags flags(const QModelIndex &index) const override
{
int row = index.row(), column = index.column();
if (row < 0 || column < 0) {
return Qt::NoItemFlags;
}
assert(row >= 0 && row < this->rows_.size() && column >= 0 &&
column < this->columnCount_);

View file

@ -3,17 +3,26 @@
#include <pajlada/serialize.hpp>
#include "common/Credentials.hpp"
#include "common/SignalVectorModel.hpp"
#include "singletons/Paths.hpp"
#include "util/CombinePath.hpp"
#include "util/RapidjsonHelpers.hpp"
#include "util/StandardItemHelper.hpp"
#include <QSaveFile>
namespace chatterino {
namespace {
QString configPath()
{
return combinePath(getPaths()->settingsDirectory, "irc.json");
}
class Model : public SignalVectorModel<IrcConnection_>
{
public:
Model(QObject *parent)
: SignalVectorModel<IrcConnection_>(7, parent)
: SignalVectorModel<IrcConnection_>(8, parent)
{
}
@ -39,7 +48,7 @@ namespace {
void getRowFromItem(const IrcConnection_ &item,
std::vector<QStandardItem *> &row)
{
setStringItem(row[0], item.host);
setStringItem(row[0], item.host, false);
setStringItem(row[1], QString::number(item.port));
setBoolItem(row[2], item.ssl);
setStringItem(row[3], item.user);
@ -50,8 +59,6 @@ namespace {
};
} // namespace
static std::atomic_int currentId;
//inline QString escape(QString str)
//{
// return str.replace(":", "::");
@ -120,19 +127,8 @@ Irc::Irc()
this->servers_.erase(server);
}
});
}
IrcConnection_ IrcConnection_::unique()
{
IrcConnection_ c;
c.host = "localhost";
c.id = currentId++;
c.port = 6667;
c.ssl = false;
c.user = "xD";
c.nick = "xD";
c.real = "xD";
return c;
this->connections.delayedItemsChanged.connect([this] { this->save(); });
}
QAbstractTableModel *Irc::newConnectionModel(QObject *parent)
@ -160,4 +156,84 @@ Irc &Irc::getInstance()
return irc;
}
int Irc::uniqueId()
{
/// XXX: also check for channels
int i = this->currentId_ + 1;
auto it = this->servers_.find(i);
while (it != this->servers_.end())
{
i++;
it = this->servers_.find(i);
}
return (this->currentId_ = i);
}
void Irc::save()
{
QJsonDocument doc;
QJsonObject root;
QJsonArray servers;
for (auto &&conn : this->connections)
{
QJsonObject obj;
obj.insert("host", conn.host);
obj.insert("port", conn.port);
obj.insert("ssl", conn.ssl);
obj.insert("username", conn.user);
obj.insert("nickname", conn.nick);
obj.insert("realname", conn.real);
//obj.insert("password", conn.password);
obj.insert("id", conn.id);
servers.append(obj);
}
root.insert("servers", servers);
doc.setObject(root);
QSaveFile file(configPath());
file.open(QIODevice::WriteOnly);
file.write(doc.toJson());
file.commit();
}
void Irc::load()
{
if (this->loaded_)
return;
this->loaded_ = true;
QString config = configPath();
QFile file(configPath());
file.open(QIODevice::ReadOnly);
auto doc = QJsonDocument::fromJson(file.readAll());
auto object = doc.object();
std::unordered_set<int> ids;
for (auto server : doc.object().value("servers").toArray())
{
auto obj = server.toObject();
IrcConnection_ conn;
conn.host = obj.value("host").toString(conn.host);
conn.port = obj.value("port").toInt(conn.port);
conn.ssl = obj.value("ssl").toBool(conn.ssl);
conn.user = obj.value("username").toString(conn.user);
conn.nick = obj.value("nickname").toString(conn.nick);
conn.real = obj.value("realname").toString(conn.real);
// conn.password = obj.value("password").toString(conn.password);
conn.id = obj.value("id").toInt(conn.id);
// duplicate id's are not allowed :(
if (ids.find(conn.id) == ids.end())
{
this->connections.appendItem(conn);
ids.insert(conn.id);
}
}
}
} // namespace chatterino

View file

@ -10,21 +10,21 @@ class QAbstractTableModel;
namespace chatterino {
//enum IrcAuthType { Anonymous, /*Sals,*/ Pass, MsgNickServ, NickServ };
struct IrcConnection_ {
QString host;
int port;
bool ssl;
int port = 6667;
bool ssl = false;
QString user;
QString nick;
QString real;
// IrcAuthType authType = Anonymous;
QString password;
int id;
// makes an IrcConnection with a unique id
static IrcConnection_ unique();
};
class Irc
@ -39,10 +39,18 @@ public:
ChannelPtr getOrAddChannel(int serverId, QString name);
void save();
void load();
int uniqueId();
signals:
void connectionUpdated(int id);
private:
int currentId_{};
bool loaded_{};
// 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.

View file

@ -135,7 +135,7 @@ void Paths::initSubDirectories()
this->messageLogDirectory = makePath("Logs");
this->miscDirectory = makePath("Misc");
this->twitchProfileAvatars = makePath("ProfileAvatars");
QDir().mkdir(this->twitchProfileAvatars + "/twitch");
//QDir().mkdir(this->twitchProfileAvatars + "/twitch");
}
Paths *getPaths()

View file

@ -1,16 +1,50 @@
#include "IrcConnectionEditor.hpp"
#include "ui_IrcConnectionEditor.h"
IrcConnectionEditor::IrcConnectionEditor(bool isAdd, QWidget *parent)
: QDialog(parent)
, ui(new Ui::IrcConnectionEditor)
namespace chatterino {
IrcConnectionEditor::IrcConnectionEditor(const IrcConnection_ &data, bool isAdd,
QWidget *parent)
: QDialog(parent, Qt::WindowStaysOnTopHint)
, ui_(new Ui::IrcConnectionEditor)
, data_(data)
{
ui->setupUi(this);
this->ui_->setupUi(this);
this->setWindowTitle(QString(isAdd ? "Add " : "Edit ") + "Irc Connection");
}
QObject::connect(this->ui_->userNameLineEdit, &QLineEdit::textChanged, this,
[this](const QString &text) {
this->ui_->nickNameLineEdit->setPlaceholderText(text);
this->ui_->realNameLineEdit->setPlaceholderText(text);
});
this->ui_->serverLineEdit->setText(data.host);
this->ui_->portSpinBox->setValue(data.port);
this->ui_->securityCheckBox->setChecked(data.ssl);
this->ui_->userNameLineEdit->setText(data.user);
this->ui_->nickNameLineEdit->setText(data.nick);
this->ui_->realNameLineEdit->setText(data.real);
this->ui_->passwordLineEdit->setText(data.password);
} // namespace chatterino
IrcConnectionEditor::~IrcConnectionEditor()
{
delete ui;
delete ui_;
}
IrcConnection_ IrcConnectionEditor::data()
{
auto data = this->data_;
data.host = this->ui_->serverLineEdit->text();
data.port = this->ui_->portSpinBox->value();
data.ssl = this->ui_->securityCheckBox->isChecked();
data.user = this->ui_->userNameLineEdit->text();
data.nick = this->ui_->nickNameLineEdit->text();
data.real = this->ui_->realNameLineEdit->text();
data.password = this->ui_->passwordLineEdit->text();
return data;
}
} // namespace chatterino

View file

@ -1,19 +1,32 @@
#pragma once
#include <QDialog>
#include <boost/optional.hpp>
#include "providers/irc/Irc2.hpp"
#include "widgets/BaseWindow.hpp"
namespace Ui {
class IrcConnectionEditor;
}
namespace chatterino {
struct IrcConnection_;
class IrcConnectionEditor : public QDialog
{
Q_OBJECT
public:
explicit IrcConnectionEditor(bool isAdd = false, QWidget *parent = nullptr);
explicit IrcConnectionEditor(const IrcConnection_ &data, bool isAdd = false,
QWidget *parent = nullptr);
~IrcConnectionEditor();
IrcConnection_ data();
private:
Ui::IrcConnectionEditor *ui;
Ui::IrcConnectionEditor *ui_;
IrcConnection_ data_;
};
} // namespace chatterino

View file

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>IrcConnectionEditor</class>
<widget class="QDialog" name="IrcConnectionEditor">
<widget class="QWidget" name="IrcConnectionEditor">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>264</width>
<height>258</height>
<height>350</height>
</rect>
</property>
<property name="windowTitle">
@ -19,12 +19,16 @@
<item row="0" column="0">
<widget class="QLabel" name="serverLabel">
<property name="text">
<string>Server:</string>
<string>Host:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="serverLineEdit"/>
<widget class="QLineEdit" name="serverLineEdit">
<property name="placeholderText">
<string>irc.example.com</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="portLabel">
@ -94,21 +98,78 @@
<widget class="QLineEdit" name="nickNameLineEdit"/>
</item>
<item row="6" column="0">
<widget class="QLabel" name="realNameLabel">
<property name="text">
<string>Real Name:</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLineEdit" name="realNameLineEdit"/>
</item>
<item row="8" column="0">
<widget class="QLabel" name="loginMethodLabel">
<property name="text">
<string>Login method:</string>
</property>
</widget>
</item>
<item row="9" column="0">
<widget class="QLabel" name="passwordLabel">
<property name="text">
<string>Password:</string>
</property>
</widget>
</item>
<item row="6" column="1">
<item row="9" column="1">
<widget class="QLineEdit" name="passwordLineEdit">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
<item row="7" column="0">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="8" column="1">
<widget class="QComboBox" name="comboBox">
<item>
<property name="text">
<string>Anonymous</string>
</property>
</item>
<item>
<property name="text">
<string>PASS</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
@ -121,6 +182,16 @@
</item>
</layout>
</widget>
<tabstops>
<tabstop>serverLineEdit</tabstop>
<tabstop>portSpinBox</tabstop>
<tabstop>securityCheckBox</tabstop>
<tabstop>userNameLineEdit</tabstop>
<tabstop>nickNameLineEdit</tabstop>
<tabstop>realNameLineEdit</tabstop>
<tabstop>comboBox</tabstop>
<tabstop>passwordLineEdit</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
@ -130,8 +201,8 @@
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
<x>254</x>
<y>248</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
@ -146,8 +217,8 @@
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
<x>254</x>
<y>248</y>
</hint>
<hint type="destinationlabel">
<x>286</x>

View file

@ -26,7 +26,9 @@ IrcConnectionPopup::IrcConnectionPopup(QWidget *parent)
this->setScaleIndependantSize(800, 500);
view->addButtonPressed.connect([] {
Irc::getInstance().connections.appendItem(IrcConnection_::unique());
auto unique = IrcConnection_{};
unique.id = Irc::getInstance().uniqueId();
Irc::getInstance().connections.appendItem(unique);
});
// init layout

View file

@ -138,34 +138,52 @@ SelectChannelDialog::SelectChannelDialog(QWidget *parent)
auto view = this->ui_.irc.servers = new EditableModelView(
Irc::getInstance().newConnectionModel(this));
view->setTitles(
{"real", "port", "ssl", "user", "nick", "real", "password"});
view->setTitles({"host", "port", "ssl", "user", "nick", "real",
"password", "login command"});
view->getTableView()->horizontalHeader()->resizeSection(0, 140);
view->getTableView()->horizontalHeader()->resizeSection(1, 30);
view->getTableView()->horizontalHeader()->resizeSection(1, 40);
view->getTableView()->horizontalHeader()->resizeSection(2, 30);
//view->getTableView()->horizontalHeader()->setVisible(false);
view->getTableView()->horizontalHeader()->setSectionHidden(1, true);
view->getTableView()->horizontalHeader()->setSectionHidden(2, true);
view->getTableView()->horizontalHeader()->setSectionHidden(6, true);
view->getTableView()->horizontalHeader()->setSectionHidden(7, true);
view->addButtonPressed.connect([] {
Irc::getInstance().connections.appendItem(
IrcConnection_::unique());
auto unique = IrcConnection_{};
unique.id = Irc::getInstance().uniqueId();
Irc::getInstance().connections.appendItem(unique);
});
QObject::connect(
view->getTableView(), &QTableView::clicked,
[](const QModelIndex &index) {
auto data =
Irc::getInstance()
.connections.getVector()[size_t(index.row())];
auto editor = new IrcConnectionEditor(data);
if (editor->exec() == QDialog::Accepted)
{
auto data = editor->data();
auto &&conns =
Irc::getInstance().connections.getVector();
int i = 0;
for (auto &&conn : conns)
{
if (conn.id == data.id)
{
Irc::getInstance().connections.removeItem(i);
Irc::getInstance().connections.insertItem(data,
i);
}
i++;
}
}
});
outerBox->addWidget(view);
// auto box = outerBox.emplace<QHBoxLayout>().withoutMargin();
// auto conns = box.emplace<QListView>();
// conns->addActions({new QAction("hackint")});
// auto buttons = box.emplace<QVBoxLayout>().withoutMargin();
// buttons.emplace<QPushButton>("Add").onClick(this, [this]() {
// (new IrcConnectionPopup(this))
// ->show(); // XXX: don't show multiple
// });
// buttons.emplace<QPushButton>("Edit");
// buttons.emplace<QPushButton>("Remove");
// buttons->addStretch(1);
}
{
@ -174,24 +192,6 @@ SelectChannelDialog::SelectChannelDialog(QWidget *parent)
this->ui_.irc.channel = box.emplace<QLineEdit>().getElement();
}
// auto vbox = obj.setLayoutType<QVBoxLayout>();
// auto form = vbox.emplace<QFormLayout>();
// auto servers = new QComboBox;
// auto accounts = new QComboBox;
//servers->setEditable(true);
//servers->addItems(
// {"irc://irc.hackint.org:6697", "irc://irc.somethingelse.com:6667"});
// form->addRow("Server:", servers);
// form->addRow("Account:", accounts);
// form->addRow("Channel:", new QLineEdit());
// form->addRow("User name:", new QLineEdit());
// form->addRow("First nick choice:", new QLineEdit());
// form->addRow("Second nick choice:", new QLineEdit());
// form->addRow("Third nick choice:", new QLineEdit());
auto tab = notebook->addPage(obj.getElement());
tab->setCustomTitle("Irc");
}