diff --git a/channel.cpp b/channel.cpp index 2580eb0ba..702956bb1 100644 --- a/channel.cpp +++ b/channel.cpp @@ -24,6 +24,94 @@ Channel::Channel(const QString &channel) // } } +void +Channel::reloadBttvEmotes() +{ + QNetworkAccessManager *manager = new QNetworkAccessManager(); + + QUrl url("https://api.frankerfacez.com/v1/set/global"); + QNetworkRequest request(url); + + QNetworkReply *reply = manager->get(request); + + QObject::connect(reply, &QNetworkReply::finished, [=] { + if (reply->error() == QNetworkReply::NetworkError::NoError) { + QByteArray data = reply->readAll(); + QJsonDocument jsonDoc(QJsonDocument::fromJson(data)); + QJsonObject root = jsonDoc.object(); + + auto sets = root.value("sets").toObject(); + + for (const QJsonValue &set : sets) { + auto emoticons = set.toObject().value("emoticons").toArray(); + + for (const QJsonValue &emote : emoticons) { + QJsonObject object = emote.toObject(); + + // margins + + int id = object.value("id").toInt(); + QString code = object.value("name").toString(); + + QJsonObject urls = object.value("urls").toObject(); + QString url1 = "http:" + urls.value("1").toString(); + + Emotes::getBttvEmotes().insert( + code, new messages::LazyLoadedImage( + url1, 1, code, code + "\nGlobal Ffz Emote")); + } + } + } + + reply->deleteLater(); + manager->deleteLater(); + }); +} + +void +Channel::reloadFfzEmotes() +{ + QNetworkAccessManager *manager = new QNetworkAccessManager(); + + QUrl url("https://api.frankerfacez.com/v1/set/global"); + QNetworkRequest request(url); + + QNetworkReply *reply = manager->get(request); + + QObject::connect(reply, &QNetworkReply::finished, [=] { + if (reply->error() == QNetworkReply::NetworkError::NoError) { + QByteArray data = reply->readAll(); + QJsonDocument jsonDoc(QJsonDocument::fromJson(data)); + QJsonObject root = jsonDoc.object(); + + auto sets = root.value("sets").toObject(); + + for (const QJsonValue &set : sets) { + auto emoticons = set.toObject().value("emoticons").toArray(); + + for (const QJsonValue &emote : emoticons) { + QJsonObject object = emote.toObject(); + + // margins + + int id = object.value("id").toInt(); + QString code = object.value("name").toString(); + + QJsonObject urls = object.value("urls").toObject(); + QString url1 = "http:" + urls.value("1").toString(); + + Emotes::getBttvEmotes().insert( + code, new messages::LazyLoadedImage( + url1, 1, code, code + "\nGlobal Ffz Emote")); + } + } + } + + reply->deleteLater(); + manager->deleteLater(); + }); +} + QVector> Channel::getMessagesClone() { diff --git a/channel.h b/channel.h index 1f59031ed..fa864d635 100644 --- a/channel.h +++ b/channel.h @@ -99,6 +99,13 @@ public: return messages; } + void + reloadChannelEmotes() + { + reloadBttvEmotes(); + reloadFfzEmotes(); + } + private: QVector> messages; @@ -117,6 +124,9 @@ private: int streamViewerCount; QString streamStatus; QString streamGame; + + void reloadBttvEmotes(); + void reloadFfzEmotes(); }; } diff --git a/emotes.cpp b/emotes.cpp index b8aaed1e8..b8b690b44 100644 --- a/emotes.cpp +++ b/emotes.cpp @@ -2,6 +2,14 @@ #include "resources.h" #include +#include +#include +#include +#include +#include +#include +#include +#include namespace chatterino { @@ -25,6 +33,100 @@ Emotes::Emotes() { } +void +Emotes::loadGlobalEmotes() +{ + loadBttvEmotes(); + loadFfzEmotes(); +} + +void +Emotes::loadBttvEmotes() +{ + // bttv + QNetworkAccessManager *manager = new QNetworkAccessManager(); + + QUrl url("https://api.betterttv.net/2/emotes"); + QNetworkRequest request(url); + + QNetworkReply *reply = manager->get(request); + + QObject::connect(reply, &QNetworkReply::finished, [=] { + if (reply->error() == QNetworkReply::NetworkError::NoError) { + QByteArray data = reply->readAll(); + QJsonDocument jsonDoc(QJsonDocument::fromJson(data)); + QJsonObject root = jsonDoc.object(); + + auto emotes = root.value("emotes").toArray(); + + QString _template = "https:" + root.value("urlTemplate").toString(); + + for (const QJsonValue &emote : emotes) { + QString id = emote.toObject().value("id").toString(); + QString code = emote.toObject().value("code").toString(); + // emote.value("imageType").toString(); + + QString tmp = _template; + tmp.detach(); + QString url = + tmp.replace("{{id}}", id).replace("{{image}}", "1x"); + + Emotes::getBttvEmotes().insert( + code, new messages::LazyLoadedImage( + url, 1, code, code + "\nGlobal Bttv Emote")); + } + } + + reply->deleteLater(); + manager->deleteLater(); + }); +} + +void +Emotes::loadFfzEmotes() +{ + // bttv + QNetworkAccessManager *manager = new QNetworkAccessManager(); + + QUrl url("https://api.frankerfacez.com/v1/set/global"); + QNetworkRequest request(url); + + QNetworkReply *reply = manager->get(request); + + QObject::connect(reply, &QNetworkReply::finished, [=] { + if (reply->error() == QNetworkReply::NetworkError::NoError) { + QByteArray data = reply->readAll(); + QJsonDocument jsonDoc(QJsonDocument::fromJson(data)); + QJsonObject root = jsonDoc.object(); + + auto sets = root.value("sets").toObject(); + + for (const QJsonValue &set : sets) { + auto emoticons = set.toObject().value("emoticons").toArray(); + + for (const QJsonValue &emote : emoticons) { + QJsonObject object = emote.toObject(); + + // margins + + int id = object.value("id").toInt(); + QString code = object.value("name").toString(); + + QJsonObject urls = object.value("urls").toObject(); + QString url1 = "http:" + urls.value("1").toString(); + + Emotes::getBttvEmotes().insert( + code, new messages::LazyLoadedImage( + url1, 1, code, code + "\nGlobal Ffz Emote")); + } + } + } + + reply->deleteLater(); + manager->deleteLater(); + }); +} + messages::LazyLoadedImage * Emotes::getTwitchEmoteById(const QString &name, long id) { diff --git a/emotes.h b/emotes.h index e4407a6b9..3e0389591 100644 --- a/emotes.h +++ b/emotes.h @@ -102,6 +102,9 @@ private: static QString getTwitchEmoteLink(long id, qreal &scale); + static void loadFfzEmotes(); + static void loadBttvEmotes(); + static int generation; }; } diff --git a/main.cpp b/main.cpp index a8366a6a1..c04b27514 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include "channels.h" #include "colorscheme.h" #include "emojis.h" +#include "emotes.h" #include "ircmanager.h" #include "resources.h" #include "settings.h" @@ -21,6 +22,7 @@ main(int argc, char *argv[]) Settings::getInstance().load(); Resources::load(); Emojis::loadEmojis(); + Emotes::loadGlobalEmotes(); ColorScheme::getInstance().setColors(0, -0.8); diff --git a/messages/lazyloadedimage.cpp b/messages/lazyloadedimage.cpp index 15bfcdf26..09a9ad37c 100644 --- a/messages/lazyloadedimage.cpp +++ b/messages/lazyloadedimage.cpp @@ -66,7 +66,8 @@ LazyLoadedImage::loadImage() Emotes::incGeneration(); Windows::layoutVisibleChatWidgets(); - delete manager; + reply->deleteLater(); + manager->deleteLater(); }); // })); } diff --git a/messages/lazyloadedimage.h b/messages/lazyloadedimage.h index 9cde36869..482af09cf 100644 --- a/messages/lazyloadedimage.h +++ b/messages/lazyloadedimage.h @@ -11,15 +11,15 @@ class LazyLoadedImage { public: explicit LazyLoadedImage(const QString &url, qreal scale = 1, - const QString &getName = "", - const QString &getTooltip = "", - const QMargins &getMargin = QMargins(), - bool getIsHat = false); + const QString &name = "", + const QString &tooltip = "", + const QMargins &margin = QMargins(), + bool isHat = false); explicit LazyLoadedImage(QPixmap *pixmap, qreal scale = 1, - const QString &getName = "", - const QString &getTooltip = "", - const QMargins &getMargin = QMargins(), - bool getIsHat = false); + const QString &name = "", + const QString &tooltip = "", + const QMargins &margin = QMargins(), + bool isHat = false); const QPixmap * getPixmap()