GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/changeemaildialog.cpp Lines: 49 78 62.8 %
Date: 2018-06-18 21:15:20 Branches: 47 136 34.6 %

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-2018  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
4
    mFirstEmailField(new TextField(this, std::string(), LoseFocusOnTab_true,
51

4
        nullptr, std::string(), false)),
52
4
    mSecondEmailField(new TextField(this, std::string(), LoseFocusOnTab_true,
53

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

2
        "change_email", BUTTON_SKIN, this)),
57
    // TRANSLATORS: button in change email dialog
58

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

2
    mWrongDataNoticeListener(new WrongDataNoticeListener),
60



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

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

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

6
}