From 1664eb672f0bc13910f9c7fdffdce57b910f163b Mon Sep 17 00:00:00 2001 From: Przemek Pawlas <3606072+Destroy666x@users.noreply.github.com> Date: Wed, 8 Nov 2023 15:00:42 +0100 Subject: [PATCH] Verify if channel given channel name is valid --- .../twitch/channel-selection.cpp | 40 +++++++++++++++++-- .../twitch/channel-selection.hpp | 9 ++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/macro-external/twitch/channel-selection.cpp b/src/macro-external/twitch/channel-selection.cpp index 042d5af1..fc981516 100644 --- a/src/macro-external/twitch/channel-selection.cpp +++ b/src/macro-external/twitch/channel-selection.cpp @@ -26,7 +26,7 @@ void TwitchChannel::Save(obs_data_t *obj) const obs_data_set_obj(obj, "channel", data); } -std::string TwitchChannel::GetUserID(const TwitchToken &token) +std::string TwitchChannel::GetUserID(const TwitchToken &token) const { static std::map userIDCache; auto it = userIDCache.find(std::string(_name)); @@ -179,13 +179,16 @@ bool TwitchChannel::IsValid(const std::string &id) const TwitchChannelSelection::TwitchChannelSelection(QWidget *parent) : QWidget(parent), _channelName(new VariableLineEdit(this)), - _openChannel(new QPushButton( - obs_module_text("AdvSceneSwitcher.channel.open"))) + _openChannel(new QPushButton(obs_module_text( + "AdvSceneSwitcher.twitch.selection.channel.open"))) { QWidget::connect(_channelName, SIGNAL(editingFinished()), this, SLOT(SelectionChanged())); QWidget::connect(_openChannel, SIGNAL(pressed()), this, SLOT(OpenChannel())); + QWidget::connect(this, SIGNAL(ChannelChanged(const TwitchChannel &)), + this, + SLOT(SetOpenChannelState(const TwitchChannel &))); auto layout = new QHBoxLayout(); layout->setContentsMargins(0, 0, 0, 0); @@ -197,6 +200,13 @@ TwitchChannelSelection::TwitchChannelSelection(QWidget *parent) void TwitchChannelSelection::SetChannel(const TwitchChannel &channel) { _channelName->setText(channel._name); + + emit ChannelChanged(channel); +} + +void TwitchChannelSelection::SetToken(const std::weak_ptr &token) +{ + _token = token; } void TwitchChannelSelection::OpenChannel() @@ -207,10 +217,34 @@ void TwitchChannelSelection::OpenChannel() QString::fromStdString(temp)); } +void TwitchChannelSelection::SetOpenChannelState(const TwitchChannel &channel) +{ + auto token = _token.lock(); + + if (!token) { + _openChannel->setToolTip(obs_module_text( + "AdvSceneSwitcher.twitch.selection.channel.open.tooltip.noAccount")); + _openChannel->setDisabled(false); + return; + } + + if (channel.GetUserID(*token) != "invalid") { + _openChannel->setToolTip(obs_module_text( + "AdvSceneSwitcher.twitch.selection.channel.open.tooltip.details")); + _openChannel->setDisabled(false); + return; + } + + _openChannel->setToolTip(obs_module_text( + "AdvSceneSwitcher.twitch.selection.channel.open.tooltip.noChannel")); + _openChannel->setDisabled(true); +} + void TwitchChannelSelection::SelectionChanged() { TwitchChannel channel; channel._name = _channelName->text().toStdString(); + emit ChannelChanged(channel); } diff --git a/src/macro-external/twitch/channel-selection.hpp b/src/macro-external/twitch/channel-selection.hpp index 52b5a371..5c26121d 100644 --- a/src/macro-external/twitch/channel-selection.hpp +++ b/src/macro-external/twitch/channel-selection.hpp @@ -48,7 +48,8 @@ struct ChannelInfo { struct TwitchChannel { void Load(obs_data_t *obj); void Save(obs_data_t *obj) const; - std::string GetUserID(const TwitchToken &); + StringVariable GetName() const { return _name; }; + std::string GetUserID(const TwitchToken &token) const; bool IsValid(const std::string &id) const; std::optional GetLiveInfo(const TwitchToken &); std::optional GetInfo(const TwitchToken &); @@ -64,11 +65,13 @@ class TwitchChannelSelection : public QWidget { public: TwitchChannelSelection(QWidget *parent); - void SetChannel(const TwitchChannel &); + void SetChannel(const TwitchChannel &channel); + void SetToken(const std::weak_ptr &token); private slots: void SelectionChanged(); void OpenChannel(); + void SetOpenChannelState(const TwitchChannel &channel); signals: void ChannelChanged(const TwitchChannel &); @@ -76,6 +79,8 @@ signals: private: VariableLineEdit *_channelName; QPushButton *_openChannel; + + std::weak_ptr _token; }; } // namespace advss