2017-06-11 09:31:45 +02:00
|
|
|
|
#include "accountmanager.hpp"
|
2017-07-28 19:46:53 +02:00
|
|
|
|
#include "common.hpp"
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
2017-07-23 14:16:13 +02:00
|
|
|
|
#include <pajlada/settings/setting.hpp>
|
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
|
namespace chatterino {
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
2017-05-30 18:24:55 +02:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
inline QString getEnvString(const char *target)
|
|
|
|
|
{
|
|
|
|
|
char *val = std::getenv(target);
|
|
|
|
|
if (val == nullptr) {
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QString(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
|
AccountManager::AccountManager()
|
2017-09-24 18:14:22 +02:00
|
|
|
|
: currentUser("/accounts/current", "")
|
|
|
|
|
, twitchAnonymousUser("justinfan64537", "", "")
|
2017-04-12 17:46:44 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 19:46:53 +02:00
|
|
|
|
void AccountManager::load()
|
|
|
|
|
{
|
|
|
|
|
auto keys = pajlada::Settings::SettingManager::getObjectKeys("/accounts");
|
|
|
|
|
|
2017-09-24 18:14:22 +02:00
|
|
|
|
bool first = true;
|
|
|
|
|
|
2017-07-28 19:46:53 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-24 18:14:22 +02:00
|
|
|
|
if (first) {
|
|
|
|
|
this->setCurrentTwitchUser(qS(username));
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 19:46:53 +02:00
|
|
|
|
twitch::TwitchUser user(qS(username), qS(oauthToken), qS(clientID));
|
|
|
|
|
|
|
|
|
|
this->addTwitchUser(user);
|
2017-08-19 15:30:53 +02:00
|
|
|
|
|
|
|
|
|
printf("Adding user %s(%s)\n", username.c_str(), userID.c_str());
|
2017-07-28 19:46:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 16:06:23 +02:00
|
|
|
|
twitch::TwitchUser &AccountManager::getTwitchAnon()
|
2017-04-12 17:46:44 +02:00
|
|
|
|
{
|
2017-06-11 12:00:53 +02:00
|
|
|
|
return this->twitchAnonymousUser;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
}
|
2017-04-13 16:06:23 +02:00
|
|
|
|
|
2017-05-30 18:24:55 +02:00
|
|
|
|
twitch::TwitchUser &AccountManager::getTwitchUser()
|
|
|
|
|
{
|
2017-06-11 12:00:53 +02:00
|
|
|
|
std::lock_guard<std::mutex> lock(this->twitchUsersMutex);
|
2017-05-30 18:24:55 +02:00
|
|
|
|
|
2017-06-11 12:00:53 +02:00
|
|
|
|
if (this->twitchUsers.size() == 0) {
|
|
|
|
|
return this->getTwitchAnon();
|
2017-05-30 18:24:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-24 18:14:22 +02:00
|
|
|
|
QString currentUsername = QString::fromStdString(this->currentUser);
|
2017-08-19 15:37:56 +02:00
|
|
|
|
|
|
|
|
|
for (auto &user : this->twitchUsers) {
|
|
|
|
|
if (user.getUserName() == currentUsername) {
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-11 12:00:53 +02:00
|
|
|
|
return this->twitchUsers.front();
|
2017-05-30 18:24:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-19 15:37:56 +02:00
|
|
|
|
void AccountManager::setCurrentTwitchUser(const QString &username)
|
|
|
|
|
{
|
2017-09-24 18:14:22 +02:00
|
|
|
|
this->currentUser.setValue(username.toStdString());
|
2017-08-19 15:37:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 16:06:23 +02:00
|
|
|
|
std::vector<twitch::TwitchUser> AccountManager::getTwitchUsers()
|
|
|
|
|
{
|
2017-06-11 12:00:53 +02:00
|
|
|
|
std::lock_guard<std::mutex> lock(this->twitchUsersMutex);
|
|
|
|
|
|
|
|
|
|
return std::vector<twitch::TwitchUser>(this->twitchUsers);
|
2017-04-13 16:06:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AccountManager::removeTwitchUser(const QString &userName)
|
|
|
|
|
{
|
2017-06-11 12:00:53 +02:00
|
|
|
|
std::lock_guard<std::mutex> lock(this->twitchUsersMutex);
|
2017-04-13 16:06:23 +02:00
|
|
|
|
|
2017-06-11 12:00:53 +02:00
|
|
|
|
for (auto it = this->twitchUsers.begin(); it != this->twitchUsers.end(); it++) {
|
2017-04-13 16:06:23 +02:00
|
|
|
|
if ((*it).getUserName() == userName) {
|
2017-06-11 12:00:53 +02:00
|
|
|
|
this->twitchUsers.erase(it);
|
2017-04-13 16:06:23 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AccountManager::addTwitchUser(const twitch::TwitchUser &user)
|
|
|
|
|
{
|
2017-06-11 12:00:53 +02:00
|
|
|
|
std::lock_guard<std::mutex> lock(this->twitchUsersMutex);
|
2017-04-13 16:06:23 +02:00
|
|
|
|
|
2017-06-11 12:00:53 +02:00
|
|
|
|
this->twitchUsers.push_back(user);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
}
|
2017-05-27 17:45:40 +02:00
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
|
} // namespace chatterino
|