From c20629c16535b488ecda24e15316e6fbf114d19b Mon Sep 17 00:00:00 2001 From: TranRed Date: Sat, 27 Apr 2019 13:47:13 +0200 Subject: [PATCH] Refactored opening from toasts implemented maps and map look up functions --- src/singletons/Settings.hpp | 7 +- src/singletons/Toasts.cpp | 80 ++++++++++++++----- src/singletons/Toasts.hpp | 12 +++ .../settingspages/NotificationPage.cpp | 13 ++- 4 files changed, 85 insertions(+), 27 deletions(-) diff --git a/src/singletons/Settings.hpp b/src/singletons/Settings.hpp index 34252c8c7..6a17024d2 100644 --- a/src/singletons/Settings.hpp +++ b/src/singletons/Settings.hpp @@ -4,7 +4,7 @@ #include "controllers/highlights/HighlightPhrase.hpp" #include "controllers/moderationactions/ModerationAction.hpp" -#include "widgets/helper/CommonTexts.hpp" +#include "singletons/Toasts.hpp" #include #include @@ -173,8 +173,9 @@ public: "qrc:/sounds/ping3.wav"}; BoolSetting notificationToast = {"/notifications/enableToast", false}; - QStringSetting openFromToast = {"/notifications/openFromToast", - OPEN_IN_BROWSER}; + QStringSetting openFromToast = { + "/notifications/openFromToast", + Toasts::findStringFromReaction(ToastReactions::openInBrowser)}; /// External tools // Streamlink diff --git a/src/singletons/Toasts.cpp b/src/singletons/Toasts.cpp index e2f396e91..2352cccd0 100644 --- a/src/singletons/Toasts.cpp +++ b/src/singletons/Toasts.cpp @@ -27,6 +27,18 @@ namespace chatterino { +std::map Toasts::reactionToString = { + {ToastReactions::openInBrowser, OPEN_IN_BROWSER}, + {ToastReactions::openInPlayer, OPEN_PLAYER_IN_BROWSER}, + {ToastReactions::openInStreamlink, OPEN_IN_STREAMLINK}, + {ToastReactions::dontOpen, DONT_OPEN}}; + +std::map Toasts::stringToReaction = { + {OPEN_IN_BROWSER, ToastReactions::openInBrowser}, + {OPEN_PLAYER_IN_BROWSER, ToastReactions::openInPlayer}, + {OPEN_IN_STREAMLINK, ToastReactions::openInStreamlink}, + {DONT_OPEN, ToastReactions::dontOpen}}; + bool Toasts::isEnabled() { #ifdef Q_OS_WIN @@ -36,6 +48,31 @@ bool Toasts::isEnabled() return false; } +QString Toasts::findStringFromReaction(const ToastReactions &reaction) +{ + auto iterator = Toasts::reactionToString.find(reaction); + if (iterator != Toasts::reactionToString.end()) + { + return iterator->second; + } + else + { + return DONT_OPEN; + } +} + +ToastReactions Toasts::findReactionFromString(const QString &string) +{ + auto iterator = Toasts::stringToReaction.find(string); + if (iterator != Toasts::stringToReaction.end()) + { + return iterator->second; + } + else + { + return ToastReactions::dontOpen; + } +} void Toasts::sendChannelNotification(const QString &channelName, Platform p) { #ifdef Q_OS_WIN @@ -87,30 +124,31 @@ public: } void toastActivated() const { - QString openingMode = getSettings()->openFromToast; QString link; - if (openingMode == OPEN_IN_BROWSER) + switch (Toasts::findReactionFromString(getSettings()->openFromToast)) { - if (platform_ == Platform::Twitch) + case ToastReactions::openInBrowser: + if (platform_ == Platform::Twitch) + { + link = "http://www.twitch.tv/" + channelName_; + } + QDesktopServices::openUrl(QUrl(link)); + break; + case ToastReactions::openInPlayer: + if (platform_ == Platform::Twitch) + { + link = "https://player.twitch.tv/?channel=" + channelName_; + } + QDesktopServices::openUrl(QUrl(link)); + break; + case ToastReactions::openInStreamlink: { - link = "http://www.twitch.tv/" + channelName_; + openStreamlinkForChannel(channelName_); + break; } - QDesktopServices::openUrl(QUrl(link)); + // the fourth and last option is "don't open" + // in this case obviously nothing should happen } - else if (openingMode == OPEN_PLAYER_IN_BROWSER) - { - if (platform_ == Platform::Twitch) - { - link = "https://player.twitch.tv/?channel=" + channelName_; - } - QDesktopServices::openUrl(QUrl(link)); - } - else if (openingMode == OPEN_IN_STREAMLINK) - { - openStreamlinkForChannel(channelName_); - } - // the fourth and last option is "don't open" - // in this case obviously nothing should happen } void toastActivated(int actionIndex) const @@ -135,8 +173,8 @@ void Toasts::sendWindowsNotification(const QString &channelName, Platform p) std::wstring widestr = std::wstring(utf8_text.begin(), utf8_text.end()); templ.setTextField(widestr, WinToastLib::WinToastTemplate::FirstLine); - - if (getSettings()->openFromToast != DONT_OPEN) + if (Toasts::findReactionFromString(getSettings()->openFromToast) != + ToastReactions::dontOpen) { QString mode = getSettings()->openFromToast; mode = mode.toLower(); diff --git a/src/singletons/Toasts.hpp b/src/singletons/Toasts.hpp index 3a245346f..a7ea91861 100644 --- a/src/singletons/Toasts.hpp +++ b/src/singletons/Toasts.hpp @@ -7,10 +7,21 @@ namespace chatterino { enum class Platform : uint8_t; +enum class ToastReactions { + openInBrowser, + openInPlayer, + openInStreamlink, + dontOpen +}; + class Toasts final : public Singleton { public: void sendChannelNotification(const QString &channelName, Platform p); + static QString findStringFromReaction(const ToastReactions &reaction); + static ToastReactions findReactionFromString(const QString &string); + static std::map reactionToString; + static std::map stringToReaction; static bool isEnabled(); @@ -18,6 +29,7 @@ private: #ifdef Q_OS_WIN void sendWindowsNotification(const QString &channelName, Platform p); #endif + static void fetchChannelAvatar( const QString channelName, std::function successCallback); diff --git a/src/widgets/settingspages/NotificationPage.cpp b/src/widgets/settingspages/NotificationPage.cpp index 69a25ea7d..85aab7bb0 100644 --- a/src/widgets/settingspages/NotificationPage.cpp +++ b/src/widgets/settingspages/NotificationPage.cpp @@ -4,8 +4,8 @@ #include "controllers/notifications/NotificationController.hpp" #include "controllers/notifications/NotificationModel.hpp" #include "singletons/Settings.hpp" +#include "singletons/Toasts.hpp" #include "util/LayoutCreator.hpp" -#include "widgets/helper/CommonTexts.hpp" #include "widgets/helper/EditableModelView.hpp" #include @@ -45,10 +45,17 @@ NotificationPage::NotificationPage() openIn.emplace("Open stream from Toast: ") ->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); + openIn .append(this->createComboBox( - {OPEN_IN_BROWSER, OPEN_PLAYER_IN_BROWSER, - OPEN_IN_STREAMLINK, DONT_OPEN}, + {Toasts::findStringFromReaction( + ToastReactions::openInBrowser), + Toasts::findStringFromReaction( + ToastReactions::openInPlayer), + Toasts::findStringFromReaction( + ToastReactions::openInStreamlink), + Toasts::findStringFromReaction( + ToastReactions::dontOpen)}, getSettings()->openFromToast)) ->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);