diff options
author | Andreas Habel <mail@exceptionfault.de> | 2009-10-06 22:00:44 +0200 |
---|---|---|
committer | Andreas Habel <mail@exceptionfault.de> | 2009-10-06 22:05:42 +0200 |
commit | 610dc30ceecdfe538f71826689630e0f28c278cc (patch) | |
tree | c986e5d55850c908e62e46c090c3c0b73505edd4 /src | |
parent | c1b93b0bfe2d7c4568de26af85523f83e72293d4 (diff) | |
download | mana-610dc30ceecdfe538f71826689630e0f28c278cc.tar.gz mana-610dc30ceecdfe538f71826689630e0f28c278cc.tar.bz2 mana-610dc30ceecdfe538f71826689630e0f28c278cc.tar.xz mana-610dc30ceecdfe538f71826689630e0f28c278cc.zip |
Implemented server selection list loaded from online xml file.
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/serverdialog.cpp | 121 | ||||
-rw-r--r-- | src/gui/serverdialog.h | 3 | ||||
-rw-r--r-- | src/main.cpp | 6 | ||||
-rw-r--r-- | src/main.h | 6 | ||||
-rw-r--r-- | src/net/serverinfo.h | 7 |
5 files changed, 125 insertions, 18 deletions
diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp index 4f42906a..3d254e59 100644 --- a/src/gui/serverdialog.cpp +++ b/src/gui/serverdialog.cpp @@ -36,6 +36,7 @@ #include "net/net.h" +#include "utils/xml.h" #include "utils/gettext.h" #include "utils/stringutils.h" @@ -52,9 +53,12 @@ int ServersListModel::getNumberOfElements() std::string ServersListModel::getElementAt(int elementIndex) { - std::string myServer = std::string(servers.at(elementIndex).hostname); + std::string myServer = servers.at(elementIndex).name; + myServer += " ("; + myServer += std::string(servers.at(elementIndex).hostname); myServer += ":"; myServer += toString(servers.at(elementIndex).port); + myServer += ")"; return myServer; } @@ -73,27 +77,51 @@ void ServersListModel::addElement(ServerInfo server) ServerDialog::ServerDialog(ServerInfo *serverInfo): Window(_("Choose Your Server")), mServerInfo(serverInfo) { + mServerDescription = new Label(std::string()); gcn::Label *serverLabel = new Label(_("Server:")); gcn::Label *portLabel = new Label(_("Port:")); mServerNameField = new TextField(mServerInfo->hostname); mPortField = new TextField(toString(mServerInfo->port)); - // Add the most used servers from config mMostUsedServersListModel = new ServersListModel; 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, ""); currentConfig = "MostUsedServerPort" + toString(i); currentServer.port = (short) config.getValue(currentConfig, 0); + if (!currentServer.hostname.empty() && currentServer.port != 0) { - mMostUsedServersListModel->addElement(currentServer); + 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); + } + } } @@ -113,18 +141,20 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo): mMostUsedServersList->setSelected(0); - place(0, 0, serverLabel); - place(0, 1, portLabel); - place(1, 0, mServerNameField, 3).setPadding(2); - place(1, 1, mPortField, 3).setPadding(2); - place(0, 2, usedScroll, 4, 5).setPadding(2); - place(2, 7, mQuitButton); - place(3, 7, mConnectButton); + place(0, 0, mServerDescription, 2); + place(0, 1, serverLabel); + place(0, 2, portLabel); + place(1, 1, mServerNameField, 3).setPadding(2); + place(1, 2, mPortField, 3).setPadding(2); + place(0, 3, usedScroll, 4, 5).setPadding(2); + place(2, 8, mQuitButton); + place(3, 8, mConnectButton); // Make sure the list has enough height - getLayout().setRowHeight(2, 60); + getLayout().setRowHeight(3, 80); + - reflowLayout(250, 0); + reflowLayout(300, 0); center(); setVisible(true); @@ -138,6 +168,7 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo): mConnectButton->requestFocus(); } } + } ServerDialog::~ServerDialog() @@ -187,6 +218,9 @@ void ServerDialog::action(const gcn::ActionEvent &event) { tempServer = mMostUsedServersListModel->getServer(i); + currentConfig = "MostUsedServerDescription" + toString(i); + config.setValue(currentConfig, tempServer.name); + currentConfig = "MostUsedServerName" + toString(i); config.setValue(currentConfig, tempServer.hostname); @@ -212,6 +246,69 @@ void ServerDialog::valueChanged(const gcn::SelectionEvent &event) // Update the server and post fields according to the new selection const ServerInfo myServer = mMostUsedServersListModel->getServer(index); + mServerDescription->setCaption(myServer.name); mServerNameField->setText(myServer.hostname); mPortField->setText(toString(myServer.port)); } + +void ServerDialog::loadServerlist() +{ + ServerInfo currentServer; + currentServer.clear(); + + // try to load the configuration value for the onlineServerList + std::string listFile = config.getValue("onlineServerList", "void"); + // if there is no entry, try to load the file from the default updatehost + if (listFile == "void") + listFile = config.getValue("updatehost", "http://updates.themanaworld.org") + + "/serverlist.xml"; + + xmlDocPtr doc = xmlReadFile(listFile.c_str(), NULL, 0); + if (doc == NULL) + { + logger->log("Failed to load online serverlist from %s", listFile.c_str()); + return; + } + + xmlNodePtr rootNode = xmlDocGetRootElement(doc); + int version = XML::getProperty(rootNode, "version", 3); + + if (version != 1) + { + fprintf(stderr, "Online server list has wrong version"); + return; + } + + for_each_xml_child_node(server, rootNode) + { + if (xmlStrEqual(server->name, BAD_CAST "server")) + { + //check wether the version matches + #ifdef TMWSERV_SUPPORT + if (XML::getProperty(server, "type", "unknown") != "TMWSERV") + continue; + #endif + + #ifdef EATHENA_SUPPORT + if (XML::getProperty(server, "type", "unknown") != "EATHENA") + continue; + #endif + + currentServer.clear(); + currentServer.name = XML::getProperty(server, "name", std::string()); + + for_each_xml_child_node(subnode, server) + { + if (xmlStrEqual(subnode->name, BAD_CAST "connection")) + { + currentServer.hostname = XML::getProperty(subnode, "hostname", std::string()); + currentServer.port = XML::getProperty(subnode, "port", DEFAULT_PORT); + } + } + + mMostUsedServersListModel->addElement(currentServer); + } + } + + xmlFreeDoc(doc); +} diff --git a/src/gui/serverdialog.h b/src/gui/serverdialog.h index 432d3a99..e6e366b7 100644 --- a/src/gui/serverdialog.h +++ b/src/gui/serverdialog.h @@ -106,8 +106,11 @@ class ServerDialog : public Window, void valueChanged(const gcn::SelectionEvent &event); private: + void loadServerlist(); + gcn::TextField *mServerNameField; gcn::TextField *mPortField; + gcn::Label *mServerDescription; gcn::Button *mQuitButton; gcn::Button *mConnectButton; diff --git a/src/main.cpp b/src/main.cpp index 52847b3e..82e8ff50 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -121,12 +121,6 @@ #include <sys/stat.h> #endif -#ifdef TWMSERV_SUPPORT -#define DEFAULT_PORT 9601 -#else -#define DEFAULT_PORT 6901 -#endif - namespace { struct SetupListener : public gcn::ActionListener @@ -77,6 +77,12 @@ #define MAX_CHARACTER_COUNT 3 +#ifdef TWMSERV_SUPPORT +#define DEFAULT_PORT 9601 +#else +#define DEFAULT_PORT 6901 +#endif + /* * Client different States */ diff --git a/src/net/serverinfo.h b/src/net/serverinfo.h index ebc67722..f410e516 100644 --- a/src/net/serverinfo.h +++ b/src/net/serverinfo.h @@ -27,11 +27,13 @@ class ServerInfo { public: + std::string name; std::string hostname; unsigned short port; void clear() { + name.clear(); hostname.clear(); port = 0; } @@ -40,6 +42,11 @@ public: { return (hostname == other.hostname && port == other.port); } + + bool operator!=(const ServerInfo &other) + { + return (hostname != other.hostname || port != other.port); + } }; #endif // SERVERINFO_H |