mirror-chatterino2/src/widgets/accountswitchwidget.cpp

82 lines
2.2 KiB
C++
Raw Normal View History

#include "accountswitchwidget.hpp"
#include "const.hpp"
2017-12-31 22:58:35 +01:00
#include "singletons/accountmanager.hpp"
namespace chatterino {
namespace widgets {
AccountSwitchWidget::AccountSwitchWidget(QWidget *parent)
: QListWidget(parent)
{
2017-12-31 22:58:35 +01:00
singletons::AccountManager &accountManager = singletons::AccountManager::getInstance();
this->addItem(ANONYMOUS_USERNAME_LABEL);
2017-12-31 22:58:35 +01:00
for (const auto &userName : accountManager.Twitch.getUsernames()) {
this->addItem(userName);
}
2017-12-31 22:58:35 +01:00
accountManager.Twitch.userListUpdated.connect([this, &accountManager]() {
this->blockSignals(true);
this->clear();
this->addItem(ANONYMOUS_USERNAME_LABEL);
2017-12-31 22:58:35 +01:00
for (const auto &userName : accountManager.Twitch.getUsernames()) {
this->addItem(userName);
}
this->refreshSelection();
this->blockSignals(false);
});
this->refreshSelection();
2017-12-31 22:58:35 +01:00
QObject::connect(this, &QListWidget::clicked, [this, &accountManager] {
if (!this->selectedItems().isEmpty()) {
QString newUsername = this->currentItem()->text();
if (newUsername.compare(ANONYMOUS_USERNAME_LABEL, Qt::CaseInsensitive) == 0) {
2017-12-31 22:58:35 +01:00
accountManager.Twitch.currentUsername = "";
} else {
2017-12-31 22:58:35 +01:00
accountManager.Twitch.currentUsername = newUsername.toStdString();
}
}
});
}
void AccountSwitchWidget::refresh()
{
this->refreshSelection();
}
void AccountSwitchWidget::refreshSelection()
{
this->blockSignals(true);
// Select the currently logged in user
if (this->count() > 0) {
2017-12-31 22:58:35 +01:00
auto currentUser = singletons::AccountManager::getInstance().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 widgets
} // namespace chatterino