diff options
Diffstat (limited to 'src/gui/serverdialog.cpp')
-rw-r--r-- | src/gui/serverdialog.cpp | 329 |
1 files changed, 110 insertions, 219 deletions
diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp index 39ff19a89..59bdf9660 100644 --- a/src/gui/serverdialog.cpp +++ b/src/gui/serverdialog.cpp @@ -28,6 +28,7 @@ #include "logger.h" #include "main.h" +#include "gui/editserverdialog.h" #include "gui/gui.h" #include "gui/logindialog.h" #include "gui/okdialog.h" @@ -117,8 +118,8 @@ std::string ServersListModel::getElementAt(int elementIndex) const ServerInfo &server = mServers->at(elementIndex); std::string myServer; myServer += server.hostname; - myServer += ":"; - myServer += toString(server.port); +// myServer += ":"; +// myServer += toString(server.port); return myServer; } @@ -138,20 +139,6 @@ void ServersListModel::setVersionString(int index, const std::string &version) } } -std::string TypeListModel::getElementAt(int elementIndex) -{ - if (elementIndex == 0) - return "TmwAthena"; - else if (elementIndex == 1) - return "Evol"; -#ifdef MANASERV_SUPPORT - else if (elementIndex == 2) - return "ManaServ"; -#endif - else - return "Unknown"; -} - class ServersListBox : public ListBox { public: @@ -196,20 +183,24 @@ public: graphics->setColor(mTextColor); int top; + int x = 2; if (!info.name.empty()) { graphics->setFont(boldFont); + x += boldFont->getWidth(info.name) + 15; graphics->drawText(info.name, 2, y); - top = y + height / 2; + top = y + boldFont->getHeight() + 2; } else { - top = y + height / 4; + top = y + height / 4 + 2; } graphics->setFont(getFont()); + if (!info.description.empty()) + graphics->drawText(info.description, x, y); graphics->drawText(model->getElementAt(i), 2, top); if (info.version.first > 0) @@ -224,7 +215,7 @@ public: unsigned int getRowHeight() const { - return 2 * getFont()->getHeight(); + return 2 * getFont()->getHeight() + 5; } private: gcn::Color mHighlightColor; @@ -249,11 +240,6 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): setWindowName("ServerDialog"); - Label *serverLabel = new Label(_("Server:")); - Label *portLabel = new Label(_("Port:")); - - mServerNameField = new TextField(mServerInfo->hostname); - mPortField = new TextField(toString(mServerInfo->port)); mPersistentIPCheckBox = new CheckBox(_("Use same ip for game sub servers"), config.getBoolValue("usePersistentIP"), this, "persitent ip"); @@ -268,66 +254,33 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): ScrollArea *usedScroll = new ScrollArea(mServersList); usedScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); - Label *typeLabel = new Label(_("Server type:")); - mTypeListModel = new TypeListModel(); - mTypeField = new DropDown(mTypeListModel); - switch (serverInfo->type) - { - case ServerInfo::MANASERV: -#ifdef MANASERV_SUPPORT - mTypeField->setSelected(2); - break; -#endif - default: - case ServerInfo::UNKNOWN: - case ServerInfo::TMWATHENA: - mTypeField->setSelected(0); - break; - case ServerInfo::EVOL: - mTypeField->setSelected(1); - break; - } - int n = 1; + int n = 0; mDescription = new Label(std::string()); mQuitButton = new Button(_("Quit"), "quit", this); mLoadButton = new Button(_("Load"), "load", this); mConnectButton = new Button(_("Connect"), "connect", this); - mManualEntryButton = new Button(_("Custom Server"), "addEntry", this); + mAddEntryButton = new Button(_("Add"), "addEntry", this); + mEditEntryButton = new Button(_("Edit"), "editEntry", this); mDeleteButton = new Button(_("Delete"), "remove", this); - mServerNameField->setActionEventId("connect"); - mPortField->setActionEventId("connect"); - - mServerNameField->addActionListener(this); - mPortField->addActionListener(this); - mManualEntryButton->addActionListener(this); mServersList->addSelectionListener(this); usedScroll->setVerticalScrollAmount(0); - place(0, 0, serverLabel); - place(1, 0, mServerNameField, 5).setPadding(3); - place(0, 1, portLabel); - place(1, 1, mPortField, 5).setPadding(3); - place(0, 2, typeLabel); - place(1, 2, mTypeField, 5).setPadding(3); - place(0, 2 + n, usedScroll, 6, 5).setPadding(3); - place(0, 7 + n, mDescription, 6); - place(0, 8 + n, mPersistentIPCheckBox, 6); - place(0, 9 + n, mManualEntryButton); - place(1, 9 + n, mDeleteButton); - place(2, 9 + n, mLoadButton); - place(4, 9 + n, mQuitButton); - place(5, 9 + n, mConnectButton); + place(0, 0 + n, usedScroll, 7, 5).setPadding(3); + place(0, 5 + n, mDescription, 7); + place(0, 6 + n, mPersistentIPCheckBox, 7); + place(0, 7 + n, mAddEntryButton); + place(1, 7 + n, mEditEntryButton); + place(2, 7 + n, mLoadButton); + place(3, 7 + n, mDeleteButton); + place(5, 7 + n, mQuitButton); + place(6, 7 + n, mConnectButton); // Make sure the list has enough height - getLayout().setRowHeight(3, 80); + getLayout().setRowHeight(0, 80); -/* - reflowLayout(400, 300); - setDefaultSize(400, 300, ImageRect::CENTER); -*/ // Do this manually instead of calling reflowLayout so we can enforce a // minimum width. int width = 0, height = 0; @@ -349,21 +302,10 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): loadWindowState(); - setFieldsReadOnly(true); mServersList->setSelected(0); // Do this after for the Delete button setVisible(true); - if (mServerNameField->getText().empty()) - { - mServerNameField->requestFocus(); - } - else - { - if (mPortField->getText().empty()) - mPortField->requestFocus(); - else - mConnectButton->requestFocus(); - } + mConnectButton->requestFocus(); loadServers(true); @@ -381,82 +323,48 @@ ServerDialog::~ServerDialog() } delete mServersListModel; mServersListModel = nullptr; - delete mTypeListModel; - mTypeListModel = nullptr; } void ServerDialog::action(const gcn::ActionEvent &event) { - if (event.getId() == "ok") - { - // Give focus back to the server dialog. - mServerNameField->requestFocus(); - } - else if (event.getId() == "connect") + if (event.getId() == "connect") { - // Check login - if (mServerNameField->getText().empty() - || mPortField->getText().empty()) - { - OkDialog *dlg = new OkDialog(_("Error"), - _("Please type both the address and the port of a server.")); - dlg->addActionListener(this); - } - else - { - if (mDownload) - mDownload->cancel(); - - mQuitButton->setEnabled(false); - mConnectButton->setEnabled(false); - mLoadButton->setEnabled(false); + if (Client::getState() == STATE_CONNECT_SERVER) + return; - mServerInfo->hostname = mServerNameField->getText(); - mServerInfo->port = static_cast<short>( - atoi(mPortField->getText().c_str())); + if (mDownload) + mDownload->cancel(); - if (mTypeField) - { - switch (mTypeField->getSelected()) - { - case 0: - mServerInfo->type = ServerInfo::TMWATHENA; - break; - case 1: - mServerInfo->type = ServerInfo::EVOL; - break; -#ifdef MANASERV_SUPPORT - case 2: - mServerInfo->type = ServerInfo::MANASERV; - break; -#endif - default: - mServerInfo->type = ServerInfo::UNKNOWN; - } - } - else - { - mServerInfo->type = ServerInfo::TMWATHENA; - } + mQuitButton->setEnabled(false); + mConnectButton->setEnabled(false); + mLoadButton->setEnabled(false); - // Save the selected server - mServerInfo->save = true; + int index = mServersList->getSelected(); + if (index < 0) + return; - if (chatLogger) - chatLogger->setServerName(mServerInfo->hostname); + ServerInfo server = mServers.at(index); + mServerInfo->hostname = server.hostname; + mServerInfo->port = server.port; + mServerInfo->type = server.type; + mServerInfo->name = server.name; + mServerInfo->description = server.description; + mServerInfo->save = true; - saveCustomServers(*mServerInfo); + if (chatLogger) + chatLogger->setServerName(mServerInfo->hostname); - if (!LoginDialog::savedPasswordKey.empty()) - { - if (mServerInfo->hostname != LoginDialog::savedPasswordKey) - LoginDialog::savedPassword = ""; - } + saveCustomServers(*mServerInfo); - config.setValue("usePersistentIP", - mPersistentIPCheckBox->isSelected()); - Client::setState(STATE_CONNECT_SERVER); + if (!LoginDialog::savedPasswordKey.empty()) + { + if (mServerInfo->hostname != LoginDialog::savedPasswordKey) + LoginDialog::savedPassword = ""; } + + config.setValue("usePersistentIP", + mPersistentIPCheckBox->isSelected()); + Client::setState(STATE_CONNECT_SERVER); } else if (event.getId() == "quit") { @@ -470,15 +378,23 @@ void ServerDialog::action(const gcn::ActionEvent &event) } else if (event.getId() == "addEntry") { - setFieldsReadOnly(false); + new EditServerDialog(this, ServerInfo(), -1); + } + else if (event.getId() == "editEntry") + { + int index = mServersList->getSelected(); + if (index >= 0) + new EditServerDialog(this, mServers.at(index), index); } else if (event.getId() == "remove") { int index = mServersList->getSelected(); - mServersList->setSelected(0); - mServers.erase(mServers.begin() + index); - - saveCustomServers(); + if (index >= 0) + { + mServersList->setSelected(0); + mServers.erase(mServers.begin() + index); + saveCustomServers(); + } } } @@ -503,34 +419,6 @@ void ServerDialog::valueChanged(const gcn::SelectionEvent &) // Update the server and post fields according to the new selection const ServerInfo &myServer = mServersListModel->getServer(index); - mDescription->setCaption(myServer.description); - mServerNameField->setText(myServer.hostname); - mPortField->setText(toString(myServer.port)); - if (mTypeField) - { - switch (myServer.type) - { - case ServerInfo::TMWATHENA: - case ServerInfo::UNKNOWN: -#ifdef MANASERV_SUPPORT - default: - mTypeField->setSelected(0); - break; - case ServerInfo::MANASERV: - mTypeField->setSelected(2); - break; -#else - case ServerInfo::MANASERV: - default: - mTypeField->setSelected(0); - break; -#endif - case ServerInfo::EVOL: - mTypeField->setSelected(1); - break; - } - } - setFieldsReadOnly(true); mDeleteButton->setEnabled(myServer.save); } @@ -577,30 +465,6 @@ void ServerDialog::logic() Window::logic(); } -void ServerDialog::setFieldsReadOnly(bool readOnly) -{ - if (!readOnly) - { - mDescription->setCaption(std::string()); - mServersList->setSelected(-1); - - mServerNameField->setText(std::string()); - mPortField->setText(std::string("6901")); - - mServerNameField->requestFocus(); - } - - mManualEntryButton->setEnabled(readOnly); - mDeleteButton->setEnabled(false); - mLoadButton->setEnabled(readOnly); - mDescription->setVisible(readOnly); - - mServerNameField->setEnabled(!readOnly); - mPortField->setEnabled(!readOnly); - if (mTypeField) - mTypeField->setEnabled(!readOnly); -} - void ServerDialog::downloadServerList() { // Try to load the configuration value for the onlineServerList @@ -647,6 +511,9 @@ void ServerDialog::loadServers(bool addNew) return; } + const std::string lang = getLangShort(); + const std::string description2 = "description_" + lang; + for_each_xml_child_node(serverNode, rootNode) { if (!xmlNameEqual(serverNode, "server")) @@ -696,7 +563,9 @@ void ServerDialog::loadServers(bool addNew) server.port = defaultPortForServerType(server.type); } } - else if (xmlNameEqual(subNode, "description")) + else if ((xmlNameEqual(subNode, "description") + && server.description.empty()) || (!lang.empty() + && xmlNameEqual(subNode, description2.c_str()))) { server.description = reinterpret_cast<const char*>( subNode->xmlChildrenNode->content); @@ -716,6 +585,7 @@ void ServerDialog::loadServers(bool addNew) // Use the name listed in the server list mServers[i].name = server.name; mServers[i].version = server.version; + mServers[i].description = server.description; mServersListModel->setVersionString(i, version); found = true; break; @@ -731,12 +601,16 @@ void ServerDialog::loadCustomServers() for (int i = 0; i < MAX_SERVERLIST; ++i) { const std::string index = toString(i); - const std::string nameKey = "MostUsedServerName" + index; + const std::string nameKey = "MostUsedServerDescName" + index; + const std::string descKey = "MostUsedServerDescription" + index; + const std::string hostKey = "MostUsedServerName" + index; const std::string typeKey = "MostUsedServerType" + index; const std::string portKey = "MostUsedServerPort" + index; ServerInfo server; - server.hostname = config.getValue(nameKey, ""); + server.name = config.getValue(nameKey, ""); + server.description = config.getValue(descKey, ""); + server.hostname = config.getValue(hostKey, ""); server.type = ServerInfo::parseType(config.getValue(typeKey, "")); const int defaultPort = defaultPortForServerType(server.type); @@ -752,21 +626,29 @@ void ServerDialog::loadCustomServers() } } -void ServerDialog::saveCustomServers(const ServerInfo ¤tServer) +void ServerDialog::saveCustomServers(const ServerInfo ¤tServer, + int index) { // Make sure the current server is mentioned first if (currentServer.isValid()) { - ServerInfos::iterator i, i_end = mServers.end(); - for (i = mServers.begin(); i != i_end; ++i) + if (index >= 0 && (unsigned)index < mServers.size()) { - if (*i == currentServer) + mServers[index] = currentServer; + } + else + { + ServerInfos::iterator i, i_end = mServers.end(); + for (i = mServers.begin(); i != i_end; ++i) { - mServers.erase(i); - break; + if (*i == currentServer) + { + mServers.erase(i); + break; + } } + mServers.insert(mServers.begin(), currentServer); } - mServers.insert(mServers.begin(), currentServer); } int savedServerCount = 0; @@ -780,15 +662,19 @@ void ServerDialog::saveCustomServers(const ServerInfo ¤tServer) 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; + const std::string num = toString(savedServerCount); + const std::string nameKey = "MostUsedServerDescName" + num; + const std::string descKey = "MostUsedServerDescription" + num; + const std::string hostKey = "MostUsedServerName" + num; + const std::string typeKey = "MostUsedServerType" + num; + const std::string portKey = "MostUsedServerPort" + num; - config.setValue(nameKey, toString(server.hostname)); + config.setValue(nameKey, toString(server.name)); + config.setValue(descKey, toString(server.description)); + config.setValue(hostKey, toString(server.hostname)); config.setValue(typeKey, serverTypeToString(server.type)); config.setValue(portKey, toString(server.port)); - ++savedServerCount; + ++ savedServerCount; } // Insert an invalid entry at the end to make the loading stop there @@ -799,7 +685,7 @@ void ServerDialog::saveCustomServers(const ServerInfo ¤tServer) int ServerDialog::downloadUpdate(void *ptr, DownloadStatus status, size_t total, size_t remaining) { - if (status == DOWNLOAD_STATUS_CANCELLED) + if (!ptr || status == DOWNLOAD_STATUS_CANCELLED) return -1; ServerDialog *sd = reinterpret_cast<ServerDialog*>(ptr); @@ -853,3 +739,8 @@ int ServerDialog::downloadUpdate(void *ptr, DownloadStatus status, return 0; } + +void ServerDialog::updateServer(ServerInfo server, int index) +{ + saveCustomServers(server, index); +} |