00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "changepassworddialog.h"
00023
00024 #include "main.h"
00025 #include "log.h"
00026 #include "logindata.h"
00027
00028 #include "gui/register.h"
00029 #include "gui/ok_dialog.h"
00030
00031 #include "gui/widgets/button.h"
00032 #include "gui/widgets/passwordfield.h"
00033 #include "gui/widgets/textfield.h"
00034 #include "gui/widgets/label.h"
00035
00036 #include "utils/gettext.h"
00037 #include "utils/strprintf.h"
00038
00039 #include <string>
00040 #include <sstream>
00041
00042 ChangePasswordDialog::ChangePasswordDialog(Window *parent, LoginData *loginData):
00043 Window(_("Change Password"), true, parent),
00044 mWrongDataNoticeListener(new WrongDataNoticeListener),
00045 mLoginData(loginData)
00046 {
00047 gcn::Label *accountLabel = new Label(strprintf(_("Account: %s"),
00048 mLoginData->username.c_str()));
00049 gcn::Label *oldPassLabel = new Label(_("Password:"));
00050 mOldPassField = new PasswordField;
00051 gcn::Label *newPassLabel = new Label(_("Type New Password twice:"));
00052 mFirstPassField = new PasswordField;
00053 mSecondPassField = new PasswordField;
00054 mChangePassButton = new Button(_("Change Password"), "change_password", this);
00055 mCancelButton = new Button(_("Cancel"), "cancel", this);
00056
00057 const int width = 200;
00058 const int height = 170;
00059 setContentSize(width, height);
00060
00061 accountLabel->setPosition(5, 5);
00062 accountLabel->setWidth(130);
00063 oldPassLabel->setPosition(
00064 5, accountLabel->getY() + accountLabel->getHeight() + 7);
00065 oldPassLabel->setWidth(130);
00066
00067 mOldPassField->setPosition(
00068 5, oldPassLabel->getY() + oldPassLabel->getHeight() + 7);
00069 mOldPassField->setWidth(130);
00070
00071 newPassLabel->setPosition(
00072 5, mOldPassField->getY() + mOldPassField->getHeight() + 7);
00073 newPassLabel->setWidth(width - 5);
00074
00075 mFirstPassField->setPosition(
00076 5, newPassLabel->getY() + newPassLabel->getHeight() + 7);
00077 mFirstPassField->setWidth(130);
00078
00079 mSecondPassField->setPosition(
00080 5, mFirstPassField->getY() + mFirstPassField->getHeight() + 7);
00081 mSecondPassField->setWidth(130);
00082
00083 mCancelButton->setPosition(
00084 width - 5 - mCancelButton->getWidth(),
00085 height - 5 - mCancelButton->getHeight());
00086 mChangePassButton->setPosition(
00087 mCancelButton->getX() - 5 - mChangePassButton->getWidth(),
00088 mCancelButton->getY());
00089
00090 add(accountLabel);
00091 add(oldPassLabel);
00092 add(mOldPassField);
00093 add(newPassLabel);
00094 add(mFirstPassField);
00095 add(mSecondPassField);
00096 add(mChangePassButton);
00097 add(mCancelButton);
00098
00099 setLocationRelativeTo(getParent());
00100 setVisible(true);
00101 mOldPassField->requestFocus();
00102
00103 mOldPassField->setActionEventId("change_password");
00104 mFirstPassField->setActionEventId("change_password");
00105 mSecondPassField->setActionEventId("change_password");
00106 }
00107
00108 ChangePasswordDialog::~ChangePasswordDialog()
00109 {
00110 delete mWrongDataNoticeListener;
00111 }
00112
00113 void
00114 ChangePasswordDialog::action(const gcn::ActionEvent &event)
00115 {
00116 if (event.getId() == "cancel")
00117 {
00118 scheduleDelete();
00119 }
00120 else if (event.getId() == "change_password")
00121 {
00122
00123 const std::string username = mLoginData->username.c_str();
00124 const std::string oldPassword = mOldPassField->getText();
00125 const std::string newFirstPass = mFirstPassField->getText();
00126 const std::string newSecondPass = mSecondPassField->getText();
00127 logger->log("ChangePasswordDialog::Password change, Username is %s",
00128 username.c_str());
00129
00130 std::stringstream errorMsg;
00131 int error = 0;
00132
00133
00134 if (oldPassword.empty())
00135 {
00136
00137 errorMsg << "Enter the old Password first.";
00138 error = 1;
00139 }
00140 else if (newFirstPass.length() < LEN_MIN_PASSWORD)
00141 {
00142
00143 errorMsg << "The new password needs to be at least "
00144 << LEN_MIN_PASSWORD
00145 << " characters long.";
00146 error = 2;
00147 }
00148 else if (newFirstPass.length() > LEN_MAX_PASSWORD - 1 )
00149 {
00150
00151 errorMsg << "The new password needs to be less than "
00152 << LEN_MAX_PASSWORD
00153 << " characters long.";
00154 error = 2;
00155 }
00156 else if (newFirstPass != newSecondPass)
00157 {
00158
00159 errorMsg << "The new password entries mismatch.";
00160 error = 3;
00161 }
00162
00163 if (error > 0)
00164 {
00165 if (error == 1)
00166 {
00167 mWrongDataNoticeListener->setTarget(this->mOldPassField);
00168 }
00169 else if (error == 2)
00170 {
00171 mWrongDataNoticeListener->setTarget(this->mFirstPassField);
00172 }
00173 else if (error == 3)
00174 {
00175 mWrongDataNoticeListener->setTarget(this->mSecondPassField);
00176 }
00177
00178 OkDialog *dlg = new OkDialog("Error", errorMsg.str());
00179 dlg->addActionListener(mWrongDataNoticeListener);
00180 }
00181 else
00182 {
00183
00184 mChangePassButton->setEnabled(false);
00185
00186 mLoginData->password = oldPassword;
00187 mLoginData->newPassword = newFirstPass;
00188 state = STATE_CHANGEPASSWORD_ATTEMPT;
00189 scheduleDelete();
00190 }
00191
00192 }
00193 }