Refactored opening from toasts

implemented maps and map look up functions
This commit is contained in:
TranRed 2019-04-27 13:47:13 +02:00 committed by pajlada
parent 2be81aa877
commit c20629c165
4 changed files with 85 additions and 27 deletions

View file

@ -4,7 +4,7 @@
#include "controllers/highlights/HighlightPhrase.hpp"
#include "controllers/moderationactions/ModerationAction.hpp"
#include "widgets/helper/CommonTexts.hpp"
#include "singletons/Toasts.hpp"
#include <pajlada/settings/setting.hpp>
#include <pajlada/settings/settinglistener.hpp>
@ -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

View file

@ -27,6 +27,18 @@
namespace chatterino {
std::map<ToastReactions, QString> Toasts::reactionToString = {
{ToastReactions::openInBrowser, OPEN_IN_BROWSER},
{ToastReactions::openInPlayer, OPEN_PLAYER_IN_BROWSER},
{ToastReactions::openInStreamlink, OPEN_IN_STREAMLINK},
{ToastReactions::dontOpen, DONT_OPEN}};
std::map<QString, ToastReactions> 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();

View file

@ -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<ToastReactions, QString> reactionToString;
static std::map<QString, ToastReactions> 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<void(QString)> successCallback);

View file

@ -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 <QCheckBox>
@ -45,10 +45,17 @@ NotificationPage::NotificationPage()
openIn.emplace<QLabel>("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);