GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/maileditwindow.cpp Lines: 62 127 48.8 %
Date: 2017-11-29 Branches: 91 232 39.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  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", this)),
58
    // TRANSLATORS: mail edit window button
59

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

4
    mAddButton(new Button(this, _("Add"), "add", 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

2
    mToField(new TextField(this)),
73

2
    mSubjectField(new TextField(this)),
74
    mMoneyField(new IntTextField(this, 0, 0,
75

2
        settings.enableNewMailSystem ? INT_MAX : 10000000)),
76

2
    mMessageField(new TextField(this)),
77
    mInventory(new Inventory(InventoryType::MailEdit,
78

2
        settings.enableNewMailSystem ? -1 : 1)),
79

2
    mItemContainer(new ItemContainer(this, mInventory)),
80
2
    mItemScrollArea(new ScrollArea(this, mItemContainer,
81

8
        fromBool(getOptionBool("showitemsbackground"), Opaque),
82

4
        "mailedit_listbackground.xml")),
83










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

6
}