From 95532bf76d70bafbb7d0bc0e2dd0fc92fd7f74cf Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Sun, 5 Oct 2014 20:59:37 +0300
Subject: Add char rename button if server support it.

---
 src/CMakeLists.txt                    |  2 ++
 src/Makefile.am                       |  2 ++
 src/gui/windows/charselectdialog.cpp  | 22 +++++++++++++++
 src/gui/windows/charselectdialog.h    |  1 +
 src/listeners/charrenamelistener.cpp  | 46 +++++++++++++++++++++++++++++++
 src/listeners/charrenamelistener.h    | 52 +++++++++++++++++++++++++++++++++++
 src/net/charserverhandler.h           |  2 +-
 src/net/eathena/charserverhandler.cpp | 30 ++++++++++++++++----
 src/net/eathena/charserverhandler.h   |  2 +-
 src/net/tmwa/charserverhandler.cpp    |  2 +-
 src/net/tmwa/charserverhandler.h      |  2 +-
 11 files changed, 153 insertions(+), 10 deletions(-)
 create mode 100644 src/listeners/charrenamelistener.cpp
 create mode 100644 src/listeners/charrenamelistener.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 04aafa099..7383177f0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -795,6 +795,8 @@ SET(SRCS
     listeners/awaylistener.cpp
     listeners/awaylistener.h
     listeners/baselistener.hpp
+    listeners/charrenamelistener.cpp
+    listeners/charrenamelistener.h
     actormanager.cpp
     actormanager.h
     animatedsprite.cpp
diff --git a/src/Makefile.am b/src/Makefile.am
index e16287f51..aec94c27b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -916,6 +916,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \
 	      listeners/awaylistener.cpp \
 	      listeners/awaylistener.h \
 	      listeners/baselistener.hpp \
+	      listeners/charrenamelistener.cpp \
+	      listeners/charrenamelistener.h \
 	      actormanager.cpp \
 	      actormanager.h \
 	      animatedsprite.cpp \
diff --git a/src/gui/windows/charselectdialog.cpp b/src/gui/windows/charselectdialog.cpp
index 5b4822a17..32a3ea410 100644
--- a/src/gui/windows/charselectdialog.cpp
+++ b/src/gui/windows/charselectdialog.cpp
@@ -32,6 +32,7 @@
 
 #include "being/attributes.h"
 
+#include "listeners/charrenamelistener.h"
 #include "listeners/pincodelistener.h"
 
 #include "gui/dialogtype.h"
@@ -53,6 +54,7 @@
 #include "net/logindata.h"
 #include "net/loginhandler.h"
 #include "net/registrationoptions.h"
+#include "net/serverfeatures.h"
 
 #include "debug.h"
 
@@ -80,6 +82,8 @@ CharSelectDialog::CharSelectDialog(LoginData *const data) :
     mInfoButton(new Button(this, _("Info"), "info", this)),
     // TRANSLATORS: char select dialog. button.
     mDeleteButton(new Button(this, _("Delete"), "delete", this)),
+    // TRANSLATORS: char select dialog. button.
+    mRenameButton(nullptr),
     mCharacterView(nullptr),
     mCharacterEntries(0),
     mCharServerHandler(charServerHandler),
@@ -123,6 +127,12 @@ CharSelectDialog::CharSelectDialog(LoginData *const data) :
 
     placer(n, 0, mDeleteButton);
     n ++;
+    if (serverFeatures->haveCharRename())
+    {
+        mRenameButton = new Button(this, _("Rename"), "rename", this);
+        placer(n, 0, mRenameButton);
+        n ++;
+    }
     placer(n, 0, mInfoButton);
     n ++;
 
@@ -221,6 +231,18 @@ void CharSelectDialog::action(const ActionEvent &event)
             (new CharDeleteConfirm(this, selected))->postInit();
             return;
         }
+        else if (eventId == "rename"
+                 && mCharacterEntries[selected]->getCharacter())
+        {
+            LocalPlayer *const player = mCharacterEntries[
+                selected]->getCharacter()->dummy;
+            EditDialog *const dialog = new EditDialog(
+                _("Please enter new name"), player->getName(), "OK");
+            dialog->postInit();
+            charRenameListener.setId(player->getId());
+            charRenameListener.setDialog(dialog);
+            dialog->addActionListener(&charRenameListener);
+        }
         else if (eventId == "info")
         {
             Net::Character *const character = mCharacterEntries[
diff --git a/src/gui/windows/charselectdialog.h b/src/gui/windows/charselectdialog.h
index a6acd9c32..97981df5b 100644
--- a/src/gui/windows/charselectdialog.h
+++ b/src/gui/windows/charselectdialog.h
@@ -115,6 +115,7 @@ class CharSelectDialog final : public Window,
         Button *mPlayButton;
         Button *mInfoButton;
         Button *mDeleteButton;
+        Button *mRenameButton;
         CharacterViewBase *mCharacterView;
 
         std::vector<CharacterDisplay*> mCharacterEntries;
diff --git a/src/listeners/charrenamelistener.cpp b/src/listeners/charrenamelistener.cpp
new file mode 100644
index 000000000..e39226517
--- /dev/null
+++ b/src/listeners/charrenamelistener.cpp
@@ -0,0 +1,46 @@
+/*
+ *  The ManaPlus Client
+ *  Copyright (C) 2011-2014  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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "listeners/charrenamelistener.h"
+
+#include "gui/windows/charselectdialog.h"
+#include "gui/windows/editdialog.h"
+
+#include "debug.h"
+
+CharRenameListener charRenameListener;
+
+CharRenameListener::CharRenameListener() :
+    ActionListener(),
+    mDialog(nullptr),
+    mId(0)
+{
+}
+
+void CharRenameListener::action(const ActionEvent &event)
+{
+    if (event.getId() != "OK")
+        return;
+    if (mDialog)
+    {
+        charServerHandler->renameCharacter(mId, mDialog->getMsg());
+        mDialog = nullptr;
+    }
+}
diff --git a/src/listeners/charrenamelistener.h b/src/listeners/charrenamelistener.h
new file mode 100644
index 000000000..bc1dd02a6
--- /dev/null
+++ b/src/listeners/charrenamelistener.h
@@ -0,0 +1,52 @@
+/*
+ *  The ManaPlus Client
+ *  Copyright (C) 2011-2014  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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LISTENERS_CHARRENAMELISTENER_H
+#define LISTENERS_CHARRENAMELISTENER_H
+
+#include "listeners/actionlistener.h"
+
+#include "localconsts.h"
+
+class EditDialog;
+
+class CharRenameListener final : public ActionListener
+{
+    public:
+        CharRenameListener();
+
+        A_DELETE_COPY(CharRenameListener)
+
+        void action(const ActionEvent &event) override final;
+
+        void setId(const int id)
+        { mId = id; }
+
+        void setDialog(EditDialog *const dialog)
+        { mDialog = dialog; }
+
+    protected:
+        EditDialog *mDialog;
+        int mId;
+};
+
+extern CharRenameListener charRenameListener;
+
+#endif  // LISTENERS_CHARRENAMELISTENER_H
diff --git a/src/net/charserverhandler.h b/src/net/charserverhandler.h
index 3b45e3e33..07135c40e 100644
--- a/src/net/charserverhandler.h
+++ b/src/net/charserverhandler.h
@@ -63,7 +63,7 @@ class CharServerHandler notfinal
 
         virtual void deleteCharacter(Net::Character *const character) = 0;
 
-        virtual void renameCharacter(Net::Character *const character,
+        virtual void renameCharacter(const int id,
                                      const std::string &newName) = 0;
 
         virtual void switchCharacter() const = 0;
diff --git a/src/net/eathena/charserverhandler.cpp b/src/net/eathena/charserverhandler.cpp
index 323fa681c..ee40d5e65 100644
--- a/src/net/eathena/charserverhandler.cpp
+++ b/src/net/eathena/charserverhandler.cpp
@@ -28,7 +28,10 @@
 
 #include "being/attributes.h"
 
+#include "gui/dialogtype.h"
+
 #include "gui/windows/charcreatedialog.h"
+#include "gui/windows/okdialog.h"
 
 #include "net/character.h"
 #include "net/logindata.h"
@@ -45,6 +48,7 @@
 #include "resources/db/itemdb.h"
 
 #include "utils/dtor.h"
+#include "utils/gettext.h"
 
 #include "debug.h"
 
@@ -476,20 +480,34 @@ void CharServerHandler::processCharCreate(Net::MessageIn &msg)
     BLOCK_END("CharServerHandler::processCharCreate")
 }
 
-void CharServerHandler::renameCharacter(Net::Character *const character,
+void CharServerHandler::renameCharacter(const int id,
                                         const std::string &newName)
 {
-    if (!character)
-        return;
-
     createOutPacket(CMSG_CHAR_RENAME);
-    outMsg.writeInt32(mSelectedCharacter->dummy->getId(), "char id");
+    outMsg.writeInt32(id, "char id");
     outMsg.writeString(newName, 24, "name");
 }
 
 void CharServerHandler::processCharRename(Net::MessageIn &msg)
 {
-    msg.readInt16("flag");
+    if (msg.readInt16("flag"))
+    {
+        // TRANSLATORS: info message
+        new OkDialog(_("Info"), _("Character renamed."),
+            // TRANSLATORS: ok dialog button
+            _("OK"),
+            DialogType::OK,
+            true, true, nullptr, 260);
+    }
+    else
+    {
+        // TRANSLATORS: info message
+        new OkDialog(_("Info"), _("Character rename error."),
+            // TRANSLATORS: ok dialog button
+            _("Error"),
+            DialogType::ERROR,
+            true, true, nullptr, 260);
+    }
 }
 
 }  // namespace EAthena
diff --git a/src/net/eathena/charserverhandler.h b/src/net/eathena/charserverhandler.h
index b88c96778..c59ab8c64 100644
--- a/src/net/eathena/charserverhandler.h
+++ b/src/net/eathena/charserverhandler.h
@@ -51,7 +51,7 @@ class CharServerHandler final : public MessageHandler,
                           const unsigned char look,
                           const std::vector<int> &stats) const override final;
 
-        void renameCharacter(Net::Character *const character,
+        void renameCharacter(const int id,
                              const std::string &newName) override final;
 
         void deleteCharacter(Net::Character *const character) override final;
diff --git a/src/net/tmwa/charserverhandler.cpp b/src/net/tmwa/charserverhandler.cpp
index 274120a83..95b26a89d 100644
--- a/src/net/tmwa/charserverhandler.cpp
+++ b/src/net/tmwa/charserverhandler.cpp
@@ -511,7 +511,7 @@ void CharServerHandler::processCharCreate2(Net::MessageIn &msg)
     BLOCK_END("CharServerHandler::processCharCreate2")
 }
 
-void CharServerHandler::renameCharacter(Net::Character *const character,
+void CharServerHandler::renameCharacter(const int id,
                                         const std::string &newName)
 {
 }
diff --git a/src/net/tmwa/charserverhandler.h b/src/net/tmwa/charserverhandler.h
index 29af074a5..22e616003 100644
--- a/src/net/tmwa/charserverhandler.h
+++ b/src/net/tmwa/charserverhandler.h
@@ -53,7 +53,7 @@ class CharServerHandler final : public MessageHandler,
 
         void deleteCharacter(Net::Character *const character) override final;
 
-        void renameCharacter(Net::Character *const character,
+        void renameCharacter(const int id,
                              const std::string &newName) override final;
 
         void switchCharacter() const override final;
-- 
cgit v1.2.3-70-g09d2