GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/mailviewwindow.cpp Lines: 68 139 48.9 %
Date: 2021-03-17 Branches: 80 230 34.8 %

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

1
        this)),
62
    mGetMoneyButton(nullptr),
63
    // TRANSLATORS: mail view window button
64

2
    mCloseButton(new Button(this, _("Close"), "close", BUTTON_SKIN, this)),
65

1
    mPrevButton(new Button(this, "<", "prev", BUTTON_SKIN, this)),
66

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

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

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

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

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

1
        message->text.c_str()))),
82

1
    mInventory(new Inventory(InventoryType::MailView, itemsCount)),
83
    mItemContainer(new ItemContainer(this, mInventory, 100000,
84

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

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

2
        "mailview_listbackground.xml")),
88







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

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


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

3
}