GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/mailwindow.cpp Lines: 47 189 24.9 %
Date: 2017-11-29 Branches: 62 242 25.6 %

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/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/stdmove.h"
50
#include "utils/stringutils.h"
51
52
#include "debug.h"
53
54
MailWindow *mailWindow = nullptr;
55
56
2
MailWindow::MailWindow() :
57
    // TRANSLATORS: mail window name
58
2
    Window(_("Mail"), Modal_false, nullptr, "mail.xml"),
59
    ActionListener(),
60
    mMessages(),
61
    mMessagesMap(),
62

2
    mMailModel(new ExtendedNamesModel),
63


8
    mListBox(CREATEWIDGETR(ExtendedListBox,
64
        this, mMailModel, "extendedlistbox.xml", 0)),
65
2
    mListScrollArea(new ScrollArea(this, mListBox,
66

8
        fromBool(getOptionBool("showlistbackground"), Opaque),
67

4
        "mail_listbackground.xml")),
68
    // TRANSLATORS: mail window button
69

4
    mRefreshButton(new Button(this, _("Refresh"), "refresh", this)),
70
    // TRANSLATORS: mail window button
71

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

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

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

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







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

6
}