mirror-chatterino2/src/widgets/AccountSwitchWidget.cpp

100 lines
2.4 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "AccountSwitchWidget.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "common/Common.hpp"
2018-06-26 14:09:39 +02:00
#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();
2017-12-31 22:58:35 +01:00
this->addItem(ANONYMOUS_USERNAME_LABEL);
2018-10-21 13:43:02 +02:00
for (const auto &userName : app->accounts->twitch.getUsernames())
{
this->addItem(userName);
}
2018-05-26 20:26:25 +02:00
app->accounts->twitch.userListUpdated.connect([=]() {
this->blockSignals(true);
this->clear();
this->addItem(ANONYMOUS_USERNAME_LABEL);
2018-10-21 13:43:02 +02:00
for (const auto &userName : app->accounts->twitch.getUsernames())
{
this->addItem(userName);
}
this->refreshSelection();
this->blockSignals(false);
});
this->refreshSelection();
QObject::connect(this, &QListWidget::clicked, [=] {
2018-10-21 13:43:02 +02:00
if (!this->selectedItems().isEmpty())
{
QString newUsername = this->currentItem()->text();
2018-08-06 21:17:03 +02:00
if (newUsername.compare(ANONYMOUS_USERNAME_LABEL,
2018-10-21 13:43:02 +02:00
Qt::CaseInsensitive) == 0)
{
2018-05-26 20:26:25 +02:00
app->accounts->twitch.currentUsername = "";
2018-10-21 13:43:02 +02:00
}
else
{
2018-08-06 21:17:03 +02:00
app->accounts->twitch.currentUsername =
newUsername.toStdString();
}
}
});
}
void AccountSwitchWidget::refresh()
{
this->refreshSelection();
}
void AccountSwitchWidget::refreshSelection()
{
this->blockSignals(true);
// Select the currently logged in user
2018-10-21 13:43:02 +02:00
if (this->count() > 0)
{
auto app = getApp();
2018-05-26 20:26:25 +02:00
auto currentUser = app->accounts->twitch.getCurrent();
2018-10-21 13:43:02 +02:00
if (currentUser->isAnon())
{
this->setCurrentRow(0);
2018-10-21 13:43:02 +02:00
}
else
{
const QString &currentUsername = currentUser->getUserName();
2018-10-21 13:43:02 +02:00
for (int i = 0; i < this->count(); ++i)
{
QString itemText = this->item(i)->text();
2018-10-21 13:43:02 +02:00
if (itemText.compare(currentUsername, Qt::CaseInsensitive) == 0)
{
this->setCurrentRow(i);
break;
}
}
}
}
this->blockSignals(false);
}
} // namespace chatterino