mirror-chatterino2/src/accountmanager.cpp

117 lines
2.9 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "accountmanager.hpp"
#include "common.hpp"
2017-04-12 17:46:44 +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
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-06-11 12:00:53 +02:00
: twitchAnonymousUser("justinfan64537", "", "")
2017-04-12 17:46:44 +02:00
{
}
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);
printf("Adding user %s(%s)\n", username.c_str(), userID.c_str());
}
}
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
twitch::TwitchUser &AccountManager::getTwitchUser()
{
2017-06-11 12:00:53 +02:00
std::lock_guard<std::mutex> lock(this->twitchUsersMutex);
2017-06-11 12:00:53 +02:00
if (this->twitchUsers.size() == 0) {
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;
}
}
2017-06-11 12:00:53 +02:00
return this->twitchUsers.front();
}
void AccountManager::setCurrentTwitchUser(const QString &username)
{
pajlada::Settings::Setting<std::string>::set("/accounts/current", username.toStdString());
}
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-04-14 17:52:22 +02:00
} // namespace chatterino