ManaPlus
maileditwindow.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 "being/playerinfo.h"
27 
30 
31 #include "gui/widgets/button.h"
35 #include "gui/widgets/label.h"
36 #include "gui/widgets/scrollarea.h"
37 
38 #include "resources/item/item.h"
39 
40 #include "net/mail2handler.h"
41 #include "net/mailhandler.h"
42 
43 #include "utils/delete2.h"
44 #include "utils/gettext.h"
45 
46 #include <climits>
47 
48 #include "debug.h"
49 
51 
53  // TRANSLATORS: mail edit window name
54  Window(_("Edit mail"), Modal_false, nullptr, "mailedit.xml"),
56  FocusListener(),
57  // TRANSLATORS: mail edit window button
58  mSendButton(new Button(this, _("Send"), "send", BUTTON_SKIN, this)),
59  // TRANSLATORS: mail edit window button
60  mCloseButton(new Button(this, _("Close"), "close", BUTTON_SKIN, this)),
61  // TRANSLATORS: mail edit window button
62  mAddButton(new Button(this, _("Add"), "add", BUTTON_SKIN, this)),
63  // TRANSLATORS: mail edit window label
64  mToLabel(new Label(this, _("To:"))),
65  // TRANSLATORS: mail edit window label
66  mSubjectLabel(new Label(this, _("Subject:"))),
67  // TRANSLATORS: mail edit window label
68  mMoneyLabel(new Label(this, _("Money:"))),
69  // TRANSLATORS: mail edit window label
70  mItemLabel(new Label(this, _("Item:"))),
71  // TRANSLATORS: mail edit window label
72  mMessageLabel(new Label(this, _("Message:"))),
73  mToField(new TextField(this, std::string(), LoseFocusOnTab_true,
74  nullptr, std::string(), false)),
75  mSubjectField(new TextField(this, std::string(), LoseFocusOnTab_true,
76  nullptr, std::string(), false)),
77  mMoneyField(new IntTextField(this, 0, 0,
78  settings.enableNewMailSystem ? INT_MAX : 10000000, Enable_true, 0)),
79  mMessageField(new TextField(this, std::string(), LoseFocusOnTab_true,
80  nullptr, std::string(), false)),
82  settings.enableNewMailSystem ? -1 : 1)),
83  mItemContainer(new ItemContainer(this, mInventory, 100000,
85  mItemScrollArea(new ScrollArea(this, mItemContainer,
86  fromBool(getOptionBool("showitemsbackground", false), Opaque),
87  "mailedit_listbackground.xml")),
88  mUseMail2(settings.enableNewMailSystem)
89 {
90  setWindowName("MailEdit");
91  setCloseButton(true);
92  setResizable(true);
93  setCloseButton(true);
94  setSaveVisible(false);
95  setStickyButtonLock(true);
97 
98  setDefaultSize(380, 200, ImagePosition::CENTER, 0, 0);
99  setMinWidth(200);
100  setMinHeight(200);
101  center();
102 
103  ContainerPlacer placer(nullptr, nullptr);
104  placer = getPlacer(0, 0);
105 
106  mToField->setWidth(100);
107  mSubjectField->setWidth(100);
108  mMessageField->setWidth(100);
111  mToField->addFocusListener(this);
112 
113  placer(0, 0, mToLabel, 1, 1);
114  placer(1, 0, mToField, 3, 1);
115  placer(0, 1, mSubjectLabel, 1, 1);
116  placer(1, 1, mSubjectField, 3, 1);
117  placer(0, 2, mMoneyLabel, 1, 1);
118  placer(1, 2, mMoneyField, 3, 1);
119  placer(0, 3, mItemLabel, 1, 1);
120  placer(1, 3, mItemScrollArea, 2, 2);
121  placer(3, 4, mAddButton, 1, 1);
122 
123  placer(0, 5, mMessageLabel, 1, 1);
124  placer(1, 5, mMessageField, 3, 1);
125  placer(0, 6, mSendButton, 1, 1);
126  placer(3, 6, mCloseButton, 1, 1);
127 
128  loadWindowState();
129  if (mUseMail2)
130  mSendButton->setEnabled(false);
131  enableVisibleSound(true);
132 }
133 
135 {
136  mailEditWindow = nullptr;
138 }
139 
141 {
142  const std::string &eventId = event.getId();
143  if (eventId == "close")
144  {
145  close();
146  }
147  else if (eventId == "send")
148  {
149  sendMail();
150  }
151  else if (eventId == "add")
152  {
153  Item *const item = inventoryWindow->getSelectedItem();
154 
155  if (item == nullptr)
156  return;
157 
159  this,
160  item,
161  0,
162  0);
163  }
164 }
165 
166 void MailEditWindow::addItem(const Item *const item,
167  const int amount)
168 {
169  if (item == nullptr)
170  return;
171  mInventory->addItem(item->getId(),
172  item->getType(),
173  amount,
174  item->getRefine(),
175  item->getColor(),
176  item->getIdentified(),
177  item->getDamaged(),
178  item->getFavorite(),
179  Equipm_false,
181 }
182 
183 void MailEditWindow::setSubject(const std::string &str)
184 {
185  mSubjectField->setText(str);
186 }
187 
188 void MailEditWindow::setTo(const std::string &str)
189 {
190  mToField->setText(str);
191  mSendButton->setEnabled(true);
192 }
193 
194 void MailEditWindow::setMessage(const std::string &str)
195 {
196  mMessageField->setText(str);
197 }
198 
200 {
201  if (mUseMail2)
203  mailEditWindow = nullptr;
204  scheduleDelete();
205 }
206 
208 {
209  return mInventory;
210 }
211 
213 {
214  const int money = mMoneyField->getValue();
215  std::string subject = mSubjectField->getText();
216  if (subject.empty())
217  {
218  // TRANSLATORS: empty mail message subject
219  subject.append(_("empty subject"));
220  }
221  if (mUseMail2)
222  {
224  subject,
226  money);
227  }
228  else
229  {
230  if (money != 0)
231  mailHandler->setAttachMoney(money);
232  const Item *const tempItem = mInventory->getItem(0);
233  if (tempItem != nullptr)
234  {
235  const Inventory *const inv = PlayerInfo::getInventory();
236  if (inv != nullptr)
237  {
238  const Item *const item = inv->findItem(
239  tempItem->getId(), ItemColor_one);
240  if (item != nullptr)
241  {
243  tempItem->getQuantity());
244  }
245  }
246  }
247 
249  subject,
251  }
252 }
253 
255 {
257 }
258 
260 {
261  if (!mUseMail2)
262  return;
263 
264  if (event.getSource() == mToField)
265  {
266  const std::string to = mToField->getText();
267  if (to != mail2Handler->getCheckedName())
268  {
270  to,
271  std::string(),
272  std::string(),
273  0);
274  mSendButton->setEnabled(false);
275  }
276  else
277  {
278  mSendButton->setEnabled(true);
279  }
280  }
281 }
282 
284 {
285  mSendButton->setEnabled(true);
286 }
#define fromBool(val, name)
Definition: booldefines.h:49
const std::string BUTTON_SKIN
Definition: button.h:89
Definition: button.h:102
Definition: event.h:79
Widget * getSource() const
Definition: event.h:104
int getValue() const
Item * getSelectedItem() const
Item * getItem(const int index) const
Definition: inventory.cpp:83
Item * findItem(const int itemId, const ItemColor color) const
Definition: inventory.cpp:94
int addItem(const int id, const ItemTypeT type, const int quantity, const uint8_t refine, const ItemColor color, const Identified identified, const Damaged damaged, const Favorite favorite, const Equipm equipment, const Equipped equipped)
Definition: inventory.cpp:115
static void showWindow(const ItemAmountWindowUsageT usage, Window *const parent, Item *const item, int maxRange, const int tag)
Definition: item.h:50
uint8_t getRefine() const
Definition: item.h:141
int getQuantity() const
Definition: item.h:105
int getInvIndex() const
Definition: item.h:165
int getId() const
Definition: item.h:81
Favorite getFavorite() const
Definition: item.h:205
ItemColor getColor() const
Definition: item.h:181
Identified getIdentified() const
Definition: item.h:193
ItemTypeT getType() const
Definition: item.h:225
Damaged getDamaged() const
Definition: item.h:199
Definition: label.h:91
Label * mItemLabel
void focusLost(const Event &event)
void addItem(const Item *const item, const int amount)
ScrollArea * mItemScrollArea
Inventory * getInventory() const
Label * mSubjectLabel
Inventory * mInventory
void setMessage(const std::string &str)
IntTextField * mMoneyField
TextField * mMessageField
void setSubject(const std::string &str)
Button * mSendButton
TextField * mToField
ItemContainer * mItemContainer
Button * mAddButton
TextField * mSubjectField
void action(const ActionEvent &event)
void setTo(const std::string &str)
Button * mCloseButton
Label * mMoneyLabel
Label * mMessageLabel
virtual std::string getCheckedName() 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 sendMail(const std::string &to, const std::string &title, const std::string &body, const int64_t &money) const =0
virtual void cancelWriteMail() const =0
virtual void setAttachMoney(const int money) const =0
virtual void send(const std::string &name, const std::string &title, std::string message) const =0
virtual void setAttach(const int index, const int amount) const =0
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
void setHeight(int height)
const std::string & getText() const
Definition: textfield.h:224
void setText(const std::string &text)
Definition: textfield.cpp:803
void setWidth(const int width)
Definition: widget.cpp:133
void setEnabled(const bool enabled)
Definition: widget.h:352
void addFocusListener(FocusListener *const focusListener)
Definition: widget.cpp:282
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 new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
const bool Enable_true
Definition: enable.h:30
const bool Equipm_false
Definition: equipm.h:30
const bool Equipped_false
Definition: equipped.h:30
const bool ForceQuantity_false
Definition: forcequantity.h:30
#define _(s)
Definition: gettext.h:35
InventoryWindow * inventoryWindow
const ItemColor ItemColor_one
Definition: itemcolor.h:30
#define nullptr
Definition: localconsts.h:45
const bool LoseFocusOnTab_true
Net::Mail2Handler * mail2Handler
Definition: net.cpp:113
MailEditWindow * mailEditWindow
Net::MailHandler * mailHandler
Definition: net.cpp:114
const bool Modal_false
Definition: modal.h:30
Inventory * getInventory()
Definition: playerinfo.cpp:195
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
const bool Visible_true
Definition: visible.h:30