From c1ddf4c0840bb15016dc69af4a4751d09908393f Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 8 Jan 2010 16:03:29 +0200 Subject: Small refactoring in chat auto completing --- src/gui/chat.cpp | 58 +++++++++++++++++++++++++++++++++++-------------- src/gui/chat.h | 4 ++++ src/gui/partywindow.cpp | 23 +++----------------- src/gui/partywindow.h | 3 ++- 4 files changed, 51 insertions(+), 37 deletions(-) (limited to 'src/gui') diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index af0c3808..c9defe33 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -531,13 +531,16 @@ void ChatWindow::autoComplete() ChatTab *cTab = static_cast(mChatTabs->getSelectedTab()); + std::vector nameList; if (cTab && cTab->getType() == ChatTab::PARTY) { - newName = partyWindow->getAutoCompleteName(name); + partyWindow->getNames(nameList); + newName = autoComplete(nameList, name); } if (newName == "") { - newName = beingManager->getAutoCompletePlayerName(name); + beingManager->getPlayerNames(nameList, true); + newName = autoComplete(nameList, name); } if (newName == "") { @@ -556,10 +559,44 @@ void ChatWindow::autoComplete() } } +std::string ChatWindow::autoComplete(std::vector &names, + std::string partName) const +{ + std::vector::iterator i = names.begin(); + toLower(partName); + std::string newName(""); + + while (i != names.end()) + { + if (!i->empty()) + { + std::string name = *i; + toLower(name); + + std::string::size_type pos = name.find(partName, 0); + if (pos == 0) + { + if (newName != "") + { + toLower(newName); + newName = findSameSubstring(name, newName); + } + else + { + newName = *i; + } + } + } + ++i; + } + + return newName; +} + std::string ChatWindow::autoCompleteHistory(std::string partName) { History::iterator i = mHistory.begin(); - std::string newName = ""; + std::vector nameList; while (i != mHistory.end()) { @@ -572,20 +609,9 @@ std::string ChatWindow::autoCompleteHistory(std::string partName) line = line.substr(0, f); if (line != "") { - std::string::size_type pos = line.find(partName, 0); - if (pos == 0) - { - if (newName != "") - { - newName = findSameSubstring(line, newName); - } - else - { - newName = line; - } - } + nameList.push_back(line); } ++i; } - return newName; + return autoComplete(nameList, partName); } diff --git a/src/gui/chat.h b/src/gui/chat.h index b64ea5e8..4516257f 100644 --- a/src/gui/chat.h +++ b/src/gui/chat.h @@ -32,6 +32,7 @@ #include #include #include +#include class BrowserBox; class Channel; @@ -191,6 +192,9 @@ class ChatWindow : public Window, std::string autoCompleteHistory(std::string partName); + std::string autoComplete(std::vector &names, + std::string partName) const; + /** Used for showing item popup on clicking links **/ ItemLinkHandler *mItemLinkHandler; Recorder *mRecorder; diff --git a/src/gui/partywindow.cpp b/src/gui/partywindow.cpp index ae8112c8..67f1d4c0 100644 --- a/src/gui/partywindow.cpp +++ b/src/gui/partywindow.cpp @@ -254,11 +254,10 @@ void PartyWindow::buildLayout() } } -std::string PartyWindow::getAutoCompleteName(std::string partName) +void PartyWindow::getNames(std::vector &names) { PartyList::iterator i = mMembers.begin(); - std::transform(partName.begin(), partName.end(), partName.begin(), tolower); - std::string newName(""); + names.clear(); while (i != mMembers.end()) { @@ -266,24 +265,8 @@ std::string PartyWindow::getAutoCompleteName(std::string partName) if (member->getAvatar() && member->getAvatar()->getName() != "") { std::string name = member->getAvatar()->getName(); - std::transform(name.begin(), name.end(), name.begin(), tolower); - - std::string::size_type pos = name.find(partName, 0); - if (pos == 0) - { - if (newName != "") - { - std::transform(newName.begin(), newName.end(), newName.begin(), tolower); - newName = findSameSubstring(name, newName); - } - else - { - newName = member->getAvatar()->getName(); - } - } + names.push_back(name); } ++i; } - - return newName; } diff --git a/src/gui/partywindow.h b/src/gui/partywindow.h index ed217910..0fe2986f 100644 --- a/src/gui/partywindow.h +++ b/src/gui/partywindow.h @@ -32,6 +32,7 @@ #include #include +#include class PartyWindow; @@ -127,7 +128,7 @@ class PartyWindow : public Window, gcn::ActionListener void clearMembers(); - std::string getAutoCompleteName(std::string partName); + void getNames(std::vector &names); private: /** -- cgit v1.2.3-70-g09d2