Implement an advanced method of adding a user to chatterino.

You can now switch between multiple users in the settings dialog.
(Requires a restart for reconnecting etc)
This commit is contained in:
Rasmus Karlsson 2017-08-19 15:37:56 +02:00
parent 60afaa588b
commit 8d2e48f944
13 changed files with 271 additions and 38 deletions

View file

@ -67,9 +67,24 @@ twitch::TwitchUser &AccountManager::getTwitchUser()
return this->getTwitchAnon(); return this->getTwitchAnon();
} }
std::string currentUser = pajlada::Settings::Setting<std::string>::get("/accounts/current");
QString currentUsername = QString::fromStdString(currentUser);
for (auto &user : this->twitchUsers) {
if (user.getUserName() == currentUsername) {
return user;
}
}
return this->twitchUsers.front(); return this->twitchUsers.front();
} }
void AccountManager::setCurrentTwitchUser(const QString &username)
{
pajlada::Settings::Setting<std::string>::set("/accounts/current", username.toStdString());
}
std::vector<twitch::TwitchUser> AccountManager::getTwitchUsers() std::vector<twitch::TwitchUser> AccountManager::getTwitchUsers()
{ {
std::lock_guard<std::mutex> lock(this->twitchUsersMutex); std::lock_guard<std::mutex> lock(this->twitchUsersMutex);

View file

@ -29,6 +29,8 @@ public:
// Remove twitch user with the given username // Remove twitch user with the given username
bool removeTwitchUser(const QString &userName); bool removeTwitchUser(const QString &userName);
void setCurrentTwitchUser(const QString &username);
// Add twitch user to the list of available twitch users // Add twitch user to the list of available twitch users
void addTwitchUser(const twitch::TwitchUser &user); void addTwitchUser(const twitch::TwitchUser &user);

View file

@ -31,8 +31,13 @@ IrcManager::IrcManager(ChannelManager &_channelManager, Resources &_resources,
, resources(_resources) , resources(_resources)
, emoteManager(_emoteManager) , emoteManager(_emoteManager)
, windowManager(_windowManager) , windowManager(_windowManager)
, _account(AccountManager::getInstance().getTwitchUser()) , _account(AccountManager::getInstance().getTwitchAnon())
, currentUser("/accounts/current")
{ {
this->currentUser.getValueChangedSignal().connect([](const auto &newUsername) {
// TODO: Implement
qDebug() << "Current user changed, fetch new credentials and reconnect";
});
} }
const twitch::TwitchUser &IrcManager::getUser() const const twitch::TwitchUser &IrcManager::getUser() const

View file

@ -58,6 +58,8 @@ private:
// variables // variables
twitch::TwitchUser _account; twitch::TwitchUser _account;
pajlada::Settings::Setting<std::string> currentUser;
std::shared_ptr<Communi::IrcConnection> writeConnection = nullptr; std::shared_ptr<Communi::IrcConnection> writeConnection = nullptr;
std::shared_ptr<Communi::IrcConnection> readConnection = nullptr; std::shared_ptr<Communi::IrcConnection> readConnection = nullptr;

View file

@ -15,7 +15,6 @@ SettingsManager::SettingsManager()
, showTimestampSeconds("/appearance/messages/showTimestampSeconds", true) , showTimestampSeconds("/appearance/messages/showTimestampSeconds", true)
, showBadges("/appearance/messages/showBadges", true) , showBadges("/appearance/messages/showBadges", true)
, streamlinkPath("/behaviour/streamlinkPath", "") , streamlinkPath("/behaviour/streamlinkPath", "")
, selectedUser(_settingsItems, "selectedUser", "")
, emoteScale(_settingsItems, "emoteScale", 1.0) , emoteScale(_settingsItems, "emoteScale", 1.0)
, mouseScrollMultiplier(_settingsItems, "mouseScrollMultiplier", 1.0) , mouseScrollMultiplier(_settingsItems, "mouseScrollMultiplier", 1.0)
, scaleEmotesByLineHeight(_settingsItems, "scaleEmotesByLineHeight", false) , scaleEmotesByLineHeight(_settingsItems, "scaleEmotesByLineHeight", false)

View file

@ -46,7 +46,6 @@ public:
pajlada::Settings::Setting<std::string> streamlinkPath; pajlada::Settings::Setting<std::string> streamlinkPath;
// Settings // Settings
Setting<QString> selectedUser;
Setting<float> emoteScale; Setting<float> emoteScale;
Setting<float> mouseScrollMultiplier; Setting<float> mouseScrollMultiplier;
Setting<bool> scaleEmotesByLineHeight; Setting<bool> scaleEmotesByLineHeight;

View file

@ -365,8 +365,11 @@ void TwitchMessageBuilder::parseHighlights()
{ {
static auto player = new QMediaPlayer; static auto player = new QMediaPlayer;
SettingsManager &settings = SettingsManager::getInstance(); SettingsManager &settings = SettingsManager::getInstance();
static pajlada::Settings::Setting<std::string> currentUser("/accounts/currentUser");
if (this->ircMessage->nick() == settings.selectedUser.get()) { QString currentUsername = QString::fromStdString(currentUser.getValue());
if (this->ircMessage->nick() == currentUsername) {
// Do nothing. Highlights cannot be triggered by yourself // Do nothing. Highlights cannot be triggered by yourself
return; return;
} }
@ -393,9 +396,8 @@ void TwitchMessageBuilder::parseHighlights()
// TODO: This vector should only be rebuilt upon highlights being changed // TODO: This vector should only be rebuilt upon highlights being changed
std::vector<Highlight> activeHighlights; std::vector<Highlight> activeHighlights;
if (settings.enableHighlightsSelf.get() && settings.selectedUser.get().size() > 0) { if (settings.enableHighlightsSelf.get() && currentUsername.size() > 0) {
activeHighlights.emplace_back(settings.selectedUser.get(), activeHighlights.emplace_back(currentUsername, settings.enableHighlightSound.get(),
settings.enableHighlightSound.get(),
settings.enableHighlightTaskbar.get()); settings.enableHighlightTaskbar.get());
} }
const auto &highlightProperties = settings.highlightProperties.get(); const auto &highlightProperties = settings.highlightProperties.get();

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "credentials.hpp"
#include <QEventLoop> #include <QEventLoop>
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
@ -16,6 +18,67 @@
namespace chatterino { namespace chatterino {
namespace util { namespace util {
namespace twitch {
static void get(QString url, std::function<void(QJsonObject &)> successCallback)
{
auto manager = new QNetworkAccessManager();
QUrl requestUrl(url);
QNetworkRequest request(requestUrl);
request.setRawHeader("Client-ID", getDefaultClientID());
request.setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
QNetworkReply *reply = manager->get(request);
QObject::connect(reply, &QNetworkReply::finished, [=] {
if (reply->error() == QNetworkReply::NetworkError::NoError) {
QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
if (!jsonDoc.isNull()) {
QJsonObject rootNode = jsonDoc.object();
successCallback(rootNode);
}
}
reply->deleteLater();
manager->deleteLater();
});
}
static void getUserID(QString username, std::function<void(QString)> successCallback)
{
get("https://api.twitch.tv/kraken/users?login=" + username, [=](const QJsonObject &root) {
if (!root.value("users").isArray()) {
qDebug() << "API Error while getting user id, users is not an array";
return;
}
auto users = root.value("users").toArray();
if (users.size() != 1) {
qDebug() << "API Error while getting user id, users array size is not 1";
return;
}
if (!users[0].isObject()) {
qDebug() << "API Error while getting user id, first user is not an object";
return;
}
auto firstUser = users[0].toObject();
auto id = firstUser.value("_id");
if (!id.isString()) {
qDebug()
<< "API Error: while getting user id, first user object `_id` key is not a string";
return;
}
successCallback(id.toString());
});
}
} // namespace twitch
static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> successCallback, static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> successCallback,
QNetworkAccessManager *manager = nullptr) QNetworkAccessManager *manager = nullptr)
{ {

View file

@ -279,8 +279,7 @@ void ChatWidgetView::updateMessageBuffer(messages::MessageRef *messageRef, QPixm
// this->selectionMax.messageIndex >= messageIndex) { // this->selectionMax.messageIndex >= messageIndex) {
// painter.fillRect(buffer->rect(), QColor(24, 55, 25)); // painter.fillRect(buffer->rect(), QColor(24, 55, 25));
//} else { //} else {
painter.fillRect(buffer->rect(), painter.fillRect(buffer->rect(), (messageRef->getMessage()->getCanHighlightTab())
(messageRef->getMessage()->getCanHighlightTab())
? this->colorScheme.ChatBackgroundHighlighted ? this->colorScheme.ChatBackgroundHighlighted
: this->colorScheme.ChatBackground); : this->colorScheme.ChatBackground);
//} //}

View file

@ -1,4 +1,5 @@
#include "widgets/logindialog.hpp" #include "widgets/logindialog.hpp"
#include "util/urlfetch.hpp"
#include <QClipboard> #include <QClipboard>
#include <QDebug> #include <QDebug>
@ -9,9 +10,9 @@
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
LoginWidget::LoginWidget() BasicLoginWidget::BasicLoginWidget()
{ {
this->setLayout(&this->ui.mainLayout); this->setLayout(&this->ui.layout);
this->ui.loginButton.setText("Log in (Opens in browser)"); this->ui.loginButton.setText("Log in (Opens in browser)");
this->ui.pasteCodeButton.setText("Paste code"); this->ui.pasteCodeButton.setText("Paste code");
@ -19,22 +20,11 @@ LoginWidget::LoginWidget()
this->ui.horizontalLayout.addWidget(&this->ui.loginButton); this->ui.horizontalLayout.addWidget(&this->ui.loginButton);
this->ui.horizontalLayout.addWidget(&this->ui.pasteCodeButton); this->ui.horizontalLayout.addWidget(&this->ui.pasteCodeButton);
this->ui.verticalLayout.addLayout(&this->ui.horizontalLayout); this->ui.layout.addLayout(&this->ui.horizontalLayout);
this->ui.mainLayout.addLayout(&this->ui.verticalLayout);
this->ui.buttonBox.setStandardButtons(QDialogButtonBox::Close);
this->ui.mainLayout.addWidget(&this->ui.buttonBox);
connect(&this->ui.buttonBox, &QDialogButtonBox::rejected, [this]() {
this->close(); //
});
connect(&this->ui.loginButton, &QPushButton::clicked, []() { connect(&this->ui.loginButton, &QPushButton::clicked, []() {
printf("open login in browser\n"); printf("open login in browser\n");
QDesktopServices::openUrl(QUrl("https://pajlada.se/chatterino/#chatterino")); QDesktopServices::openUrl(QUrl("https://pajlada.se/chatterino/#chatterino"));
}); });
connect(&this->ui.pasteCodeButton, &QPushButton::clicked, []() { connect(&this->ui.pasteCodeButton, &QPushButton::clicked, []() {
@ -81,5 +71,108 @@ LoginWidget::LoginWidget()
}); });
} }
AdvancedLoginWidget::AdvancedLoginWidget()
{
this->setLayout(&this->ui.layout);
this->ui.instructionsLabel.setText("1. Fill in your username\n2. Fill in your user ID or press "
"the 'Get user ID from username' button\n3. Fill in your "
"Client ID\n4. Fill in your OAuth Token\n5. Press Add User");
this->ui.instructionsLabel.setWordWrap(true);
this->ui.layout.addWidget(&this->ui.instructionsLabel);
this->ui.layout.addLayout(&this->ui.formLayout);
this->ui.layout.addLayout(&this->ui.buttonUpperRow.layout);
this->ui.layout.addLayout(&this->ui.buttonLowerRow.layout);
this->refreshButtons();
/// Form
this->ui.formLayout.addRow("Username", &this->ui.usernameInput);
this->ui.formLayout.addRow("User ID", &this->ui.userIDInput);
this->ui.formLayout.addRow("Client ID", &this->ui.clientIDInput);
this->ui.formLayout.addRow("Oauth token", &this->ui.oauthTokenInput);
this->ui.oauthTokenInput.setEchoMode(QLineEdit::Password);
connect(&this->ui.userIDInput, &QLineEdit::textChanged, [=]() { this->refreshButtons(); });
connect(&this->ui.usernameInput, &QLineEdit::textChanged, [=]() { this->refreshButtons(); });
connect(&this->ui.clientIDInput, &QLineEdit::textChanged, [=]() { this->refreshButtons(); });
connect(&this->ui.oauthTokenInput, &QLineEdit::textChanged, [=]() { this->refreshButtons(); });
/// Upper button row
this->ui.buttonUpperRow.addUserButton.setText("Add user");
this->ui.buttonUpperRow.clearFieldsButton.setText("Clear fields");
this->ui.buttonUpperRow.layout.addWidget(&this->ui.buttonUpperRow.addUserButton);
this->ui.buttonUpperRow.layout.addWidget(&this->ui.buttonUpperRow.clearFieldsButton);
connect(&this->ui.buttonUpperRow.clearFieldsButton, &QPushButton::clicked, [=]() {
this->ui.userIDInput.clear();
this->ui.usernameInput.clear();
this->ui.clientIDInput.clear();
this->ui.oauthTokenInput.clear();
});
connect(&this->ui.buttonUpperRow.addUserButton, &QPushButton::clicked, [=]() {
std::string userID = this->ui.userIDInput.text().toStdString();
std::string username = this->ui.usernameInput.text().toStdString();
std::string clientID = this->ui.clientIDInput.text().toStdString();
std::string oauthToken = this->ui.oauthTokenInput.text().toStdString();
qDebug() << "Success! mr";
pajlada::Settings::Setting<std::string>::set("/accounts/uid" + userID + "/username",
username);
pajlada::Settings::Setting<std::string>::set("/accounts/uid" + userID + "/userID", userID);
pajlada::Settings::Setting<std::string>::set("/accounts/uid" + userID + "/clientID",
clientID);
pajlada::Settings::Setting<std::string>::set("/accounts/uid" + userID + "/oauthToken",
oauthToken);
});
/// Lower button row
this->ui.buttonLowerRow.fillInUserIDButton.setText("Get user ID from username");
this->ui.buttonLowerRow.layout.addWidget(&this->ui.buttonLowerRow.fillInUserIDButton);
connect(&this->ui.buttonLowerRow.fillInUserIDButton, &QPushButton::clicked, [=]() {
util::twitch::getUserID(this->ui.usernameInput.text(), [=](const QString &userID) {
this->ui.userIDInput.setText(userID); //
});
});
}
void AdvancedLoginWidget::refreshButtons()
{
this->ui.buttonLowerRow.fillInUserIDButton.setEnabled(!this->ui.usernameInput.text().isEmpty());
if (this->ui.userIDInput.text().isEmpty() || this->ui.usernameInput.text().isEmpty() ||
this->ui.clientIDInput.text().isEmpty() || this->ui.oauthTokenInput.text().isEmpty()) {
this->ui.buttonUpperRow.addUserButton.setEnabled(false);
} else {
this->ui.buttonUpperRow.addUserButton.setEnabled(true);
}
}
LoginWidget::LoginWidget()
{
this->setLayout(&this->ui.mainLayout);
this->ui.mainLayout.addWidget(&this->ui.tabWidget);
this->ui.tabWidget.addTab(&this->ui.basic, "Basic");
this->ui.tabWidget.addTab(&this->ui.advanced, "Advanced");
this->ui.buttonBox.setStandardButtons(QDialogButtonBox::Close);
connect(&this->ui.buttonBox, &QDialogButtonBox::rejected, [this]() {
this->close(); //
});
this->ui.mainLayout.addWidget(&this->ui.buttonBox);
}
} // namespace widgets } // namespace widgets
} // namespace chatterino } // namespace chatterino

View file

@ -8,14 +8,65 @@
#include <QtWidgets/QButtonGroup> #include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog> #include <QtWidgets/QDialog>
#include <QtWidgets/QDialogButtonBox> #include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QHBoxLayout> #include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView> #include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton> #include <QtWidgets/QPushButton>
#include <QtWidgets/QTabWidget>
#include <QtWidgets/QVBoxLayout> #include <QtWidgets/QVBoxLayout>
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
class BasicLoginWidget : public QWidget
{
public:
BasicLoginWidget();
struct {
QVBoxLayout layout;
QHBoxLayout horizontalLayout;
QPushButton loginButton;
QPushButton pasteCodeButton;
} ui;
};
class AdvancedLoginWidget : public QWidget
{
public:
AdvancedLoginWidget();
void refreshButtons();
struct {
QVBoxLayout layout;
QLabel instructionsLabel;
QFormLayout formLayout;
QLineEdit userIDInput;
QLineEdit usernameInput;
QLineEdit clientIDInput;
QLineEdit oauthTokenInput;
struct {
QHBoxLayout layout;
QPushButton addUserButton;
QPushButton clearFieldsButton;
} buttonUpperRow;
struct {
QHBoxLayout layout;
QPushButton fillInUserIDButton;
} buttonLowerRow;
} ui;
};
class LoginWidget : public QDialog class LoginWidget : public QDialog
{ {
public: public:
@ -25,11 +76,13 @@ private:
struct { struct {
QVBoxLayout mainLayout; QVBoxLayout mainLayout;
QVBoxLayout verticalLayout; QTabWidget tabWidget;
QHBoxLayout horizontalLayout;
QPushButton loginButton;
QPushButton pasteCodeButton;
QDialogButtonBox buttonBox; QDialogButtonBox buttonBox;
BasicLoginWidget basic;
AdvancedLoginWidget advanced;
} ui; } ui;
}; };

View file

@ -82,7 +82,7 @@ void ScrollBar::scrollToBottom()
bool ScrollBar::isAtBottom() const bool ScrollBar::isAtBottom() const
{ {
return ((this->getMaximum() - this->getLargeChange()) - this->getCurrentValue()) <= 0.00001; return ((this->getMaximum() - this->getLargeChange()) - this->getCurrentValue()) <= 1;
} }
void ScrollBar::setMaximum(qreal value) void ScrollBar::setMaximum(qreal value)

View file

@ -108,21 +108,22 @@ void SettingsDialog::addTabs()
listWidget->addItem(user.getUserName()); listWidget->addItem(user.getUserName());
} }
if (listWidget->count()) { if (listWidget->count() > 0) {
int itemIndex = 0; const auto &currentUser = AccountManager::getInstance().getTwitchUser();
for (; itemIndex < listWidget->count(); ++itemIndex) { QString currentUsername = currentUser.getUserName();
if (listWidget->item(itemIndex)->text().compare(settings.selectedUser.get(), for (int i = 0; i < listWidget->count(); ++i) {
Qt::CaseInsensitive)) { QString itemText = listWidget->item(i)->text();
++itemIndex; if (itemText.compare(currentUsername, Qt::CaseInsensitive) == 0) {
listWidget->setCurrentRow(i);
break; break;
} }
} }
listWidget->setCurrentRow(itemIndex);
} }
QObject::connect(listWidget, &QListWidget::clicked, this, [&, listWidget] { QObject::connect(listWidget, &QListWidget::clicked, this, [&, listWidget] {
if (!listWidget->selectedItems().isEmpty()) { if (!listWidget->selectedItems().isEmpty()) {
settings.selectedUser.set(listWidget->currentItem()->text()); AccountManager::getInstance().setCurrentTwitchUser(
listWidget->currentItem()->text());
} }
}); });