From be3c85d72d75c2ed21dedd44f0d311f442ce8a2c Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Tue, 19 Dec 2017 16:13:02 +0100 Subject: [PATCH] Move some variables into const.hpp Clean up some code in the AccountManager --- chatterino.pro | 3 ++- src/accountmanager.cpp | 39 ++++++++++++++++------------- src/accountmanager.hpp | 2 ++ src/const.hpp | 15 +++++++++++ src/twitch/twitchuser.cpp | 3 ++- src/widgets/accountswitchwidget.cpp | 9 +++---- src/widgets/settingsdialog.cpp | 3 ++- 7 files changed, 49 insertions(+), 25 deletions(-) create mode 100644 src/const.hpp diff --git a/chatterino.pro b/chatterino.pro index 88f4f474..9e64e994 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -175,7 +175,8 @@ HEADERS += \ src/util/irchelpers.hpp \ src/util/helpers.hpp \ src/widgets/accountswitchwidget.hpp \ - src/widgets/accountswitchpopupwidget.hpp + src/widgets/accountswitchpopupwidget.hpp \ + src/const.hpp PRECOMPILED_HEADER = diff --git a/src/accountmanager.cpp b/src/accountmanager.cpp index fbb5b7c4..0ad12bee 100644 --- a/src/accountmanager.cpp +++ b/src/accountmanager.cpp @@ -1,5 +1,6 @@ #include "accountmanager.hpp" #include "common.hpp" +#include "const.hpp" #include "debug/log.hpp" namespace chatterino { @@ -18,6 +19,27 @@ inline QString getEnvString(const char *target) } // namespace +TwitchAccountManager::TwitchAccountManager() +{ + this->anonymousUser.reset(new twitch::TwitchUser(twitch::ANONYMOUS_USERNAME, "", "")); + + this->currentUsername.connect([this](const auto &newValue, auto) { + QString newUsername(QString::fromStdString(newValue)); + auto user = this->findUserByUsername(newUsername); + if (user) { + debug::Log("[AccountManager:currentUsernameChanged] User successfully updated to {}", + newUsername); + this->currentUser = user; + } else { + debug::Log( + "[AccountManager:currentUsernameChanged] User successfully updated to anonymous"); + this->currentUser = this->anonymousUser; + } + + this->userChanged.invoke(); + }); +} + std::shared_ptr TwitchAccountManager::getCurrent() { if (!this->currentUser) { @@ -180,23 +202,6 @@ TwitchAccountManager::AddUserResponse TwitchAccountManager::addUser( AccountManager::AccountManager() { - this->Twitch.anonymousUser.reset(new twitch::TwitchUser("justinfan64537", "", "")); - - this->Twitch.currentUsername.connect([this](const auto &newValue, auto) { - QString newUsername(QString::fromStdString(newValue)); - auto user = this->Twitch.findUserByUsername(newUsername); - if (user) { - debug::Log("[AccountManager:currentUsernameChanged] User successfully updated to {}", - newUsername); - this->Twitch.currentUser = user; - } else { - debug::Log( - "[AccountManager:currentUsernameChanged] User successfully updated to anonymous"); - this->Twitch.currentUser = this->Twitch.anonymousUser; - } - - this->Twitch.userChanged.invoke(); - }); } void AccountManager::load() diff --git a/src/accountmanager.hpp b/src/accountmanager.hpp index 0e98d134..14640bf6 100644 --- a/src/accountmanager.hpp +++ b/src/accountmanager.hpp @@ -14,6 +14,8 @@ class AccountManager; class TwitchAccountManager { public: + TwitchAccountManager(); + struct UserData { QString username; QString userID; diff --git a/src/const.hpp b/src/const.hpp new file mode 100644 index 00000000..a1c7a273 --- /dev/null +++ b/src/const.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace chatterino { + +static const QString ANONYMOUS_USERNAME_LABEL(" - anonymous - "); + +namespace twitch { + +static const QString ANONYMOUS_USERNAME("justinfan64537"); + +} // namespace twitch + +} // namespace chatterino diff --git a/src/twitch/twitchuser.cpp b/src/twitch/twitchuser.cpp index 23024648..6d6b75cd 100644 --- a/src/twitch/twitchuser.cpp +++ b/src/twitch/twitchuser.cpp @@ -1,4 +1,5 @@ #include "twitchuser.hpp" +#include "const.hpp" #include "util/urlfetch.hpp" namespace chatterino { @@ -9,7 +10,7 @@ TwitchUser::TwitchUser(const QString &username, const QString &oauthToken, : IrcUser2(username, username, username, "oauth:" + oauthToken) , _oauthClient(oauthClient) , _oauthToken(oauthToken) - , _isAnon(username.startsWith("justinfan")) + , _isAnon(username == ANONYMOUS_USERNAME) { } diff --git a/src/widgets/accountswitchwidget.cpp b/src/widgets/accountswitchwidget.cpp index d34793e8..5ffc5ff3 100644 --- a/src/widgets/accountswitchwidget.cpp +++ b/src/widgets/accountswitchwidget.cpp @@ -1,5 +1,6 @@ #include "accountswitchwidget.hpp" #include "accountmanager.hpp" +#include "const.hpp" namespace chatterino { namespace widgets { @@ -7,9 +8,7 @@ namespace widgets { AccountSwitchWidget::AccountSwitchWidget(QWidget *parent) : QListWidget(parent) { - static QString anonUsername(" - anonymous - "); - - this->addItem(anonUsername); + this->addItem(ANONYMOUS_USERNAME_LABEL); for (const auto &userName : AccountManager::getInstance().Twitch.getUsernames()) { this->addItem(userName); @@ -20,7 +19,7 @@ AccountSwitchWidget::AccountSwitchWidget(QWidget *parent) this->clear(); - this->addItem(anonUsername); + this->addItem(ANONYMOUS_USERNAME_LABEL); for (const auto &userName : AccountManager::getInstance().Twitch.getUsernames()) { this->addItem(userName); @@ -36,7 +35,7 @@ AccountSwitchWidget::AccountSwitchWidget(QWidget *parent) QObject::connect(this, &QListWidget::clicked, [this] { if (!this->selectedItems().isEmpty()) { QString newUsername = this->currentItem()->text(); - if (newUsername.compare(anonUsername, Qt::CaseInsensitive) == 0) { + if (newUsername.compare(ANONYMOUS_USERNAME_LABEL, Qt::CaseInsensitive) == 0) { AccountManager::getInstance().Twitch.currentUsername = ""; } else { AccountManager::getInstance().Twitch.currentUsername = newUsername.toStdString(); diff --git a/src/widgets/settingsdialog.cpp b/src/widgets/settingsdialog.cpp index 289d9527..4309a704 100644 --- a/src/widgets/settingsdialog.cpp +++ b/src/widgets/settingsdialog.cpp @@ -1,5 +1,6 @@ #include "widgets/settingsdialog.hpp" #include "accountmanager.hpp" +#include "const.hpp" #include "debug/log.hpp" #include "twitch/twitchmessagebuilder.hpp" #include "twitch/twitchuser.hpp" @@ -130,7 +131,7 @@ QVBoxLayout *SettingsDialog::createAccountsTab() connect(removeButton, &QPushButton::clicked, [this]() { qDebug() << "TODO: Implement"; // auto selectedUser = this->ui.accountSwitchWidget->currentItem()->text(); - if (selectedUser == " - anonymous - ") { + if (selectedUser == ANONYMOUS_USERNAME_LABEL) { // Do nothing return; }