mirror-chatterino2/src/accountmanager.cpp

75 lines
1.6 KiB
C++
Raw Normal View History

2017-04-18 02:29:32 +02:00
#include "accountmanager.h"
2017-04-12 17:46:44 +02:00
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-04-13 16:06:23 +02:00
: _twitchAnon("justinfan64537", "", "")
, _twitchUsers()
, _twitchUsersMutex()
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, ""));
}
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-04-13 16:06:23 +02:00
return _twitchAnon;
2017-04-12 17:46:44 +02:00
}
2017-04-13 16:06:23 +02:00
twitch::TwitchUser &AccountManager::getTwitchUser()
{
std::lock_guard<std::mutex> lock(_twitchUsersMutex);
if (_twitchUsers.size() == 0) {
return _twitchAnon;
}
return _twitchUsers.front();
}
2017-04-13 16:06:23 +02:00
std::vector<twitch::TwitchUser> AccountManager::getTwitchUsers()
{
return std::vector<twitch::TwitchUser>(_twitchUsers);
}
bool AccountManager::removeTwitchUser(const QString &userName)
{
std::lock_guard<std::mutex> lock(_twitchUsersMutex);
for (auto it = _twitchUsers.begin(); it != _twitchUsers.end(); it++) {
if ((*it).getUserName() == userName) {
_twitchUsers.erase(it);
return true;
}
}
return false;
}
void AccountManager::addTwitchUser(const twitch::TwitchUser &user)
{
std::lock_guard<std::mutex> lock(_twitchUsersMutex);
_twitchUsers.push_back(user);
2017-04-12 17:46:44 +02:00
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino