diff options
Diffstat (limited to 'src/gui/register.cpp')
-rw-r--r-- | src/gui/register.cpp | 179 |
1 files changed, 113 insertions, 66 deletions
diff --git a/src/gui/register.cpp b/src/gui/register.cpp index 051c0fa4..216ac211 100644 --- a/src/gui/register.cpp +++ b/src/gui/register.cpp @@ -1,55 +1,52 @@ /* * The Mana World - * Copyright 2004 The Mana World Development Team + * Copyright (C) 2004 The Mana World Development Team * * This file is part of The Mana World. * - * The Mana World is free software; you can redistribute it and/or modify + * 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. * - * The Mana World is distributed in the hope that it will be useful, + * 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 The Mana World; if not, write to the Free Software + * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "register.h" - -#include <string> -#include <sstream> - #include <guichan/widgets/label.hpp> -#include "../main.h" #include "../configuration.h" #include "../log.h" #include "../logindata.h" +#include "../main.h" #include "button.h" #include "checkbox.h" #include "login.h" +#include "ok_dialog.h" #include "passwordfield.h" #include "radiobutton.h" +#include "register.h" #include "textfield.h" -#include "ok_dialog.h" + +#include "widgets/layout.h" #include "../utils/gettext.h" #include "../utils/strprintf.h" +#include "../utils/stringutils.h" -void -WrongDataNoticeListener::setTarget(gcn::TextField *textField) +void WrongDataNoticeListener::setTarget(gcn::TextField *textField) { mTarget = textField; } -void -WrongDataNoticeListener::action(const gcn::ActionEvent &event) +void WrongDataNoticeListener::action(const gcn::ActionEvent &event) { if (event.getId() == "ok") { @@ -57,54 +54,61 @@ WrongDataNoticeListener::action(const gcn::ActionEvent &event) } } + RegisterDialog::RegisterDialog(LoginData *loginData): Window(_("Register")), - mWrongDataNoticeListener(new WrongDataNoticeListener()), + mWrongDataNoticeListener(new WrongDataNoticeListener), mLoginData(loginData) { gcn::Label *userLabel = new gcn::Label(_("Name:")); gcn::Label *passwordLabel = new gcn::Label(_("Password:")); gcn::Label *confirmLabel = new gcn::Label(_("Confirm:")); - gcn::Label *emailLabel = new gcn::Label(_("Email:")); +#ifdef EATHENA_SUPPORT + gcn::Label *serverLabel = new gcn::Label(_("Server:")); + gcn::Label *portLabel = new gcn::Label(_("Port:")); +#endif mUserField = new TextField(loginData->username); mPasswordField = new PasswordField(loginData->password); - mConfirmField = new PasswordField(); - mEmailField = new TextField(); + mConfirmField = new PasswordField; +#ifdef EATHENA_SUPPORT + mServerField = new TextField(loginData->hostname); + mPortField = new TextField(toString(loginData->port)); + mMaleButton = new RadioButton(_("Male"), "sex", true); + mFemaleButton = new RadioButton(_("Female"), "sex", false); +#endif mRegisterButton = new Button(_("Register"), "register", this); mCancelButton = new Button(_("Cancel"), "cancel", this); - const int width = 220; - const int height = 130; - setContentSize(width, height); - - mUserField->setPosition(65, 5); - mUserField->setWidth(width - 70); - mPasswordField->setPosition( - 65, mUserField->getY() + mUserField->getHeight() + 7); - mPasswordField->setWidth(mUserField->getWidth()); - mConfirmField->setPosition( - 65, mPasswordField->getY() + mPasswordField->getHeight() + 7); - mConfirmField->setWidth(mUserField->getWidth()); - mEmailField->setPosition( - 65, mConfirmField->getY() + mConfirmField->getHeight() + 7); - mEmailField->setWidth(mUserField->getWidth()); - - userLabel->setPosition(5, mUserField->getY() + 1); - passwordLabel->setPosition(5, mPasswordField->getY() + 1); - confirmLabel->setPosition(5, mConfirmField->getY() + 1); - emailLabel->setPosition(5, mEmailField->getY() + 1); - - mCancelButton->setPosition( - width - mCancelButton->getWidth() - 5, - height - mCancelButton->getHeight() - 5); - mRegisterButton->setPosition( - mCancelButton->getX() - mRegisterButton->getWidth() - 5, - height - mRegisterButton->getHeight() - 5); + ContainerPlacer place; + place = getPlacer(0, 0); + place(0, 0, userLabel); + place(0, 1, passwordLabel); + place(0, 2, confirmLabel); +#ifdef EATHENA_SUPPORT + place(1, 3, mMaleButton); + place(2, 3, mFemaleButton); + place(0, 4, serverLabel); + place(0, 5, portLabel); +#endif + place(1, 0, mUserField, 3).setPadding(2); + place(1, 1, mPasswordField, 3).setPadding(2); + place(1, 2, mConfirmField, 3).setPadding(2); +#ifdef EATHENA_SUPPORT + place(1, 4, mServerField, 3).setPadding(2); + place(1, 5, mPortField, 3).setPadding(2); +#endif + place = getPlacer(0, 2); + place(1, 0, mRegisterButton); + place(2, 0, mCancelButton); + reflowLayout(250, 0); mUserField->addKeyListener(this); mPasswordField->addKeyListener(this); mConfirmField->addKeyListener(this); - mEmailField->addKeyListener(this); +#ifdef EATHENA_SUPPORT + mServerField->addKeyListener(this); + mPortField->addKeyListener(this); +#endif /* TODO: * This is a quick and dirty way to respond to the ENTER key, regardless of @@ -114,22 +118,18 @@ RegisterDialog::RegisterDialog(LoginData *loginData): mUserField->setActionEventId("register"); mPasswordField->setActionEventId("register"); mConfirmField->setActionEventId("register"); - mEmailField->setActionEventId("register"); + mUserField->addActionListener(this); mPasswordField->addActionListener(this); mConfirmField->addActionListener(this); - mEmailField->addActionListener(this); - - add(userLabel); - add(passwordLabel); - add(emailLabel); - add(confirmLabel); - add(mUserField); - add(mPasswordField); - add(mConfirmField); - add(mEmailField); - add(mRegisterButton); - add(mCancelButton); + +#ifdef EATHENA_SUPPORT + mServerField->setActionEventId("register"); + mPortField->setActionEventId("register"); + + mServerField->addActionListener(this); + mPortField->addActionListener(this); +#endif setLocationRelativeTo(getParent()); setVisible(true); @@ -144,8 +144,7 @@ RegisterDialog::~RegisterDialog() delete mWrongDataNoticeListener; } -void -RegisterDialog::action(const gcn::ActionEvent &event) +void RegisterDialog::action(const gcn::ActionEvent &event) { if (event.getId() == "cancel") { @@ -225,25 +224,73 @@ RegisterDialog::action(const gcn::ActionEvent &event) mLoginData->username = mUserField->getText(); mLoginData->password = mPasswordField->getText(); +#ifdef EATHENA_SUPPORT + mLoginData->hostname = mServerField->getText(); + mLoginData->port = getUShort(mPortField->getText()); + mLoginData->username += mFemaleButton->isSelected() ? "_F" : "_M"; +#else mLoginData->email = mEmailField->getText(); +#endif mLoginData->registerLogin = true; +#ifdef TMWSERV_SUPPORT state = STATE_REGISTER_ATTEMPT; +#else + state = STATE_ACCOUNT; +#endif } } } -void -RegisterDialog::keyPressed(gcn::KeyEvent &keyEvent) +void RegisterDialog::keyPressed(gcn::KeyEvent &keyEvent) { mRegisterButton->setEnabled(canSubmit()); } -bool -RegisterDialog::canSubmit() +bool RegisterDialog::canSubmit() const { return !mUserField->getText().empty() && !mPasswordField->getText().empty() && !mConfirmField->getText().empty() && +#ifdef EATHENA_SUPPORT + !mServerField->getText().empty() && + isUShort(mPortField->getText()) && +#endif state == STATE_REGISTER; } + +#ifdef EATHENA_SUPPORT +bool RegisterDialog::isUShort(const std::string &str) +{ + if (str.empty()) + { + return false; + } + unsigned long l = 0; + for (std::string::const_iterator strPtr = str.begin(), strEnd = str.end(); + strPtr != strEnd; ++strPtr) + { + if (*strPtr < '0' || *strPtr > '9') + { + return false; + } + l = l * 10 + (*strPtr - '0'); // *strPtr - '0' will never be negative + if (l > 65535) + { + return false; + } + } + return true; +} + +unsigned short RegisterDialog::getUShort(const std::string &str) +{ + unsigned long l = 0; + for (std::string::const_iterator strPtr = str.begin(), strEnd = str.end(); + strPtr != strEnd; ++strPtr) + { + l = l * 10 + (*strPtr - '0'); + } + return static_cast<unsigned short>(l); +} +#endif |