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