From 87203c11203e9a80f75907b22c6858d3ec5bdc14 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 16 Dec 2017 18:11:36 +0100 Subject: [PATCH] Add system messages upon connecting/disconnecting --- src/channelmanager.cpp | 10 ++++++++++ src/channelmanager.hpp | 2 ++ src/ircmanager.cpp | 25 +++++++++++++++++++++++++ src/ircmanager.hpp | 3 +++ src/messages/message.cpp | 28 ++++++++++++++++++++++++++++ src/messages/message.hpp | 2 ++ 6 files changed, 70 insertions(+) diff --git a/src/channelmanager.cpp b/src/channelmanager.cpp index 7f26232c9..9e9cdd2c7 100644 --- a/src/channelmanager.cpp +++ b/src/channelmanager.cpp @@ -136,4 +136,14 @@ WindowManager &ChannelManager::getWindowManager() return this->windowManager; } +void ChannelManager::doOnAll(std::function)> func) +{ + for (const auto &channel : this->twitchChannels) { + func(std::get<0>(channel)); + } + + func(this->whispersChannel); + func(this->mentionsChannel); +} + } // namespace chatterino diff --git a/src/channelmanager.hpp b/src/channelmanager.hpp index a4cd71b30..77a5712a4 100644 --- a/src/channelmanager.hpp +++ b/src/channelmanager.hpp @@ -32,6 +32,8 @@ public: EmoteManager &getEmoteManager(); WindowManager &getWindowManager(); + void doOnAll(std::function)> func); + // Special channels const std::shared_ptr whispersChannel; const std::shared_ptr mentionsChannel; diff --git a/src/ircmanager.cpp b/src/ircmanager.cpp index 4c3f79852..f145dc01e 100644 --- a/src/ircmanager.cpp +++ b/src/ircmanager.cpp @@ -53,6 +53,11 @@ IrcManager::IrcManager(ChannelManager &_channelManager, Resources &_resources, &IrcManager::messageReceived); QObject::connect(this->readConnection.get(), &Communi::IrcConnection::privateMessageReceived, this, &IrcManager::privateMessageReceived); + + QObject::connect(this->readConnection.get(), &Communi::IrcConnection::connected, this, + &IrcManager::onConnected); + QObject::connect(this->readConnection.get(), &Communi::IrcConnection::disconnected, this, + &IrcManager::onDisconnected); } void IrcManager::setUser(std::shared_ptr newAccount) @@ -370,4 +375,24 @@ void IrcManager::removeIgnoredUser(QString const &username) } } +void IrcManager::onConnected() +{ + std::shared_ptr msg(Message::createSystemMessage("connected to chat")); + + this->channelManager.doOnAll([msg](std::shared_ptr channel) { + assert(channel); + channel->addMessage(msg); + }); +} + +void IrcManager::onDisconnected() +{ + std::shared_ptr msg(Message::createSystemMessage("disconnected from chat")); + + this->channelManager.doOnAll([msg](std::shared_ptr channel) { + assert(channel); + channel->addMessage(msg); + }); +} + } // namespace chatterino diff --git a/src/ircmanager.hpp b/src/ircmanager.hpp index 6bdbe4fce..c64c98a4d 100644 --- a/src/ircmanager.hpp +++ b/src/ircmanager.hpp @@ -85,6 +85,9 @@ private: void handleWhisperMessage(Communi::IrcMessage *message); void handleUserNoticeMessage(Communi::IrcMessage *message); void handleModeMessage(Communi::IrcMessage *message); + + void onConnected(); + void onDisconnected(); }; } // namespace chatterino diff --git a/src/messages/message.cpp b/src/messages/message.cpp index 0635e8639..2d15e6515 100644 --- a/src/messages/message.cpp +++ b/src/messages/message.cpp @@ -70,5 +70,33 @@ const QString &Message::getId() const return this->id; } +/// Static +Message *Message::createSystemMessage(const QString &text) +{ + Message *message = new Message; + + std::time_t t; + time(&t); + char timeStampBuffer[69]; + + // Add word for timestamp with no seconds + strftime(timeStampBuffer, 69, "%H:%M", localtime(&t)); + QString timestampNoSeconds(timeStampBuffer); + message->getWords().push_back(Word(timestampNoSeconds, Word::TimestampNoSeconds, + MessageColor(MessageColor::System), QString(), QString())); + + // Add word for timestamp with seconds + strftime(timeStampBuffer, 69, "%H:%M:%S", localtime(&t)); + QString timestampWithSeconds(timeStampBuffer); + message->getWords().push_back(Word(timestampWithSeconds, Word::TimestampWithSeconds, + MessageColor(MessageColor::System), QString(), QString())); + + Word word(text, Word::Type::Default, MessageColor(MessageColor::Type::System), text, text); + + message->getWords().push_back(word); + + return message; +} + } // namespace messages } // namespace chatterino diff --git a/src/messages/message.hpp b/src/messages/message.hpp index 349ee3cb3..fded9131d 100644 --- a/src/messages/message.hpp +++ b/src/messages/message.hpp @@ -33,6 +33,8 @@ public: const QString text; bool centered = false; + static Message *createSystemMessage(const QString &text); + private: static LazyLoadedImage *badgeStaff; static LazyLoadedImage *badgeAdmin;