diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-03-24 13:05:27 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-03-24 13:05:27 +0000 |
commit | b5a95c2e1151ce61a2615cffcbb6821c150f40fd (patch) | |
tree | daca526b12ba0d9f761af3d65d033719ea50c8d3 | |
parent | d5e8513e085b6fc8da2ea4bb7151ac8e98be74c8 (diff) | |
download | mana-b5a95c2e1151ce61a2615cffcbb6821c150f40fd.tar.gz mana-b5a95c2e1151ce61a2615cffcbb6821c150f40fd.tar.bz2 mana-b5a95c2e1151ce61a2615cffcbb6821c150f40fd.tar.xz mana-b5a95c2e1151ce61a2615cffcbb6821c150f40fd.zip |
Make sure that when a network error occurs, the error message is available and
shown to the user. Also, only enable Register button when all necessary fields
are filled in.
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | src/gui/login.cpp | 2 | ||||
-rw-r--r-- | src/gui/login.h | 2 | ||||
-rw-r--r-- | src/gui/register.cpp | 25 | ||||
-rw-r--r-- | src/gui/register.h | 17 | ||||
-rw-r--r-- | src/main.cpp | 7 | ||||
-rw-r--r-- | src/net/network.cpp | 50 | ||||
-rw-r--r-- | src/net/network.h | 67 |
8 files changed, 129 insertions, 46 deletions
@@ -11,6 +11,11 @@ * src/net/charserverhandler.cpp: Fixed crash since now logic is called after non-selected characters are deleted, by properly setting deleted instances to 0 and going back to initial selection. + * src/main.cpp, src/net/network.h, src/net/network.cpp: Make sure that + when a network error occurs, the error message is available and shown + to the user. + * src/gui/register.h, src/gui/register.cpp: Only enable Register + button when all necessary fields are filled in. 2007-03-23 Bjørn Lindeijer <bjorn@lindeijer.nl> diff --git a/src/gui/login.cpp b/src/gui/login.cpp index 4e4c5b34..2c13f204 100644 --- a/src/gui/login.cpp +++ b/src/gui/login.cpp @@ -148,7 +148,7 @@ LoginDialog::action(const gcn::ActionEvent &event) } void -LoginDialog::keyPressed(gcn::KeyEvent& keyEvent) +LoginDialog::keyPressed(gcn::KeyEvent &keyEvent) { mOkButton->setEnabled(canSubmit()); } diff --git a/src/gui/login.h b/src/gui/login.h index 981665d5..e08120cb 100644 --- a/src/gui/login.h +++ b/src/gui/login.h @@ -62,7 +62,7 @@ class LoginDialog : public Window, public gcn::ActionListener, /** * Called when a key is pressed in one of the text fields. */ - void keyPressed(gcn::KeyEvent& keyEvent); + void keyPressed(gcn::KeyEvent &keyEvent); private: /** diff --git a/src/gui/register.cpp b/src/gui/register.cpp index ad3a6388..b42a7af8 100644 --- a/src/gui/register.cpp +++ b/src/gui/register.cpp @@ -108,6 +108,11 @@ RegisterDialog::RegisterDialog(LoginData *loginData): mCancelButton->getX() - mRegisterButton->getWidth() - 5, height - mRegisterButton->getHeight() - 5); + mUserField->addKeyListener(this); + mPasswordField->addKeyListener(this); + mConfirmField->addKeyListener(this); + mServerField->addKeyListener(this); + /* TODO: * This is a quick and dirty way to respond to the ENTER key, regardless of * which text field is selected. There may be a better way now with the new @@ -139,6 +144,8 @@ RegisterDialog::RegisterDialog(LoginData *loginData): setVisible(true); mUserField->requestFocus(); mUserField->setCaretPosition(mUserField->getText().length()); + + mRegisterButton->setEnabled(canSubmit()); } RegisterDialog::~RegisterDialog() @@ -153,7 +160,7 @@ RegisterDialog::action(const gcn::ActionEvent &event) { state = LOGIN_STATE; } - else if (event.getId() == "register") + else if (event.getId() == "register" && canSubmit()) { const std::string user = mUserField->getText(); logger->log("RegisterDialog::register Username is %s", user.c_str()); @@ -235,3 +242,19 @@ RegisterDialog::action(const gcn::ActionEvent &event) } } } + +void +RegisterDialog::keyPressed(gcn::KeyEvent &keyEvent) +{ + mRegisterButton->setEnabled(canSubmit()); +} + +bool +RegisterDialog::canSubmit() +{ + return !mUserField->getText().empty() && + !mPasswordField->getText().empty() && + !mConfirmField->getText().empty() && + !mServerField->getText().empty() && + state == REGISTER_STATE; +} diff --git a/src/gui/register.h b/src/gui/register.h index 6b23c97f..a80594af 100644 --- a/src/gui/register.h +++ b/src/gui/register.h @@ -26,6 +26,7 @@ #include <iosfwd> #include <guichan/actionlistener.hpp> +#include <guichan/keylistener.hpp> #include "window.h" #include "../guichanfwd.h" @@ -51,7 +52,8 @@ class WrongDataNoticeListener : public gcn::ActionListener { * * \ingroup Interface */ -class RegisterDialog : public Window, public gcn::ActionListener +class RegisterDialog : public Window, public gcn::ActionListener, + public gcn::KeyListener { public: /** @@ -72,10 +74,19 @@ class RegisterDialog : public Window, public gcn::ActionListener */ void action(const gcn::ActionEvent &event); - // Made them public to have the possibility to request focus - // from external functions. + /** + * Called when a key is pressed in one of the text fields. + */ + void keyPressed(gcn::KeyEvent &keyEvent); private: + /** + * Returns whether submit can be enabled. This is true in the register + * state, when all necessary fields have some text. + */ + bool + canSubmit(); + gcn::TextField *mUserField; gcn::TextField *mPasswordField; gcn::TextField *mConfirmField; diff --git a/src/main.cpp b/src/main.cpp index 79275e15..11600b6f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -633,7 +633,12 @@ int main(int argc, char *argv[]) if (network->getState() == Network::NET_ERROR) { state = ERROR_STATE; - errorMessage = "Got disconnected from server!"; + + if (!network->getError().empty()) { + errorMessage = network->getError(); + } else { + errorMessage = "Got disconnected from server!"; + } } if (!login_wallpaper) diff --git a/src/net/network.cpp b/src/net/network.cpp index 8da0033d..c90e0201 100644 --- a/src/net/network.cpp +++ b/src/net/network.cpp @@ -28,6 +28,8 @@ #include "../log.h" +#include <sstream> + /** Warning: buffers and other variables are shared, so there can be only one connection active at a time */ @@ -126,8 +128,7 @@ bool Network::connect(const std::string &address, short port) if (address.empty()) { - logger->log("Empty address given to Network::connect()!"); - mState = NET_ERROR; + setError("Empty address given to Network::connect()!"); return false; } @@ -145,8 +146,7 @@ bool Network::connect(const std::string &address, short port) mWorkerThread = SDL_CreateThread(networkThread, this); if (!mWorkerThread) { - logger->log("Unable to create network worker thread"); - mState = NET_ERROR; + setError("Unable to create network worker thread"); return false; } @@ -172,12 +172,9 @@ void Network::disconnect() void Network::registerHandler(MessageHandler *handler) { - const Uint16 *i = handler->handledMessages; - - while(*i) + for (const Uint16 *i = handler->handledMessages; *i; i++) { mMessageHandlers[*i] = handler; - i++; } handler->setNetwork(this); @@ -232,8 +229,8 @@ void Network::flush() ret = SDLNet_TCP_Send(mSocket, mOutBuffer, mOutSize); if (ret < (int)mOutSize) { - logger->log("Error in SDLNet_TCP_Send(): %s", SDLNet_GetError()); - mState = NET_ERROR; + setError("Error in SDLNet_TCP_Send(): " + + std::string(SDLNet_GetError())); } mOutSize = 0; SDL_mutexV(mMutex); @@ -314,8 +311,9 @@ bool Network::realConnect() if (SDLNet_ResolveHost(&ipAddress, mAddress.c_str(), mPort) == -1) { - logger->log("Error in SDLNet_ResolveHost(): %s", SDLNet_GetError()); - mState = NET_ERROR; + std::string error = "Unable to resolve host \"" + mAddress + "\""; + setError(error); + logger->log("SDLNet_ResolveHost: %s", error.c_str()); return false; } @@ -325,7 +323,7 @@ bool Network::realConnect() if (!mSocket) { logger->log("Error in SDLNet_TCP_Open(): %s", SDLNet_GetError()); - mState = NET_ERROR; + setError(SDLNet_GetError()); return false; } @@ -343,15 +341,15 @@ void Network::receive() if (!(set = SDLNet_AllocSocketSet(1))) { - logger->log("Error in SDLNet_AllocSocketSet(): %s", SDLNet_GetError()); - mState = NET_ERROR; + setError("Error in SDLNet_AllocSocketSet(): " + + std::string(SDLNet_GetError())); return; } if (SDLNet_TCP_AddSocket(set, mSocket) == -1) { - logger->log("Error in SDLNet_AddSocket(): %s", SDLNet_GetError()); - mState = NET_ERROR; + setError("Error in SDLNet_AddSocket(): " + + std::string(SDLNet_GetError())); } while (mState == CONNECTED) @@ -381,8 +379,8 @@ void Network::receive() } else if (ret < 0) { - logger->log("Error in SDLNet_TCP_Recv(): %s", SDLNet_GetError()); - mState = NET_ERROR; + setError("Error in SDLNet_TCP_Recv(): " + + std::string(SDLNet_GetError())); } else { mInSize += ret; @@ -407,8 +405,10 @@ void Network::receive() default: // more than one socket is ready.. // this should not happen since we only listen once socket. - logger->log("Error in SDLNet_TCP_Recv(), %d sockets are ready : %s", numReady, SDLNet_GetError()); - mState = NET_ERROR; + std::stringstream errorStream; + errorStream << "Error in SDLNet_TCP_Recv(), " << numReady + << " sockets are ready: " << SDLNet_GetError(); + setError(errorStream.str()); break; } } @@ -434,6 +434,14 @@ char *iptostring(int address) return asciiIP; } +void +Network::setError(const std::string& error) +{ + logger->log("Network error: %s", error.c_str()); + mError = error; + mState = NET_ERROR; +} + Uint16 Network::readWord(int pos) { #if SDL_BYTEORDER == SDL_BIG_ENDIAN diff --git a/src/net/network.h b/src/net/network.h index 13929ec1..d6fec761 100644 --- a/src/net/network.h +++ b/src/net/network.h @@ -41,29 +41,52 @@ class Network friend class MessageOut; Network(); + ~Network(); - bool connect(const std::string &address, short port); - void disconnect(); + bool + connect(const std::string &address, short port); + + void + disconnect(); + + void + registerHandler(MessageHandler *handler); + + void + unregisterHandler(MessageHandler *handler); + + void + clearHandlers(); + + int + getState() const { return mState; } - void registerHandler(MessageHandler *handler); - void unregisterHandler(MessageHandler *handler); - void clearHandlers(); + const std::string& + getError() const { return mError; } - int getState() const { return mState; } - bool isConnected() const { return mState == CONNECTED; } + bool + isConnected() const { return mState == CONNECTED; } - int getInSize() const { return mInSize; } + int + getInSize() const { return mInSize; } - void skip(int len); + void + skip(int len); - bool messageReady(); - MessageIn getNextMessage(); + bool + messageReady(); - void dispatchMessages(); - void flush(); + MessageIn + getNextMessage(); - // ERROR replaced by NET_ERROR because already defined in Windows + void + dispatchMessages(); + + void + flush(); + + // ERROR replaced by NET_ERROR because already defined in Windows enum { IDLE, CONNECTED, @@ -73,7 +96,17 @@ class Network }; protected: - Uint16 readWord(int pos); + void + setError(const std::string& error); + + Uint16 + readWord(int pos); + + bool + realConnect(); + + void + receive(); TCPsocket mSocket; @@ -86,6 +119,7 @@ class Network unsigned int mToSkip; int mState; + std::string mError; SDL_Thread *mWorkerThread; SDL_mutex *mMutex; @@ -93,9 +127,6 @@ class Network typedef std::map<Uint16, MessageHandler*> MessageHandlers; typedef MessageHandlers::iterator MessageHandlerIterator; MessageHandlers mMessageHandlers; - - bool realConnect(); - void receive(); }; /** Convert an address from int format to string */ |