From b20fdc0da6a1a5be5284f693eb11b18a42aa1aec Mon Sep 17 00:00:00 2001 From: fourtf Date: Wed, 11 Sep 2019 13:17:36 +0200 Subject: [PATCH] smol irc fixes --- src/providers/irc/AbstractIrcServer.cpp | 26 ++++++++------- src/providers/irc/AbstractIrcServer.hpp | 2 ++ src/providers/irc/IrcServer.cpp | 22 +++++++++++-- src/providers/irc/IrcServer.hpp | 2 ++ src/singletons/Settings.hpp | 4 +++ src/widgets/dialogs/IrcConnectionEditor.ui | 2 +- src/widgets/dialogs/SelectChannelDialog.cpp | 36 ++++++++++++++++----- 7 files changed, 72 insertions(+), 22 deletions(-) diff --git a/src/providers/irc/AbstractIrcServer.cpp b/src/providers/irc/AbstractIrcServer.cpp index 8aab88df..fa8de283 100644 --- a/src/providers/irc/AbstractIrcServer.cpp +++ b/src/providers/irc/AbstractIrcServer.cpp @@ -164,26 +164,24 @@ ChannelPtr AbstractIrcServer::getOrAddChannel(const QString &dirtyChannelName) return Channel::getEmpty(); } - QString clojuresInCppAreShit = channelName; - this->channels.insert(channelName, chan); - chan->destroyed.connect([this, clojuresInCppAreShit] { + this->connections_.emplace_back(chan->destroyed.connect([this, + channelName] { // fourtf: issues when the server itself is destroyed - log("[AbstractIrcServer::addChannel] {} was destroyed", - clojuresInCppAreShit); - this->channels.remove(clojuresInCppAreShit); + log("[AbstractIrcServer::addChannel] {} was destroyed", channelName); + this->channels.remove(channelName); if (this->readConnection_) { - this->readConnection_->sendRaw("PART #" + clojuresInCppAreShit); + this->readConnection_->sendRaw("PART #" + channelName); } if (this->writeConnection_ && this->hasSeparateWriteConnection()) { - this->writeConnection_->sendRaw("PART #" + clojuresInCppAreShit); + this->writeConnection_->sendRaw("PART #" + channelName); } - }); + })); // join irc channel { @@ -191,12 +189,18 @@ ChannelPtr AbstractIrcServer::getOrAddChannel(const QString &dirtyChannelName) if (this->readConnection_) { - this->readConnection_->sendRaw("JOIN #" + channelName); + if (this->readConnection_->isConnected()) + { + this->readConnection_->sendRaw("JOIN #" + channelName); + } } if (this->writeConnection_ && this->hasSeparateWriteConnection()) { - this->writeConnection_->sendRaw("JOIN #" + channelName); + if (this->readConnection_->isConnected()) + { + this->writeConnection_->sendRaw("JOIN #" + channelName); + } } } diff --git a/src/providers/irc/AbstractIrcServer.hpp b/src/providers/irc/AbstractIrcServer.hpp index c02b8bd4..23f22a40 100644 --- a/src/providers/irc/AbstractIrcServer.hpp +++ b/src/providers/irc/AbstractIrcServer.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -84,6 +85,7 @@ private: std::mutex connectionMutex_; // bool autoReconnect_ = false; + pajlada::Signals::SignalHolder connections_; }; } // namespace chatterino diff --git a/src/providers/irc/IrcServer.cpp b/src/providers/irc/IrcServer.cpp index 832c67f5..077437a4 100644 --- a/src/providers/irc/IrcServer.cpp +++ b/src/providers/irc/IrcServer.cpp @@ -57,8 +57,10 @@ void IrcServer::initializeConnection(IrcConnection *connection, bool isRead, connection->setPort(this->data_->port); connection->setUserName(this->data_->user); - connection->setNickName(this->data_->nick); - connection->setRealName(this->data_->real); + connection->setNickName(this->data_->nick.isEmpty() ? this->data_->user + : this->data_->nick); + connection->setRealName(this->data_->real.isEmpty() ? this->data_->user + : this->data_->nick); connection->setPassword(this->data_->password); } @@ -72,6 +74,21 @@ bool IrcServer::hasSeparateWriteConnection() const return false; } +void IrcServer::onReadConnected(IrcConnection *connection) +{ + AbstractIrcServer::onReadConnected(connection); + + std::lock_guard lock(this->channelMutex); + + for (auto &&weak : this->channels) + { + if (auto channel = weak.lock()) + { + connection->sendRaw("JOIN #" + channel->getName()); + } + } +} + void IrcServer::privateMessageReceived(Communi::IrcPrivateMessage *message) { auto target = message->target(); @@ -81,6 +98,7 @@ void IrcServer::privateMessageReceived(Communi::IrcPrivateMessage *message) { MessageBuilder builder; + builder.emplace(); builder.emplace(message->nick() + ":", MessageElementFlag::Username); builder.emplace(message->content(), diff --git a/src/providers/irc/IrcServer.hpp b/src/providers/irc/IrcServer.hpp index 9a7310a5..8cac05f3 100644 --- a/src/providers/irc/IrcServer.hpp +++ b/src/providers/irc/IrcServer.hpp @@ -25,6 +25,8 @@ protected: bool isWrite) override; std::shared_ptr createChannel(const QString &channelName) override; bool hasSeparateWriteConnection() const override; + + void onReadConnected(IrcConnection *connection) override; void privateMessageReceived(Communi::IrcPrivateMessage *message) override; void readConnectionMessageReceived(Communi::IrcMessage *message) override; diff --git a/src/singletons/Settings.hpp b/src/singletons/Settings.hpp index 2ab20d06..1d0d87be 100644 --- a/src/singletons/Settings.hpp +++ b/src/singletons/Settings.hpp @@ -207,6 +207,10 @@ public: QStringSetting cachePath = {"/cache/path", ""}; + /// UI + IntSetting lastSelectChannelTab = {"/ui/lastSelectChannelTab", 0}; + IntSetting lastSelectIrcConn = {"/ui/lastSelectIrcConn", 0}; + private: void updateModerationActions(); }; diff --git a/src/widgets/dialogs/IrcConnectionEditor.ui b/src/widgets/dialogs/IrcConnectionEditor.ui index 53e8faab..7fc35694 100644 --- a/src/widgets/dialogs/IrcConnectionEditor.ui +++ b/src/widgets/dialogs/IrcConnectionEditor.ui @@ -1,7 +1,7 @@ IrcConnectionEditor - + 0 diff --git a/src/widgets/dialogs/SelectChannelDialog.cpp b/src/widgets/dialogs/SelectChannelDialog.cpp index 5126355a..59fc9c77 100644 --- a/src/widgets/dialogs/SelectChannelDialog.cpp +++ b/src/widgets/dialogs/SelectChannelDialog.cpp @@ -147,23 +147,29 @@ SelectChannelDialog::SelectChannelDialog(QWidget *parent) view->getTableView()->horizontalHeader()->setSectionHidden(1, true); view->getTableView()->horizontalHeader()->setSectionHidden(2, true); + view->getTableView()->horizontalHeader()->setSectionHidden(4, true); + view->getTableView()->horizontalHeader()->setSectionHidden(5, true); view->getTableView()->horizontalHeader()->setSectionHidden(6, true); view->getTableView()->horizontalHeader()->setSectionHidden(7, true); view->addButtonPressed.connect([] { auto unique = IrcConnection_{}; unique.id = Irc::getInstance().uniqueId(); - Irc::getInstance().connections.appendItem(unique); + + auto editor = new IrcConnectionEditor(unique); + if (editor->exec() == QDialog::Accepted) + { + Irc::getInstance().connections.appendItem(unique); + } }); QObject::connect( - view->getTableView(), &QTableView::clicked, + view->getTableView(), &QTableView::doubleClicked, [](const QModelIndex &index) { - auto data = + auto editor = new IrcConnectionEditor( Irc::getInstance() - .connections.getVector()[size_t(index.row())]; + .connections.getVector()[size_t(index.row())]); - auto editor = new IrcConnectionEditor(data); if (editor->exec() == QDialog::Accepted) { auto data = editor->data(); @@ -219,10 +225,24 @@ SelectChannelDialog::SelectChannelDialog(QWidget *parent) auto *shortcut_cancel = new QShortcut(QKeySequence("Esc"), this); QObject::connect(shortcut_cancel, &QShortcut::activated, [=] { this->close(); }); + + // restore ui state + this->ui_.notebook->selectIndex(getSettings()->lastSelectChannelTab); + this->ui_.irc.servers->getTableView()->selectRow( + getSettings()->lastSelectIrcConn); } void SelectChannelDialog::ok() { + // save ui state + getSettings()->lastSelectChannelTab = + this->ui_.notebook->getSelectedIndex(); + getSettings()->lastSelectIrcConn = this->ui_.irc.servers->getTableView() + ->selectionModel() + ->currentIndex() + .row(); + + // accept and close this->hasSelectedChannel_ = true; this->close(); } @@ -339,14 +359,14 @@ IndirectChannel SelectChannelDialog::getSelectedChannel() const if (row >= 0 && row < int(vector.size())) { return Irc::getInstance().getOrAddChannel( - vector[row].id, this->ui_.irc.channel->text()); + vector[size_t(row)].id, this->ui_.irc.channel->text()); } else { return Channel::getEmpty(); } } - break; + //break; } return this->selectedChannel_; @@ -360,7 +380,7 @@ bool SelectChannelDialog::hasSeletedChannel() const bool SelectChannelDialog::EventFilter::eventFilter(QObject *watched, QEvent *event) { - auto *widget = (QWidget *)watched; + auto *widget = static_cast(watched); if (event->type() == QEvent::FocusIn) {