ManaPlus
mailviewwindow.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 
23 
24 #include "settings.h"
25 
26 #include "net/mail2handler.h"
27 #include "net/mailhandler.h"
28 
29 #include "gui/mailmessage.h"
30 
32 #include "gui/windows/mailwindow.h"
33 
34 #include "gui/widgets/button.h"
38 #include "gui/widgets/label.h"
39 #include "gui/widgets/scrollarea.h"
40 
41 #include "utils/delete2.h"
42 #include "utils/gettext.h"
43 #include "utils/stringutils.h"
44 
46 #include "debug.h"
47 
49 
51  const int itemsCount) :
52  // TRANSLATORS: mail view window name
53  Window(_("View mail"), Modal_false, nullptr, "mailview.xml"),
55  mMessage(message),
56  mGetAttachButton(new Button(this,
57  // TRANSLATORS: mail view attach / items button
58  settings.enableNewMailSystem ? _("Get items") : _("Get attach"),
59  "attach",
61  this)),
62  mGetMoneyButton(nullptr),
63  // TRANSLATORS: mail view window button
64  mCloseButton(new Button(this, _("Close"), "close", BUTTON_SKIN, this)),
65  mPrevButton(new Button(this, "<", "prev", BUTTON_SKIN, this)),
66  mNextButton(new Button(this, ">", "next", BUTTON_SKIN, this)),
67  // TRANSLATORS: mail view window button
68  mReplyButton(new Button(this, _("Reply"), "reply", BUTTON_SKIN, this)),
69  // TRANSLATORS: mail view window label
70  mTimeLabel(new Label(this, strprintf("%s %s", _("Time:"),
71  message->strTime.c_str()))),
72  mMoneyLabel(nullptr),
73  // TRANSLATORS: mail view window label
74  mFromLabel(new Label(this, strprintf("%s %s", _("From:"),
75  message->sender.c_str()))),
76  // TRANSLATORS: mail view window label
77  mSubjectLabel(new Label(this, strprintf("%s %s", _("Subject:"),
78  message->title.c_str()))),
79  // TRANSLATORS: mail view window label
80  mMessageLabel(new Label(this, strprintf("%s %s", _("Message:"),
81  message->text.c_str()))),
83  mItemContainer(new ItemContainer(this, mInventory, 100000,
85  mItemScrollArea(new ScrollArea(this, mItemContainer,
86  fromBool(getOptionBool("showitemsbackground", false), Opaque),
87  "mailview_listbackground.xml")),
88  mUseMail2(settings.enableNewMailSystem)
89 {
90  setWindowName("MailView");
91  setCloseButton(true);
92  setResizable(true);
93  setSaveVisible(false);
94  setStickyButtonLock(true);
96 
97  setDefaultSize(380, 370, ImagePosition::CENTER, 0, 0);
98  setMinWidth(200);
99  setMinHeight(100);
100  center();
102 
104 
105  ContainerPlacer placer(nullptr, nullptr);
106  placer = getPlacer(0, 0);
107 
108  int n = 0;
109  placer(0, n++, mTimeLabel, 1, 1);
110  placer(0, n++, mFromLabel, 1, 1);
111  placer(0, n++, mSubjectLabel, 1, 1);
112  if (message->money != 0)
113  {
114  mMoneyLabel = new Label(this, strprintf("%s %u",
115  // TRANSLATORS: mail view window label
116  _("Money:"),
117  CAST_U32(message->money)));
118  placer(0, n++, mMoneyLabel, 1, 1);
119  }
120  placer(0, n++, mMessageLabel, 1, 1);
121  placer(0, n++, mItemScrollArea, 1, 1);
122 
123  if (mUseMail2 && message->money != 0)
124  {
125  mGetMoneyButton = new Button(this,
126  // TRANSLATORS: mail view attached money button
127  _("Get money"),
128  "money",
129  BUTTON_SKIN,
130  this);
131  placer(0, n++, mGetMoneyButton, 1, 1);
132  }
133  placer(0, n++, mGetAttachButton, 1, 1);
135 
136  ContainerPlacer placer2(nullptr, nullptr);
137  placer2 = getPlacer(0, n);
138 
139  placer2(0, 0, mPrevButton, 1, 1);
140  placer2(1, 0, mNextButton, 1, 1);
141  placer2(3, 0, mReplyButton, 1, 1);
142  placer2(4, 0, mCloseButton, 1, 1);
143 
144  loadWindowState();
145  enableVisibleSound(true);
146 }
147 
149 {
150  if (mUseMail2)
151  {
152  mMessage = nullptr;
153  }
154  else
155  {
158  }
160  mailViewWindow = nullptr;
161 }
162 
164 {
165  const std::string &eventId = event.getId();
166  if (eventId == "close")
167  {
168  scheduleDelete();
169  }
170  else if (eventId == "attach")
171  {
172  if (mUseMail2)
173  {
175  mMessage->id);
176  }
177  else
178  {
180  }
181  }
182  else if (eventId == "money")
183  {
184  if (mUseMail2)
185  {
187  mMessage->id);
188  }
189  }
190  else if (eventId == "next")
191  {
192  if (mMessage != nullptr)
194  }
195  else if (eventId == "prev")
196  {
197  if (mMessage != nullptr)
199  }
200  else if (eventId == "reply")
201  {
202  if (mMessage == nullptr)
203  return;
204  if (mailEditWindow != nullptr)
211  mMessage->sender,
212  std::string(),
213  std::string(),
214  0);
215  scheduleDelete();
216  }
217 }
218 
220 {
221  return mInventory;
222 }
223 
225 {
226  if ((mMessage->money != 0 && !mUseMail2) ||
227  mInventory->getLastUsedSlot() != -1)
228  {
230  }
231  else
232  {
234  }
235 }
236 
238 {
241 }
242 
243 void MailViewWindow::removeItems(const int64_t mailId)
244 {
245  if (mailId != mMessage->id)
246  return;
247  mInventory->clear();
248  mMessage->type = static_cast<MailMessageType::Type>(
250  mMessage->type = static_cast<MailMessageType::Type>(
253  if (mailWindow != nullptr)
255 }
256 
257 void MailViewWindow::removeMoney(const int64_t mailId)
258 {
259  if (mailId != mMessage->id)
260  return;
261  mMessage->type = static_cast<MailMessageType::Type>(
263  mMessage->type = static_cast<MailMessageType::Type>(
265 
266  mMessage->money = 0;
267 
268  if (mMoneyLabel == nullptr)
269  return;
270 
271  if (mGetMoneyButton != nullptr)
273 
275  // TRANSLATORS: mail view window label
276  _("Money:"),
277  0));
278  if (mailWindow != nullptr)
280 }
#define fromBool(val, name)
Definition: booldefines.h:49
const std::string BUTTON_SKIN
Definition: button.h:89
#define CAST_S32
Definition: cast.h:30
#define CAST_U32
Definition: cast.h:31
Definition: button.h:102
void clear()
Definition: inventory.cpp:238
int getLastUsedSlot() const
Definition: inventory.cpp:291
Definition: label.h:91
void setCaption(const std::string &caption)
Definition: label.cpp:264
void setMessage(const std::string &str)
void setSubject(const std::string &str)
void setTo(const std::string &str)
ItemContainer * mItemContainer
Label * mMessageLabel
Button * mGetMoneyButton
Label * mMoneyLabel
MailMessage * mMessage
Button * mCloseButton
Button * mGetAttachButton
void removeItems(const int64_t mailId)
Label * mFromLabel
void removeMoney(const int64_t mailId)
Inventory * mInventory
ScrollArea * mItemScrollArea
Button * mNextButton
void action(const ActionEvent &event)
Button * mPrevButton
MailViewWindow(MailMessage *const message, const int itemsCount)
Label * mSubjectLabel
Label * mTimeLabel
Button * mReplyButton
Inventory * getInventory() const
void refreshMailNames()
Definition: mailwindow.cpp:390
void viewPrev(const int64_t id)
Definition: mailwindow.cpp:306
void viewNext(const int64_t id)
Definition: mailwindow.cpp:278
MailOpenTypeT getOpenType() const
Definition: mailwindow.h:79
virtual void requestItems(const MailOpenTypeT openType, const int64_t mailId) const =0
virtual void requestMoney(const MailOpenTypeT openType, const int64_t mailId) const =0
virtual void queueCheckName(const MailQueueTypeT type, const std::string &to, const std::string &title, const std::string &body, const int64_t &money) const =0
virtual void getAttach(const int msgId) const =0
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
void setHeight(int height)
void setVisible(Visible visible)
Definition: widget.cpp:225
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
virtual void setVisible(Visible visible)
Definition: window.cpp:778
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
virtual void scheduleDelete()
Definition: window.cpp:831
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:772
void setDefaultSize()
Definition: window.cpp:1198
void loadWindowState()
Definition: window.cpp:1087
#define CREATEWIDGETV0(var, type)
Definition: createwidget.h:32
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
const bool ForceQuantity_false
Definition: forcequantity.h:30
#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
Inventory * mInventory
Definition: playerinfo.cpp:59
bool Opaque
Definition: opaque.h:30
Settings settings
Definition: settings.cpp:32
const bool ShowEmptyRows_false
Definition: showemptyrows.h:30
std::string strprintf(const char *const format,...)
int64_t money
Definition: mailmessage.h:57
std::string sender
Definition: mailmessage.h:51
int64_t id
Definition: mailmessage.h:54
MailMessageType::Type type
Definition: mailmessage.h:58
std::string title
Definition: mailmessage.h:50
std::string text
Definition: mailmessage.h:53
const bool Visible_false
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30