2017-06-11 09:31:45 +02:00
|
|
|
|
#include "accountmanager.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-06-11 12:00:53 +02:00
|
|
|
|
: twitchAnonymousUser("justinfan64537", "", "")
|
2017-04-12 17:46:44 +02:00
|
|
|
|
{
|
2017-05-30 18:24:55 +02:00
|
|
|
|
QString envUsername = getEnvString("CHATTERINO2_USERNAME");
|
|
|
|
|
QString envOauthToken = getEnvString("CHATTERINO2_OAUTH");
|
|
|
|
|
|
|
|
|
|
if (!envUsername.isEmpty() && !envOauthToken.isEmpty()) {
|
|
|
|
|
this->addTwitchUser(twitch::TwitchUser(envUsername, envOauthToken, ""));
|
|
|
|
|
}
|
2017-07-23 14:16:13 +02:00
|
|
|
|
|
|
|
|
|
pajlada::Settings::Setting<std::string>::set(
|
|
|
|
|
"/accounts/current/roomID", "11148817", pajlada::Settings::SettingOption::DoNotWriteToJSON);
|
2017-04-12 17:46:44 +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-06-11 12:00:53 +02:00
|
|
|
|
return this->twitchUsers.front();
|
2017-05-30 18:24:55 +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
|