From b939ab9402d0f8bc4cac933b4ec118f0d9e901a9 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 10 Mar 2014 18:06:39 +0300 Subject: Remove duplicate code from whoisonline. --- src/gui/windows/whoisonline.cpp | 149 +++++++++++++++++----------------------- src/gui/windows/whoisonline.h | 12 ++-- 2 files changed, 69 insertions(+), 92 deletions(-) (limited to 'src/gui') diff --git a/src/gui/windows/whoisonline.cpp b/src/gui/windows/whoisonline.cpp index dabc8c7c1..2052aeab3 100644 --- a/src/gui/windows/whoisonline.cpp +++ b/src/gui/windows/whoisonline.cpp @@ -93,6 +93,10 @@ WhoIsOnline::WhoIsOnline() : mOnlineNicks(), // TRANSLATORS: who is online. button. mUpdateButton(new Button(this, _("Update"), "update", this)), + mFriends(), + mNeutral(), + mDisregard(), + mEnemy(), mAllowUpdate(true), mShowLevel(false), mUpdateOnlineList(config.getBoolValue("updateOnlineList")), @@ -201,22 +205,18 @@ void WhoIsOnline::handleLink(const std::string& link, MouseEvent *event) } } -void WhoIsOnline::updateWindow(std::vector &restrict friends, - std::vector &restrict neutral, - std::vector &restrict disregard, - std::vector &restrict enemy, - size_t numOnline) +void WhoIsOnline::updateWindow(size_t numOnline) { // Set window caption // TRANSLATORS: who is online window name setCaption(_("Who Is Online - ") + toString(numOnline)); // List the online people - std::sort(friends.begin(), friends.end(), nameCompare); - std::sort(neutral.begin(), neutral.end(), nameCompare); - std::sort(disregard.begin(), disregard.end(), nameCompare); + std::sort(mFriends.begin(), mFriends.end(), nameCompare); + std::sort(mNeutral.begin(), mNeutral.end(), nameCompare); + std::sort(mDisregard.begin(), mDisregard.end(), nameCompare); bool addedFromSection(false); - FOR_EACH (std::vector::const_iterator, it, friends) + FOR_EACH (std::vector::const_iterator, it, mFriends) { mBrowserBox->addRow((*it)->getText()); addedFromSection = true; @@ -226,7 +226,7 @@ void WhoIsOnline::updateWindow(std::vector &restrict friends, mBrowserBox->addRow("---"); addedFromSection = false; } - FOR_EACH (std::vector::const_iterator, it, enemy) + FOR_EACH (std::vector::const_iterator, it, mEnemy) { mBrowserBox->addRow((*it)->getText()); addedFromSection = true; @@ -236,15 +236,15 @@ void WhoIsOnline::updateWindow(std::vector &restrict friends, mBrowserBox->addRow("---"); addedFromSection = false; } - FOR_EACH (std::vector::const_iterator, it, neutral) + FOR_EACH (std::vector::const_iterator, it, mNeutral) { mBrowserBox->addRow((*it)->getText()); addedFromSection = true; } - if (addedFromSection == true && !disregard.empty()) + if (addedFromSection == true && !mDisregard.empty()) mBrowserBox->addRow("---"); - FOR_EACH (std::vector::const_iterator, it, disregard) + FOR_EACH (std::vector::const_iterator, it, mDisregard) mBrowserBox->addRow((*it)->getText()); if (mScrollArea->getVerticalMaxScroll() < @@ -255,14 +255,47 @@ void WhoIsOnline::updateWindow(std::vector &restrict friends, } } +void WhoIsOnline::handlerPlayerRelation(const std::string &nick, + OnlinePlayer *const player) +{ + switch (player_relations.getRelation(nick)) + { + case PlayerRelation::NEUTRAL: + default: + setNeutralColor(player); + mNeutral.push_back(player); + break; + + case PlayerRelation::FRIEND: + player->setText("2"); + if (mGroupFriends) + mFriends.push_back(player); + else + mNeutral.push_back(player); + break; + + case PlayerRelation::DISREGARDED: + case PlayerRelation::BLACKLISTED: + player->setText("8"); + mDisregard.push_back(player); + break; + + case PlayerRelation::ENEMY2: + player->setText("1"); + mEnemy.push_back(player); + break; + + case PlayerRelation::IGNORED: + case PlayerRelation::ERASED: + // Ignore the ignored. + break; + } +} + void WhoIsOnline::loadList(std::vector &list) { mBrowserBox->clearRows(); const size_t numOnline = list.size(); - std::vector friends; - std::vector neutral; - std::vector disregard; - std::vector enemy; FOR_EACH (std::set::iterator, itd, mOnlinePlayers) delete *itd; @@ -281,41 +314,10 @@ void WhoIsOnline::loadList(std::vector &list) if (!mShowLevel) player->setLevel(0); - switch (player_relations.getRelation(nick)) - { - case PlayerRelation::NEUTRAL: - default: - setNeutralColor(player); - neutral.push_back(player); - break; - - case PlayerRelation::FRIEND: - player->setText("2"); - if (mGroupFriends) - friends.push_back(player); - else - neutral.push_back(player); - break; - - case PlayerRelation::DISREGARDED: - case PlayerRelation::BLACKLISTED: - player->setText("8"); - disregard.push_back(player); - break; - - case PlayerRelation::ENEMY2: - player->setText("1"); - enemy.push_back(player); - break; - - case PlayerRelation::IGNORED: - case PlayerRelation::ERASED: - // Ignore the ignored. - break; - } + handlerPlayerRelation(nick, player); } - updateWindow(friends, neutral, disregard, enemy, numOnline); + updateWindow(numOnline); if (!mOnlineNicks.empty()) { if (chatWindow) @@ -324,6 +326,10 @@ void WhoIsOnline::loadList(std::vector &list) socialWindow->updateActiveList(); } updateSize(); + mFriends.clear(); + mNeutral.clear(); + mDisregard.clear(); + mEnemy.clear(); } void WhoIsOnline::loadWebList() @@ -343,10 +349,6 @@ void WhoIsOnline::loadWebList() bool listStarted(false); std::string lineStr; int numOnline(0); - std::vector friends; - std::vector neutral; - std::vector disregard; - std::vector enemy; // Tokenize and add each line separately char *line = strtok(mMemoryBuffer, "\n"); @@ -441,38 +443,7 @@ void WhoIsOnline::loadWebList() player->setIsGM(true); numOnline++; - switch (player_relations.getRelation(nick)) - { - case PlayerRelation::NEUTRAL: - default: - setNeutralColor(player); - neutral.push_back(player); - break; - - case PlayerRelation::FRIEND: - player->setText("2"); - if (mGroupFriends) - friends.push_back(player); - else - neutral.push_back(player); - break; - - case PlayerRelation::DISREGARDED: - case PlayerRelation::BLACKLISTED: - player->setText("8"); - disregard.push_back(player); - break; - - case PlayerRelation::ENEMY2: - player->setText("1"); - enemy.push_back(player); - break; - - case PlayerRelation::IGNORED: - case PlayerRelation::ERASED: - // Ignore the ignored. - break; - } + handlerPlayerRelation(nick, player); } } else if (lineStr.find("------------------------------") @@ -483,11 +454,15 @@ void WhoIsOnline::loadWebList() line = strtok(nullptr, "\n"); } - updateWindow(friends, neutral, disregard, enemy, numOnline); + updateWindow(numOnline); // Free the memory buffer now that we don't need it anymore free(mMemoryBuffer); mMemoryBuffer = nullptr; + mFriends.clear(); + mNeutral.clear(); + mDisregard.clear(); + mEnemy.clear(); } size_t WhoIsOnline::memoryWrite(void *ptr, size_t size, diff --git a/src/gui/windows/whoisonline.h b/src/gui/windows/whoisonline.h index bc04c8066..271bf6ffd 100644 --- a/src/gui/windows/whoisonline.h +++ b/src/gui/windows/whoisonline.h @@ -162,6 +162,8 @@ private: void updateSize(); + void handlerPlayerRelation(const std::string &nick, + OnlinePlayer *const player); /** * The thread function that download the files. */ @@ -178,11 +180,7 @@ private: const std::string &restrict color) const A_WARN_UNUSED; - void updateWindow(std::vector &restrict friends, - std::vector &restrict neutral, - std::vector &restrict disregard, - std::vector &restrict enemy, - size_t numOnline); + void updateWindow(size_t numOnline); enum DownloadStatus { @@ -216,6 +214,10 @@ private: std::set mOnlineNicks; Button *mUpdateButton; + std::vector mFriends; + std::vector mNeutral; + std::vector mDisregard; + std::vector mEnemy; bool mAllowUpdate; bool mShowLevel; bool mUpdateOnlineList; -- cgit v1.2.3-70-g09d2