2018-08-11 12:47:03 +02:00
|
|
|
#include "Toasts.hpp"
|
|
|
|
|
|
|
|
#include "Application.hpp"
|
2018-08-19 15:09:00 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-08-11 12:47:03 +02:00
|
|
|
#include "controllers/notifications/NotificationController.hpp"
|
|
|
|
#include "providers/twitch/TwitchChannel.hpp"
|
2018-08-19 15:09:00 +02:00
|
|
|
#include "providers/twitch/TwitchCommon.hpp"
|
2018-08-11 12:47:03 +02:00
|
|
|
#include "providers/twitch/TwitchServer.hpp"
|
|
|
|
|
2018-08-12 18:54:32 +02:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
2018-08-11 16:11:51 +02:00
|
|
|
#include <wintoastlib.h>
|
|
|
|
|
2018-08-12 18:54:32 +02:00
|
|
|
#endif
|
|
|
|
|
2018-08-11 16:11:51 +02:00
|
|
|
#include <QDesktopServices>
|
2018-08-19 15:09:00 +02:00
|
|
|
#include <QFileInfo>
|
|
|
|
//#include <QtNetwork>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
2018-08-11 16:11:51 +02:00
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2018-08-11 12:47:03 +02:00
|
|
|
namespace chatterino {
|
|
|
|
|
2018-08-12 18:54:32 +02:00
|
|
|
bool Toasts::isEnabled()
|
|
|
|
{
|
|
|
|
return WinToastLib::WinToast::isCompatible() &&
|
|
|
|
getApp()->settings->notificationToast;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Toasts::sendChannelNotification(const QString &channelName, Platform p)
|
2018-08-11 12:47:03 +02:00
|
|
|
{
|
2018-08-11 16:11:51 +02:00
|
|
|
#ifdef Q_OS_WIN
|
2018-08-12 18:54:32 +02:00
|
|
|
|
|
|
|
sendWindowsNotification(channelName, p);
|
|
|
|
return;
|
|
|
|
|
2018-08-11 16:11:51 +02:00
|
|
|
#endif
|
|
|
|
// OSX
|
|
|
|
|
|
|
|
// LINUX
|
2018-08-11 12:47:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-11 16:11:51 +02:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
2018-08-19 15:09:00 +02:00
|
|
|
/*const override void CustomToastHandler::toastActivated()
|
|
|
|
{
|
|
|
|
}*/
|
|
|
|
|
2018-08-11 16:11:51 +02:00
|
|
|
class CustomHandler : public WinToastLib::IWinToastHandler
|
|
|
|
{
|
2018-08-19 15:09:00 +02:00
|
|
|
private:
|
|
|
|
QString channelName_;
|
|
|
|
Platform platform_;
|
|
|
|
|
2018-08-11 16:11:51 +02:00
|
|
|
public:
|
2018-08-19 15:09:00 +02:00
|
|
|
CustomHandler(QString channelName, Platform p)
|
|
|
|
: channelName_(channelName)
|
|
|
|
, platform_(p)
|
|
|
|
{
|
|
|
|
}
|
2018-08-11 16:11:51 +02:00
|
|
|
void toastActivated() const
|
|
|
|
{
|
2018-08-19 15:09:00 +02:00
|
|
|
QString link;
|
|
|
|
if (platform_ == Platform::Twitch) {
|
|
|
|
link = "http://www.twitch.tv/" + channelName_;
|
|
|
|
}
|
2018-08-11 16:11:51 +02:00
|
|
|
QDesktopServices::openUrl(QUrl(link));
|
|
|
|
}
|
|
|
|
|
|
|
|
void toastActivated(int actionIndex) const
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void toastFailed() const
|
|
|
|
{
|
|
|
|
}
|
2018-08-19 15:09:00 +02:00
|
|
|
|
2018-08-11 16:11:51 +02:00
|
|
|
void toastDismissed(WinToastDismissalReason state) const
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-12 18:54:32 +02:00
|
|
|
void Toasts::sendWindowsNotification(const QString &channelName, Platform p)
|
2018-08-19 15:09:00 +02:00
|
|
|
{
|
|
|
|
// 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)
|
2018-08-11 16:11:51 +02:00
|
|
|
{
|
|
|
|
WinToastLib::WinToastTemplate templ = WinToastLib::WinToastTemplate(
|
|
|
|
WinToastLib::WinToastTemplate::ImageAndText02);
|
2018-08-19 15:09:00 +02:00
|
|
|
QString str = channelName + " is live!";
|
2018-08-11 16:11:51 +02:00
|
|
|
std::string utf8_text = str.toUtf8().constData();
|
|
|
|
std::wstring widestr = std::wstring(utf8_text.begin(), utf8_text.end());
|
|
|
|
|
|
|
|
templ.setTextField(widestr, WinToastLib::WinToastTemplate::FirstLine);
|
|
|
|
templ.setTextField(L"Click here to open in browser",
|
|
|
|
WinToastLib::WinToastTemplate::SecondLine);
|
2018-08-19 15:09:00 +02:00
|
|
|
QString Path;
|
|
|
|
if (p == Platform::Twitch) {
|
|
|
|
Path =
|
|
|
|
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) +
|
|
|
|
"2/cache/profileAvatars/twitch/" + channelName + ".png";
|
|
|
|
}
|
|
|
|
std::string temp_Utf8 = Path.toUtf8().constData();
|
|
|
|
std::wstring imagePath = std::wstring(temp_Utf8.begin(), temp_Utf8.end());
|
|
|
|
templ.setImagePath(imagePath);
|
2018-08-12 20:21:21 +02:00
|
|
|
if (getApp()->settings->notificationPlaySound) {
|
|
|
|
templ.setAudioOption(
|
|
|
|
WinToastLib::WinToastTemplate::AudioOption::Silent);
|
|
|
|
}
|
2018-08-11 16:11:51 +02:00
|
|
|
WinToastLib::WinToast::instance()->setAppName(L"Chatterino2");
|
|
|
|
int mbstowcs(wchar_t * aumi_version, const char *CHATTERINO_VERSION,
|
|
|
|
size_t size);
|
|
|
|
std::string(CHATTERINO_VERSION);
|
|
|
|
std::wstring aumi_version =
|
|
|
|
std::wstring(CHATTERINO_VERSION.begin(), CHATTERINO_VERSION.end());
|
|
|
|
WinToastLib::WinToast::instance()->setAppUserModelId(
|
|
|
|
WinToastLib::WinToast::configureAUMI(L"", L"Chatterino 2", L"",
|
|
|
|
aumi_version));
|
|
|
|
WinToastLib::WinToast::instance()->initialize();
|
2018-08-19 15:09:00 +02:00
|
|
|
WinToastLib::WinToast::instance()->showToast(
|
|
|
|
templ, new CustomHandler(channelName, p));
|
2018-08-11 16:11:51 +02:00
|
|
|
}
|
2018-08-19 15:09:00 +02:00
|
|
|
|
2018-08-11 16:11:51 +02:00
|
|
|
#endif
|
2018-08-19 15:09:00 +02:00
|
|
|
/*
|
|
|
|
void Toasts::fetchChannelAvatar(const QString &channelName,
|
|
|
|
std::function<void(QString)> 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<void(QString)> successCallback)
|
|
|
|
{
|
|
|
|
QString requestUrl("https://api.twitch.tv/kraken/users?login=" +
|
|
|
|
channelName);
|
|
|
|
|
|
|
|
NetworkRequest request(requestUrl);
|
|
|
|
request.setCaller(QThread::currentThread());
|
|
|
|
request.makeAuthorizedV5(getDefaultClientID());
|
|
|
|
request.setTimeout(30000);
|
|
|
|
request.onSuccess([successCallback](auto result) mutable -> 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 avatar, first user object "
|
|
|
|
"`avatar` key "
|
|
|
|
"is not a "
|
|
|
|
"string");
|
|
|
|
successCallback("");
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
successCallback(avatar.toString());
|
|
|
|
return Success;
|
|
|
|
});
|
2018-08-11 12:47:03 +02:00
|
|
|
|
2018-08-19 15:09:00 +02:00
|
|
|
request.execute();
|
|
|
|
}
|
2018-08-11 12:47:03 +02:00
|
|
|
} // namespace chatterino
|