From bf4c9cebb1b79d8f546c8c98246ff29516001bf5 Mon Sep 17 00:00:00 2001 From: fanway <23499543+fanway@users.noreply.github.com> Date: Sun, 6 Dec 2020 16:07:33 +0300 Subject: [PATCH] Use display/localized names in tab and split titles (#2189) --- CHANGELOG.md | 1 + src/Application.cpp | 2 +- src/Application.hpp | 2 +- src/common/Channel.cpp | 5 +++ src/common/Channel.hpp | 2 ++ src/providers/twitch/TwitchChannel.cpp | 48 ++++++++++++++++++++++++++ src/providers/twitch/TwitchChannel.hpp | 8 ++++- src/widgets/splits/Split.cpp | 4 +++ src/widgets/splits/SplitContainer.cpp | 2 +- src/widgets/splits/SplitHeader.cpp | 2 +- 10 files changed, 71 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32bceeea0..9f52032b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ - Minor: Allow highlights to be excluded from `/mentions`. Excluded highlights will not trigger tab highlights either. (#1793, #2036) - Minor: Flag all popup dialogs as actual dialogs so they get the relevant window manager hints (#1843, #2182, #2185, #2232, #2234) - Minor: Don't show update button for nightly builds on macOS and Linux, this was already the case for Windows (#2163, #2164) +- Minor: Tab and split titles now use display/localized channel names (#2189) - Minor: Add a setting to limit the amount of historical messages loaded from the Recent Messages API (#2250, #2252) - Bugfix: Fix crash occurring when pressing Escape in the Color Picker Dialog (#1843) - Bugfix: Fix bug where the "check user follow state" event could trigger a network request requesting the user to follow or unfollow a user. By itself its quite harmless as it just repeats to Twitch the same follow state we had, so no follows should have been lost by this but it meant there was a rogue network request that was fired that could cause a crash (#1906) diff --git a/src/Application.cpp b/src/Application.cpp index 93549c1e6..08bb0c998 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -49,10 +49,10 @@ Application::Application(Settings &_settings, Paths &_paths) : themes(&this->emplace()) , fonts(&this->emplace()) , emotes(&this->emplace()) + , accounts(&this->emplace()) , windows(&this->emplace()) , toasts(&this->emplace()) - , accounts(&this->emplace()) , commands(&this->emplace()) , notifications(&this->emplace()) , twitch2(&this->emplace()) diff --git a/src/Application.hpp b/src/Application.hpp index 4a9006667..9c575ae3a 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -50,10 +50,10 @@ public: Theme *const themes{}; Fonts *const fonts{}; Emotes *const emotes{}; + AccountController *const accounts{}; WindowManager *const windows{}; Toasts *const toasts{}; - AccountController *const accounts{}; CommandController *const commands{}; NotificationController *const notifications{}; TwitchIrcServer *const twitch2{}; diff --git a/src/common/Channel.cpp b/src/common/Channel.cpp index 455f0b705..c18271655 100644 --- a/src/common/Channel.cpp +++ b/src/common/Channel.cpp @@ -49,6 +49,11 @@ const QString &Channel::getDisplayName() const return this->getName(); } +const QString &Channel::getLocalizedName() const +{ + return this->getName(); +} + bool Channel::isTwitchChannel() const { return this->type_ >= Type::Twitch && this->type_ < Type::TwitchEnd; diff --git a/src/common/Channel.hpp b/src/common/Channel.hpp index 7b2e545c3..3bcdd9a26 100644 --- a/src/common/Channel.hpp +++ b/src/common/Channel.hpp @@ -53,10 +53,12 @@ public: pajlada::Signals::Signal &> messagesAddedAtStart; pajlada::Signals::Signal messageReplaced; pajlada::Signals::NoArgSignal destroyed; + pajlada::Signals::NoArgSignal displayNameChanged; Type getType() const; const QString &getName() const; virtual const QString &getDisplayName() const; + virtual const QString &getLocalizedName() const; bool isTwitchChannel() const; virtual bool isEmpty() const; LimitedQueueSnapshot getMessageSnapshot(); diff --git a/src/providers/twitch/TwitchChannel.cpp b/src/providers/twitch/TwitchChannel.cpp index 1f8a61bea..7bd0169fb 100644 --- a/src/providers/twitch/TwitchChannel.cpp +++ b/src/providers/twitch/TwitchChannel.cpp @@ -137,6 +137,7 @@ TwitchChannel::TwitchChannel(const QString &name, FfzEmotes &ffz) : Channel(name, Channel::Type::Twitch) , ChannelChatters(*static_cast(this)) + , nameOptions{name, name} , subscriptionUrl_("https://www.twitch.tv/subs/" + name) , channelUrl_("https://twitch.tv/" + name) , popoutPlayerUrl_("https://player.twitch.tv/?parent=twitch.tv&channel=" + @@ -202,6 +203,7 @@ TwitchChannel::TwitchChannel(const QString &name, void TwitchChannel::initialize() { + this->fetchDisplayName(); this->refreshChatters(); this->refreshBadges(); } @@ -216,6 +218,26 @@ bool TwitchChannel::canSendMessage() const return !this->isEmpty(); } +const QString &TwitchChannel::getDisplayName() const +{ + return this->nameOptions.displayName; +} + +void TwitchChannel::setDisplayName(const QString &name) +{ + this->nameOptions.displayName = name; +} + +const QString &TwitchChannel::getLocalizedName() const +{ + return this->nameOptions.localizedName; +} + +void TwitchChannel::setLocalizedName(const QString &name) +{ + this->nameOptions.localizedName = name; +} + void TwitchChannel::refreshBTTVChannelEmotes(bool manualRefresh) { BttvEmotes::loadChannel( @@ -761,6 +783,32 @@ void TwitchChannel::refreshChatters() .execute(); } +void TwitchChannel::fetchDisplayName() +{ + getHelix()->getUserByName( + this->getName(), + [weak = weakOf(this)](const auto &user) { + auto shared = weak.lock(); + if (!shared) + return; + auto channel = static_cast(shared.get()); + if (QString::compare(user.displayName, channel->getName(), + Qt::CaseInsensitive) == 0) + { + channel->setDisplayName(user.displayName); + channel->setLocalizedName(user.displayName); + } + else + { + channel->setLocalizedName(QString("%1(%2)") + .arg(channel->getName()) + .arg(user.displayName)); + } + channel->displayNameChanged.invoke(); + }, + [] {}); +} + void TwitchChannel::refreshBadges() { auto url = Url{"https://badges.twitch.tv/v1/badges/channels/" + diff --git a/src/providers/twitch/TwitchChannel.hpp b/src/providers/twitch/TwitchChannel.hpp index 86ecaad2d..9b79c141e 100644 --- a/src/providers/twitch/TwitchChannel.hpp +++ b/src/providers/twitch/TwitchChannel.hpp @@ -121,7 +121,7 @@ private: struct NameOptions { QString displayName; QString localizedName; - }; + } nameOptions; protected: explicit TwitchChannel(const QString &channelName, @@ -137,6 +137,7 @@ private: void refreshBadges(); void refreshCheerEmotes(); void loadRecentMessages(); + void fetchDisplayName(); void setLive(bool newLiveStatus); void setMod(bool value); @@ -144,6 +145,11 @@ private: void setStaff(bool value); void setRoomId(const QString &id); void setRoomModes(const RoomModes &roomModes_); + void setDisplayName(const QString &name); + void setLocalizedName(const QString &name); + + const QString &getDisplayName() const override; + const QString &getLocalizedName() const override; // Data const QString subscriptionUrl_; diff --git a/src/widgets/splits/Split.cpp b/src/widgets/splits/Split.cpp index 865bb0725..b3c962bae 100644 --- a/src/widgets/splits/Split.cpp +++ b/src/widgets/splits/Split.cpp @@ -381,6 +381,10 @@ void Split::setChannel(IndirectChannel newChannel) this->header_->setViewersButtonVisible(false); } + this->channel_.get()->displayNameChanged.connect([this] { + this->container_->refreshTab(); + }); + this->channelChanged.invoke(); // Queue up save because: Split channel changed diff --git a/src/widgets/splits/SplitContainer.cpp b/src/widgets/splits/SplitContainer.cpp index 7305298c9..91f4cca59 100644 --- a/src/widgets/splits/SplitContainer.cpp +++ b/src/widgets/splits/SplitContainer.cpp @@ -788,7 +788,7 @@ void SplitContainer::refreshTabTitle() for (const auto &chatWidget : this->splits_) { - auto channelName = chatWidget->getChannel()->getName(); + auto channelName = chatWidget->getChannel()->getLocalizedName(); if (channelName.isEmpty()) { continue; diff --git a/src/widgets/splits/SplitHeader.cpp b/src/widgets/splits/SplitHeader.cpp index 503787452..bd5330cbc 100644 --- a/src/widgets/splits/SplitHeader.cpp +++ b/src/widgets/splits/SplitHeader.cpp @@ -659,7 +659,7 @@ void SplitHeader::updateChannelText() this->isLive_ = false; this->tooltipText_ = QString(); - auto title = channel->getName(); + auto title = channel->getLocalizedName(); if (indirectChannel.getType() == Channel::Type::TwitchWatching) title = "watching: " + (title.isEmpty() ? "none" : title);