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"
|
|
|
|
#include "Const.hpp"
|
|
|
|
#include "controllers/accounts/AccountController.hpp"
|
2017-12-19 02:16:01 +01:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace widgets {
|
|
|
|
|
|
|
|
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-05-26 20:26:25 +02:00
|
|
|
for (const auto &userName : app->accounts->twitch.getUsernames()) {
|
2017-12-19 02:16:01 +01:00
|
|
|
this->addItem(userName);
|
|
|
|
}
|
|
|
|
|
2018-05-26 20:26:25 +02:00
|
|
|
app->accounts->twitch.userListUpdated.connect([=]() {
|
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-05-26 20:26:25 +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();
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
QObject::connect(this, &QListWidget::clicked, [=] {
|
2017-12-19 02:16:01 +01:00
|
|
|
if (!this->selectedItems().isEmpty()) {
|
|
|
|
QString newUsername = this->currentItem()->text();
|
2017-12-19 16:13:02 +01:00
|
|
|
if (newUsername.compare(ANONYMOUS_USERNAME_LABEL, Qt::CaseInsensitive) == 0) {
|
2018-05-26 20:26:25 +02:00
|
|
|
app->accounts->twitch.currentUsername = "";
|
2017-12-19 02:16:01 +01:00
|
|
|
} else {
|
2018-05-26 20:26:25 +02:00
|
|
|
app->accounts->twitch.currentUsername = newUsername.toStdString();
|
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
|
|
|
|
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
|
|
|
|
|
|
|
if (currentUser->isAnon()) {
|
|
|
|
this->setCurrentRow(0);
|
|
|
|
} else {
|
|
|
|
const QString ¤tUsername = 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 widgets
|
|
|
|
} // namespace chatterino
|