From 292968544ed758ae58c29b613cb89a7b67e551fa Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 1 Feb 2012 04:15:19 +0300 Subject: Rename CustomServerDialog to EditServerDialog. Use EditServerDialog for adding new servers to ServerDialog. --- src/CMakeLists.txt | 2 + src/Makefile.am | 2 + src/gui/customserverdialog.cpp | 208 ----------------------------- src/gui/customserverdialog.h | 98 -------------- src/gui/editserverdialog.cpp | 239 +++++++++++++++++++++++++++++++++ src/gui/editserverdialog.h | 104 +++++++++++++++ src/gui/serverdialog.cpp | 296 ++++++++++++++--------------------------- src/gui/serverdialog.h | 39 ++---- src/net/serverinfo.h | 2 +- 9 files changed, 453 insertions(+), 537 deletions(-) delete mode 100644 src/gui/customserverdialog.cpp delete mode 100644 src/gui/customserverdialog.h create mode 100644 src/gui/editserverdialog.cpp create mode 100644 src/gui/editserverdialog.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d2c003559..955795a14 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -245,6 +245,8 @@ SET(SRCS gui/debugwindow.h gui/didyouknowwindow.cpp gui/didyouknowwindow.h + gui/editserverdialog.cpp + gui/editserverdialog.h gui/emotepopup.cpp gui/emotepopup.h gui/equipmentwindow.cpp diff --git a/src/Makefile.am b/src/Makefile.am index b39145c63..0e76af87c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -248,6 +248,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ gui/debugwindow.h \ gui/didyouknowwindow.cpp \ gui/didyouknowwindow.h \ + gui/editserverdialog.cpp \ + gui/editserverdialog.h \ gui/emotepopup.cpp \ gui/emotepopup.h \ gui/equipmentwindow.cpp \ diff --git a/src/gui/customserverdialog.cpp b/src/gui/customserverdialog.cpp deleted file mode 100644 index b84595d0d..000000000 --- a/src/gui/customserverdialog.cpp +++ /dev/null @@ -1,208 +0,0 @@ -/* - * The Mana Client - * Copyright (C) 2011-2012 The Mana Developers - * - * This file is part of The Mana Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "gui/customserverdialog.h" - -#include "configuration.h" - -#include "gui/okdialog.h" -#include "gui/sdlinput.h" -#include "gui/serverdialog.h" - -#include "gui/widgets/button.h" -#include "gui/widgets/dropdown.h" -#include "gui/widgets/label.h" -#include "gui/widgets/layout.h" -#include "gui/widgets/textfield.h" - -#include "utils/gettext.h" - -std::string TypeListModel::getElementAt(int elementIndex) -{ - if (elementIndex == 0) - return "TmwAthena"; - else if (elementIndex == 1) - return "ManaServ"; - else - return "Unknown"; -} - -CustomServerDialog::CustomServerDialog(ServerDialog *parent, int index): - Window(_("Custom Server"), true, parent), - mServerDialog(parent), - mIndex(index) -{ - setWindowName("CustomServerDialog"); - - Label *nameLabel = new Label(_("Name:")); - Label *serverAdressLabel = new Label(_("Address:")); - Label *portLabel = new Label(_("Port:")); - Label *typeLabel = new Label(_("Server type:")); - Label *descriptionLabel = new Label(_("Description:")); - mServerAddressField = new TextField(std::string()); - mPortField = new TextField(std::string()); - - mTypeListModel = new TypeListModel(); - mTypeField = new DropDown(mTypeListModel); - mTypeField->setSelected(0); // TmwAthena by default for now. - - mNameField = new TextField(std::string()); - mDescriptionField = new TextField(std::string()); - - mOkButton = new Button(_("Ok"), "addServer", this); - mCancelButton = new Button(_("Cancel"), "cancel", this); - - mServerAddressField->addActionListener(this); - mPortField->addActionListener(this); - - place(0, 0, nameLabel); - place(1, 0, mNameField, 4).setPadding(3); - place(0, 1, serverAdressLabel); - place(1, 1, mServerAddressField, 4).setPadding(3); - place(0, 2, portLabel); - place(1, 2, mPortField, 4).setPadding(3); - place(0, 3, typeLabel); - place(1, 3, mTypeField).setPadding(3); - place(0, 4, descriptionLabel); - place(1, 4, mDescriptionField, 4).setPadding(3); - place(4, 5, mOkButton); - place(3, 5, mCancelButton); - - // Do this manually instead of calling reflowLayout so we can enforce a - // minimum width. - int width = 0, height = 0; - getLayout().reflow(width, height); - if (width < 300) - { - width = 300; - getLayout().reflow(width, height); - } - if (height < 120) - { - height = 120; - getLayout().reflow(width, height); - } - - setContentSize(width, height); - - setMinWidth(getWidth()); - setMinHeight(getHeight()); - setDefaultSize(getWidth(), getHeight(), ImageRect::CENTER); - - setResizable(false); - addKeyListener(this); - - loadWindowState(); - - // Add the entry's info when in modify mode. - if (index > -1) - { - const ServerInfo &serverInfo = mServerDialog->mServers[index]; - mNameField->setText(serverInfo.name); - mDescriptionField->setText(serverInfo.description); - mServerAddressField->setText(serverInfo.hostname); - mPortField->setText(toString(serverInfo.port)); - mTypeField->setSelected(serverInfo.type ? ServerInfo::MANASERV : - ServerInfo::TMWATHENA); - } - - setLocationRelativeTo(getParentWindow()); - setVisible(true); - - mNameField->requestFocus(); -} - -CustomServerDialog::~CustomServerDialog() -{ - delete mTypeListModel; -} - -void CustomServerDialog::logic() -{ - Window::logic(); -} - -void CustomServerDialog::action(const gcn::ActionEvent &event) -{ - if (event.getId() == "ok") - { - // Give focus back to the server dialog. - mServerAddressField->requestFocus(); - } - if (event.getId() == "addServer") - { - // Check the given information - if (mServerAddressField->getText().empty() - || mPortField->getText().empty()) - { - OkDialog *dlg = new OkDialog(_("Error"), - _("Please at least type both the address and the port " - "of the server.")); - dlg->addActionListener(this); - } - else - { - mCancelButton->setEnabled(false); - mOkButton->setEnabled(false); - - ServerInfo serverInfo; - serverInfo.name = mNameField->getText(); - serverInfo.description = mDescriptionField->getText(); - serverInfo.hostname = mServerAddressField->getText(); - serverInfo.port = (short) atoi(mPortField->getText().c_str()); - switch (mTypeField->getSelected()) - { - case 0: - serverInfo.type = ServerInfo::TMWATHENA; - break; - case 1: - serverInfo.type = ServerInfo::MANASERV; - break; - default: - serverInfo.type = ServerInfo::UNKNOWN; - } - - // Tell the server has to be saved - serverInfo.save = true; - - //Add server - mServerDialog->saveCustomServers(serverInfo, mIndex); - scheduleDelete(); - } - } - else if (event.getId() == "cancel") - { - scheduleDelete(); - } -} - -void CustomServerDialog::keyPressed(gcn::KeyEvent &keyEvent) -{ - gcn::Key key = keyEvent.getKey(); - - if (key.getValue() == Key::ESCAPE) - { - scheduleDelete(); - } - else if (key.getValue() == Key::ENTER) - { - action(gcn::ActionEvent(NULL, mOkButton->getActionEventId())); - } -} diff --git a/src/gui/customserverdialog.h b/src/gui/customserverdialog.h deleted file mode 100644 index 25c320e81..000000000 --- a/src/gui/customserverdialog.h +++ /dev/null @@ -1,98 +0,0 @@ -/* - * The Mana Client - * Copyright (C) 2011-2012 The Mana Developers - * - * This file is part of The Mana Client. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef CUSTOMSERVERDIALOG_H -#define CUSTOMSERVERDIALOG_H - -class Button; -class Label; -class TextField; -class DropDown; -class ServerDialog; -class TypeListModel; - -#include "gui/widgets/window.h" - -#include "net/serverinfo.h" - -#include -#include -#include - - -/** - * Server Type List Model - */ -class TypeListModel : public gcn::ListModel -{ - public: - TypeListModel() {} - - /** - * Used to get number of line in the list - */ - int getNumberOfElements() { return 2; } - - /** - * Used to get an element from the list - */ - std::string getElementAt(int elementIndex); -}; - -/** - * The custom server addition dialog. - * - * \ingroup Interface - */ -class CustomServerDialog : public Window, - public gcn::ActionListener, - public gcn::KeyListener -{ - public: - CustomServerDialog(ServerDialog *parent, int index = -1); - - ~CustomServerDialog(); - - /** - * Called when receiving actions from the widgets. - */ - void action(const gcn::ActionEvent &event); - - void keyPressed(gcn::KeyEvent &keyEvent); - - void logic(); - - private: - TextField *mServerAddressField; - TextField *mPortField; - TextField *mNameField; - TextField *mDescriptionField; - Button *mOkButton; - Button *mCancelButton; - - DropDown *mTypeField; - TypeListModel *mTypeListModel; - - ServerDialog *mServerDialog; - // The index of the entry to modify, -1 when only adding a new entry. - int mIndex; -}; - -#endif // CUSTOMSERVERDIALOG_H diff --git a/src/gui/editserverdialog.cpp b/src/gui/editserverdialog.cpp new file mode 100644 index 000000000..43457456e --- /dev/null +++ b/src/gui/editserverdialog.cpp @@ -0,0 +1,239 @@ +/* + * The Mana Client + * Copyright (C) 2011-2012 The Mana Developers + * Copyright (C) 2012 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "gui/editserverdialog.h" + +#include "configuration.h" + +#include "gui/okdialog.h" +#include "gui/sdlinput.h" +#include "gui/serverdialog.h" + +#include "gui/widgets/button.h" +#include "gui/widgets/dropdown.h" +#include "gui/widgets/label.h" +#include "gui/widgets/layout.h" +#include "gui/widgets/textfield.h" + +#include "utils/gettext.h" + +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"; +} + +EditServerDialog::EditServerDialog(ServerDialog *parent, ServerInfo server, + int index) : + Window(_("Edit Server"), true, parent), + mServerDialog(parent), + mServer(server), + mIndex(index) +{ + setWindowName("EditServerDialog"); + + Label *nameLabel = new Label(_("Name:")); + Label *serverAdressLabel = new Label(_("Address:")); + Label *portLabel = new Label(_("Port:")); + Label *typeLabel = new Label(_("Server type:")); + Label *descriptionLabel = new Label(_("Description:")); + mServerAddressField = new TextField(std::string()); + mPortField = new TextField(std::string()); + mPortField->setNumeric(true); + mPortField->setRange(1, 65535); + + mTypeListModel = new TypeListModel(); + mTypeField = new DropDown(mTypeListModel); + mTypeField->setSelected(0); // TmwAthena by default for now. + + mNameField = new TextField(std::string()); + mDescriptionField = new TextField(std::string()); + + mOkButton = new Button(_("Ok"), "addServer", this); + mCancelButton = new Button(_("Cancel"), "cancel", this); + + mServerAddressField->addActionListener(this); + mPortField->addActionListener(this); + + place(0, 0, nameLabel); + place(1, 0, mNameField, 4).setPadding(3); + place(0, 1, serverAdressLabel); + place(1, 1, mServerAddressField, 4).setPadding(3); + place(0, 2, portLabel); + place(1, 2, mPortField, 4).setPadding(3); + place(0, 3, typeLabel); + place(1, 3, mTypeField).setPadding(3); + place(0, 4, descriptionLabel); + place(1, 4, mDescriptionField, 4).setPadding(3); + place(4, 5, mOkButton); + place(3, 5, mCancelButton); + + // Do this manually instead of calling reflowLayout so we can enforce a + // minimum width. + int width = 0, height = 0; + getLayout().reflow(width, height); + if (width < 300) + { + width = 300; + getLayout().reflow(width, height); + } + if (height < 120) + { + height = 120; + getLayout().reflow(width, height); + } + + setContentSize(width, height); + + setMinWidth(getWidth()); + setMinHeight(getHeight()); + setDefaultSize(getWidth(), getHeight(), ImageRect::CENTER); + + setResizable(false); + addKeyListener(this); + + loadWindowState(); + + mNameField->setText(mServer.name); + mDescriptionField->setText(mServer.description); + mServerAddressField->setText(mServer.hostname); + mPortField->setText(toString(mServer.port)); + + switch (mServer.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; + } + + setLocationRelativeTo(getParentWindow()); + setVisible(true); + + mNameField->requestFocus(); +} + +EditServerDialog::~EditServerDialog() +{ + delete mTypeListModel; +} + +void EditServerDialog::logic() +{ + Window::logic(); +} + +void EditServerDialog::action(const gcn::ActionEvent &event) +{ + if (event.getId() == "ok") + { + // Give focus back to the server dialog. + mServerAddressField->requestFocus(); + } + if (event.getId() == "addServer") + { + // Check the given information + if (mServerAddressField->getText().empty() + || mPortField->getText().empty()) + { + OkDialog *dlg = new OkDialog(_("Error"), + _("Please at least type both the address and the port " + "of the server.")); + dlg->addActionListener(this); + } + else + { + mCancelButton->setEnabled(false); + mOkButton->setEnabled(false); + + mServer.name = mNameField->getText(); + mServer.description = mDescriptionField->getText(); + mServer.hostname = mServerAddressField->getText(); + mServer.port = (short) atoi(mPortField->getText().c_str()); + + if (mTypeField) + { + switch (mTypeField->getSelected()) + { + case 0: + mServer.type = ServerInfo::TMWATHENA; + break; + case 1: + mServer.type = ServerInfo::EVOL; + break; +#ifdef MANASERV_SUPPORT + case 2: + mServer.type = ServerInfo::MANASERV; + break; +#endif + default: + mServer.type = ServerInfo::UNKNOWN; + } + } + else + { + mServer.type = ServerInfo::TMWATHENA; + } + + // Tell the server has to be saved + mServer.save = true; + + //Add server + mServerDialog->updateServer(mServer, mIndex); + scheduleDelete(); + } + } + else if (event.getId() == "cancel") + { + scheduleDelete(); + } +} + +void EditServerDialog::keyPressed(gcn::KeyEvent &keyEvent) +{ + gcn::Key key = keyEvent.getKey(); + + if (key.getValue() == Key::ESCAPE) + { + scheduleDelete(); + } + else if (key.getValue() == Key::ENTER) + { + action(gcn::ActionEvent(NULL, mOkButton->getActionEventId())); + } +} diff --git a/src/gui/editserverdialog.h b/src/gui/editserverdialog.h new file mode 100644 index 000000000..6035b0e50 --- /dev/null +++ b/src/gui/editserverdialog.h @@ -0,0 +1,104 @@ +/* + * The Mana Client + * Copyright (C) 2011-2012 The Mana Developers + * Copyright (C) 2012 The ManaPlus Developers + * + * This file is part of The Mana Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef EDITSERVERDIALOG_H +#define EDITSERVERDIALOG_H + +class Button; +class Label; +class TextField; +class DropDown; +class ServerDialog; +class TypeListModel; + +#include "gui/widgets/window.h" + +#include "net/serverinfo.h" + +#include +#include +#include + + +/** + * Server Type List Model + */ +class TypeListModel : public gcn::ListModel +{ + public: + TypeListModel() {} + + /** + * Used to get number of line in the list + */ + int getNumberOfElements() +#ifdef MANASERV_SUPPORT + { return 3; } +#else + { return 2; } +#endif + + /** + * Used to get an element from the list + */ + std::string getElementAt(int elementIndex); +}; + +/** + * The custom server addition dialog. + * + * \ingroup Interface + */ +class EditServerDialog : public Window, + public gcn::ActionListener, + public gcn::KeyListener +{ + public: + EditServerDialog(ServerDialog *parent, ServerInfo server, int index); + + ~EditServerDialog(); + + /** + * Called when receiving actions from the widgets. + */ + void action(const gcn::ActionEvent &event); + + void keyPressed(gcn::KeyEvent &keyEvent); + + void logic(); + + private: + TextField *mServerAddressField; + TextField *mPortField; + TextField *mNameField; + TextField *mDescriptionField; + Button *mOkButton; + Button *mCancelButton; + + DropDown *mTypeField; + TypeListModel *mTypeListModel; + + ServerDialog *mServerDialog; + ServerInfo mServer; + int mIndex; +}; + +#endif // EDITSERVERDIALOG_H diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp index 39ff19a89..cf2c3f0f9 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: @@ -249,11 +236,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 +250,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; @@ -353,17 +302,7 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): 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 +320,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( - 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 +375,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(); + } } } @@ -504,32 +417,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); @@ -581,24 +468,14 @@ 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(); + mDescription->setCaption(std::string()); } - mManualEntryButton->setEnabled(readOnly); + mAddEntryButton->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() @@ -731,12 +608,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 +633,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()) + { + mServers[index] = currentServer; + } + else { - if (*i == currentServer) + 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 +669,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 @@ -853,3 +746,8 @@ int ServerDialog::downloadUpdate(void *ptr, DownloadStatus status, return 0; } + +void ServerDialog::updateServer(ServerInfo server, int index) +{ + saveCustomServers(server, index); +} diff --git a/src/gui/serverdialog.h b/src/gui/serverdialog.h index d2fe0d25b..0618d4c4c 100644 --- a/src/gui/serverdialog.h +++ b/src/gui/serverdialog.h @@ -83,30 +83,6 @@ class ServersListModel : public gcn::ListModel ServerDialog *mParent; }; -/** - * Server Type List Model - */ -class TypeListModel : public gcn::ListModel -{ - public: - TypeListModel() {} - - /** - * Used to get number of line in the list - */ - int getNumberOfElements() -#ifdef MANASERV_SUPPORT - { return 3; } -#else - { return 2; } -#endif - - /** - * Used to get an element from the list - */ - std::string getElementAt(int elementIndex); -}; - /** * The server choice dialog. @@ -147,12 +123,16 @@ class ServerDialog : public Window, void logic(); + void updateServer(ServerInfo server, int index); + protected: friend class ServersListModel; MutexLocker lock() { return MutexLocker(&mMutex); } private: + friend class EditServerDialog; + /** * Called to load a list of available server from an online xml file. */ @@ -160,28 +140,25 @@ class ServerDialog : public Window, void loadServers(bool addNew = true); void loadCustomServers(); - void saveCustomServers(const ServerInfo ¤tServer = ServerInfo()); + void saveCustomServers(const ServerInfo ¤tServer = ServerInfo(), + int index = -1); static int downloadUpdate(void *ptr, DownloadStatus status, size_t total, size_t remaining); void setFieldsReadOnly(bool readOnly); - TextField *mServerNameField; - TextField *mPortField; Label *mDescription; Button *mQuitButton; Button *mConnectButton; - Button *mManualEntryButton; + Button *mAddEntryButton; + Button *mEditEntryButton; Button *mDeleteButton; Button *mLoadButton; ListBox *mServersList; ServersListModel *mServersListModel; - DropDown *mTypeField; - TypeListModel *mTypeListModel; - const std::string &mDir; enum ServerDialogDownloadStatus diff --git a/src/net/serverinfo.h b/src/net/serverinfo.h index fb6a99a33..b4bb1a2d8 100644 --- a/src/net/serverinfo.h +++ b/src/net/serverinfo.h @@ -54,7 +54,7 @@ public: ServerInfo() { type = TMWATHENA; - port = 0; + port = 6901; save = false; version.first = 0; } -- cgit v1.2.3-70-g09d2