mirror-chatterino2/src/accountmanager.cpp

109 lines
2.8 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
{
QString envUsername = getEnvString("CHATTERINO2_USERNAME");
QString envOauthToken = getEnvString("CHATTERINO2_OAUTH");
if (!envUsername.isEmpty() && !envOauthToken.isEmpty()) {
this->addTwitchUser(twitch::TwitchUser(envUsername, envOauthToken, ""));
}
pajlada::Settings::Setting<std::string>::set(
"/accounts/current/roomID", "11148817", pajlada::Settings::SettingOption::DoNotWriteToJSON);
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);
}
}
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();
}
2017-06-11 12:00:53 +02:00
return this->twitchUsers.front();
}
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