more random refactoring

This commit is contained in:
Rasmus Karlsson 2017-06-11 12:00:53 +02:00
parent 1d6f558452
commit 4bfe58f14d
6 changed files with 29 additions and 24 deletions

View file

@ -17,9 +17,7 @@ inline QString getEnvString(const char *target)
} // namespace
AccountManager::AccountManager()
: _twitchAnon("justinfan64537", "", "")
, _twitchUsers()
, _twitchUsersMutex()
: twitchAnonymousUser("justinfan64537", "", "")
{
QString envUsername = getEnvString("CHATTERINO2_USERNAME");
QString envOauthToken = getEnvString("CHATTERINO2_OAUTH");
@ -31,32 +29,34 @@ AccountManager::AccountManager()
twitch::TwitchUser &AccountManager::getTwitchAnon()
{
return _twitchAnon;
return this->twitchAnonymousUser;
}
twitch::TwitchUser &AccountManager::getTwitchUser()
{
std::lock_guard<std::mutex> lock(_twitchUsersMutex);
std::lock_guard<std::mutex> lock(this->twitchUsersMutex);
if (_twitchUsers.size() == 0) {
return _twitchAnon;
if (this->twitchUsers.size() == 0) {
return this->getTwitchAnon();
}
return _twitchUsers.front();
return this->twitchUsers.front();
}
std::vector<twitch::TwitchUser> AccountManager::getTwitchUsers()
{
return std::vector<twitch::TwitchUser>(_twitchUsers);
std::lock_guard<std::mutex> lock(this->twitchUsersMutex);
return std::vector<twitch::TwitchUser>(this->twitchUsers);
}
bool AccountManager::removeTwitchUser(const QString &userName)
{
std::lock_guard<std::mutex> lock(_twitchUsersMutex);
std::lock_guard<std::mutex> lock(this->twitchUsersMutex);
for (auto it = _twitchUsers.begin(); it != _twitchUsers.end(); it++) {
for (auto it = this->twitchUsers.begin(); it != this->twitchUsers.end(); it++) {
if ((*it).getUserName() == userName) {
_twitchUsers.erase(it);
this->twitchUsers.erase(it);
return true;
}
}
@ -66,9 +66,9 @@ bool AccountManager::removeTwitchUser(const QString &userName)
void AccountManager::addTwitchUser(const twitch::TwitchUser &user)
{
std::lock_guard<std::mutex> lock(_twitchUsersMutex);
std::lock_guard<std::mutex> lock(this->twitchUsersMutex);
_twitchUsers.push_back(user);
this->twitchUsers.push_back(user);
}
} // namespace chatterino

View file

@ -18,19 +18,24 @@ public:
twitch::TwitchUser &getTwitchAnon();
// Returns first user from _twitchUsers, or _twitchAnon if _twitchUsers is empty
// Returns first user from twitchUsers, or twitchAnonymousUser if twitchUsers is empty
twitch::TwitchUser &getTwitchUser();
// Return a copy of the current available twitch users
std::vector<twitch::TwitchUser> getTwitchUsers();
// Remove twitch user with the given username
bool removeTwitchUser(const QString &userName);
// Add twitch user to the list of available twitch users
void addTwitchUser(const twitch::TwitchUser &user);
private:
AccountManager();
twitch::TwitchUser _twitchAnon;
std::vector<twitch::TwitchUser> _twitchUsers;
std::mutex _twitchUsersMutex;
twitch::TwitchUser twitchAnonymousUser;
std::vector<twitch::TwitchUser> twitchUsers;
std::mutex twitchUsersMutex;
};
} // namespace chatterino

View file

@ -124,7 +124,7 @@ SharedMessage TwitchMessageBuilder::parse(const Communi::IrcPrivateMessage *ircM
displayName + (hasLocalizedName ? (" (" + ircMessage->account() + ")") : QString());
if (args.isSentWhisper) {
userDisplayString += IrcManager::getInstance().getUser().getUserName() + " -> ";
userDisplayString += IrcManager::getInstance().getUser().getUserName();
}
if (args.isReceivedWhisper) {

View file

@ -8,22 +8,23 @@ TwitchUser::TwitchUser(const QString &username, const QString &oauthToken,
: IrcUser2(username, username, username, "oauth:" + oauthToken)
, _oauthClient(oauthClient)
, _oauthToken(oauthToken)
, _isAnon(username.startsWith("justinfan"))
{
}
const QString &TwitchUser::getOAuthClient() const
{
return _oauthClient;
return this->_oauthClient;
}
const QString &TwitchUser::getOAuthToken() const
{
return _oauthToken;
return this->_oauthToken;
}
bool TwitchUser::isAnon() const
{
return IrcUser2::getNickName().startsWith("justinfan");
return this->_isAnon;
}
} // namespace twitch

View file

@ -20,6 +20,7 @@ public:
private:
QString _oauthClient;
QString _oauthToken;
bool _isAnon;
};
} // namespace twitch

View file

@ -101,8 +101,6 @@ void ChatWidget::setChannelName(const QString &_newChannelName)
// update messages
this->messages.clear();
printf("Set channel name xD %s\n", qPrintable(_newChannelName));
if (newChannelName.isEmpty()) {
this->channel = nullptr;
} else {