mirror-chatterino2/src/widgets/AccountSwitchWidget.cpp
nerix 86e71c8bd9
Migrate to C++ 20 & switch to websocketpp develop branch (#4252)
* feat: c++ 20

* fix: c++ 20 deprecations

* fix(msvc): warnings

* chore: add changelog entry

* fix: formatting

* Update websocketpp to the `develop` branch

* Specify other template type in FlagsEnum != operator

* Remove the user of simple template ids in our websocketpp template class

Also standardizes the file a bit by using nested namespaces, using
pragma once

* fix: turn `MAGIC_MESSAGE_SUFFIX` into a `QString`

* hacky unhacky hacky const char hack

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
2022-12-24 11:56:11 +00:00

99 lines
2.3 KiB
C++

#include "AccountSwitchWidget.hpp"
#include "Application.hpp"
#include "common/Common.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchCommon.hpp"
namespace chatterino {
AccountSwitchWidget::AccountSwitchWidget(QWidget *parent)
: QListWidget(parent)
{
auto app = getApp();
this->addItem(ANONYMOUS_USERNAME_LABEL);
for (const auto &userName : app->accounts->twitch.getUsernames())
{
this->addItem(userName);
}
app->accounts->twitch.userListUpdated.connect([=, this]() {
this->blockSignals(true);
this->clear();
this->addItem(ANONYMOUS_USERNAME_LABEL);
for (const auto &userName : app->accounts->twitch.getUsernames())
{
this->addItem(userName);
}
this->refreshSelection();
this->blockSignals(false);
});
this->refreshSelection();
QObject::connect(this, &QListWidget::clicked, [=, this] {
if (!this->selectedItems().isEmpty())
{
QString newUsername = this->currentItem()->text();
if (newUsername.compare(ANONYMOUS_USERNAME_LABEL,
Qt::CaseInsensitive) == 0)
{
app->accounts->twitch.currentUsername = "";
}
else
{
app->accounts->twitch.currentUsername = newUsername;
}
}
});
}
void AccountSwitchWidget::refresh()
{
this->refreshSelection();
}
void AccountSwitchWidget::refreshSelection()
{
this->blockSignals(true);
// Select the currently logged in user
if (this->count() > 0)
{
auto app = getApp();
auto currentUser = app->accounts->twitch.getCurrent();
if (currentUser->isAnon())
{
this->setCurrentRow(0);
}
else
{
const QString &currentUsername = currentUser->getUserName();
for (int i = 0; i < this->count(); ++i)
{
QString itemText = this->item(i)->text();
if (itemText.compare(currentUsername, Qt::CaseInsensitive) == 0)
{
this->setCurrentRow(i);
break;
}
}
}
}
this->blockSignals(false);
}
} // namespace chatterino