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/maileditwindow.h"
23 
24 #include "settings.h"
25 
26 #include "being/playerinfo.h"
27 
28 #include "gui/windows/inventorywindow.h"
29 #include "gui/windows/itemamountwindow.h"
30 
31 #include "gui/widgets/button.h"
32 #include "gui/widgets/containerplacer.h"
33 #include "gui/widgets/inttextfield.h"
34 #include "gui/widgets/itemcontainer.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 
50 MailEditWindow *mailEditWindow = nullptr;
51 
MailEditWindow()52 MailEditWindow::MailEditWindow() :
53     // TRANSLATORS: mail edit window name
54     Window(_("Edit mail"), Modal_false, nullptr, "mailedit.xml"),
55     ActionListener(),
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)),
81     mInventory(new Inventory(InventoryType::MailEdit,
82         settings.enableNewMailSystem ? -1 : 1)),
83     mItemContainer(new ItemContainer(this, mInventory, 100000,
84         ShowEmptyRows_false, ForceQuantity_false)),
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);
96     setVisible(Visible_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);
109     mItemScrollArea->setHeight(100);
110     mItemScrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
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 
~MailEditWindow()134 MailEditWindow::~MailEditWindow()
135 {
136     mailEditWindow = nullptr;
137     delete2(mInventory)
138 }
139 
action(const ActionEvent & event)140 void MailEditWindow::action(const ActionEvent &event)
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 
158         ItemAmountWindow::showWindow(ItemAmountWindowUsage::MailAdd,
159             this,
160             item,
161             0,
162             0);
163     }
164 }
165 
addItem(const Item * const item,const int amount)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,
180         Equipped_false);
181 }
182 
setSubject(const std::string & str)183 void MailEditWindow::setSubject(const std::string &str)
184 {
185     mSubjectField->setText(str);
186 }
187 
setTo(const std::string & str)188 void MailEditWindow::setTo(const std::string &str)
189 {
190     mToField->setText(str);
191     mSendButton->setEnabled(true);
192 }
193 
setMessage(const std::string & str)194 void MailEditWindow::setMessage(const std::string &str)
195 {
196     mMessageField->setText(str);
197 }
198 
close()199 void MailEditWindow::close()
200 {
201     if (mUseMail2)
202         mail2Handler->cancelWriteMail();
203     mailEditWindow = nullptr;
204     scheduleDelete();
205 }
206 
getInventory() const207 Inventory *MailEditWindow::getInventory() const
208 {
209     return mInventory;
210 }
211 
sendMail()212 void MailEditWindow::sendMail()
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     {
223         mail2Handler->sendMail(mToField->getText(),
224             subject,
225             mMessageField->getText(),
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                 {
242                     mailHandler->setAttach(item->getInvIndex(),
243                         tempItem->getQuantity());
244                 }
245             }
246         }
247 
248         mailHandler->send(mToField->getText(),
249             subject,
250             mMessageField->getText());
251     }
252 }
253 
updateItems()254 void MailEditWindow::updateItems()
255 {
256     mItemContainer->updateMatrix();
257 }
258 
focusLost(const Event & event)259 void MailEditWindow::focusLost(const Event &event)
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         {
269             mail2Handler->queueCheckName(MailQueueType::ValidateTo,
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 
validatedTo()283 void MailEditWindow::validatedTo()
284 {
285     mSendButton->setEnabled(true);
286 }
287