GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/mailwindow.cpp Lines: 48 194 24.7 %
Date: 2018-07-14 Branches: 61 242 25.2 %

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/mailwindow.h"
22
23
#include "settings.h"
24
25
#include "enums/gui/layouttype.h"
26
27
#include "net/mail2handler.h"
28
#include "net/mailhandler.h"
29
30
#include "gui/models/extendednamesmodel.h"
31
32
#include "gui/mailmessage.h"
33
34
#include "gui/windows/maileditwindow.h"
35
#include "gui/windows/mailviewwindow.h"
36
#include "gui/windows/setupwindow.h"
37
38
#include "gui/widgets/button.h"
39
#include "gui/widgets/containerplacer.h"
40
#include "gui/widgets/createwidget.h"
41
#include "gui/widgets/extendedlistbox.h"
42
#include "gui/widgets/layout.h"
43
#include "gui/widgets/scrollarea.h"
44
45
#include "utils/delete2.h"
46
#include "utils/dtor.h"
47
#include "utils/foreach.h"
48
#include "utils/gettext.h"
49
#include "utils/stringutils.h"
50
51
#include "debug.h"
52
53
MailWindow *mailWindow = nullptr;
54
55
1
MailWindow::MailWindow() :
56
    // TRANSLATORS: mail window name
57
1
    Window(_("Mail"), Modal_false, nullptr, "mail.xml"),
58
    ActionListener(),
59
    mMessages(),
60
    mMessagesMap(),
61

1
    mMailModel(new ExtendedNamesModel),
62


4
    mListBox(CREATEWIDGETR(ExtendedListBox,
63
        this, mMailModel, "extendedlistbox.xml", 0)),
64
1
    mListScrollArea(new ScrollArea(this, mListBox,
65

4
        fromBool(getOptionBool("showlistbackground", false), Opaque),
66

2
        "mail_listbackground.xml")),
67
    // TRANSLATORS: mail window button
68
1
    mRefreshButton(new Button(this, _("Refresh"), "refresh",
69

1
        BUTTON_SKIN, this)),
70
    // TRANSLATORS: mail window button
71

2
    mNewButton(new Button(this, _("New"), "new", BUTTON_SKIN, this)),
72
    // TRANSLATORS: mail window button
73

2
    mDeleteButton(new Button(this, _("Delete"), "delete", BUTTON_SKIN, this)),
74
    mReturnButton(new Button(this,
75
        // TRANSLATORS: mail window button
76
1
        settings.enableNewMailSystem ? _("Get old") : _("Return"),
77

1
        "return", BUTTON_SKIN, this)),
78
    // TRANSLATORS: mail window button
79

2
    mOpenButton(new Button(this, _("Open"), "open", BUTTON_SKIN, this)),
80
    mOpenType(MailOpenType::Mail),
81
1
    mUseMail2(settings.enableNewMailSystem),
82







54
    mLastPage(false)
83
{
84
5
    setWindowName("Mail");
85
1
    setCloseButton(true);
86
1
    setResizable(true);
87
1
    setCloseButton(true);
88
2
    setSaveVisible(true);
89
1
    setStickyButtonLock(true);
90
91
1
    if (setupWindow != nullptr)
92
        setupWindow->registerWindowForReset(this);
93
94
1
    setDefaultSize(310, 180, ImagePosition::CENTER, 0, 0);
95
1
    setMinWidth(310);
96
1
    setMinHeight(250);
97
1
    center();
98
99
1
    mListScrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
100
101
1
    ContainerPlacer placer(nullptr, nullptr);
102
1
    placer = getPlacer(0, 0);
103
104
2
    placer(0, 0, mListScrollArea, 4, 5).setPadding(3);
105
1
    placer(4, 0, mRefreshButton, 1, 1);
106
1
    placer(4, 1, mOpenButton, 1, 1);
107
1
    placer(4, 2, mNewButton, 1, 1);
108
1
    placer(4, 3, mDeleteButton, 1, 1);
109
1
    placer(4, 4, mReturnButton, 1, 1);
110
111
1
    Layout &layout = getLayout();
112
1
    layout.setRowHeight(0, LayoutType::SET);
113
114
1
    loadWindowState();
115
2
    enableVisibleSound(true);
116
1
}
117
118
6
MailWindow::~MailWindow()
119
{
120
2
    delete_all(mMessages);
121
1
    delete2(mMailModel);
122
1
    delete2(mailViewWindow);
123
1
    delete2(mailEditWindow);
124
2
}
125
126
void MailWindow::action(const ActionEvent &event)
127
{
128
    const std::string &eventId = event.getId();
129
    if (eventId == "refresh")
130
    {
131
        refreshMails();
132
    }
133
    else if (eventId == "new")
134
    {
135
        if (mailEditWindow == nullptr)
136
        {
137
            CREATEWIDGETV0(mailEditWindow, MailEditWindow);
138
        }
139
    }
140
    else if (eventId == "open")
141
    {
142
        const int sel = mListBox->getSelected();
143
        if (sel < 0)
144
            return;
145
        const MailMessage *const mail = mMessages[sel];
146
        if (mUseMail2)
147
            mail2Handler->readMail(mOpenType, mail->id);
148
        else
149
            mailHandler->readMessage(CAST_S32(mail->id));
150
    }
151
    else if (eventId == "delete")
152
    {
153
        const int sel = mListBox->getSelected();
154
        if (sel < 0)
155
            return;
156
        const MailMessage *const mail = mMessages[sel];
157
        if (mUseMail2)
158
            mail2Handler->deleteMail(mOpenType, mail->id);
159
        else
160
            mailHandler->deleteMessage(CAST_S32(mail->id));
161
    }
162
    else if (eventId == "return")
163
    {
164
        if (mUseMail2)
165
        {
166
            const size_t idx = mMessages.size();
167
            if (idx == 0)
168
                mail2Handler->refreshMailList(MailOpenType::Mail, 0);
169
            else
170
                mail2Handler->nextPage(mOpenType, mMessages[idx - 1]->id);
171
        }
172
        else
173
        {
174
            const int sel = mListBox->getSelected();
175
            if (sel < 0)
176
                return;
177
            const MailMessage *const mail = mMessages[sel];
178
            mailHandler->returnMessage(CAST_S32(mail->id));
179
        }
180
    }
181
}
182
183
void MailWindow::clear()
184
{
185
    delete_all(mMessages);
186
    mMessages.clear();
187
    mMessagesMap.clear();
188
    mMailModel->clear();
189
    mListBox->setSelected(-1);
190
}
191
192
std::string MailWindow::getMailHeader(const MailMessage *const message) const
193
{
194
    if (mUseMail2)
195
    {
196
        std::string header;
197
        if (message->expired)
198
        {
199
            // TRANSLATORS: expired mail message prefix
200
            header = _("(expired)");
201
        }
202
        if (message->read)
203
            header.append(" ");
204
        else
205
            header.append("U");
206
        const MailMessageType::Type type = message->type;
207
        if ((type & MailMessageType::Money) != 0)
208
            header.append("M");
209
        else
210
            header.append(" ");
211
        if ((type & MailMessageType::Item) != 0)
212
            header.append("I");
213
        else
214
            header.append(" ");
215
        header.append(" ").append(message->title);
216
        return header;
217
    }
218
    return strprintf("%s %s",
219
        message->read ? " " : "U",
220
        message->title.c_str());
221
}
222
223
void MailWindow::addMail(MailMessage *const message)
224
{
225
    if (message == nullptr)
226
        return;
227
    mMessages.push_back(message);
228
    mMailModel->add(getMailHeader(message));
229
    mMessagesMap[message->id] = message;
230
}
231
232
void MailWindow::removeMail(const int64_t id)
233
{
234
    std::map<int64_t, MailMessage*>::iterator it1 = mMessagesMap.find(id);
235
    if (it1 != mMessagesMap.end())
236
        mMessagesMap.erase(it1);
237
238
    mMailModel->clear();
239
240
    FOR_EACH (STD_VECTOR<MailMessage*>::iterator, it, mMessages)
241
    {
242
        MailMessage *message = *it;
243
        if ((message != nullptr) && message->id == id)
244
        {
245
            mMessages.erase(it);
246
            delete message;
247
            break;
248
        }
249
    }
250
251
    FOR_EACH (STD_VECTOR<MailMessage*>::iterator, it, mMessages)
252
    {
253
        MailMessage *message = *it;
254
        if (message != nullptr)
255
            mMailModel->add(getMailHeader(message));
256
    }
257
}
258
259
void MailWindow::showMessage(MailMessage *const mail,
260
                             const int itemsCount)
261
{
262
    if (mail == nullptr)
263
        return;
264
    const std::map<int64_t, MailMessage*>::const_iterator
265
        it = mMessagesMap.find(mail->id);
266
    if (it != mMessagesMap.end())
267
    {
268
        const MailMessage *const mail2 = (*it).second;
269
        mail->time = mail2->time;
270
        mail->strTime = mail2->strTime;
271
    }
272
    delete mailViewWindow;
273
    CREATEWIDGETV(mailViewWindow, MailViewWindow, mail,
274
        itemsCount);
275
}
276
277
void MailWindow::viewNext(const int64_t id)
278
{
279
    FOR_EACH (STD_VECTOR<MailMessage*>::iterator, it, mMessages)
280
    {
281
        MailMessage *message = *it;
282
        if (message != nullptr &&
283
            message->id == id)
284
        {
285
            ++ it;
286
            if (it == mMessages.end())
287
            {
288
                it = mMessages.begin();
289
                mListBox->setSelected(0);
290
            }
291
            else
292
            {
293
                mListBox->setSelected(mListBox->getSelected() + 1);
294
            }
295
            message = *it;
296
            if (mUseMail2)
297
                mail2Handler->readMail(mOpenType, message->id);
298
            else
299
                mailHandler->readMessage(CAST_S32(message->id));
300
            return;
301
        }
302
    }
303
}
304
305
void MailWindow::viewPrev(const int64_t id)
306
{
307
    FOR_EACH (STD_VECTOR<MailMessage*>::iterator, it, mMessages)
308
    {
309
        MailMessage *message = *it;
310
        if (message != nullptr &&
311
            message->id == id)
312
        {
313
            if (it == mMessages.begin())
314
            {
315
                it = mMessages.end();
316
                mListBox->setSelected(CAST_S32(mMessages.size()) - 1);
317
            }
318
            else
319
            {
320
                mListBox->setSelected(mListBox->getSelected() - 1);
321
            }
322
            -- it;
323
            message = *it;
324
            if (mUseMail2)
325
                mail2Handler->readMail(mOpenType, message->id);
326
            else
327
                mailHandler->readMessage(CAST_S32(message->id));
328
            return;
329
        }
330
    }
331
}
332
333
void MailWindow::mouseClicked(MouseEvent &event)
334
{
335
    if (event.getButton() == MouseButton::LEFT)
336
    {
337
        event.consume();
338
        if (event.getClickCount() == 2 &&
339
            event.getSource() == mListBox)
340
        {
341
            action(ActionEvent(mListBox, "open"));
342
        }
343
    }
344
}
345
346
void MailWindow::postConnection()
347
{
348
    refreshMails();
349
}
350
351
void MailWindow::refreshMails()
352
{
353
    if (mUseMail2)
354
    {
355
        clear();
356
        mail2Handler->refreshMailList(MailOpenType::Mail, 0);
357
        mLastPage = false;
358
        mReturnButton->setEnabled(true);
359
    }
360
    else
361
    {
362
        mailHandler->refresh();
363
    }
364
}
365
366
void MailWindow::createMail(const std::string &to)
367
{
368
    if (mailEditWindow != nullptr)
369
        return;
370
371
    CREATEWIDGETV0(mailEditWindow, MailEditWindow);
372
    mailEditWindow->setTo(to);
373
}
374
375
MailMessage *MailWindow::findMail(const int64_t id)
376
{
377
    std::map<int64_t, MailMessage*>::iterator it = mMessagesMap.find(id);
378
    if (it != mMessagesMap.end())
379
        return (*it).second;
380
    return nullptr;
381
}
382
383
void MailWindow::setLastPage()
384
{
385
    mLastPage = true;
386
    mReturnButton->setEnabled(false);
387
}
388
389
void MailWindow::refreshMailNames()
390
{
391
    mMailModel->clear();
392
    FOR_EACH (STD_VECTOR<MailMessage*>::iterator, it, mMessages)
393
    {
394
        MailMessage *message = *it;
395
        if (message != nullptr)
396
            mMailModel->add(getMailHeader(message));
397
    }
398

3
}