From 5e7c538997a2a01c5139ed315b53a1fe5d7cb90c Mon Sep 17 00:00:00 2001 From: Blue Sans Douze Date: Mon, 21 Mar 2011 01:37:14 +0100 Subject: Add option for sorting servers list (issue 316) --- src/gui/serverdialog.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src/gui/serverdialog.cpp') diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp index ca8da9b4..b3311f55 100644 --- a/src/gui/serverdialog.cpp +++ b/src/gui/serverdialog.cpp @@ -52,6 +52,7 @@ #include #include #include +#include static const int MAX_SERVERLIST = 6; @@ -591,6 +592,59 @@ void ServerDialog::loadServers() if (!found) mServers.push_back(server); } + + reorderList(config.getIntValue("serverListOrder")); +} + +/** + * Returns true if serv1 must appear before serv2 + */ +bool ServerDialog::sortByLastUsage(const ServerInfo& serv1, const ServerInfo& serv2) +{ + int rank1 = -1; + int rank2 = -1; + + for (int i = 0; i < MAX_SERVERLIST; ++i) + { + const std::string index = toString(i); + const std::string nameKey = "MostUsedServerName" + index; + std::string serv = config.getValue(nameKey, ""); + if (serv == serv1.hostname) + rank1 = i; + else if (serv == serv2.hostname) + rank2 = i; + } + + if (rank1 > rank2) + return true; + + if (rank2 > rank1) + return false; + + if (rank1 == rank2) + return ServerDialog::sortByName(serv1, serv2); +} + +/** + * Returns true if serv1 must appear before serv2 + */ +bool ServerDialog::sortByName(const ServerInfo& serv1, const ServerInfo& serv2) +{ + return compareStrI(serv1.name, serv2.name) < 0; +} + +/** + * Reorders the server list + * @param orderBy + * - 0 : Order by last change (default) + * - 1 : Order by name + */ +void ServerDialog::reorderList(int orderBy) +{ + if (orderBy == 0) + std::sort(mServers.begin(), mServers.end(), ServerDialog::sortByLastUsage); + else + std::sort(mServers.begin(), mServers.end(), ServerDialog::sortByName); } void ServerDialog::loadCustomServers() -- cgit v1.2.3-60-g2f50