GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/mailviewwindow.cpp Lines: 68 137 49.6 %
Date: 2018-06-18 21:15:20 Branches: 80 230 34.8 %

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/mailviewwindow.h"
22
23
#include "settings.h"
24
25
#include "net/mail2handler.h"
26
#include "net/mailhandler.h"
27
28
#include "gui/mailmessage.h"
29
30
#include "gui/windows/maileditwindow.h"
31
#include "gui/windows/mailwindow.h"
32
33
#include "gui/widgets/button.h"
34
#include "gui/widgets/containerplacer.h"
35
#include "gui/widgets/createwidget.h"
36
#include "gui/widgets/itemcontainer.h"
37
#include "gui/widgets/label.h"
38
#include "gui/widgets/scrollarea.h"
39
40
#include "utils/delete2.h"
41
#include "utils/gettext.h"
42
#include "utils/stringutils.h"
43
44
#include "resources/inventory/inventory.h"
45
#include "debug.h"
46
47
MailViewWindow *mailViewWindow = nullptr;
48
49
2
MailViewWindow::MailViewWindow(MailMessage *const message,
50
2
                               const int itemsCount) :
51
    // TRANSLATORS: mail view window name
52
2
    Window(_("View mail"), Modal_false, nullptr, "mailview.xml"),
53
    ActionListener(),
54
    mMessage(message),
55
    mGetAttachButton(new Button(this,
56
        // TRANSLATORS: mail view attach / items button
57
2
        settings.enableNewMailSystem ? _("Get items") : _("Get attach"),
58
        "attach",
59
        BUTTON_SKIN,
60

2
        this)),
61
    mGetMoneyButton(nullptr),
62
    // TRANSLATORS: mail view window button
63

4
    mCloseButton(new Button(this, _("Close"), "close", BUTTON_SKIN, this)),
64

2
    mPrevButton(new Button(this, "<", "prev", BUTTON_SKIN, this)),
65

2
    mNextButton(new Button(this, ">", "next", BUTTON_SKIN, this)),
66
    // TRANSLATORS: mail view window button
67

4
    mReplyButton(new Button(this, _("Reply"), "reply", BUTTON_SKIN, this)),
68
    // TRANSLATORS: mail view window label
69
6
    mTimeLabel(new Label(this, strprintf("%s %s", _("Time:"),
70

2
        message->strTime.c_str()))),
71
    mMoneyLabel(nullptr),
72
    // TRANSLATORS: mail view window label
73
6
    mFromLabel(new Label(this, strprintf("%s %s", _("From:"),
74

2
        message->sender.c_str()))),
75
    // TRANSLATORS: mail view window label
76
6
    mSubjectLabel(new Label(this, strprintf("%s %s", _("Subject:"),
77

2
        message->title.c_str()))),
78
    // TRANSLATORS: mail view window label
79
6
    mMessageLabel(new Label(this, strprintf("%s %s", _("Message:"),
80

2
        message->text.c_str()))),
81

2
    mInventory(new Inventory(InventoryType::MailView, itemsCount)),
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
        "mailview_listbackground.xml")),
87







112
    mUseMail2(settings.enableNewMailSystem)
88
{
89
10
    setWindowName("MailView");
90
2
    setCloseButton(true);
91
2
    setResizable(true);
92
4
    setSaveVisible(false);
93
2
    setStickyButtonLock(true);
94
2
    setVisible(Visible_true);
95
96
2
    setDefaultSize(380, 370, ImagePosition::CENTER, 0, 0);
97
2
    setMinWidth(200);
98
2
    setMinHeight(100);
99
2
    center();
100
2
    mItemScrollArea->setHeight(100);
101
102
2
    mItemScrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
103
104
2
    ContainerPlacer placer(nullptr, nullptr);
105
2
    placer = getPlacer(0, 0);
106
107
2
    int n = 0;
108
2
    placer(0, n++, mTimeLabel, 1, 1);
109
2
    placer(0, n++, mFromLabel, 1, 1);
110
2
    placer(0, n++, mSubjectLabel, 1, 1);
111
2
    if (message->money != 0)
112
    {
113
        mMoneyLabel = new Label(this, strprintf("%s %u",
114
            // TRANSLATORS: mail view window label
115
            _("Money:"),
116
            CAST_U32(message->money)));
117
        placer(0, n++, mMoneyLabel, 1, 1);
118
    }
119
2
    placer(0, n++, mMessageLabel, 1, 1);
120
2
    placer(0, n++, mItemScrollArea, 1, 1);
121
122

2
    if (mUseMail2 && message->money != 0)
123
    {
124
        mGetMoneyButton = new Button(this,
125
            // TRANSLATORS: mail view attached money button
126
            _("Get money"),
127
            "money",
128
            BUTTON_SKIN,
129
            this);
130
        placer(0, n++, mGetMoneyButton, 1, 1);
131
    }
132
2
    placer(0, n++, mGetAttachButton, 1, 1);
133
2
    updateAttachButton();
134
135
2
    ContainerPlacer placer2(nullptr, nullptr);
136
2
    placer2 = getPlacer(0, n);
137
138
2
    placer2(0, 0, mPrevButton, 1, 1);
139
2
    placer2(1, 0, mNextButton, 1, 1);
140
2
    placer2(3, 0, mReplyButton, 1, 1);
141
2
    placer2(4, 0, mCloseButton, 1, 1);
142
143
2
    loadWindowState();
144
4
    enableVisibleSound(true);
145
2
}
146
147
8
MailViewWindow::~MailViewWindow()
148
{
149
2
    if (mUseMail2)
150
    {
151
2
        mMessage = nullptr;
152
    }
153
    else
154
    {
155
        delete2(mMessage);
156
        delete2(mGetMoneyButton);
157
    }
158
2
    delete2(mInventory);
159
2
    mailViewWindow = nullptr;
160
4
}
161
162
void MailViewWindow::action(const ActionEvent &event)
163
{
164
    const std::string &eventId = event.getId();
165
    if (eventId == "close")
166
    {
167
        scheduleDelete();
168
    }
169
    else if (eventId == "attach")
170
    {
171
        if (mUseMail2)
172
        {
173
            mail2Handler->requestItems(mailWindow->getOpenType(),
174
                mMessage->id);
175
        }
176
        else
177
        {
178
            mailHandler->getAttach(CAST_S32(mMessage->id));
179
        }
180
    }
181
    else if (eventId == "money")
182
    {
183
        if (mUseMail2)
184
        {
185
            mail2Handler->requestMoney(mailWindow->getOpenType(),
186
                mMessage->id);
187
        }
188
    }
189
    else if (eventId == "next")
190
    {
191
        if (mMessage != nullptr)
192
            mailWindow->viewNext(mMessage->id);
193
    }
194
    else if (eventId == "prev")
195
    {
196
        if (mMessage != nullptr)
197
            mailWindow->viewPrev(mMessage->id);
198
    }
199
    else if (eventId == "reply")
200
    {
201
        if (mMessage == nullptr)
202
            return;
203
        if (mailEditWindow != nullptr)
204
            mailEditWindow->scheduleDelete();
205
        CREATEWIDGETV0(mailEditWindow, MailEditWindow);
206
        mailEditWindow->setTo(mMessage->sender);
207
        mailEditWindow->setSubject("Re:" + mMessage->title);
208
        mailEditWindow->setMessage(">" + mMessage->text);
209
        mail2Handler->queueCheckName(MailQueueType::ValidateTo,
210
            mMessage->sender,
211
            std::string(),
212
            std::string(),
213
            0);
214
        scheduleDelete();
215
    }
216
}
217
218
Inventory *MailViewWindow::getInventory() const
219
{
220
    return mInventory;
221
}
222
223
2
void MailViewWindow::updateAttachButton()
224
{
225

4
    if ((mMessage->money != 0 && !mUseMail2) ||
226
2
        mInventory->getLastUsedSlot() != -1)
227
    {
228
        mGetAttachButton->setVisible(Visible_true);
229
    }
230
    else
231
    {
232
2
        mGetAttachButton->setVisible(Visible_false);
233
    }
234
2
}
235
236
void MailViewWindow::updateItems()
237
{
238
    mItemContainer->updateMatrix();
239
    updateAttachButton();
240
}
241
242
void MailViewWindow::removeItems(const int64_t mailId)
243
{
244
    if (mailId != mMessage->id)
245
        return;
246
    mInventory->clear();
247
    mMessage->type = static_cast<MailMessageType::Type>(
248
        CAST_S32(mMessage->type) | CAST_S32(MailMessageType::Item));
249
    mMessage->type = static_cast<MailMessageType::Type>(
250
        CAST_S32(mMessage->type) ^ CAST_S32(MailMessageType::Item));
251
    updateAttachButton();
252
    if (mailWindow != nullptr)
253
        mailWindow->refreshMailNames();
254
}
255
256
void MailViewWindow::removeMoney(const int64_t mailId)
257
{
258
    if (mailId != mMessage->id)
259
        return;
260
    mMessage->type = static_cast<MailMessageType::Type>(
261
        CAST_S32(mMessage->type) | CAST_S32(MailMessageType::Money));
262
    mMessage->type = static_cast<MailMessageType::Type>(
263
        CAST_S32(mMessage->type) ^ CAST_S32(MailMessageType::Money));
264
265
    mMessage->money = 0;
266
267
    if (mMoneyLabel == nullptr)
268
        return;
269
270
    if (mGetMoneyButton != nullptr)
271
        mGetMoneyButton->setVisible(Visible_false);
272
273
    mMoneyLabel->setCaption(strprintf("%s %d",
274
        // TRANSLATORS: mail view window label
275
        _("Money:"),
276
        0));
277
    if (mailWindow != nullptr)
278
        mailWindow->refreshMailNames();
279

6
}