ManaPlus
mailwindow.cpp
Go to the documentation of this file.
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 
32 
33 #include "gui/mailmessage.h"
34 
38 
39 #include "gui/widgets/button.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 
55 
57  // TRANSLATORS: mail window name
58  Window(_("Mail"), Modal_false, nullptr, "mail.xml"),
60  mMessages(),
61  mMessagesMap(),
62  mMailModel(new ExtendedNamesModel),
64  this, mMailModel, "extendedlistbox.xml", 0)),
65  mListScrollArea(new ScrollArea(this, mListBox,
66  fromBool(getOptionBool("showlistbackground", false), Opaque),
67  "mail_listbackground.xml")),
68  // TRANSLATORS: mail window button
69  mRefreshButton(new Button(this, _("Refresh"), "refresh",
70  BUTTON_SKIN, this)),
71  // TRANSLATORS: mail window button
72  mNewButton(new Button(this, _("New"), "new", BUTTON_SKIN, this)),
73  // TRANSLATORS: mail window button
74  mDeleteButton(new Button(this, _("Delete"), "delete", BUTTON_SKIN, this)),
75  mReturnButton(new Button(this,
76  // TRANSLATORS: mail window button
77  settings.enableNewMailSystem ? _("Get old") : _("Return"),
78  "return", BUTTON_SKIN, this)),
79  // TRANSLATORS: mail window button
80  mOpenButton(new Button(this, _("Open"), "open", BUTTON_SKIN, this)),
81  mOpenType(MailOpenType::Mail),
82  mUseMail2(settings.enableNewMailSystem),
83  mLastPage(false)
84 {
85  setWindowName("Mail");
86  setCloseButton(true);
87  setResizable(true);
88  setCloseButton(true);
89  setSaveVisible(true);
90  setStickyButtonLock(true);
91 
92  if (setupWindow != nullptr)
94 
95  setDefaultSize(310, 180, ImagePosition::CENTER, 0, 0);
96  setMinWidth(310);
97  setMinHeight(250);
98  center();
99 
101 
102  ContainerPlacer placer(nullptr, nullptr);
103  placer = getPlacer(0, 0);
104 
105  placer(0, 0, mListScrollArea, 4, 5).setPadding(3);
106  placer(4, 0, mRefreshButton, 1, 1);
107  placer(4, 1, mOpenButton, 1, 1);
108  placer(4, 2, mNewButton, 1, 1);
109  placer(4, 3, mDeleteButton, 1, 1);
110  placer(4, 4, mReturnButton, 1, 1);
111 
112  Layout &layout = getLayout();
113  layout.setRowHeight(0, LayoutType::SET);
114 
115  loadWindowState();
116  enableVisibleSound(true);
117 }
118 
120 {
125 }
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  {
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)
149  else
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)
160  else
162  }
163  else if (eventId == "return")
164  {
165  if (mUseMail2)
166  {
167  const size_t idx = mMessages.size();
168  if (idx == 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];
180  }
181  }
182 }
183 
185 {
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 
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;
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  {
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  {
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 
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 
348 {
349  refreshMails();
350 }
351 
353 {
354  if (mUseMail2)
355  {
356  clear();
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 
373  mailEditWindow->setTo(to);
374 }
375 
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 
385 {
386  mLastPage = true;
387  mReturnButton->setEnabled(false);
388 }
389 
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 }
#define fromBool(val, name)
Definition: booldefines.h:49
const std::string BUTTON_SKIN
Definition: button.h:89
#define CAST_S32
Definition: cast.h:30
Definition: button.h:102
Widget * getSource() const
Definition: event.h:104
void add(const std::string &str)
void setRowHeight(const int n, const int h)
Definition: layoutcell.cpp:128
Definition: layout.h:45
int getSelected() const
Definition: listbox.h:168
void setSelected(const int selected)
Definition: listbox.cpp:399
void setTo(const std::string &str)
Button * mRefreshButton
Definition: mailwindow.h:97
void refreshMailNames()
Definition: mailwindow.cpp:390
std::vector< MailMessage * > mMessages
Definition: mailwindow.h:92
bool mLastPage
Definition: mailwindow.h:104
std::map< int64_t, MailMessage * > mMessagesMap
Definition: mailwindow.h:93
Button * mReturnButton
Definition: mailwindow.h:100
ExtendedNamesModel * mMailModel
Definition: mailwindow.h:94
void refreshMails()
Definition: mailwindow.cpp:352
void mouseClicked(MouseEvent &event)
Definition: mailwindow.cpp:334
void clear()
Definition: mailwindow.cpp:184
void postConnection()
Definition: mailwindow.cpp:347
bool mUseMail2
Definition: mailwindow.h:103
void removeMail(const int64_t id)
Definition: mailwindow.cpp:233
ScrollArea * mListScrollArea
Definition: mailwindow.h:96
void addMail(MailMessage *const message)
Definition: mailwindow.cpp:224
ExtendedListBox * mListBox
Definition: mailwindow.h:95
void viewPrev(const int64_t id)
Definition: mailwindow.cpp:306
std::string getMailHeader(const MailMessage *const message) const
Definition: mailwindow.cpp:193
void action(const ActionEvent &event)
Definition: mailwindow.cpp:127
Button * mOpenButton
Definition: mailwindow.h:101
MailMessage * findMail(const int64_t id)
Definition: mailwindow.cpp:376
void showMessage(MailMessage *const mail, const int itemsCount)
Definition: mailwindow.cpp:260
MailOpenTypeT mOpenType
Definition: mailwindow.h:102
Button * mNewButton
Definition: mailwindow.h:98
Button * mDeleteButton
Definition: mailwindow.h:99
void viewNext(const int64_t id)
Definition: mailwindow.cpp:278
static void createMail(const std::string &to)
Definition: mailwindow.cpp:367
void setLastPage()
Definition: mailwindow.cpp:384
MouseButtonT getButton() const
Definition: mouseevent.h:116
int getClickCount() const
Definition: mouseevent.h:147
virtual void refreshMailList(const MailOpenTypeT openType, const int64_t mailId) const =0
virtual void deleteMail(const MailOpenTypeT openType, const int64_t mailId) const =0
virtual void readMail(const MailOpenTypeT openType, const int64_t mailId) const =0
virtual void nextPage(const MailOpenTypeT openType, const int64_t mailId) const =0
virtual void refresh() const =0
virtual void readMessage(const int msgId) const =0
virtual void returnMessage(const int msgId) const =0
virtual void deleteMessage(const int msgId) const =0
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
void registerWindowForReset(Window *const window)
void setEnabled(const bool enabled)
Definition: widget.h:352
Definition: window.h:102
void center()
Definition: window.cpp:1417
void setSaveVisible(const bool save)
Definition: window.h:300
void setResizable(const bool resize)
Definition: window.cpp:627
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1391
Layout & getLayout()
Definition: window.cpp:1365
void setWindowName(const std::string &name)
Definition: window.h:355
void setMinHeight(const int height)
Definition: window.cpp:604
void setMinWidth(const int width)
Definition: window.cpp:591
void enableVisibleSound(bool b)
Definition: window.h:481
void setCloseButton(const bool flag)
Definition: window.cpp:749
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:772
void setDefaultSize()
Definition: window.cpp:1198
void loadWindowState()
Definition: window.cpp:1087
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
#define CREATEWIDGETV0(var, type)
Definition: createwidget.h:32
#define CREATEWIDGETV(var, type,...)
Definition: createwidget.h:25
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
void delete_all(Container &c)
Definition: dtor.h:56
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define _(s)
Definition: gettext.h:35
#define nullptr
Definition: localconsts.h:45
Net::Mail2Handler * mail2Handler
Definition: net.cpp:113
MailEditWindow * mailEditWindow
Net::MailHandler * mailHandler
Definition: net.cpp:114
MailViewWindow * mailViewWindow
MailWindow * mailWindow
Definition: mailwindow.cpp:54
const bool Modal_false
Definition: modal.h:30
std::vector< std::string > mMessages
Definition: deaddb.cpp:36
bool Opaque
Definition: opaque.h:30
Settings settings
Definition: settings.cpp:32
SetupWindow * setupWindow
Definition: setupwindow.cpp:64
std::string strprintf(const char *const format,...)
int64_t id
Definition: mailmessage.h:54
MailMessageType::Type type
Definition: mailmessage.h:58
std::string strTime
Definition: mailmessage.h:52
std::string title
Definition: mailmessage.h:50
bool expired
Definition: mailmessage.h:60