GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/changeemaildialog.cpp Lines: 49 82 59.8 %
Date: 2021-03-17 Branches: 46 136 33.8 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008-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/changeemaildialog.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/label.h"
33
#include "gui/widgets/textfield.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
1
ChangeEmailDialog::ChangeEmailDialog(LoginData &data) :
48
    // TRANSLATORS: change email dialog header
49
1
    Window(_("Change Email Address"), Modal_true, nullptr, "changeemail.xml"),
50
    ActionListener(),
51
2
    mFirstEmailField(new TextField(this, std::string(), LoseFocusOnTab_true,
52

2
        nullptr, std::string(), false)),
53
2
    mSecondEmailField(new TextField(this, std::string(), LoseFocusOnTab_true,
54

2
        nullptr, std::string(), false)),
55
    // TRANSLATORS: button in change email dialog
56
1
    mChangeEmailButton(new Button(this, _("Change Email Address"),
57

1
        "change_email", BUTTON_SKIN, this)),
58
    // TRANSLATORS: button in change email dialog
59

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

1
    mWrongDataNoticeListener(new WrongDataNoticeListener),
61



28
    mLoginData(&data)
62
{
63
    // TRANSLATORS: label in change email dialog
64
3
    Label *const accountLabel = new Label(this, strprintf(_("Account: %s"),
65

1
        mLoginData->username.c_str()));
66
    Label *const newEmailLabel = new Label(this,
67
        // TRANSLATORS: label in change email dialog
68

4
        _("Type new email address twice:"));
69
70
1
    const int width = 200;
71
1
    const int height = 130;
72
1
    setContentSize(width, height);
73
74
1
    accountLabel->setPosition(5, 5);
75
1
    accountLabel->setWidth(130);
76
77
1
    newEmailLabel->setPosition(
78
4
            5, accountLabel->getY() + accountLabel->getHeight() + 7);
79
1
    newEmailLabel->setWidth(width - 5);
80
81
1
    mFirstEmailField->setPosition(
82
4
            5, newEmailLabel->getY() + newEmailLabel->getHeight() + 7);
83
1
    mFirstEmailField->setWidth(130);
84
85
1
    mSecondEmailField->setPosition(
86
4
            5, mFirstEmailField->getY() + mFirstEmailField->getHeight() + 7);
87
1
    mSecondEmailField->setWidth(130);
88
89
2
    mCancelButton->setPosition(
90
2
            width - 5 - mCancelButton->getWidth(),
91
3
            height - 5 - mCancelButton->getHeight());
92
3
    mChangeEmailButton->setPosition(
93
3
            mCancelButton->getX() - 5 - mChangeEmailButton->getWidth(),
94
2
            mCancelButton->getY());
95
96
1
    add(accountLabel);
97
1
    add(newEmailLabel);
98
1
    add(mFirstEmailField);
99
1
    add(mSecondEmailField);
100
1
    add(mChangeEmailButton);
101
1
    add(mCancelButton);
102
103
1
    center();
104
1
    mFirstEmailField->requestFocus();
105
106
5
    mFirstEmailField->setActionEventId("change_email");
107
5
    mSecondEmailField->setActionEventId("change_email");
108
1
}
109
110
4
ChangeEmailDialog::~ChangeEmailDialog()
111
{
112
2
    delete2(mWrongDataNoticeListener)
113
2
}
114
115
void ChangeEmailDialog::action(const ActionEvent &event)
116
{
117
    const std::string &eventId = event.getId();
118
    if (eventId == "cancel")
119
    {
120
        client->setState(State::CHAR_SELECT);
121
    }
122
    else if (eventId == "change_email")
123
    {
124
        const std::string username = mLoginData->username;
125
        const std::string &newFirstEmail = mFirstEmailField->getText();
126
        const std::string &newSecondEmail = mSecondEmailField->getText();
127
        logger->log("ChangeEmailDialog::Email change, Username is %s",
128
                     username.c_str());
129
130
        std::stringstream errorMsg;
131
        int error = 0;
132
133
        const unsigned int min = loginHandler->getMinPasswordLength();
134
        const unsigned int max = loginHandler->getMaxPasswordLength();
135
136
        if (newFirstEmail.length() < min)
137
        {
138
            // First email address too short
139
            // TRANSLATORS: change email error
140
            errorMsg << strprintf(_("The new email address needs to be at "
141
                "least %u characters long."), min);
142
            error = 1;
143
        }
144
        else if (newFirstEmail.length() > max)
145
        {
146
            // First email address too long
147
            // TRANSLATORS: change email error
148
            errorMsg << strprintf(_("The new email address needs to be "
149
                "less than %u characters long."), max);
150
            error = 1;
151
        }
152
        else if (newFirstEmail != newSecondEmail)
153
        {
154
            // Second Pass mismatch
155
            // TRANSLATORS: change email error
156
            errorMsg << _("The email address entries mismatch.");
157
            error = 2;
158
        }
159
160
        if (error > 0)
161
        {
162
            if (error == 1)
163
                mWrongDataNoticeListener->setTarget(this->mFirstEmailField);
164
            else  // if (error == 2)
165
                mWrongDataNoticeListener->setTarget(this->mSecondEmailField);
166
167
            OkDialog *const dlg = CREATEWIDGETR(OkDialog,
168
                // TRANSLATORS: change email error header
169
                _("Error"),
170
                errorMsg.str(),
171
                // TRANSLATORS: ok dialog button
172
                _("OK"),
173
                DialogType::ERROR,
174
                Modal_true,
175
                ShowCenter_true,
176
                nullptr,
177
                260);
178
            dlg->addActionListener(mWrongDataNoticeListener);
179
        }
180
        else
181
        {
182
            // No errors detected, change account password.
183
            mChangeEmailButton->setEnabled(false);
184
            // Set the new email address
185
            mLoginData->email = newFirstEmail;
186
            client->setState(State::CHANGEEMAIL_ATTEMPT);
187
        }
188
    }
189

3
}