diff options
Diffstat (limited to 'src/gui/serverdialog.cpp')
-rw-r--r-- | src/gui/serverdialog.cpp | 125 |
1 files changed, 70 insertions, 55 deletions
diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp index 6939d1e7..d68e2eca 100644 --- a/src/gui/serverdialog.cpp +++ b/src/gui/serverdialog.cpp @@ -21,6 +21,7 @@ #include "gui/serverdialog.h" +#include "chatlog.h" #include "client.h" #include "configuration.h" #include "gui.h" @@ -29,7 +30,6 @@ #include "gui/okdialog.h" #include "gui/sdlinput.h" -#include "gui/theme.h" #include "gui/widgets/button.h" #include "gui/widgets/dropdown.h" @@ -41,16 +41,18 @@ #include "net/net.h" +#include "resources/theme.h" + #include "utils/gettext.h" #include "utils/stringutils.h" #include "utils/xml.h" -#include "widgets/dropdown.h" #include <guichan/font.hpp> #include <cstdlib> #include <iostream> #include <string> +#include <algorithm> static const int MAX_SERVERLIST = 6; @@ -198,18 +200,13 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): mDownloadStatus(DOWNLOADING_PREPARING), mDownloadProgress(-1.0f), mServers(ServerInfos()), -#ifndef MANASERV_SUPPORT - mManaservServers(ServerInfos()), -#endif mServerInfo(serverInfo) { setWindowName("ServerDialog"); Label *serverLabel = new Label(_("Server:")); Label *portLabel = new Label(_("Port:")); -#ifdef MANASERV_SUPPORT Label *typeLabel = new Label(_("Server type:")); -#endif mServerNameField = new TextField(mServerInfo->hostname); mPortField = new TextField(toString(mServerInfo->port)); @@ -247,7 +244,6 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): place(1, 0, mServerNameField, 4).setPadding(3); place(0, 1, portLabel); place(1, 1, mPortField, 4).setPadding(3); -#ifdef MANASERV_SUPPORT place(0, 2, typeLabel); place(1, 2, mTypeField, 4).setPadding(3); place(0, 3, usedScroll, 5, 5).setPadding(3); @@ -256,14 +252,6 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): place(1, 9, mDeleteButton); place(3, 9, mQuitButton); place(4, 9, mConnectButton); -#else - place(0, 2, usedScroll, 5, 5).setPadding(3); - place(0, 7, mDescription, 5); - place(0, 8, mManualEntryButton); - place(1, 8, mDeleteButton); - place(3, 8, mQuitButton); - place(4, 8, mConnectButton); -#endif // Make sure the list has enough height getLayout().setRowHeight(3, 80); @@ -360,6 +348,8 @@ void ServerDialog::action(const gcn::ActionEvent &event) // Save the selected server mServerInfo->save = true; + chatLogger->setServerName(mServerInfo->hostname); + saveCustomServers(*mServerInfo); Client::setState(STATE_CONNECT_SERVER); @@ -427,6 +417,16 @@ void ServerDialog::valueChanged(const gcn::SelectionEvent &) mDeleteButton->setEnabled(myServer.save); } +void ServerDialog::mouseClicked(gcn::MouseEvent &mouseEvent) +{ + if (mouseEvent.getSource() == mServersList && + isDoubleClick(mServersList->getSelected())) + { + action(gcn::ActionEvent(mConnectButton, + mConnectButton->getActionEventId())); + } +} + void ServerDialog::logic() { { @@ -468,12 +468,7 @@ void ServerDialog::setFieldsReadOnly(bool readOnly) mServersList->setSelected(-1); mServerNameField->setText(std::string()); -#ifdef MANASERV_SUPPORT mPortField->setText(std::string()); -#else - mPortField->setText(std::string("6901")); -#endif - mServerNameField->requestFocus(); } @@ -489,10 +484,10 @@ void ServerDialog::setFieldsReadOnly(bool readOnly) void ServerDialog::downloadServerList() { // Try to load the configuration value for the onlineServerList - std::string listFile = branding.getValue("onlineServerList", std::string()); + std::string listFile = branding.getStringValue("onlineServerList"); if (listFile.empty()) - listFile = config.getValue("onlineServerList", std::string()); + listFile = config.getStringValue("onlineServerList"); // Fall back to manasource.org when neither branding nor config set it if (listFile.empty()) @@ -594,13 +589,61 @@ void ServerDialog::loadServers() } } -#ifdef MANASERV_SUPPORT if (!found) -#else - if (!found && server.type != ServerInfo::MANASERV) -#endif 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; + + 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() @@ -625,14 +668,7 @@ void ServerDialog::loadCustomServers() server.save = true; -#ifdef MANASERV_SUPPORT mServers.push_back(server); -#else - if (server.type == ServerInfo::MANASERV) - mManaservServers.push_back(server); - else - mServers.push_back(server); -#endif } } @@ -675,27 +711,6 @@ void ServerDialog::saveCustomServers(const ServerInfo ¤tServer) ++savedServerCount; } -#ifndef MANASERV_SUPPORT - for (unsigned i = 0; - i < mManaservServers.size() && savedServerCount < MAX_SERVERLIST; ++i) - { - const ServerInfo &server = mManaservServers.at(i); - - // Only save servers that were loaded from settings - if (!(server.save && server.isValid())) - continue; - - const std::string index = toString(savedServerCount); - const std::string nameKey = "MostUsedServerName" + index; - const std::string typeKey = "MostUsedServerType" + index; - const std::string portKey = "MostUsedServerPort" + index; - - config.setValue(nameKey, toString(server.hostname)); - config.setValue(typeKey, serverTypeToString(server.type)); - config.setValue(portKey, toString(server.port)); - ++savedServerCount; - } -#endif // Insert an invalid entry at the end to make the loading stop there if (savedServerCount < MAX_SERVERLIST) config.setValue("MostUsedServerName" + toString(savedServerCount), ""); |