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

View file

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

View file

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

View file

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

View file

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

View file

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