diff --git a/chatterino.pro b/chatterino.pro index 73b4eeedf..c9987cfc7 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -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 \ diff --git a/src/accountmanager.cpp b/src/accountmanager.cpp index c96831422..38d7e8917 100644 --- a/src/accountmanager.cpp +++ b/src/accountmanager.cpp @@ -1,4 +1,5 @@ #include "accountmanager.hpp" +#include "common.hpp" #include @@ -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::get("/accounts/" + uid + "/username"); + std::string userID = + pajlada::Settings::Setting::get("/accounts/" + uid + "/userID"); + std::string clientID = + pajlada::Settings::Setting::get("/accounts/" + uid + "/clientID"); + std::string oauthToken = + pajlada::Settings::Setting::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; diff --git a/src/accountmanager.hpp b/src/accountmanager.hpp index 0bf710229..a6b89bd0e 100644 --- a/src/accountmanager.hpp +++ b/src/accountmanager.hpp @@ -16,6 +16,8 @@ public: return instance; } + void load(); + twitch::TwitchUser &getTwitchAnon(); // Returns first user from twitchUsers, or twitchAnonymousUser if twitchUsers is empty diff --git a/src/application.cpp b/src/application.cpp index 254a006db..841a4b5ac 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -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(); diff --git a/src/ircmanager.cpp b/src/ircmanager.cpp index 4aa3ba30c..8dc1701a8 100644 --- a/src/ircmanager.cpp +++ b/src/ircmanager.cpp @@ -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); diff --git a/src/widgets/logindialog.cpp b/src/widgets/logindialog.cpp new file mode 100644 index 000000000..a8dbbcd30 --- /dev/null +++ b/src/widgets/logindialog.cpp @@ -0,0 +1,85 @@ +#include "widgets/logindialog.hpp" + +#include +#include +#include +#include +#include + +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 ¶m : 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::set("/accounts/uid" + userID + "/username", + username); + pajlada::Settings::Setting::set("/accounts/uid" + userID + "/userID", + userID); + pajlada::Settings::Setting::set("/accounts/uid" + userID + "/clientID", + clientID); + pajlada::Settings::Setting::set("/accounts/uid" + userID + "/oauthToken", + oauthToken); + } + }); +} + +} // namespace widgets +} // namespace chatterino diff --git a/src/widgets/logindialog.hpp b/src/widgets/logindialog.hpp new file mode 100644 index 000000000..0f864bd18 --- /dev/null +++ b/src/widgets/logindialog.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "widgets/basewidget.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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 diff --git a/src/widgets/settingsdialog.cpp b/src/widgets/settingsdialog.cpp index 93dbeb5da..14de56c5c 100644 --- a/src/widgets/settingsdialog.cpp +++ b/src/widgets/settingsdialog.cpp @@ -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()); }