From 8cf8f82d0b933dab43372fb0afdda58687ce6af6 Mon Sep 17 00:00:00 2001
From: Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>
Date: Wed, 23 Nov 2011 19:01:08 +0100
Subject: Added a modify button to the server dialog.

This was righteously requested by Ablu as missing
when dealing with one's own entries.

Reviewed-by: Ablu
---
 src/gui/customserverdialog.cpp | 19 ++++++++++++---
 src/gui/customserverdialog.h   |  4 +++-
 src/gui/serverdialog.cpp       | 53 ++++++++++++++++++++++++++++++++----------
 src/gui/serverdialog.h         |  9 ++++++-
 4 files changed, 68 insertions(+), 17 deletions(-)

(limited to 'src')

diff --git a/src/gui/customserverdialog.cpp b/src/gui/customserverdialog.cpp
index 304665d6..589440a2 100644
--- a/src/gui/customserverdialog.cpp
+++ b/src/gui/customserverdialog.cpp
@@ -44,9 +44,10 @@ std::string TypeListModel::getElementAt(int elementIndex)
         return "Unknown";
 }
 
-CustomServerDialog::CustomServerDialog(ServerDialog *parent):
+CustomServerDialog::CustomServerDialog(ServerDialog *parent, int index):
     Window(_("Add a custom Server"), true, static_cast<Window*>(parent)),
-    mServerDialog(parent)
+    mServerDialog(parent),
+    mIndex(index)
 {
     setWindowName("CustomServerDialog");
 
@@ -110,6 +111,18 @@ CustomServerDialog::CustomServerDialog(ServerDialog *parent):
 
     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);
+    }
+
     setVisible(true);
 
     mNameField->requestFocus();
@@ -169,7 +182,7 @@ void CustomServerDialog::action(const gcn::ActionEvent &event)
             serverInfo.save = true;
 
             //Add server
-            mServerDialog->saveCustomServers(serverInfo);
+            mServerDialog->saveCustomServers(serverInfo, mIndex);
             scheduleDelete();
         }
     }
diff --git a/src/gui/customserverdialog.h b/src/gui/customserverdialog.h
index af19d9c7..a9f02abc 100644
--- a/src/gui/customserverdialog.h
+++ b/src/gui/customserverdialog.h
@@ -66,7 +66,7 @@ class CustomServerDialog : public Window,
                      public gcn::KeyListener
 {
     public:
-        CustomServerDialog(ServerDialog *parent);
+        CustomServerDialog(ServerDialog *parent, int index = -1);
 
         ~CustomServerDialog();
 
@@ -91,6 +91,8 @@ class CustomServerDialog : public Window,
         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/serverdialog.cpp b/src/gui/serverdialog.cpp
index d580d4d3..0e97dd0d 100644
--- a/src/gui/serverdialog.cpp
+++ b/src/gui/serverdialog.cpp
@@ -207,19 +207,21 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir):
 
     mQuitButton = new Button(_("Quit"), "quit", this);
     mConnectButton = new Button(_("Connect"), "connect", this);
-    mManualEntryButton = new Button(_("Custom Server"), "addEntry", this);
+    mManualEntryButton = new Button(_("Add custom Server..."), "addEntry", this);
+    mModifyButton = new Button(_("Modify..."), "modify", this);
     mDeleteButton = new Button(_("Delete"), "remove", this);
 
     mServersList->setActionEventId("connect");
     mServersList->addSelectionListener(this);
     usedScroll->setVerticalScrollAmount(0);
 
-    place(0, 0, usedScroll, 5, 5).setPadding(3);
+    place(0, 0, usedScroll, 6, 5).setPadding(3);
     place(0, 5, mDescription, 5);
     place(0, 6, mManualEntryButton);
-    place(1, 6, mDeleteButton);
-    place(3, 6, mQuitButton);
-    place(4, 6, mConnectButton);
+    place(1, 6, mModifyButton);
+    place(2, 6, mDeleteButton);
+    place(4, 6, mQuitButton);
+    place(5, 6, mConnectButton);
 
     // Make sure the list has enough height
     getLayout().setRowHeight(3, 80);
@@ -290,6 +292,7 @@ void ServerDialog::action(const gcn::ActionEvent &event)
             mConnectButton->setEnabled(false);
             mDeleteButton->setEnabled(false);
             mManualEntryButton->setEnabled(false);
+            mModifyButton->setEnabled(false);
 
             const ServerInfo &serverInfo = mServersListModel->getServer(index);
             mServerInfo->hostname = serverInfo.hostname;
@@ -314,6 +317,21 @@ void ServerDialog::action(const gcn::ActionEvent &event)
         // Add a custom server: It will delete itself using guichan logic.
         new CustomServerDialog(this);
     }
+    else if (event.getId() == "modify")
+    {
+        int index = mServersList->getSelected();
+        // Check whether a server is selected.
+        if (index < 0)
+        {
+            OkDialog *dlg = new OkDialog(_("Error"),
+                _("Please select a custom server."));
+            dlg->addActionListener(this);
+        }
+        else
+        {
+            new CustomServerDialog(this, index);
+        }
+    }
     else if (event.getId() == "remove")
     {
         int index = mServersList->getSelected();
@@ -344,6 +362,7 @@ void ServerDialog::valueChanged(const gcn::SelectionEvent &)
     if (index == -1)
     {
         mDeleteButton->setEnabled(false);
+        mModifyButton->setEnabled(false);
         return;
     }
 
@@ -352,6 +371,7 @@ void ServerDialog::valueChanged(const gcn::SelectionEvent &)
     mDescription->setCaption(myServer.description);
 
     mDeleteButton->setEnabled(myServer.save);
+    mModifyButton->setEnabled(myServer.save);
 }
 
 void ServerDialog::mouseClicked(gcn::MouseEvent &mouseEvent)
@@ -543,22 +563,29 @@ void ServerDialog::loadCustomServers()
     }
 }
 
-void ServerDialog::saveCustomServers(const ServerInfo &currentServer)
+void ServerDialog::saveCustomServers(const ServerInfo &currentServer, int index)
 {
     ServerInfos::iterator it, it_end = mServers.end();
 
     // Make sure the current server is mentioned first
     if (currentServer.isValid())
     {
-        for (it = mServers.begin(); it != it_end; ++it)
+        if (index > -1)
+        {
+            mServers[index] = currentServer;
+        }
+        else
         {
-            if (*it == currentServer)
+            for (it = mServers.begin(); it != it_end; ++it)
             {
-                mServers.erase(it);
-                break;
+                if (*it == currentServer)
+                {
+                    mServers.erase(it);
+                    break;
+                }
             }
+            mServers.insert(mServers.begin(), currentServer);
         }
-        mServers.insert(mServers.begin(), currentServer);
     }
 
     int savedServerCount = 0;
@@ -592,7 +619,9 @@ void ServerDialog::saveCustomServers(const ServerInfo &currentServer)
         config.setValue("MostUsedServerName" + toString(savedServerCount), "");
 
     // Restore the correct description
-    mDescription->setCaption(mServers[0].description);
+    if (index < 0)
+        index = 0;
+    mDescription->setCaption(mServers[index].description);
 }
 
 int ServerDialog::downloadUpdate(void *ptr, DownloadStatus status,
diff --git a/src/gui/serverdialog.h b/src/gui/serverdialog.h
index 611f65e9..1d7db0b2 100644
--- a/src/gui/serverdialog.h
+++ b/src/gui/serverdialog.h
@@ -116,7 +116,13 @@ class ServerDialog : public Window,
         MutexLocker lock() { return MutexLocker(&mMutex); }
 
         friend class CustomServerDialog;
-        void saveCustomServers(const ServerInfo &currentServer = ServerInfo());
+        /**
+         * Saves the new server entry in the custom server list.
+         * Removes the given entry when the serverInfo is empty.
+         * Modifies the server entry given at index when it's not -1.
+         */
+        void saveCustomServers(const ServerInfo &currentServer = ServerInfo(),
+                               int index = -1);
 
     private:
         /**
@@ -134,6 +140,7 @@ class ServerDialog : public Window,
         Button *mQuitButton;
         Button *mConnectButton;
         Button *mManualEntryButton;
+        Button *mModifyButton;
         Button *mDeleteButton;
 
         ListBox *mServersList;
-- 
cgit v1.2.3-70-g09d2