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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2
        "mailedit_listbackground.xml")),
87







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

3
}