GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
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/mailwindow.h" |
||
23 |
|||
24 |
#include "settings.h" |
||
25 |
|||
26 |
#include "enums/gui/layouttype.h" |
||
27 |
|||
28 |
#include "net/mail2handler.h" |
||
29 |
#include "net/mailhandler.h" |
||
30 |
|||
31 |
#include "gui/models/extendednamesmodel.h" |
||
32 |
|||
33 |
#include "gui/mailmessage.h" |
||
34 |
|||
35 |
#include "gui/windows/maileditwindow.h" |
||
36 |
#include "gui/windows/mailviewwindow.h" |
||
37 |
#include "gui/windows/setupwindow.h" |
||
38 |
|||
39 |
#include "gui/widgets/button.h" |
||
40 |
#include "gui/widgets/containerplacer.h" |
||
41 |
#include "gui/widgets/createwidget.h" |
||
42 |
#include "gui/widgets/extendedlistbox.h" |
||
43 |
#include "gui/widgets/layout.h" |
||
44 |
#include "gui/widgets/scrollarea.h" |
||
45 |
|||
46 |
#include "utils/delete2.h" |
||
47 |
#include "utils/dtor.h" |
||
48 |
#include "utils/foreach.h" |
||
49 |
#include "utils/gettext.h" |
||
50 |
#include "utils/stringutils.h" |
||
51 |
|||
52 |
#include "debug.h" |
||
53 |
|||
54 |
MailWindow *mailWindow = nullptr; |
||
55 |
|||
56 |
1 |
MailWindow::MailWindow() : |
|
57 |
// TRANSLATORS: mail window name |
||
58 |
1 |
Window(_("Mail"), Modal_false, nullptr, "mail.xml"), |
|
59 |
ActionListener(), |
||
60 |
mMessages(), |
||
61 |
mMessagesMap(), |
||
62 |
✓✗✓✗ |
1 |
mMailModel(new ExtendedNamesModel), |
63 |
✓✗✓✗ ✓✗✓✗ |
4 |
mListBox(CREATEWIDGETR(ExtendedListBox, |
64 |
this, mMailModel, "extendedlistbox.xml", 0)), |
||
65 |
1 |
mListScrollArea(new ScrollArea(this, mListBox, |
|
66 |
✓✗✓✗ ✓✗ |
4 |
fromBool(getOptionBool("showlistbackground", false), Opaque), |
67 |
✓✗✓✗ |
2 |
"mail_listbackground.xml")), |
68 |
// TRANSLATORS: mail window button |
||
69 |
1 |
mRefreshButton(new Button(this, _("Refresh"), "refresh", |
|
70 |
✓✗✓✗ |
1 |
BUTTON_SKIN, this)), |
71 |
// TRANSLATORS: mail window button |
||
72 |
✓✗✓✗ |
2 |
mNewButton(new Button(this, _("New"), "new", BUTTON_SKIN, this)), |
73 |
// TRANSLATORS: mail window button |
||
74 |
✓✗✓✗ |
2 |
mDeleteButton(new Button(this, _("Delete"), "delete", BUTTON_SKIN, this)), |
75 |
mReturnButton(new Button(this, |
||
76 |
// TRANSLATORS: mail window button |
||
77 |
1 |
settings.enableNewMailSystem ? _("Get old") : _("Return"), |
|
78 |
✓✗✓✗ |
1 |
"return", BUTTON_SKIN, this)), |
79 |
// TRANSLATORS: mail window button |
||
80 |
✓✗✓✗ |
2 |
mOpenButton(new Button(this, _("Open"), "open", BUTTON_SKIN, this)), |
81 |
mOpenType(MailOpenType::Mail), |
||
82 |
1 |
mUseMail2(settings.enableNewMailSystem), |
|
83 |
✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ |
54 |
mLastPage(false) |
84 |
{ |
||
85 |
✓✗ | 5 |
setWindowName("Mail"); |
86 |
✓✗ | 1 |
setCloseButton(true); |
87 |
✓✗ | 1 |
setResizable(true); |
88 |
✓✗ | 1 |
setCloseButton(true); |
89 |
2 |
setSaveVisible(true); |
|
90 |
✓✗ | 1 |
setStickyButtonLock(true); |
91 |
|||
92 |
✗✓ | 1 |
if (setupWindow != nullptr) |
93 |
setupWindow->registerWindowForReset(this); |
||
94 |
|||
95 |
✓✗ | 1 |
setDefaultSize(310, 180, ImagePosition::CENTER, 0, 0); |
96 |
✓✗ | 1 |
setMinWidth(310); |
97 |
✓✗ | 1 |
setMinHeight(250); |
98 |
✓✗ | 1 |
center(); |
99 |
|||
100 |
✓✗ | 1 |
mListScrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER); |
101 |
|||
102 |
1 |
ContainerPlacer placer(nullptr, nullptr); |
|
103 |
✓✗ | 1 |
placer = getPlacer(0, 0); |
104 |
|||
105 |
✓✗ | 2 |
placer(0, 0, mListScrollArea, 4, 5).setPadding(3); |
106 |
✓✗ | 1 |
placer(4, 0, mRefreshButton, 1, 1); |
107 |
✓✗ | 1 |
placer(4, 1, mOpenButton, 1, 1); |
108 |
✓✗ | 1 |
placer(4, 2, mNewButton, 1, 1); |
109 |
✓✗ | 1 |
placer(4, 3, mDeleteButton, 1, 1); |
110 |
✓✗ | 1 |
placer(4, 4, mReturnButton, 1, 1); |
111 |
|||
112 |
✓✗ | 1 |
Layout &layout = getLayout(); |
113 |
✓✗ | 1 |
layout.setRowHeight(0, LayoutType::SET); |
114 |
|||
115 |
✓✗ | 1 |
loadWindowState(); |
116 |
2 |
enableVisibleSound(true); |
|
117 |
1 |
} |
|
118 |
|||
119 |
6 |
MailWindow::~MailWindow() |
|
120 |
{ |
||
121 |
2 |
delete_all(mMessages); |
|
122 |
✓✗ | 1 |
delete2(mMailModel) |
123 |
✗✓ | 1 |
delete2(mailViewWindow) |
124 |
✗✓ | 1 |
delete2(mailEditWindow) |
125 |
2 |
} |
|
126 |
|||
127 |
void MailWindow::action(const ActionEvent &event) |
||
128 |
{ |
||
129 |
const std::string &eventId = event.getId(); |
||
130 |
if (eventId == "refresh") |
||
131 |
{ |
||
132 |
refreshMails(); |
||
133 |
} |
||
134 |
else if (eventId == "new") |
||
135 |
{ |
||
136 |
if (mailEditWindow == nullptr) |
||
137 |
{ |
||
138 |
CREATEWIDGETV0(mailEditWindow, MailEditWindow); |
||
139 |
} |
||
140 |
} |
||
141 |
else if (eventId == "open") |
||
142 |
{ |
||
143 |
const int sel = mListBox->getSelected(); |
||
144 |
if (sel < 0) |
||
145 |
return; |
||
146 |
const MailMessage *const mail = mMessages[sel]; |
||
147 |
if (mUseMail2) |
||
148 |
mail2Handler->readMail(mOpenType, mail->id); |
||
149 |
else |
||
150 |
mailHandler->readMessage(CAST_S32(mail->id)); |
||
151 |
} |
||
152 |
else if (eventId == "delete") |
||
153 |
{ |
||
154 |
const int sel = mListBox->getSelected(); |
||
155 |
if (sel < 0) |
||
156 |
return; |
||
157 |
const MailMessage *const mail = mMessages[sel]; |
||
158 |
if (mUseMail2) |
||
159 |
mail2Handler->deleteMail(mOpenType, mail->id); |
||
160 |
else |
||
161 |
mailHandler->deleteMessage(CAST_S32(mail->id)); |
||
162 |
} |
||
163 |
else if (eventId == "return") |
||
164 |
{ |
||
165 |
if (mUseMail2) |
||
166 |
{ |
||
167 |
const size_t idx = mMessages.size(); |
||
168 |
if (idx == 0) |
||
169 |
mail2Handler->refreshMailList(MailOpenType::Mail, 0); |
||
170 |
else |
||
171 |
mail2Handler->nextPage(mOpenType, mMessages[idx - 1]->id); |
||
172 |
} |
||
173 |
else |
||
174 |
{ |
||
175 |
const int sel = mListBox->getSelected(); |
||
176 |
if (sel < 0) |
||
177 |
return; |
||
178 |
const MailMessage *const mail = mMessages[sel]; |
||
179 |
mailHandler->returnMessage(CAST_S32(mail->id)); |
||
180 |
} |
||
181 |
} |
||
182 |
} |
||
183 |
|||
184 |
void MailWindow::clear() |
||
185 |
{ |
||
186 |
delete_all(mMessages); |
||
187 |
mMessages.clear(); |
||
188 |
mMessagesMap.clear(); |
||
189 |
mMailModel->clear(); |
||
190 |
mListBox->setSelected(-1); |
||
191 |
} |
||
192 |
|||
193 |
std::string MailWindow::getMailHeader(const MailMessage *const message) const |
||
194 |
{ |
||
195 |
if (mUseMail2) |
||
196 |
{ |
||
197 |
std::string header; |
||
198 |
if (message->expired) |
||
199 |
{ |
||
200 |
// TRANSLATORS: expired mail message prefix |
||
201 |
header = _("(expired)"); |
||
202 |
} |
||
203 |
if (message->read) |
||
204 |
header.append(" "); |
||
205 |
else |
||
206 |
header.append("U"); |
||
207 |
const MailMessageType::Type type = message->type; |
||
208 |
if ((type & MailMessageType::Money) != 0) |
||
209 |
header.append("M"); |
||
210 |
else |
||
211 |
header.append(" "); |
||
212 |
if ((type & MailMessageType::Item) != 0) |
||
213 |
header.append("I"); |
||
214 |
else |
||
215 |
header.append(" "); |
||
216 |
header.append(" ").append(message->title); |
||
217 |
return header; |
||
218 |
} |
||
219 |
return strprintf("%s %s", |
||
220 |
message->read ? " " : "U", |
||
221 |
message->title.c_str()); |
||
222 |
} |
||
223 |
|||
224 |
void MailWindow::addMail(MailMessage *const message) |
||
225 |
{ |
||
226 |
if (message == nullptr) |
||
227 |
return; |
||
228 |
mMessages.push_back(message); |
||
229 |
mMailModel->add(getMailHeader(message)); |
||
230 |
mMessagesMap[message->id] = message; |
||
231 |
} |
||
232 |
|||
233 |
void MailWindow::removeMail(const int64_t id) |
||
234 |
{ |
||
235 |
std::map<int64_t, MailMessage*>::iterator it1 = mMessagesMap.find(id); |
||
236 |
if (it1 != mMessagesMap.end()) |
||
237 |
mMessagesMap.erase(it1); |
||
238 |
|||
239 |
mMailModel->clear(); |
||
240 |
|||
241 |
FOR_EACH (STD_VECTOR<MailMessage*>::iterator, it, mMessages) |
||
242 |
{ |
||
243 |
MailMessage *message = *it; |
||
244 |
if ((message != nullptr) && message->id == id) |
||
245 |
{ |
||
246 |
mMessages.erase(it); |
||
247 |
delete message; |
||
248 |
break; |
||
249 |
} |
||
250 |
} |
||
251 |
|||
252 |
FOR_EACH (STD_VECTOR<MailMessage*>::iterator, it, mMessages) |
||
253 |
{ |
||
254 |
MailMessage *message = *it; |
||
255 |
if (message != nullptr) |
||
256 |
mMailModel->add(getMailHeader(message)); |
||
257 |
} |
||
258 |
} |
||
259 |
|||
260 |
void MailWindow::showMessage(MailMessage *const mail, |
||
261 |
const int itemsCount) |
||
262 |
{ |
||
263 |
if (mail == nullptr) |
||
264 |
return; |
||
265 |
const std::map<int64_t, MailMessage*>::const_iterator |
||
266 |
it = mMessagesMap.find(mail->id); |
||
267 |
if (it != mMessagesMap.end()) |
||
268 |
{ |
||
269 |
const MailMessage *const mail2 = (*it).second; |
||
270 |
mail->time = mail2->time; |
||
271 |
mail->strTime = mail2->strTime; |
||
272 |
} |
||
273 |
delete mailViewWindow; |
||
274 |
CREATEWIDGETV(mailViewWindow, MailViewWindow, mail, |
||
275 |
itemsCount); |
||
276 |
} |
||
277 |
|||
278 |
void MailWindow::viewNext(const int64_t id) |
||
279 |
{ |
||
280 |
FOR_EACH (STD_VECTOR<MailMessage*>::iterator, it, mMessages) |
||
281 |
{ |
||
282 |
MailMessage *message = *it; |
||
283 |
if (message != nullptr && |
||
284 |
message->id == id) |
||
285 |
{ |
||
286 |
++ it; |
||
287 |
if (it == mMessages.end()) |
||
288 |
{ |
||
289 |
it = mMessages.begin(); |
||
290 |
mListBox->setSelected(0); |
||
291 |
} |
||
292 |
else |
||
293 |
{ |
||
294 |
mListBox->setSelected(mListBox->getSelected() + 1); |
||
295 |
} |
||
296 |
message = *it; |
||
297 |
if (mUseMail2) |
||
298 |
mail2Handler->readMail(mOpenType, message->id); |
||
299 |
else |
||
300 |
mailHandler->readMessage(CAST_S32(message->id)); |
||
301 |
return; |
||
302 |
} |
||
303 |
} |
||
304 |
} |
||
305 |
|||
306 |
void MailWindow::viewPrev(const int64_t id) |
||
307 |
{ |
||
308 |
FOR_EACH (STD_VECTOR<MailMessage*>::iterator, it, mMessages) |
||
309 |
{ |
||
310 |
MailMessage *message = *it; |
||
311 |
if (message != nullptr && |
||
312 |
message->id == id) |
||
313 |
{ |
||
314 |
if (it == mMessages.begin()) |
||
315 |
{ |
||
316 |
it = mMessages.end(); |
||
317 |
mListBox->setSelected(CAST_S32(mMessages.size()) - 1); |
||
318 |
} |
||
319 |
else |
||
320 |
{ |
||
321 |
mListBox->setSelected(mListBox->getSelected() - 1); |
||
322 |
} |
||
323 |
-- it; |
||
324 |
message = *it; |
||
325 |
if (mUseMail2) |
||
326 |
mail2Handler->readMail(mOpenType, message->id); |
||
327 |
else |
||
328 |
mailHandler->readMessage(CAST_S32(message->id)); |
||
329 |
return; |
||
330 |
} |
||
331 |
} |
||
332 |
} |
||
333 |
|||
334 |
void MailWindow::mouseClicked(MouseEvent &event) |
||
335 |
{ |
||
336 |
if (event.getButton() == MouseButton::LEFT) |
||
337 |
{ |
||
338 |
event.consume(); |
||
339 |
if (event.getClickCount() == 2 && |
||
340 |
event.getSource() == mListBox) |
||
341 |
{ |
||
342 |
action(ActionEvent(mListBox, "open")); |
||
343 |
} |
||
344 |
} |
||
345 |
} |
||
346 |
|||
347 |
void MailWindow::postConnection() |
||
348 |
{ |
||
349 |
refreshMails(); |
||
350 |
} |
||
351 |
|||
352 |
void MailWindow::refreshMails() |
||
353 |
{ |
||
354 |
if (mUseMail2) |
||
355 |
{ |
||
356 |
clear(); |
||
357 |
mail2Handler->refreshMailList(MailOpenType::Mail, 0); |
||
358 |
mLastPage = false; |
||
359 |
mReturnButton->setEnabled(true); |
||
360 |
} |
||
361 |
else |
||
362 |
{ |
||
363 |
mailHandler->refresh(); |
||
364 |
} |
||
365 |
} |
||
366 |
|||
367 |
void MailWindow::createMail(const std::string &to) |
||
368 |
{ |
||
369 |
if (mailEditWindow != nullptr) |
||
370 |
return; |
||
371 |
|||
372 |
CREATEWIDGETV0(mailEditWindow, MailEditWindow); |
||
373 |
mailEditWindow->setTo(to); |
||
374 |
} |
||
375 |
|||
376 |
MailMessage *MailWindow::findMail(const int64_t id) |
||
377 |
{ |
||
378 |
std::map<int64_t, MailMessage*>::iterator it = mMessagesMap.find(id); |
||
379 |
if (it != mMessagesMap.end()) |
||
380 |
return (*it).second; |
||
381 |
return nullptr; |
||
382 |
} |
||
383 |
|||
384 |
void MailWindow::setLastPage() |
||
385 |
{ |
||
386 |
mLastPage = true; |
||
387 |
mReturnButton->setEnabled(false); |
||
388 |
} |
||
389 |
|||
390 |
void MailWindow::refreshMailNames() |
||
391 |
{ |
||
392 |
mMailModel->clear(); |
||
393 |
FOR_EACH (STD_VECTOR<MailMessage*>::iterator, it, mMessages) |
||
394 |
{ |
||
395 |
MailMessage *message = *it; |
||
396 |
if (message != nullptr) |
||
397 |
mMailModel->add(getMailHeader(message)); |
||
398 |
} |
||
399 |
✓✗✓✗ |
3 |
} |
Generated by: GCOVR (Version 3.3) |