GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/maileditwindow.cpp Lines: 65 130 50.0 %
Date: 2018-05-19 03:07:18 Branches: 85 220 38.6 %

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
2
MailEditWindow::MailEditWindow() :
52
    // TRANSLATORS: mail edit window name
53
2
    Window(_("Edit mail"), Modal_false, nullptr, "mailedit.xml"),
54
    ActionListener(),
55
    FocusListener(),
56
    // TRANSLATORS: mail edit window button
57

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

4
        "mailedit_listbackground.xml")),
87







130
    mUseMail2(settings.enableNewMailSystem)
88
{
89
10
    setWindowName("MailEdit");
90
2
    setCloseButton(true);
91
2
    setResizable(true);
92
2
    setCloseButton(true);
93
4
    setSaveVisible(false);
94
2
    setStickyButtonLock(true);
95
2
    setVisible(Visible_true);
96
97
2
    setDefaultSize(380, 200, ImagePosition::CENTER, 0, 0);
98
2
    setMinWidth(200);
99
2
    setMinHeight(200);
100
2
    center();
101
102
2
    ContainerPlacer placer(nullptr, nullptr);
103
2
    placer = getPlacer(0, 0);
104
105
2
    mToField->setWidth(100);
106
2
    mSubjectField->setWidth(100);
107
2
    mMessageField->setWidth(100);
108
2
    mItemScrollArea->setHeight(100);
109
2
    mItemScrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
110
2
    mToField->addFocusListener(this);
111
112
2
    placer(0, 0, mToLabel, 1, 1);
113
2
    placer(1, 0, mToField, 3, 1);
114
2
    placer(0, 1, mSubjectLabel, 1, 1);
115
2
    placer(1, 1, mSubjectField, 3, 1);
116
2
    placer(0, 2, mMoneyLabel, 1, 1);
117
2
    placer(1, 2, mMoneyField, 3, 1);
118
2
    placer(0, 3, mItemLabel, 1, 1);
119
2
    placer(1, 3, mItemScrollArea, 2, 2);
120
2
    placer(3, 4, mAddButton, 1, 1);
121
122
2
    placer(0, 5, mMessageLabel, 1, 1);
123
2
    placer(1, 5, mMessageField, 3, 1);
124
2
    placer(0, 6, mSendButton, 1, 1);
125
2
    placer(3, 6, mCloseButton, 1, 1);
126
127
2
    loadWindowState();
128
2
    if (mUseMail2)
129
2
        mSendButton->setEnabled(false);
130
4
    enableVisibleSound(true);
131
2
}
132
133
10
MailEditWindow::~MailEditWindow()
134
{
135
2
    mailEditWindow = nullptr;
136
2
    delete2(mInventory);
137
4
}
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

6
}