ManaPlus
mailwindow.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 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 
31 
32 #include "gui/mailmessage.h"
33 
37 
38 #include "gui/widgets/button.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 
54 
56  // TRANSLATORS: mail window name
57  Window(_("Mail"), Modal_false, nullptr, "mail.xml"),
59  mMessages(),
60  mMessagesMap(),
61  mMailModel(new ExtendedNamesModel),
63  this, mMailModel, "extendedlistbox.xml", 0)),
64  mListScrollArea(new ScrollArea(this, mListBox,
65  fromBool(getOptionBool("showlistbackground", false), Opaque),
66  "mail_listbackground.xml")),
67  // TRANSLATORS: mail window button
68  mRefreshButton(new Button(this, _("Refresh"), "refresh",
69  BUTTON_SKIN, this)),
70  // TRANSLATORS: mail window button
71  mNewButton(new Button(this, _("New"), "new", BUTTON_SKIN, this)),
72  // TRANSLATORS: mail window button
73  mDeleteButton(new Button(this, _("Delete"), "delete", BUTTON_SKIN, this)),
74  mReturnButton(new Button(this,
75  // TRANSLATORS: mail window button
76  settings.enableNewMailSystem ? _("Get old") : _("Return"),
77  "return", BUTTON_SKIN, this)),
78  // TRANSLATORS: mail window button
79  mOpenButton(new Button(this, _("Open"), "open", BUTTON_SKIN, this)),
80  mOpenType(MailOpenType::Mail),
81  mUseMail2(settings.enableNewMailSystem),
82  mLastPage(false)
83 {
84  setWindowName("Mail");
85  setCloseButton(true);
86  setResizable(true);
87  setCloseButton(true);
88  setSaveVisible(true);
89  setStickyButtonLock(true);
90 
91  if (setupWindow != nullptr)
93 
94  setDefaultSize(310, 180, ImagePosition::CENTER, 0, 0);
95  setMinWidth(310);
96  setMinHeight(250);
97  center();
98 
100 
101  ContainerPlacer placer(nullptr, nullptr);
102  placer = getPlacer(0, 0);
103 
104  placer(0, 0, mListScrollArea, 4, 5).setPadding(3);
105  placer(4, 0, mRefreshButton, 1, 1);
106  placer(4, 1, mOpenButton, 1, 1);
107  placer(4, 2, mNewButton, 1, 1);
108  placer(4, 3, mDeleteButton, 1, 1);
109  placer(4, 4, mReturnButton, 1, 1);
110 
111  Layout &layout = getLayout();
112  layout.setRowHeight(0, LayoutType::SET);
113 
114  loadWindowState();
115  enableVisibleSound(true);
116 }
117 
119 {
124 }
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  {
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)
148  else
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)
159  else
161  }
162  else if (eventId == "return")
163  {
164  if (mUseMail2)
165  {
166  const size_t idx = mMessages.size();
167  if (idx == 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];
179  }
180  }
181 }
182 
184 {
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 
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;
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  {
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  {
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 
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 
347 {
348  refreshMails();
349 }
350 
352 {
353  if (mUseMail2)
354  {
355  clear();
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 
372  mailEditWindow->setTo(to);
373 }
374 
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 
384 {
385  mLastPage = true;
386  mReturnButton->setEnabled(false);
387 }
388 
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 }
MailWindow::mOpenType
MailOpenTypeT mOpenType
Definition: mailwindow.h:101
MailWindow::postConnection
void postConnection()
Definition: mailwindow.cpp:346
LayoutCell::setRowHeight
void setRowHeight(const int n, const int h)
Definition: layoutcell.cpp:127
ListBox::getSelected
int getSelected() const
Definition: listbox.h:167
Window::getPlacer
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1390
Net::MailHandler::returnMessage
virtual void returnMessage(const int msgId) const =0
Window::getLayout
Layout & getLayout()
Definition: window.cpp:1364
MailWindow::mMailModel
ExtendedNamesModel * mMailModel
Definition: mailwindow.h:93
MailMessage::time
int time
Definition: mailmessage.h:54
MailWindow::mLastPage
bool mLastPage
Definition: mailwindow.h:103
mailhandler.h
MailWindow::addMail
void addMail(MailMessage *const message)
Definition: mailwindow.cpp:223
settings.h
MailMessage::read
bool read
Definition: mailmessage.h:58
Window::setMinHeight
void setMinHeight(const int height)
Definition: window.cpp:603
MailWindow::getMailHeader
std::string getMailHeader(const MailMessage *const message) const
Definition: mailwindow.cpp:192
ActionEvent
Definition: actionevent.h:96
Window::center
void center()
Definition: window.cpp:1416
Window::setWindowName
void setWindowName(const std::string &name)
Definition: window.h:354
maileditwindow.h
Layout
Definition: layout.h:43
MailWindow::showMessage
void showMessage(MailMessage *const mail, const int itemsCount)
Definition: mailwindow.cpp:259
Net::MailHandler::deleteMessage
virtual void deleteMessage(const int msgId) const =0
Window
Definition: window.h:98
MailMessageType::Money
@ Money
Definition: mailmessagetype.h:33
BUTTON_SKIN
const std::string BUTTON_SKIN
Definition: button.h:88
Button
Definition: button.h:96
button.h
ExtendedListBox
Definition: extendedlistbox.h:27
mailWindow
MailWindow * mailWindow
Definition: mailwindow.cpp:53
Widget::setEnabled
void setEnabled(const bool enabled)
Definition: widget.h:351
settings
Settings settings
Definition: settings.cpp:31
MailWindow::mMessages
std::vector< MailMessage * > mMessages
Definition: mailwindow.h:91
MailWindow::mDeleteButton
Button * mDeleteButton
Definition: mailwindow.h:98
anonymous_namespace{deaddb.cpp}::mMessages
std::vector< std::string > mMessages
Definition: deaddb.cpp:35
delete2.h
containerplacer.h
MailWindow::viewPrev
void viewPrev(const int64_t id)
Definition: mailwindow.cpp:305
MailWindow::refreshMailNames
void refreshMailNames()
Definition: mailwindow.cpp:389
CREATEWIDGETV0
#define CREATEWIDGETV0(var, type)
Definition: createwidget.h:31
ImagePosition::CENTER
@ CENTER
Definition: imageposition.h:53
SetupWindow::registerWindowForReset
void registerWindowForReset(Window *const window)
Definition: setupwindow.cpp:272
Window::setStickyButtonLock
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:771
MailWindow::mouseClicked
void mouseClicked(MouseEvent &event)
Definition: mailwindow.cpp:333
fromBool
#define fromBool(val, name)
Definition: booldefines.h:48
extendedlistbox.h
ExtendedNamesModel::add
void add(const std::string &str)
Definition: extendednamesmodel.h:52
MailWindow::viewNext
void viewNext(const int64_t id)
Definition: mailwindow.cpp:277
mail2Handler
Net::Mail2Handler * mail2Handler
Definition: net.cpp:112
MailWindow::removeMail
void removeMail(const int64_t id)
Definition: mailwindow.cpp:232
mailViewWindow
MailViewWindow * mailViewWindow
Definition: mailviewwindow.cpp:47
mailviewwindow.h
delete2
#define delete2(var)
Definition: delete2.h:24
bool
Window::setCloseButton
void setCloseButton(const bool flag)
Definition: window.cpp:748
Modal_false
const bool Modal_false
Definition: modal.h:29
MailMessage::type
MailMessageType::Type type
Definition: mailmessage.h:57
ContainerPlacer
Definition: containerplacer.h:35
ScrollArea
Definition: scrollarea.h:90
MailWindow::mRefreshButton
Button * mRefreshButton
Definition: mailwindow.h:96
mailmessage.h
Window::enableVisibleSound
void enableVisibleSound(bool b)
Definition: window.h:480
ActionListener
Definition: actionlistener.h:77
Event::getSource
Widget * getSource() const
Definition: event.h:103
Window::setDefaultSize
void setDefaultSize()
Definition: window.cpp:1197
nullptr
#define nullptr
Definition: localconsts.h:44
MailViewWindow
Definition: mailviewwindow.h:36
MouseEvent::getClickCount
int getClickCount() const
Definition: mouseevent.h:146
mail2handler.h
MailWindow::refreshMails
void refreshMails()
Definition: mailwindow.cpp:351
extendednamesmodel.h
mailEditWindow
MailEditWindow * mailEditWindow
Definition: maileditwindow.cpp:49
strprintf
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
MailMessage::title
std::string title
Definition: mailmessage.h:49
MailMessage::strTime
std::string strTime
Definition: mailmessage.h:51
MailWindow::mReturnButton
Button * mReturnButton
Definition: mailwindow.h:99
createwidget.h
gettext.h
MailWindow::createMail
static void createMail(const std::string &to)
Definition: mailwindow.cpp:366
CREATEWIDGETV
#define CREATEWIDGETV(var, type,...)
Definition: createwidget.h:24
MailWindow::~MailWindow
~MailWindow()
Definition: mailwindow.cpp:118
CREATEWIDGETR
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:35
MailMessage::expired
bool expired
Definition: mailmessage.h:59
MailWindow::mListScrollArea
ScrollArea * mListScrollArea
Definition: mailwindow.h:95
MailEditWindow::setTo
void setTo(const std::string &str)
Definition: maileditwindow.cpp:187
MailWindow::mUseMail2
bool mUseMail2
Definition: mailwindow.h:102
ScrollArea::setHorizontalScrollPolicy
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
Definition: scrollarea.cpp:1214
MailMessageType::Type
Type
Definition: mailmessagetype.h:30
Window::loadWindowState
void loadWindowState()
Definition: window.cpp:1086
stringutils.h
MailWindow::findMail
MailMessage * findMail(const int64_t id)
Definition: mailwindow.cpp:375
MailOpenType::Mail
@ Mail
Definition: mailopentype.h:28
Window::setResizable
void setResizable(const bool resize)
Definition: window.cpp:626
setupwindow.h
mailHandler
Net::MailHandler * mailHandler
Definition: net.cpp:113
Net::Mail2Handler::nextPage
virtual void nextPage(const MailOpenTypeT openType, const int64_t mailId) const =0
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
foreach.h
MailOpenType
Definition: mailopentype.h:26
MailEditWindow
Definition: maileditwindow.h:38
layout.h
MailMessage::id
int64_t id
Definition: mailmessage.h:53
Net::Mail2Handler::readMail
virtual void readMail(const MailOpenTypeT openType, const int64_t mailId) const =0
MailWindow::setLastPage
void setLastPage()
Definition: mailwindow.cpp:383
setupWindow
SetupWindow * setupWindow
Definition: setupwindow.cpp:63
LayoutType::SET
@ SET
Definition: layouttype.h:37
mailwindow.h
ScrollArea::SHOW_NEVER
@ SHOW_NEVER
Definition: scrollarea.h:107
Net::MailHandler::readMessage
virtual void readMessage(const int msgId) const =0
MailWindow::mMessagesMap
std::map< int64_t, MailMessage * > mMessagesMap
Definition: mailwindow.h:92
Net::Mail2Handler::refreshMailList
virtual void refreshMailList(const MailOpenTypeT openType, const int64_t mailId) const =0
CAST_S32
#define CAST_S32
Definition: cast.h:29
Window::setSaveVisible
void setSaveVisible(const bool save)
Definition: window.h:299
new
#define new
Definition: debug_new.h:147
scrollarea.h
MailWindow::action
void action(const ActionEvent &event)
Definition: mailwindow.cpp:126
dtor.h
MailWindow::MailWindow
MailWindow()
Definition: mailwindow.cpp:55
layouttype.h
_
#define _(s)
Definition: gettext.h:34
MailWindow
Definition: mailwindow.h:42
MouseEvent::getButton
MouseButtonT getButton() const
Definition: mouseevent.h:115
MouseEvent
Definition: mouseevent.h:79
MailWindow::mListBox
ExtendedListBox * mListBox
Definition: mailwindow.h:94
ExtendedNamesModel::clear
void clear()
Definition: extendednamesmodel.cpp:59
ListBox::setSelected
void setSelected(const int selected)
Definition: listbox.cpp:398
MailMessageType::Item
@ Item
Definition: mailmessagetype.h:34
Net::MailHandler::refresh
virtual void refresh() const =0
MailWindow::mOpenButton
Button * mOpenButton
Definition: mailwindow.h:100
FOR_EACH
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
debug.h
MailWindow::mNewButton
Button * mNewButton
Definition: mailwindow.h:97
MailMessage
Definition: mailmessage.h:30
Window::setMinWidth
void setMinWidth(const int width)
Definition: window.cpp:590
Net::Mail2Handler::deleteMail
virtual void deleteMail(const MailOpenTypeT openType, const int64_t mailId) const =0
MailWindow::clear
void clear()
Definition: mailwindow.cpp:183
MouseButton::LEFT
@ LEFT
Definition: mousebutton.h:73
ExtendedNamesModel
Definition: extendednamesmodel.h:28