GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/mailrecv.cpp Lines: 0 120 0.0 %
Date: 2017-11-29 Branches: 0 62 0.0 %

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