GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/mailrecv.cpp Lines: 0 110 0.0 %
Date: 2021-03-17 Branches: 0 62 0.0 %

Line Branch Exec Source
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 "net/eathena/mailrecv.h"
23
24
#include "itemcolormanager.h"
25
#include "notifymanager.h"
26
27
#include "const/resources/item/cards.h"
28
29
#include "enums/resources/notifytypes.h"
30
31
#include "gui/mailmessage.h"
32
33
#include "gui/windows/mailviewwindow.h"
34
#include "gui/windows/mailwindow.h"
35
36
#include "net/mailhandler.h"
37
#include "net/messagein.h"
38
39
#include "utils/checkutils.h"
40
#include "utils/gettext.h"
41
#include "utils/stringutils.h"
42
43
#include "resources/inventory/inventory.h"
44
45
#include "debug.h"
46
47
namespace EAthena
48
{
49
50
void MailRecv::processMailOpen(Net::MessageIn &msg)
51
{
52
    const int flag = msg.readInt32("flag");
53
    switch (flag)
54
    {
55
        case 0:  // open window
56
            if (mailWindow != nullptr)
57
            {
58
                if (!mailWindow->isWindowVisible())
59
                    mailWindow->setVisible(Visible_true);
60
                mailWindow->requestMoveToTop();
61
            }
62
            break;
63
64
        case 1:  // close window
65
            if (mailWindow != nullptr)
66
            {
67
                if (mailWindow->isWindowVisible())
68
                    mailWindow->setVisible(Visible_false);
69
            }
70
            break;
71
72
        default:
73
            UNIMPLEMENTEDPACKETFIELD(flag);
74
            break;
75
    }
76
}
77
78
void MailRecv::processMailList(Net::MessageIn &msg)
79
{
80
    const int count = (msg.readInt16("len") - 8) / 73;
81
    const int amount = msg.readInt32("amount");
82
    if (count != amount)
83
        logger->log("error: wrong mails count");
84
    mailWindow->clear();
85
    for (int f = 0; f < count; f ++)
86
    {
87
        MailMessage *const mail = new MailMessage;
88
        mail->id = msg.readInt32("message id");
89
        mail->title = msg.readString(40, "title");
90
        mail->read = msg.readUInt8("unread flag") != 0U ? true : false;
91
        mail->sender = msg.readString(24, "sender name");
92
        mail->time = msg.readInt32("time stamp");
93
        mail->strTime = timeToStr(mail->time);
94
        mailWindow->addMail(mail);
95
    }
96
}
97
98
void MailRecv::processReadMail(Net::MessageIn &msg)
99
{
100
    const int sz = msg.readInt16("len") - 101;
101
    MailMessage *const mail = new MailMessage;
102
    mail->id = msg.readInt32("message id");
103
    mail->title = msg.readString(40, "title");
104
    mail->sender = msg.readString(24, "sender name");
105
    msg.readInt32("unused");
106
    mail->money = msg.readInt32("money");
107
108
    const int amount = msg.readInt32("item amount");
109
    const int itemId = msg.readInt16("item id");
110
    const ItemTypeT itemType = static_cast<ItemTypeT>(
111
        msg.readInt16("item type"));
112
    const uint8_t identify = msg.readUInt8("identify");
113
    const Damaged damaged = fromBool(msg.readUInt8("attribute"), Damaged);
114
    const uint8_t refine = msg.readUInt8("refine");
115
    int cards[maxCards];
116
    for (int d = 0; d < maxCards; d ++)
117
        cards[d] = msg.readUInt16("card");
118
    const int msgLen = msg.readUInt8("msg len");
119
    if (msgLen != sz)
120
        logger->log("error: wrong message size");
121
    mail->text = msg.readString(sz, "message");
122
    msg.readUInt8("zero");
123
    mail->strTime = timeToStr(mail->time);
124
    if (mailWindow == nullptr)
125
    {
126
        reportAlways("Mail window not created")
127
        delete mail;
128
        return;
129
    }
130
    mailWindow->showMessage(mail, itemId != 0 ? 1 : 0);
131
    if (mailViewWindow == nullptr)
132
    {
133
        reportAlways("Mail view window not created")
134
        return;
135
    }
136
137
    Inventory *const inventory = mailViewWindow->getInventory();
138
    if (inventory == nullptr)
139
    {
140
        reportAlways("Mail view window missing inventory")
141
        return;
142
    }
143
144
    const int slot = inventory->addItem(itemId,
145
        itemType,
146
        amount,
147
        refine,
148
        ItemColorManager::getColorFromCards(&cards[0]),
149
        fromBool(identify, Identified),
150
        damaged,
151
        Favorite_false,
152
        Equipm_false,
153
        Equipped_false);
154
    if (slot != -1)
155
        inventory->setCards(slot, cards, maxCards);
156
157
    mailViewWindow->updateItems();
158
}
159
160
void MailRecv::processGetAttachment(Net::MessageIn &msg)
161
{
162
    const uint8_t flag = msg.readUInt8("flag");
163
    switch (flag)
164
    {
165
        case 0:
166
            NotifyManager::notify(NotifyTypes::MAIL_GET_ATTACH_OK);
167
            break;
168
        case 1:
169
            NotifyManager::notify(NotifyTypes::MAIL_GET_ATTACH_ERROR);
170
            break;
171
        case 2:
172
            NotifyManager::notify(NotifyTypes::MAIL_GET_ATTACH_TOO_MANY_ITEMS);
173
            break;
174
        default:
175
            UNIMPLEMENTEDPACKETFIELD(flag);
176
            break;
177
    }
178
}
179
180
void MailRecv::processSendMailAck(Net::MessageIn &msg)
181
{
182
    const uint8_t flag = msg.readUInt8("fail flag");
183
    switch (flag)
184
    {
185
        case 0:
186
            NotifyManager::notify(NotifyTypes::MAIL_SEND_OK);
187
            break;
188
        case 1:
189
            NotifyManager::notify(NotifyTypes::MAIL_SEND_ERROR);
190
            break;
191
        default:
192
            UNIMPLEMENTEDPACKETFIELD(flag);
193
            break;
194
    }
195
}
196
197
void MailRecv::processNewMail(Net::MessageIn &msg)
198
{
199
    msg.readInt32("message id");
200
    const std::string subj = msg.readString(40, "title");
201
    const std::string sender = msg.readString(24, "sender name");
202
    NotifyManager::notify(NotifyTypes::NEW_MAIL,
203
        // TRANSLATORS: mail message notification
204
        strprintf(_("You have new mail from %s with subject %s"),
205
        sender.c_str(), subj.c_str()));
206
    mailHandler->refresh();
207
}
208
209
void MailRecv::processSetAttachmentAck(Net::MessageIn &msg)
210
{
211
    const int index = msg.readInt16("index");
212
    const int flag = msg.readUInt8("flag");
213
    if (flag != 0)
214
    {
215
        if (index != 0)
216
            NotifyManager::notify(NotifyTypes::MAIL_ATTACH_ITEM_ERROR);
217
        else
218
            NotifyManager::notify(NotifyTypes::MAIL_ATTACH_MONEY_ERROR);
219
    }
220
}
221
222
void MailRecv::processDeleteAck(Net::MessageIn &msg)
223
{
224
    const int mail = msg.readInt32("message id");
225
    const int flag = msg.readInt16("fail flag");
226
    if (flag != 0)
227
    {
228
        NotifyManager::notify(NotifyTypes::MAIL_DELETE_ERROR);
229
    }
230
    else
231
    {
232
        NotifyManager::notify(NotifyTypes::MAIL_DELETE_OK);
233
        mailWindow->removeMail(mail);
234
    }
235
}
236
237
void MailRecv::processMailReturn(Net::MessageIn &msg)
238
{
239
    const int mail = msg.readInt32("message id");
240
    const int flag = msg.readInt16("fail flag");
241
    if (flag != 0)
242
    {
243
        NotifyManager::notify(NotifyTypes::MAIL_RETURN_ERROR);
244
    }
245
    else
246
    {
247
        NotifyManager::notify(NotifyTypes::MAIL_RETURN_OK);
248
        mailWindow->removeMail(mail);
249
    }
250
}
251
252
}  // namespace EAthena