GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/changepassworddialog.cpp Lines: 30 69 43.5 %
Date: 2021-03-17 Branches: 44 140 31.4 %

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

2
    mOldPassField(new PasswordField(this, std::string())),
53

2
    mFirstPassField(new PasswordField(this, std::string())),
54

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

1
        "change_password", BUTTON_SKIN, this)),
58
    // TRANSLATORS: change password dialog button
59

2
    mCancelButton(new Button(this, _("Cancel"), "cancel", BUTTON_SKIN, this)),
60

1
    mWrongDataNoticeListener(new WrongDataNoticeListener),
61



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

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


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


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

3
}