From 5fe8ee61170aaf3fe2002cc39dfb0003221c8db4 Mon Sep 17 00:00:00 2001 From: Cranken Date: Sun, 3 Sep 2017 16:53:38 +0200 Subject: [PATCH] Added check for livestatus + tooltip. --- src/channel.cpp | 21 +------------- src/channel.hpp | 9 +++--- src/ircmanager.cpp | 3 ++ src/widgets/chatwidget.cpp | 3 ++ src/widgets/chatwidgetheader.cpp | 50 ++++++++++++++++++++++++++++++-- src/widgets/chatwidgetheader.hpp | 3 +- 6 files changed, 62 insertions(+), 27 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index 311ecd03c..76de2d168 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -29,6 +29,7 @@ Channel::Channel(WindowManager &_windowManager, EmoteManager &_emoteManager, , _subLink("https://www.twitch.tv/" + name + "/subscribe?ref=in_chat_subscriber_link") , _channelLink("https://twitch.tv/" + name) , _popoutPlayerLink("https://player.twitch.tv/?channel=" + name) + , isLive(false) // , _loggingChannel(logging::get(_name)) { qDebug() << "Open channel:" << this->name; @@ -62,26 +63,6 @@ const QString &Channel::getPopoutPlayerLink() const return _popoutPlayerLink; } -bool Channel::getIsLive() const -{ - return _isLive; -} - -int Channel::getStreamViewerCount() const -{ - return _streamViewerCount; -} - -const QString &Channel::getStreamStatus() const -{ - return _streamStatus; -} - -const QString &Channel::getStreamGame() const -{ - return _streamGame; -} - messages::LimitedQueueSnapshot Channel::getMessageSnapshot() { return _messages.getSnapshot(); diff --git a/src/channel.hpp b/src/channel.hpp index ec3e88766..2284ee1a8 100644 --- a/src/channel.hpp +++ b/src/channel.hpp @@ -53,6 +53,11 @@ public: std::string roomID; const QString name; + bool isLive; + QString streamViewerCount; + QString streamStatus; + QString streamGame; + QString streamUptime; private: // variables @@ -67,10 +72,6 @@ private: QString _channelLink; QString _popoutPlayerLink; - bool _isLive; - int _streamViewerCount; - QString _streamStatus; - QString _streamGame; // std::shared_ptr _loggingChannel; }; diff --git a/src/ircmanager.cpp b/src/ircmanager.cpp index 76240b4e7..eff587d49 100644 --- a/src/ircmanager.cpp +++ b/src/ircmanager.cpp @@ -282,6 +282,9 @@ void IrcManager::handleRoomStateMessage(Communi::IrcMessage *message) if (iterator != tags.end()) { std::string roomID = iterator.value().toString().toStdString(); + auto channel = QString(message->toData()).split("#").at(1); + channelManager.getChannel(channel)->roomID = roomID; + this->resources.loadChannelData(roomID); } } diff --git a/src/widgets/chatwidget.cpp b/src/widgets/chatwidget.cpp index 469e48ca2..44970153e 100644 --- a/src/widgets/chatwidget.cpp +++ b/src/widgets/chatwidget.cpp @@ -244,6 +244,9 @@ void ChatWidget::doCloseSplit() { NotebookPage *page = static_cast(this->parentWidget()); page->removeFromLayout(this); + QTimer* timer = this->header.findChild(); + timer->stop(); + timer->deleteLater(); } void ChatWidget::doChangeChannel() diff --git a/src/widgets/chatwidgetheader.cpp b/src/widgets/chatwidgetheader.cpp index 97964892d..9bfe05308 100644 --- a/src/widgets/chatwidgetheader.cpp +++ b/src/widgets/chatwidgetheader.cpp @@ -2,6 +2,7 @@ #include "colorscheme.hpp" #include "widgets/chatwidget.hpp" #include "widgets/notebookpage.hpp" +#include "util/urlfetch.hpp" #include #include @@ -67,16 +68,36 @@ ChatWidgetHeader::ChatWidgetHeader(ChatWidget *_chatWidget) this->rightLabel.setMinimumWidth(this->height()); this->rightLabel.getLabel().setTextFormat(Qt::RichText); this->rightLabel.getLabel().setText("ayy"); + + QTimer *timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, checkLive); + timer->start(60000); } void ChatWidgetHeader::updateChannelText() { const std::string channelName = this->chatWidget->channelName; - if (channelName.empty()) { this->channelNameLabel.setText(""); } else { - this->channelNameLabel.setText(QString::fromStdString(channelName)); + if(this->chatWidget->getChannelRef()->isLive) + { + auto channel = this->chatWidget->getChannelRef(); + this->channelNameLabel.setText(QString::fromStdString(channelName) + " (live)"); + this->setToolTip("" \ + "

" + \ + channel->streamStatus + "

" + \ + channel->streamGame + "
" \ + "Live for " + channel->streamUptime + \ + " with " + channel->streamViewerCount + " viewers" \ + "

" + ); + } + else + { + this->channelNameLabel.setText(QString::fromStdString(channelName)); + this->setToolTip(""); + } } } @@ -173,5 +194,30 @@ void ChatWidgetHeader::menuShowChangelog() { } +void ChatWidgetHeader::checkLive() +{ + auto channel = this->chatWidget->getChannelRef(); + auto id = QString::fromStdString(channel->roomID); + util::twitch::get("https://api.twitch.tv/kraken/streams/" + id,[=](QJsonObject obj){ + if(obj.value("stream").isNull()) + { + channel->isLive = false; + this->updateChannelText(); + } + else + { + channel->isLive = true; + auto stream = obj.value("stream").toObject(); + channel->streamViewerCount = QString::number(stream.value("viewers").toDouble()); + channel->streamGame = stream.value("game").toString(); + channel->streamStatus = stream.value("channel").toObject().value("status").toString(); + QDateTime since = QDateTime::fromString(stream.value("created_at").toString(),Qt::ISODate); + auto diff = since.secsTo(QDateTime::currentDateTime()); + channel->streamUptime = QString::number(diff/3600) + "h " + QString::number(diff % 3600 / 60) + "m"; + this->updateChannelText(); + } + }); +} + } // namespace widgets } // namespace chatterino diff --git a/src/widgets/chatwidgetheader.hpp b/src/widgets/chatwidgetheader.hpp index 8ce3ca740..fe437862e 100644 --- a/src/widgets/chatwidgetheader.hpp +++ b/src/widgets/chatwidgetheader.hpp @@ -27,9 +27,9 @@ class ChatWidgetHeader : public BaseWidget public: explicit ChatWidgetHeader(ChatWidget *_chatWidget); - // Update channel text from chat widget void updateChannelText(); + void checkLive(); protected: virtual void paintEvent(QPaintEvent *) override; @@ -66,6 +66,7 @@ public slots: void menuReloadChannelEmotes(); void menuManualReconnect(); void menuShowChangelog(); + }; } // namespace widgets