GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/changeemaildialog.cpp Lines: 47 76 61.8 %
Date: 2017-11-29 Branches: 51 144 35.4 %

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-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/changeemaildialog.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/label.h"
32
#include "gui/widgets/textfield.h"
33
34
#include "listeners/wrongdatanoticelistener.h"
35
36
#include "net/logindata.h"
37
#include "net/loginhandler.h"
38
39
#include "utils/delete2.h"
40
#include "utils/gettext.h"
41
42
#include <sstream>
43
44
#include "debug.h"
45
46
2
ChangeEmailDialog::ChangeEmailDialog(LoginData &data) :
47
    // TRANSLATORS: change email dialog header
48
2
    Window(_("Change Email Address"), Modal_true, nullptr, "changeemail.xml"),
49
    ActionListener(),
50

2
    mFirstEmailField(new TextField(this)),
51

2
    mSecondEmailField(new TextField(this)),
52
    // TRANSLATORS: button in change email dialog
53
2
    mChangeEmailButton(new Button(this, _("Change Email Address"),
54

2
        "change_email", this)),
55
    // TRANSLATORS: button in change email dialog
56

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

2
    mWrongDataNoticeListener(new WrongDataNoticeListener),
58





74
    mLoginData(&data)
59
{
60
    // TRANSLATORS: label in change email dialog
61
6
    Label *const accountLabel = new Label(this, strprintf(_("Account: %s"),
62

2
        mLoginData->username.c_str()));
63
    Label *const newEmailLabel = new Label(this,
64
        // TRANSLATORS: label in change email dialog
65

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

6
}