Implement stupid account manager

After logging in, you need to restart chatterino
This commit is contained in:
Rasmus Karlsson 2017-07-28 19:46:53 +02:00
parent 1296aac245
commit 467ca90fd8
8 changed files with 176 additions and 7 deletions

View file

@ -96,7 +96,8 @@ SOURCES += \
src/messagefactory.cpp \
src/widgets/basewidget.cpp \
src/widgets/resizingtextedit.cpp \
src/completionmanager.cpp
src/completionmanager.cpp \
src/widgets/logindialog.cpp
HEADERS += \
src/asyncexec.hpp \

View file

@ -1,4 +1,5 @@
#include "accountmanager.hpp"
#include "common.hpp"
#include <pajlada/settings/setting.hpp>
@ -32,6 +33,34 @@ AccountManager::AccountManager()
"/accounts/current/roomID", "11148817", pajlada::Settings::SettingOption::DoNotWriteToJSON);
}
void AccountManager::load()
{
auto keys = pajlada::Settings::SettingManager::getObjectKeys("/accounts");
for (const auto &uid : keys) {
if (uid == "current") {
continue;
}
std::string username =
pajlada::Settings::Setting<std::string>::get("/accounts/" + uid + "/username");
std::string userID =
pajlada::Settings::Setting<std::string>::get("/accounts/" + uid + "/userID");
std::string clientID =
pajlada::Settings::Setting<std::string>::get("/accounts/" + uid + "/clientID");
std::string oauthToken =
pajlada::Settings::Setting<std::string>::get("/accounts/" + uid + "/oauthToken");
if (username.empty() || userID.empty() || clientID.empty() || oauthToken.empty()) {
continue;
}
twitch::TwitchUser user(qS(username), qS(oauthToken), qS(clientID));
this->addTwitchUser(user);
}
}
twitch::TwitchUser &AccountManager::getTwitchAnon()
{
return this->twitchAnonymousUser;

View file

@ -16,6 +16,8 @@ public:
return instance;
}
void load();
twitch::TwitchUser &getTwitchAnon();
// Returns first user from twitchUsers, or twitchAnonymousUser if twitchUsers is empty

View file

@ -1,4 +1,5 @@
#include "application.hpp"
#include "accountmanager.hpp"
#include "colorscheme.hpp"
#include "logging/loggingmanager.hpp"
#include "settingsmanager.hpp"
@ -25,6 +26,10 @@ Application::Application()
// Initialize everything we need
this->emoteManager.loadGlobalEmotes();
AccountManager::getInstance().load();
this->ircManager.setUser(AccountManager::getInstance().getTwitchUser());
// XXX
SettingsManager::getInstance().updateWordTypeMask();

View file

@ -68,6 +68,9 @@ Communi::IrcConnection *IrcManager::createConnection(bool doRead)
QString username = _account.getUserName();
QString oauthClient = _account.getOAuthClient();
QString oauthToken = _account.getOAuthToken();
if (!oauthToken.startsWith("oauth:")) {
oauthToken.prepend("oauth:");
}
connection->setUserName(username);
connection->setNickName(username);

View file

@ -0,0 +1,85 @@
#include "widgets/logindialog.hpp"
#include <QClipboard>
#include <QDebug>
#include <QDesktopServices>
#include <QUrl>
#include <pajlada/settings/setting.hpp>
namespace chatterino {
namespace widgets {
LoginWidget::LoginWidget()
{
this->setLayout(&this->ui.mainLayout);
this->ui.loginButton.setText("Log in (Opens in browser)");
this->ui.pasteCodeButton.setText("Paste code");
this->ui.horizontalLayout.addWidget(&this->ui.loginButton);
this->ui.horizontalLayout.addWidget(&this->ui.pasteCodeButton);
this->ui.verticalLayout.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, [this]() {
printf("open login in browser\n");
QDesktopServices::openUrl(QUrl("https://pajlada.se/chatterino/#chatterino"));
});
connect(&this->ui.pasteCodeButton, &QPushButton::clicked, [this]() {
QClipboard *clipboard = QGuiApplication::clipboard();
QString clipboardString = clipboard->text();
QStringList parameters = clipboardString.split(';');
std::string oauthToken, clientID, username, userID;
for (const auto &param : parameters) {
QStringList kvParameters = param.split('=');
if (kvParameters.size() != 2) {
continue;
}
QString key = kvParameters[0];
QString value = kvParameters[1];
if (key == "oauth_token") {
oauthToken = value.toStdString();
} else if (key == "client_id") {
clientID = value.toStdString();
} else if (key == "username") {
username = value.toStdString();
} else if (key == "user_id") {
userID = value.toStdString();
} else {
qDebug() << "Unknown key in code: " << key;
}
}
if (oauthToken.empty() || clientID.empty() || username.empty() || userID.empty()) {
qDebug() << "Missing variables!!!!!!!!!";
} else {
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);
}
});
}
} // namespace widgets
} // namespace chatterino

View file

@ -0,0 +1,37 @@
#pragma once
#include "widgets/basewidget.hpp"
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
namespace chatterino {
namespace widgets {
class LoginWidget : public QDialog
{
public:
LoginWidget();
private:
struct {
QVBoxLayout mainLayout;
QVBoxLayout verticalLayout;
QHBoxLayout horizontalLayout;
QPushButton loginButton;
QPushButton pasteCodeButton;
QDialogButtonBox buttonBox;
} ui;
};
} // namespace widgets
} // namespace chatterino

View file

@ -2,6 +2,7 @@
#include "accountmanager.hpp"
#include "twitch/twitchmessagebuilder.hpp"
#include "twitch/twitchuser.hpp"
#include "widgets/logindialog.hpp"
#include "widgets/settingsdialogtab.hpp"
#include "windowmanager.hpp"
@ -80,8 +81,18 @@ void SettingsDialog::addTabs()
// add remove buttons
auto buttonBox = new QDialogButtonBox(this);
auto addButton = new QPushButton("add", this);
auto removeButton = new QPushButton("remove", this);
auto addButton = new QPushButton("Add", this);
auto removeButton = new QPushButton("Remove", this);
connect(addButton, &QPushButton::clicked, []() {
// TODO: fix memory leak :bbaper:
auto loginWidget = new LoginWidget();
loginWidget->show();
});
connect(removeButton, &QPushButton::clicked, []() {
qDebug() << "TODO: Implement"; //
});
buttonBox->addButton(addButton, QDialogButtonBox::YesRole);
buttonBox->addButton(removeButton, QDialogButtonBox::NoRole);
@ -91,10 +102,6 @@ void SettingsDialog::addTabs()
// listview
auto listWidget = new QListWidget(this);
listWidget->addItem("xD");
listWidget->addItem("vi von");
listWidget->addItem("monkaS");
for (auto &user : AccountManager::getInstance().getTwitchUsers()) {
listWidget->addItem(user.getUserName());
}