GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/mailviewwindow.cpp Lines: 68 132 51.5 %
Date: 2017-11-29 Branches: 80 228 35.1 %

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/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

2
        "attach", this)),
59
    mGetMoneyButton(nullptr),
60
    // TRANSLATORS: mail view window button
61

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

2
    mPrevButton(new Button(this, "<", "prev", this)),
63

2
    mNextButton(new Button(this, ">", "next", this)),
64
    // TRANSLATORS: mail view window button
65

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

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

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

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

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

2
    mInventory(new Inventory(InventoryType::MailView, itemsCount)),
80

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

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

4
        "mailview_listbackground.xml")),
84







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

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

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

6
}