2018-06-26 14:09:39 +02:00
|
|
|
#include "AccountSwitchWidget.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "Application.hpp"
|
2018-06-26 17:25:24 +02:00
|
|
|
#include "common/Common.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "controllers/accounts/AccountController.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "providers/twitch/TwitchAccount.hpp"
|
2018-06-26 17:25:24 +02:00
|
|
|
#include "providers/twitch/TwitchCommon.hpp"
|
2017-12-19 02:16:01 +01:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
AccountSwitchWidget::AccountSwitchWidget(QWidget *parent)
|
|
|
|
: QListWidget(parent)
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2017-12-31 22:58:35 +01:00
|
|
|
|
2017-12-19 16:13:02 +01:00
|
|
|
this->addItem(ANONYMOUS_USERNAME_LABEL);
|
2017-12-19 02:16:01 +01:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &userName : app->accounts->twitch.getUsernames())
|
|
|
|
{
|
2017-12-19 02:16:01 +01:00
|
|
|
this->addItem(userName);
|
|
|
|
}
|
|
|
|
|
2022-12-24 12:56:11 +01:00
|
|
|
app->accounts->twitch.userListUpdated.connect([=, this]() {
|
2017-12-19 15:12:33 +01:00
|
|
|
this->blockSignals(true);
|
|
|
|
|
|
|
|
this->clear();
|
|
|
|
|
2017-12-19 16:13:02 +01:00
|
|
|
this->addItem(ANONYMOUS_USERNAME_LABEL);
|
2017-12-19 15:12:33 +01:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &userName : app->accounts->twitch.getUsernames())
|
|
|
|
{
|
2017-12-19 15:12:33 +01:00
|
|
|
this->addItem(userName);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->refreshSelection();
|
|
|
|
|
|
|
|
this->blockSignals(false);
|
|
|
|
});
|
|
|
|
|
2017-12-19 02:16:01 +01:00
|
|
|
this->refreshSelection();
|
|
|
|
|
2022-12-24 12:56:11 +01:00
|
|
|
QObject::connect(this, &QListWidget::clicked, [=, this] {
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!this->selectedItems().isEmpty())
|
|
|
|
{
|
2017-12-19 02:16:01 +01:00
|
|
|
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-11-03 13:00:07 +01:00
|
|
|
app->accounts->twitch.currentUsername = newUsername;
|
2017-12-19 02:16:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
|
|
|
|
2018-05-26 20:26:25 +02:00
|
|
|
auto currentUser = app->accounts->twitch.getCurrent();
|
2017-12-19 02:16:01 +01:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (currentUser->isAnon())
|
|
|
|
{
|
2017-12-19 02:16:01 +01:00
|
|
|
this->setCurrentRow(0);
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-19 02:16:01 +01:00
|
|
|
const QString ¤tUsername = currentUser->getUserName();
|
2018-10-21 13:43:02 +02:00
|
|
|
for (int i = 0; i < this->count(); ++i)
|
|
|
|
{
|
2017-12-19 02:16:01 +01:00
|
|
|
QString itemText = this->item(i)->text();
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (itemText.compare(currentUsername, Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
2017-12-19 02:16:01 +01:00
|
|
|
this->setCurrentRow(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->blockSignals(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|