mirror-chatterino2/src/singletons/resourcemanager.cpp

156 lines
5.7 KiB
C++
Raw Normal View History

2017-12-31 02:21:33 +01:00
#include "resourcemanager.hpp"
2017-12-31 00:50:07 +01:00
//#include "singletons/emotemanager.hpp"
//#include "singletons/windowmanager.hpp"
2017-06-15 23:13:01 +02:00
#include "util/urlfetch.hpp"
2017-01-18 04:52:47 +01:00
#include <QPixmap>
2017-01-13 18:59:11 +01:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-01-13 18:59:11 +01:00
namespace {
2017-01-13 18:59:11 +01:00
2017-12-17 02:18:13 +01:00
inline messages::LazyLoadedImage *lli(const char *pixmapPath, qreal scale = 1)
2017-01-13 18:59:11 +01:00
{
2017-12-17 02:18:13 +01:00
return new messages::LazyLoadedImage(new QPixmap(pixmapPath), scale);
2017-01-13 18:59:11 +01:00
}
} // namespace
2017-12-31 02:21:33 +01:00
ResourceManager::ResourceManager()
2017-12-17 02:18:13 +01:00
: badgeStaff(lli(":/images/staff_bg.png"))
, badgeAdmin(lli(":/images/admin_bg.png"))
, badgeGlobalModerator(lli(":/images/globalmod_bg.png"))
, badgeModerator(lli(":/images/moderator_bg.png"))
, badgeTurbo(lli(":/images/turbo_bg.png"))
, badgeBroadcaster(lli(":/images/broadcaster_bg.png"))
, badgePremium(lli(":/images/twitchprime_bg.png"))
, badgeVerified(lli(":/images/verified.png", 0.25))
, badgeSubscriber(lli(":/images/subscriber.png", 0.25))
, badgeCollapsed(lli(":/images/collapse.png"))
2017-12-17 02:18:13 +01:00
, cheerBadge100000(lli(":/images/cheer100000"))
, cheerBadge10000(lli(":/images/cheer10000"))
, cheerBadge5000(lli(":/images/cheer5000"))
, cheerBadge1000(lli(":/images/cheer1000"))
, cheerBadge100(lli(":/images/cheer100"))
, cheerBadge1(lli(":/images/cheer1"))
, buttonBan(lli(":/images/button_ban.png", 0.25))
, buttonTimeout(lli(":/images/button_timeout.png", 0.25))
2017-01-13 18:59:11 +01:00
{
this->loadDynamicTwitchBadges();
2017-08-12 13:20:52 +02:00
this->loadChatterinoBadges();
2017-06-15 23:13:01 +02:00
}
2017-12-31 02:21:33 +01:00
ResourceManager &ResourceManager::getInstance()
2017-12-31 00:50:07 +01:00
{
2017-12-31 02:21:33 +01:00
static ResourceManager instance;
2017-12-31 00:50:07 +01:00
return instance;
}
2017-12-31 02:21:33 +01:00
ResourceManager::BadgeVersion::BadgeVersion(QJsonObject &&root)
2017-12-17 02:18:13 +01:00
: badgeImage1x(new messages::LazyLoadedImage(root.value("image_url_1x").toString()))
, badgeImage2x(new messages::LazyLoadedImage(root.value("image_url_2x").toString()))
, badgeImage4x(new messages::LazyLoadedImage(root.value("image_url_4x").toString()))
2017-06-16 08:03:13 +02:00
, description(root.value("description").toString().toStdString())
, title(root.value("title").toString().toStdString())
, clickAction(root.value("clickAction").toString().toStdString())
, clickURL(root.value("clickURL").toString().toStdString())
2017-06-15 23:13:01 +02:00
{
}
2017-12-31 02:21:33 +01:00
void ResourceManager::loadChannelData(const QString &roomID, bool bypassCache)
2017-06-15 23:13:01 +02:00
{
qDebug() << "Load channel data for" << roomID;
2017-06-15 23:13:01 +02:00
QString url = "https://badges.twitch.tv/v1/badges/channels/" + roomID + "/display?language=en";
util::NetworkRequest req(url);
req.setCaller(QThread::currentThread());
req.getJSON([this, roomID](QJsonObject &root) {
QJsonObject sets = root.value("badge_sets").toObject();
2017-12-31 02:21:33 +01:00
ResourceManager::Channel &ch = this->channels[roomID];
2017-07-02 15:11:33 +02:00
for (QJsonObject::iterator it = sets.begin(); it != sets.end(); ++it) {
QJsonObject versions = it.value().toObject().value("versions").toObject();
2017-07-02 15:11:33 +02:00
auto &badgeSet = ch.badgeSets[it.key().toStdString()];
auto &versionsMap = badgeSet.versions;
for (auto versionIt = std::begin(versions); versionIt != std::end(versions);
++versionIt) {
std::string kkey = versionIt.key().toStdString();
QJsonObject versionObj = versionIt.value().toObject();
2017-12-17 02:18:13 +01:00
BadgeVersion v(std::move(versionObj));
versionsMap.emplace(kkey, v);
}
}
2017-07-02 15:11:33 +02:00
ch.loaded = true;
});
}
2017-12-31 02:21:33 +01:00
void ResourceManager::loadDynamicTwitchBadges()
{
static QString url("https://badges.twitch.tv/v1/badges/global/display?language=en");
util::NetworkRequest req(url);
req.setCaller(QThread::currentThread());
req.getJSON([this](QJsonObject &root) {
QJsonObject sets = root.value("badge_sets").toObject();
qDebug() << "badges fetched";
for (QJsonObject::iterator it = sets.begin(); it != sets.end(); ++it) {
QJsonObject versions = it.value().toObject().value("versions").toObject();
auto &badgeSet = this->badgeSets[it.key().toStdString()];
auto &versionsMap = badgeSet.versions;
for (auto versionIt = std::begin(versions); versionIt != std::end(versions);
++versionIt) {
std::string kkey = versionIt.key().toStdString();
QJsonObject versionObj = versionIt.value().toObject();
2017-12-17 02:18:13 +01:00
BadgeVersion v(std::move(versionObj));
versionsMap.emplace(kkey, v);
}
}
this->dynamicBadgesLoaded = true;
});
}
2017-12-31 02:21:33 +01:00
void ResourceManager::loadChatterinoBadges()
2017-08-12 13:20:52 +02:00
{
this->chatterinoBadges.clear();
static QString url("https://fourtf.com/chatterino/badges.json");
util::NetworkRequest req(url);
req.setCaller(QThread::currentThread());
2017-08-12 13:20:52 +02:00
req.getJSON([this](QJsonObject &root) {
2017-08-12 13:20:52 +02:00
QJsonArray badgeVariants = root.value("badges").toArray();
qDebug() << "chatbadges fetched";
2017-08-12 13:20:52 +02:00
for (QJsonArray::iterator it = badgeVariants.begin(); it != badgeVariants.end(); ++it) {
QJsonObject badgeVariant = it->toObject();
const std::string badgeVariantTooltip =
badgeVariant.value("tooltip").toString().toStdString();
const QString &badgeVariantImageURL = badgeVariant.value("image").toString();
auto badgeVariantPtr = std::make_shared<ChatterinoBadge>(
2017-12-17 02:18:13 +01:00
badgeVariantTooltip, new messages::LazyLoadedImage(badgeVariantImageURL));
2017-08-12 13:20:52 +02:00
QJsonArray badgeVariantUsers = badgeVariant.value("users").toArray();
for (QJsonArray::iterator it = badgeVariantUsers.begin(); it != badgeVariantUsers.end();
++it) {
const std::string username = it->toString().toStdString();
this->chatterinoBadges[username] =
std::shared_ptr<ChatterinoBadge>(badgeVariantPtr);
}
}
});
}
} // namespace chatterino