From 8d5b93fe82a71490e17a442d795d9f4a30aafe26 Mon Sep 17 00:00:00 2001 From: apa420 Date: Sun, 19 Aug 2018 19:02:49 +0200 Subject: [PATCH] push for now --- chatterino.pro | 5 +- src/Application.cpp | 5 +- src/Application.hpp | 2 - src/common/DownloadManager.cpp | 80 ++++++++++++++ src/common/DownloadManager.hpp | 36 ++++++ src/singletons/Settings.hpp | 2 + src/singletons/Toasts.cpp | 104 +++++------------- src/singletons/Toasts.hpp | 7 -- .../settingspages/NotificationPage.cpp | 3 + src/widgets/splits/SplitHeader.cpp | 16 ++- 10 files changed, 170 insertions(+), 90 deletions(-) create mode 100644 src/common/DownloadManager.cpp create mode 100644 src/common/DownloadManager.hpp diff --git a/chatterino.pro b/chatterino.pro index 3252d9e6..8003eba1 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -256,7 +256,7 @@ SOURCES += \ src/util/FunctionEventFilter.cpp \ src/controllers/notifications/NotificationModel.cpp \ src/singletons/Toasts.cpp \ - src/singletons/DownloadManager.cpp + src/common/DownloadManager.cpp HEADERS += \ src/Application.hpp \ @@ -458,9 +458,8 @@ HEADERS += \ src/util/FormatTime.hpp \ src/util/FunctionEventFilter.hpp \ src/controllers/notifications/NotificationModel.hpp \ - src/controllers/notifications/NotificationPhrase.hpp \ src/singletons/Toasts.hpp \ - src/singletons/DownloadManager.hpp + src/common/DownloadManager.hpp RESOURCES += \ resources/resources.qrc \ diff --git a/src/Application.cpp b/src/Application.cpp index cf0cb2ef..cde5c1c0 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -12,7 +12,6 @@ #include "providers/ffz/FfzEmotes.hpp" #include "providers/twitch/PubsubClient.hpp" #include "providers/twitch/TwitchServer.hpp" -#include "singletons/DownloadManager.hpp" #include "singletons/Fonts.hpp" #include "singletons/Logging.hpp" #include "singletons/NativeMessaging.hpp" @@ -46,8 +45,8 @@ Application::Application(Settings &_settings, Paths &_paths) , fonts(&this->emplace()) , emotes(&this->emplace()) , windows(&this->emplace()) - , toasts(&this->emplace()) + , accounts(&this->emplace()) , commands(&this->emplace()) , highlights(&this->emplace()) @@ -57,7 +56,7 @@ Application::Application(Settings &_settings, Paths &_paths) , moderationActions(&this->emplace()) , twitch2(&this->emplace()) , logging(&this->emplace()) - , downloads(&this->emplace()) + { this->instance = this; diff --git a/src/Application.hpp b/src/Application.hpp index 64f92662..b1dbe240 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -29,7 +29,6 @@ class Settings; class Fonts; class Resources; class Toasts; -class DownloadManager; class Application { @@ -59,7 +58,6 @@ public: Emotes *const emotes{}; WindowManager *const windows{}; Toasts *const toasts{}; - DownloadManager *const downloads{}; AccountController *const accounts{}; CommandController *const commands{}; diff --git a/src/common/DownloadManager.cpp b/src/common/DownloadManager.cpp new file mode 100644 index 00000000..41d2aad6 --- /dev/null +++ b/src/common/DownloadManager.cpp @@ -0,0 +1,80 @@ +#include "DownloadManager.hpp" + +#include + +namespace chatterino { + +DownloadManager::DownloadManager(QObject *parent) + : QObject(parent) +{ + manager = new QNetworkAccessManager; +} + +DownloadManager::~DownloadManager() +{ + manager->deleteLater(); +} + +void DownloadManager::setFile(QString fileURL, const QString &channelName) +{ + QString filePath = fileURL; + QString saveFilePath; + QStringList filePathList = filePath.split('/'); + // QString fileName = filePathList.at(filePathList.count() - 1); + saveFilePath = QString( + QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + + "2/cache/profileAvatars/twitch/" + channelName + ".png"); + + QNetworkRequest request; + request.setUrl(QUrl(fileURL)); + reply = manager->get(request); + + file = new QFile; + file->setFileName(saveFilePath); + file->open(QIODevice::WriteOnly); + + connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, + SLOT(onDownloadProgress(qint64, qint64))); + connect(manager, SIGNAL(finished(QNetworkReply *)), this, + SLOT(onFinished(QNetworkReply *))); + connect(reply, SIGNAL(readyRead()), this, SLOT(onReadyRead())); + connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished())); +} + +void DownloadManager::onDownloadProgress(qint64 bytesRead, qint64 bytesTotal) +{ + qDebug(QString::number(bytesRead).toLatin1() + " - " + + QString::number(bytesTotal).toLatin1()); +} + +void DownloadManager::onFinished(QNetworkReply *reply) +{ + switch (reply->error()) { + case QNetworkReply::NoError: { + qDebug("file is downloaded successfully."); + } break; + default: { + qDebug(reply->errorString().toLatin1()); + }; + } + + if (file->isOpen()) { + file->close(); + file->deleteLater(); + } + emit downloadComplete(); +} + +void DownloadManager::onReadyRead() +{ + file->write(reply->readAll()); +} + +void DownloadManager::onReplyFinished() +{ + if (file->isOpen()) { + file->close(); + file->deleteLater(); + } +} +} // namespace chatterino diff --git a/src/common/DownloadManager.hpp b/src/common/DownloadManager.hpp new file mode 100644 index 00000000..ac49a927 --- /dev/null +++ b/src/common/DownloadManager.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "Application.hpp" + +#include +#include +#include +#include +#include +#include + +namespace chatterino { + +class DownloadManager : public QObject +{ + Q_OBJECT +public: + explicit DownloadManager(QObject *parent = 0); + virtual ~DownloadManager(); + void setFile(QString fileURL, const QString &channelName); + +private: + QNetworkAccessManager *manager; + QNetworkReply *reply; + QFile *file; + +private slots: + void onDownloadProgress(qint64, qint64); + void onFinished(QNetworkReply *); + void onReadyRead(); + void onReplyFinished(); + +signals: + void downloadComplete(); +}; +} // namespace chatterino diff --git a/src/singletons/Settings.hpp b/src/singletons/Settings.hpp index 1dc0b919..9a48d2bb 100644 --- a/src/singletons/Settings.hpp +++ b/src/singletons/Settings.hpp @@ -146,6 +146,8 @@ public: false}; BoolSetting notificationToast = {"/notifications/enableToast", false}; BoolSetting notificationDot = {"/notifications/enableDot", false}; + BoolSetting notificationSplitheaderHighlight = { + "/notifications/enableSplitheaderHighlight", false}; /// External tools // Streamlink diff --git a/src/singletons/Toasts.cpp b/src/singletons/Toasts.cpp index bf654285..c6c1fac9 100644 --- a/src/singletons/Toasts.cpp +++ b/src/singletons/Toasts.cpp @@ -1,6 +1,7 @@ #include "Toasts.hpp" #include "Application.hpp" +#include "common/DownloadManager.hpp" #include "common/NetworkRequest.hpp" #include "controllers/notifications/NotificationController.hpp" #include "providers/twitch/TwitchChannel.hpp" @@ -15,7 +16,6 @@ #include #include -//#include #include #include #include @@ -32,15 +32,38 @@ bool Toasts::isEnabled() void Toasts::sendChannelNotification(const QString &channelName, Platform p) { + // Fetch user profile avatar + if (p == Platform::Twitch) { + QFileInfo check_file( + QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + + "2/cache/profileAvatars/twitch/" + channelName + ".png"); + if (check_file.exists() && check_file.isFile()) { #ifdef Q_OS_WIN - - sendWindowsNotification(channelName, p); - return; - + this->sendWindowsNotification(channelName, p); #endif - // OSX + // OSX - // LINUX + // LINUX + + } else { + this->fetchChannelAvatar( + channelName, [this, channelName, p](QString avatarLink) { + DownloadManager *manager = new DownloadManager(); + manager->setFile(avatarLink, channelName); + manager->connect( + manager, &DownloadManager::downloadComplete, + [this, channelName, p]() { +#ifdef Q_OS_WIN + this->sendWindowsNotification(channelName, p); +#endif + // OSX + + // LINUX + }); + }); + } + } + return; } #ifdef Q_OS_WIN @@ -84,29 +107,6 @@ public: }; void Toasts::sendWindowsNotification(const QString &channelName, Platform p) -{ - // Fetch user profile avatar - if (p == Platform::Twitch) { - QFileInfo check_file( - QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + - "2/cache/profileAvatars/twitch/" + channelName + ".png"); - if (check_file.exists() && check_file.isFile()) { - qDebug() << " OMEGA2NAM "; - this->sendActualWindowsNotification(channelName, p); - } else { - qDebug() << " OMEGA1NAM "; - this->fetchChannelAvatar( - channelName, [this, channelName, p](QString avatarLink) { - qDebug() << " OMEGANAM " << avatarLink; - this->sendActualWindowsNotification(channelName, p); - }); - } - } - qDebug() << " YOU'RE TOO SLOW! "; -} - -void Toasts::sendActualWindowsNotification(const QString &channelName, - Platform p) { WinToastLib::WinToastTemplate templ = WinToastLib::WinToastTemplate( WinToastLib::WinToastTemplate::ImageAndText02); @@ -145,51 +145,7 @@ void Toasts::sendActualWindowsNotification(const QString &channelName, } #endif -/* -void Toasts::fetchChannelAvatar(const QString &channelName, - std::function successCallback) -{ - QString requestUrl("https://api.twitch.tv/kraken/users?login=" + - channelName); - NetworkRequest request(requestUrl, NetworkRequestType::Put); - request.setCaller(QThread::currentThread()); - request.makeAuthorizedV5(getDefaultClientID()); - request.setTimeout(30000); - request.onSuccess([successCallback](auto result) -> Outcome { - auto root = result.parseJson(); - if (!root.value("users").isArray()) { - Log("API Error while getting user id, users is not an array"); - successCallback(""); - return Failure; - } - auto users = root.value("users").toArray(); - if (users.size() != 1) { - Log("API Error while getting user id, users array size is not 1"); - successCallback(""); - return Failure; - } - if (!users[0].isObject()) { - Log("API Error while getting user id, first user is not an object"); - successCallback(""); - return Failure; - } - auto firstUser = users[0].toObject(); - auto avatar = firstUser.value("logo"); - if (!avatar.isString()) { - Log("API Error: while getting user logo, first user object `logo` " - "key " - "is not a " - "string"); - successCallback(""); - return Failure; - } - successCallback(avatar.toString()); - return Success; - }); - request.execute(); -} -*/ void Toasts::fetchChannelAvatar(const QString channelName, std::function successCallback) { diff --git a/src/singletons/Toasts.hpp b/src/singletons/Toasts.hpp index c51dd391..4ffb8d74 100644 --- a/src/singletons/Toasts.hpp +++ b/src/singletons/Toasts.hpp @@ -2,13 +2,7 @@ #include "Application.hpp" #include "common/Singleton.hpp" -/* -#ifdef Q_OS_WIN -#include "wintoastlib.h" - -#endif -*/ namespace chatterino { enum class Platform : uint8_t; @@ -22,7 +16,6 @@ public: private: void sendWindowsNotification(const QString &channelName, Platform p); - void sendActualWindowsNotification(const QString &channelName, Platform p); static void fetchChannelAvatar( const QString channelName, std::function successCallback); diff --git a/src/widgets/settingspages/NotificationPage.cpp b/src/widgets/settingspages/NotificationPage.cpp index 54e342c4..e856679f 100644 --- a/src/widgets/settingspages/NotificationPage.cpp +++ b/src/widgets/settingspages/NotificationPage.cpp @@ -41,6 +41,9 @@ NotificationPage::NotificationPage() settings.append( this->createCheckBox("Red dot next to live splits", getApp()->settings->notificationDot)); + settings.append(this->createCheckBox( + "Change color of Splitheader (click to remove)", + getApp()->settings->notificationSplitheaderHighlight)); settings->addStretch(1); } diff --git a/src/widgets/splits/SplitHeader.cpp b/src/widgets/splits/SplitHeader.cpp index e97f4faa..348b3f95 100644 --- a/src/widgets/splits/SplitHeader.cpp +++ b/src/widgets/splits/SplitHeader.cpp @@ -549,7 +549,21 @@ void SplitHeader::themeChangedEvent() getApp()->resources->buttons.menuLight); } - this->titleLabel->setPalette(palette); + // this->titleLabel->setPalette(palette); + auto darkPalette = palette; + darkPalette.setColor(QPalette::Window, QColor(53, 53, 53)); + darkPalette.setColor(QPalette::WindowText, Qt::white); + darkPalette.setColor(QPalette::Base, QColor(25, 25, 25)); + darkPalette.setColor(QPalette::AlternateBase, QColor(53, 53, 53)); + darkPalette.setColor(QPalette::ToolTipBase, Qt::white); + darkPalette.setColor(QPalette::ToolTipText, Qt::white); + darkPalette.setColor(QPalette::Text, Qt::white); + darkPalette.setColor(QPalette::Button, QColor(53, 53, 53)); + darkPalette.setColor(QPalette::ButtonText, Qt::white); + darkPalette.setColor(QPalette::BrightText, Qt::red); + darkPalette.setColor(QPalette::Link, QColor(42, 130, 218)); + + this->titleLabel->setPalette(darkPalette); } void SplitHeader::menuMoveSplit()