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 | |
parent | aec05fc42775374a82800980ef41c7acc7dbac1e (diff) | |
download | mana-55e37d20e1c8b6fecef5d4b52b8fce3a884bd0be.tar.gz mana-55e37d20e1c8b6fecef5d4b52b8fce3a884bd0be.tar.bz2 mana-55e37d20e1c8b6fecef5d4b52b8fce3a884bd0be.tar.xz mana-55e37d20e1c8b6fecef5d4b52b8fce3a884bd0be.zip |
Added sorting to online serverlist by last usage and merge of local and
remotely listed servers.
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/serverdialog.cpp | 97 | ||||
-rw-r--r-- | src/gui/serverdialog.h | 19 |
2 files changed, 75 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); } } diff --git a/src/gui/serverdialog.h b/src/gui/serverdialog.h index e6e366b7..62275e6a 100644 --- a/src/gui/serverdialog.h +++ b/src/gui/serverdialog.h @@ -65,10 +65,26 @@ class ServersListModel : public gcn::ListModel void addElement(ServerInfo server); /** + * Add an Element at the end of the server list if it + * doesn't exist yet. Otherwise overwrite its properties + * in the list. + * + * @param server ServerInfo to merge into the list. + */ + void mergeElement(ServerInfo server); + + /** * Add an Element at the beginning of the server list */ void addFirstElement(ServerInfo server); + /** + * Returns wheter the given server is already in the list. + * @param server Server to search in the list. + * @return True, if the server is in the list, false otherwise. + */ + bool contains(ServerInfo server); + private: std::vector<ServerInfo> servers; }; @@ -106,6 +122,9 @@ class ServerDialog : public Window, void valueChanged(const gcn::SelectionEvent &event); private: + /** + * Called to load a list of available server from an online xml file. + */ void loadServerlist(); gcn::TextField *mServerNameField; |