diff options
author | Andreas Habel <mail@exceptionfault.de> | 2009-10-07 20:13:32 +0200 |
---|---|---|
committer | Andreas Habel <mail@exceptionfault.de> | 2009-10-07 20:13:32 +0200 |
commit | 55e37d20e1c8b6fecef5d4b52b8fce3a884bd0be (patch) | |
tree | 56706557444a3f528f5384f12e0fae8a819423b6 /src/gui/serverdialog.cpp | |
parent | aec05fc42775374a82800980ef41c7acc7dbac1e (diff) | |
download | mana-client-55e37d20e1c8b6fecef5d4b52b8fce3a884bd0be.tar.gz mana-client-55e37d20e1c8b6fecef5d4b52b8fce3a884bd0be.tar.bz2 mana-client-55e37d20e1c8b6fecef5d4b52b8fce3a884bd0be.tar.xz mana-client-55e37d20e1c8b6fecef5d4b52b8fce3a884bd0be.zip |
Added sorting to online serverlist by last usage and merge of local and
remotely listed servers.
Diffstat (limited to 'src/gui/serverdialog.cpp')
-rw-r--r-- | src/gui/serverdialog.cpp | 97 |
1 files changed, 56 insertions, 41 deletions
diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp index 3d254e59..fdb2aa09 100644 --- a/src/gui/serverdialog.cpp +++ b/src/gui/serverdialog.cpp @@ -74,6 +74,34 @@ void ServersListModel::addElement(ServerInfo server) servers.push_back(server); } +void ServersListModel::mergeElement(ServerInfo server) +{ + // search through the list + for (int i = 0; i < getNumberOfElements(); i++) + { + // the server is already in the list, merge its properties + if (servers[i] == server) + { + servers[i].name = server.name; + return; + } + } + // the server is not found, add it at the end of the list + addElement(server); +} + +bool ServersListModel::contains(ServerInfo server) +{ + // search through the list + for (int i = 0; i < getNumberOfElements(); i++) + { + if (servers[i] == server) + return true; + } + return false; +} + + ServerDialog::ServerDialog(ServerInfo *serverInfo): Window(_("Choose Your Server")), mServerInfo(serverInfo) { @@ -87,20 +115,12 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo): ServerInfo currentServer; ServerInfo tempServer; - // load a list with online servers... - loadServerlist(); - // Add the most used servers from config if they are not in the online list std::string currentConfig = ""; - bool newEntry; - for (int i = 0; i <= MAX_SERVERLIST; i++) { currentServer.clear(); - currentConfig = "MostUsedServerDescription" + toString(i); - currentServer.name = config.getValue(currentConfig, ""); - currentConfig = "MostUsedServerName" + toString(i); currentServer.hostname = config.getValue(currentConfig, ""); @@ -109,22 +129,14 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo): if (!currentServer.hostname.empty() && currentServer.port != 0) { - newEntry = true; - for (int i = 0; i < mMostUsedServersListModel->getNumberOfElements(); i++) - { - tempServer = mMostUsedServersListModel->getServer(i); - if (tempServer != currentServer) - { - newEntry = false; - break; - } - if (!newEntry) - mMostUsedServersListModel->addElement(currentServer); - } - + if (!mMostUsedServersListModel->contains(currentServer)) + mMostUsedServersListModel->addElement(currentServer); } } + // load a list with online servers... + loadServerlist(); + mMostUsedServersList = new ListBox(mMostUsedServersListModel); ScrollArea *usedScroll = new ScrollArea(mMostUsedServersList); usedScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); @@ -202,30 +214,32 @@ void ServerDialog::action(const gcn::ActionEvent &event) ServerInfo tempServer; currentServer.hostname = mServerNameField->getText(); currentServer.port = (short) atoi(mPortField->getText().c_str()); - bool newEntry = true; - for (int i = 0; i < mMostUsedServersListModel->getNumberOfElements(); i++) - { - tempServer = mMostUsedServersListModel->getServer(i); - if (tempServer.hostname == mServerInfo->hostname && - tempServer.port == mServerInfo->port) - newEntry = false; - } - if (newEntry) - mMostUsedServersListModel->addFirstElement(currentServer); - // Write the entry in config + + // now rewrite the configuration... + // id = 0 is always the last selected server + config.setValue("MostUsedServerName0", currentServer.hostname); + config.setValue("MostUsedServerPort0", currentServer.port); + + // now add the rest of the list... std::string currentConfig = ""; + int configCount = 1; for (int i = 0; i < mMostUsedServersListModel->getNumberOfElements(); i++) { tempServer = mMostUsedServersListModel->getServer(i); - currentConfig = "MostUsedServerDescription" + toString(i); - config.setValue(currentConfig, tempServer.name); - - currentConfig = "MostUsedServerName" + toString(i); - config.setValue(currentConfig, tempServer.hostname); + // ensure, that our server will not be added twice + if (tempServer != currentServer) + { + currentConfig = "MostUsedServerName" + toString(configCount); + config.setValue(currentConfig, toString(tempServer.hostname)); + currentConfig = "MostUsedServerPort" + toString(configCount); + config.setValue(currentConfig, toString(tempServer.port)); + configCount++; + } - currentConfig = "MostUsedServerPort" + toString(i); - config.setValue(currentConfig, toString(tempServer.port)); + // stop if we exceed the number of maximum config entries + if (configCount >= MAX_SERVERLIST) + break; } mServerInfo->hostname = currentServer.hostname; mServerInfo->port = currentServer.port; @@ -275,7 +289,7 @@ void ServerDialog::loadServerlist() if (version != 1) { - fprintf(stderr, "Online server list has wrong version"); + logger->log("Online server list has wrong version"); return; } @@ -306,7 +320,8 @@ void ServerDialog::loadServerlist() } } - mMostUsedServersListModel->addElement(currentServer); + // merge the server into the local list + mMostUsedServersListModel->mergeElement(currentServer); } } |