GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/maileditwindow.cpp Lines: 65 132 49.2 %
Date: 2021-03-17 Branches: 84 218 38.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "gui/windows/maileditwindow.h"
23
24
#include "settings.h"
25
26
#include "being/playerinfo.h"
27
28
#include "gui/windows/inventorywindow.h"
29
#include "gui/windows/itemamountwindow.h"
30
31
#include "gui/widgets/button.h"
32
#include "gui/widgets/containerplacer.h"
33
#include "gui/widgets/inttextfield.h"
34
#include "gui/widgets/itemcontainer.h"
35
#include "gui/widgets/label.h"
36
#include "gui/widgets/scrollarea.h"
37
38
#include "resources/item/item.h"
39
40
#include "net/mail2handler.h"
41
#include "net/mailhandler.h"
42
43
#include "utils/delete2.h"
44
#include "utils/gettext.h"
45
46
#include <climits>
47
48
#include "debug.h"
49
50
MailEditWindow *mailEditWindow = nullptr;
51
52
1
MailEditWindow::MailEditWindow() :
53
    // TRANSLATORS: mail edit window name
54
1
    Window(_("Edit mail"), Modal_false, nullptr, "mailedit.xml"),
55
    ActionListener(),
56
    FocusListener(),
57
    // TRANSLATORS: mail edit window button
58

2
    mSendButton(new Button(this, _("Send"), "send", BUTTON_SKIN, this)),
59
    // TRANSLATORS: mail edit window button
60

2
    mCloseButton(new Button(this, _("Close"), "close", BUTTON_SKIN, this)),
61
    // TRANSLATORS: mail edit window button
62

2
    mAddButton(new Button(this, _("Add"), "add", BUTTON_SKIN, this)),
63
    // TRANSLATORS: mail edit window label
64

2
    mToLabel(new Label(this, _("To:"))),
65
    // TRANSLATORS: mail edit window label
66

2
    mSubjectLabel(new Label(this, _("Subject:"))),
67
    // TRANSLATORS: mail edit window label
68

2
    mMoneyLabel(new Label(this, _("Money:"))),
69
    // TRANSLATORS: mail edit window label
70

2
    mItemLabel(new Label(this, _("Item:"))),
71
    // TRANSLATORS: mail edit window label
72

2
    mMessageLabel(new Label(this, _("Message:"))),
73
2
    mToField(new TextField(this, std::string(), LoseFocusOnTab_true,
74

2
        nullptr, std::string(), false)),
75
2
    mSubjectField(new TextField(this, std::string(), LoseFocusOnTab_true,
76

2
        nullptr, std::string(), false)),
77
    mMoneyField(new IntTextField(this, 0, 0,
78

1
        settings.enableNewMailSystem ? INT_MAX : 10000000, Enable_true, 0)),
79
2
    mMessageField(new TextField(this, std::string(), LoseFocusOnTab_true,
80

2
        nullptr, std::string(), false)),
81
    mInventory(new Inventory(InventoryType::MailEdit,
82

1
        settings.enableNewMailSystem ? -1 : 1)),
83
    mItemContainer(new ItemContainer(this, mInventory, 100000,
84

1
        ShowEmptyRows_false, ForceQuantity_false)),
85
1
    mItemScrollArea(new ScrollArea(this, mItemContainer,
86

4
        fromBool(getOptionBool("showitemsbackground", false), Opaque),
87

2
        "mailedit_listbackground.xml")),
88







65
    mUseMail2(settings.enableNewMailSystem)
89
{
90
5
    setWindowName("MailEdit");
91
1
    setCloseButton(true);
92
1
    setResizable(true);
93
1
    setCloseButton(true);
94
2
    setSaveVisible(false);
95
1
    setStickyButtonLock(true);
96
1
    setVisible(Visible_true);
97
98
1
    setDefaultSize(380, 200, ImagePosition::CENTER, 0, 0);
99
1
    setMinWidth(200);
100
1
    setMinHeight(200);
101
1
    center();
102
103
1
    ContainerPlacer placer(nullptr, nullptr);
104
1
    placer = getPlacer(0, 0);
105
106
1
    mToField->setWidth(100);
107
1
    mSubjectField->setWidth(100);
108
1
    mMessageField->setWidth(100);
109
1
    mItemScrollArea->setHeight(100);
110
1
    mItemScrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
111
1
    mToField->addFocusListener(this);
112
113
1
    placer(0, 0, mToLabel, 1, 1);
114
1
    placer(1, 0, mToField, 3, 1);
115
1
    placer(0, 1, mSubjectLabel, 1, 1);
116
1
    placer(1, 1, mSubjectField, 3, 1);
117
1
    placer(0, 2, mMoneyLabel, 1, 1);
118
1
    placer(1, 2, mMoneyField, 3, 1);
119
1
    placer(0, 3, mItemLabel, 1, 1);
120
1
    placer(1, 3, mItemScrollArea, 2, 2);
121
1
    placer(3, 4, mAddButton, 1, 1);
122
123
1
    placer(0, 5, mMessageLabel, 1, 1);
124
1
    placer(1, 5, mMessageField, 3, 1);
125
1
    placer(0, 6, mSendButton, 1, 1);
126
1
    placer(3, 6, mCloseButton, 1, 1);
127
128
1
    loadWindowState();
129
1
    if (mUseMail2)
130
1
        mSendButton->setEnabled(false);
131
2
    enableVisibleSound(true);
132
1
}
133
134
5
MailEditWindow::~MailEditWindow()
135
{
136
1
    mailEditWindow = nullptr;
137
1
    delete2(mInventory)
138
2
}
139
140
void MailEditWindow::action(const ActionEvent &event)
141
{
142
    const std::string &eventId = event.getId();
143
    if (eventId == "close")
144
    {
145
        close();
146
    }
147
    else if (eventId == "send")
148
    {
149
        sendMail();
150
    }
151
    else if (eventId == "add")
152
    {
153
        Item *const item = inventoryWindow->getSelectedItem();
154
155
        if (item == nullptr)
156
            return;
157
158
        ItemAmountWindow::showWindow(ItemAmountWindowUsage::MailAdd,
159
            this,
160
            item,
161
            0,
162
            0);
163
    }
164
}
165
166
void MailEditWindow::addItem(const Item *const item,
167
                             const int amount)
168
{
169
    if (item == nullptr)
170
        return;
171
    mInventory->addItem(item->getId(),
172
        item->getType(),
173
        amount,
174
        item->getRefine(),
175
        item->getColor(),
176
        item->getIdentified(),
177
        item->getDamaged(),
178
        item->getFavorite(),
179
        Equipm_false,
180
        Equipped_false);
181
}
182
183
void MailEditWindow::setSubject(const std::string &str)
184
{
185
    mSubjectField->setText(str);
186
}
187
188
void MailEditWindow::setTo(const std::string &str)
189
{
190
    mToField->setText(str);
191
    mSendButton->setEnabled(true);
192
}
193
194
void MailEditWindow::setMessage(const std::string &str)
195
{
196
    mMessageField->setText(str);
197
}
198
199
void MailEditWindow::close()
200
{
201
    if (mUseMail2)
202
        mail2Handler->cancelWriteMail();
203
    mailEditWindow = nullptr;
204
    scheduleDelete();
205
}
206
207
Inventory *MailEditWindow::getInventory() const
208
{
209
    return mInventory;
210
}
211
212
void MailEditWindow::sendMail()
213
{
214
    const int money = mMoneyField->getValue();
215
    std::string subject = mSubjectField->getText();
216
    if (subject.empty())
217
    {
218
        // TRANSLATORS: empty mail message subject
219
        subject.append(_("empty subject"));
220
    }
221
    if (mUseMail2)
222
    {
223
        mail2Handler->sendMail(mToField->getText(),
224
            subject,
225
            mMessageField->getText(),
226
            money);
227
    }
228
    else
229
    {
230
        if (money != 0)
231
            mailHandler->setAttachMoney(money);
232
        const Item *const tempItem = mInventory->getItem(0);
233
        if (tempItem != nullptr)
234
        {
235
            const Inventory *const inv = PlayerInfo::getInventory();
236
            if (inv != nullptr)
237
            {
238
                const Item *const item = inv->findItem(
239
                    tempItem->getId(), ItemColor_one);
240
                if (item != nullptr)
241
                {
242
                    mailHandler->setAttach(item->getInvIndex(),
243
                        tempItem->getQuantity());
244
                }
245
            }
246
        }
247
248
        mailHandler->send(mToField->getText(),
249
            subject,
250
            mMessageField->getText());
251
    }
252
}
253
254
void MailEditWindow::updateItems()
255
{
256
    mItemContainer->updateMatrix();
257
}
258
259
void MailEditWindow::focusLost(const Event &event)
260
{
261
    if (!mUseMail2)
262
        return;
263
264
    if (event.getSource() == mToField)
265
    {
266
        const std::string to = mToField->getText();
267
        if (to != mail2Handler->getCheckedName())
268
        {
269
            mail2Handler->queueCheckName(MailQueueType::ValidateTo,
270
                to,
271
                std::string(),
272
                std::string(),
273
                0);
274
            mSendButton->setEnabled(false);
275
        }
276
        else
277
        {
278
            mSendButton->setEnabled(true);
279
        }
280
    }
281
}
282
283
void MailEditWindow::validatedTo()
284
{
285
    mSendButton->setEnabled(true);
286

3
}