mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Refactored opening from toasts
implemented maps and map look up functions
This commit is contained in:
parent
2be81aa877
commit
c20629c165
4 changed files with 85 additions and 27 deletions
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
#include "controllers/highlights/HighlightPhrase.hpp"
|
#include "controllers/highlights/HighlightPhrase.hpp"
|
||||||
#include "controllers/moderationactions/ModerationAction.hpp"
|
#include "controllers/moderationactions/ModerationAction.hpp"
|
||||||
#include "widgets/helper/CommonTexts.hpp"
|
#include "singletons/Toasts.hpp"
|
||||||
|
|
||||||
#include <pajlada/settings/setting.hpp>
|
#include <pajlada/settings/setting.hpp>
|
||||||
#include <pajlada/settings/settinglistener.hpp>
|
#include <pajlada/settings/settinglistener.hpp>
|
||||||
|
@ -173,8 +173,9 @@ public:
|
||||||
"qrc:/sounds/ping3.wav"};
|
"qrc:/sounds/ping3.wav"};
|
||||||
|
|
||||||
BoolSetting notificationToast = {"/notifications/enableToast", false};
|
BoolSetting notificationToast = {"/notifications/enableToast", false};
|
||||||
QStringSetting openFromToast = {"/notifications/openFromToast",
|
QStringSetting openFromToast = {
|
||||||
OPEN_IN_BROWSER};
|
"/notifications/openFromToast",
|
||||||
|
Toasts::findStringFromReaction(ToastReactions::openInBrowser)};
|
||||||
|
|
||||||
/// External tools
|
/// External tools
|
||||||
// Streamlink
|
// Streamlink
|
||||||
|
|
|
@ -27,6 +27,18 @@
|
||||||
|
|
||||||
namespace chatterino {
|
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()
|
bool Toasts::isEnabled()
|
||||||
{
|
{
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
|
@ -36,6 +48,31 @@ bool Toasts::isEnabled()
|
||||||
return false;
|
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)
|
void Toasts::sendChannelNotification(const QString &channelName, Platform p)
|
||||||
{
|
{
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
|
@ -87,30 +124,31 @@ public:
|
||||||
}
|
}
|
||||||
void toastActivated() const
|
void toastActivated() const
|
||||||
{
|
{
|
||||||
QString openingMode = getSettings()->openFromToast;
|
|
||||||
QString link;
|
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
|
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());
|
std::wstring widestr = std::wstring(utf8_text.begin(), utf8_text.end());
|
||||||
|
|
||||||
templ.setTextField(widestr, WinToastLib::WinToastTemplate::FirstLine);
|
templ.setTextField(widestr, WinToastLib::WinToastTemplate::FirstLine);
|
||||||
|
if (Toasts::findReactionFromString(getSettings()->openFromToast) !=
|
||||||
if (getSettings()->openFromToast != DONT_OPEN)
|
ToastReactions::dontOpen)
|
||||||
{
|
{
|
||||||
QString mode = getSettings()->openFromToast;
|
QString mode = getSettings()->openFromToast;
|
||||||
mode = mode.toLower();
|
mode = mode.toLower();
|
||||||
|
|
|
@ -7,10 +7,21 @@ namespace chatterino {
|
||||||
|
|
||||||
enum class Platform : uint8_t;
|
enum class Platform : uint8_t;
|
||||||
|
|
||||||
|
enum class ToastReactions {
|
||||||
|
openInBrowser,
|
||||||
|
openInPlayer,
|
||||||
|
openInStreamlink,
|
||||||
|
dontOpen
|
||||||
|
};
|
||||||
|
|
||||||
class Toasts final : public Singleton
|
class Toasts final : public Singleton
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void sendChannelNotification(const QString &channelName, Platform p);
|
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();
|
static bool isEnabled();
|
||||||
|
|
||||||
|
@ -18,6 +29,7 @@ private:
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
void sendWindowsNotification(const QString &channelName, Platform p);
|
void sendWindowsNotification(const QString &channelName, Platform p);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void fetchChannelAvatar(
|
static void fetchChannelAvatar(
|
||||||
const QString channelName,
|
const QString channelName,
|
||||||
std::function<void(QString)> successCallback);
|
std::function<void(QString)> successCallback);
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
#include "controllers/notifications/NotificationController.hpp"
|
#include "controllers/notifications/NotificationController.hpp"
|
||||||
#include "controllers/notifications/NotificationModel.hpp"
|
#include "controllers/notifications/NotificationModel.hpp"
|
||||||
#include "singletons/Settings.hpp"
|
#include "singletons/Settings.hpp"
|
||||||
|
#include "singletons/Toasts.hpp"
|
||||||
#include "util/LayoutCreator.hpp"
|
#include "util/LayoutCreator.hpp"
|
||||||
#include "widgets/helper/CommonTexts.hpp"
|
|
||||||
#include "widgets/helper/EditableModelView.hpp"
|
#include "widgets/helper/EditableModelView.hpp"
|
||||||
|
|
||||||
#include <QCheckBox>
|
#include <QCheckBox>
|
||||||
|
@ -45,10 +45,17 @@ NotificationPage::NotificationPage()
|
||||||
openIn.emplace<QLabel>("Open stream from Toast: ")
|
openIn.emplace<QLabel>("Open stream from Toast: ")
|
||||||
->setSizePolicy(QSizePolicy::Maximum,
|
->setSizePolicy(QSizePolicy::Maximum,
|
||||||
QSizePolicy::Preferred);
|
QSizePolicy::Preferred);
|
||||||
|
|
||||||
openIn
|
openIn
|
||||||
.append(this->createComboBox(
|
.append(this->createComboBox(
|
||||||
{OPEN_IN_BROWSER, OPEN_PLAYER_IN_BROWSER,
|
{Toasts::findStringFromReaction(
|
||||||
OPEN_IN_STREAMLINK, DONT_OPEN},
|
ToastReactions::openInBrowser),
|
||||||
|
Toasts::findStringFromReaction(
|
||||||
|
ToastReactions::openInPlayer),
|
||||||
|
Toasts::findStringFromReaction(
|
||||||
|
ToastReactions::openInStreamlink),
|
||||||
|
Toasts::findStringFromReaction(
|
||||||
|
ToastReactions::dontOpen)},
|
||||||
getSettings()->openFromToast))
|
getSettings()->openFromToast))
|
||||||
->setSizePolicy(QSizePolicy::Maximum,
|
->setSizePolicy(QSizePolicy::Maximum,
|
||||||
QSizePolicy::Preferred);
|
QSizePolicy::Preferred);
|
||||||
|
|
Loading…
Reference in a new issue