GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/changepassworddialog.cpp Lines: 30 65 46.2 %
Date: 2017-11-29 Branches: 48 146 32.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "gui/windows/changepassworddialog.h"
24
25
#include "client.h"
26
27
#include "gui/windows/okdialog.h"
28
29
#include "gui/widgets/button.h"
30
#include "gui/widgets/createwidget.h"
31
#include "gui/widgets/passwordfield.h"
32
#include "gui/widgets/label.h"
33
#include "gui/widgets/layoutcell.h"
34
35
#include "listeners/wrongdatanoticelistener.h"
36
37
#include "net/logindata.h"
38
#include "net/loginhandler.h"
39
40
#include "utils/delete2.h"
41
#include "utils/gettext.h"
42
43
#include <sstream>
44
45
#include "debug.h"
46
47
2
ChangePasswordDialog::ChangePasswordDialog(LoginData &data) :
48
    // TRANSLATORS: change password window name
49
2
    Window(_("Change Password"), Modal_true, nullptr, "changepassword.xml"),
50
    ActionListener(),
51

2
    mOldPassField(new PasswordField(this)),
52

2
    mFirstPassField(new PasswordField(this)),
53

2
    mSecondPassField(new PasswordField(this)),
54
    // TRANSLATORS: change password dialog button
55
2
    mChangePassButton(new Button(this, _("Change Password"),
56

2
        "change_password", this)),
57
    // TRANSLATORS: change password dialog button
58

4
    mCancelButton(new Button(this, _("Cancel"), "cancel", this)),
59

2
    mWrongDataNoticeListener(new WrongDataNoticeListener),
60





70
    mLoginData(&data)
61
{
62
    Label *const accountLabel = new Label(this,
63
        // TRANSLATORS: change password dialog label
64

6
        strprintf(_("Account: %s"), mLoginData->username.c_str()));
65
66
2
    place(0, 0, accountLabel, 3);
67
    // TRANSLATORS: change password dialog label
68


8
    place(0, 1, new Label(this, _("Password:")), 3);
69
4
    place(0, 2, mOldPassField, 3).setPadding(1);
70
    // TRANSLATORS: change password dialog label
71


8
    place(0, 3, new Label(this, _("Type new password twice:")), 3);
72
4
    place(0, 4, mFirstPassField, 3).setPadding(1);
73
4
    place(0, 5, mSecondPassField, 3).setPadding(1);
74
2
    place(1, 6, mCancelButton);
75
2
    place(2, 6, mChangePassButton);
76
2
    reflowLayout(200);
77
78
2
    center();
79
2
    mOldPassField->requestFocus();
80
81
10
    mOldPassField->setActionEventId("change_password");
82
10
    mFirstPassField->setActionEventId("change_password");
83
10
    mSecondPassField->setActionEventId("change_password");
84
2
}
85
86
6
ChangePasswordDialog::~ChangePasswordDialog()
87
{
88
4
    delete2(mWrongDataNoticeListener);
89
4
}
90
91
void ChangePasswordDialog::action(const ActionEvent &event)
92
{
93
    const std::string &eventId = event.getId();
94
    if (eventId == "cancel")
95
    {
96
        client->setState(State::CHAR_SELECT);
97
    }
98
    else if (eventId == "change_password")
99
    {
100
        const std::string username = mLoginData->username;
101
        const std::string &oldPassword = mOldPassField->getText();
102
        const std::string &newFirstPass = mFirstPassField->getText();
103
        const std::string &newSecondPass = mSecondPassField->getText();
104
        logger->log("ChangePasswordDialog::Password change, Username is %s",
105
                     username.c_str());
106
107
        std::stringstream errorMsg;
108
        int error = 0;
109
110
        const unsigned int min = loginHandler->getMinPasswordLength();
111
        const unsigned int max = loginHandler->getMaxPasswordLength();
112
113
        // Check old Password
114
        if (oldPassword.empty())
115
        {
116
            // No old password
117
            // TRANSLATORS: change password error
118
            errorMsg << _("Enter the old password first.");
119
            error = 1;
120
        }
121
        else if (newFirstPass.length() < min)
122
        {
123
            // First password too short
124
            // TRANSLATORS: change password error
125
            errorMsg << strprintf(_("The new password needs to be at least"
126
                " %u characters long."), min);
127
            error = 2;
128
        }
129
        else if (newFirstPass.length() > max)
130
        {
131
            // First password too long
132
            // TRANSLATORS: change password error
133
            errorMsg << strprintf(_("The new password needs to be less "
134
                "than %u characters long."), max);
135
            error = 2;
136
        }
137
        else if (newFirstPass != newSecondPass)
138
        {
139
            // Second Pass mismatch
140
            // TRANSLATORS: change password error
141
            errorMsg << _("The new password entries mismatch.");
142
            error = 3;
143
        }
144
145
        if (error > 0)
146
        {
147
            if (error == 1)
148
                mWrongDataNoticeListener->setTarget(this->mOldPassField);
149
            else if (error == 2)
150
                mWrongDataNoticeListener->setTarget(this->mFirstPassField);
151
            else  // if (error == 3)
152
                mWrongDataNoticeListener->setTarget(this->mSecondPassField);
153
154
            OkDialog *const dlg = CREATEWIDGETR(OkDialog,
155
                // TRANSLATORS: change password error header
156
                _("Error"),
157
                errorMsg.str(),
158
                // TRANSLATORS: ok dialog button
159
                _("OK"),
160
                DialogType::ERROR,
161
                Modal_true,
162
                ShowCenter_true,
163
                nullptr,
164
                260);
165
            dlg->addActionListener(mWrongDataNoticeListener);
166
        }
167
        else
168
        {
169
            // No errors detected, change account password.
170
            mChangePassButton->setEnabled(false);
171
            // Set the new password
172
            mLoginData->password = oldPassword;
173
            mLoginData->newPassword = newFirstPass;
174
            client->setState(State::CHANGEPASSWORD_ATTEMPT);
175
        }
176
    }
177

6
}