GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/socialwindow.cpp Lines: 77 275 28.0 %
Date: 2017-11-29 Branches: 67 332 20.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2010  The Mana Developers
4
 *  Copyright (C) 2011-2017  The ManaPlus Developers
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/socialwindow.h"
23
24
#include "gui/popups/popupmenu.h"
25
26
#include "gui/windows/setupwindow.h"
27
28
#include "gui/widgets/button.h"
29
#include "gui/widgets/label.h"
30
#include "gui/widgets/tabbedarea.h"
31
32
#include "gui/widgets/tabs/socialattacktab.h"
33
#include "gui/widgets/tabs/socialfriendstab.h"
34
#include "gui/widgets/tabs/socialguildtab.h"
35
#include "gui/widgets/tabs/socialnavigationtab.h"
36
#include "gui/widgets/tabs/socialpartytab.h"
37
#include "gui/widgets/tabs/socialpickuptab.h"
38
#include "gui/widgets/tabs/socialplayerstab.h"
39
40
#ifdef TMWA_SUPPORT
41
#include "gui/widgets/tabs/socialguildtab2.h"
42
43
#include "net/tmwa/guildmanager.h"
44
#endif  // TMWA_SUPPORT
45
46
#include "debug.h"
47
48
SocialWindow *socialWindow = nullptr;
49
50
2
SocialWindow::SocialWindow() :
51
    // TRANSLATORS: social window name
52
2
    Window(_("Social"), Modal_false, nullptr, "social.xml"),
53
    ActionListener(),
54
    PlayerRelationsListener(),
55
    mGuildInvited(0),
56
    mGuildAcceptDialog(nullptr),
57
    mGuildCreateDialog(nullptr),
58
    mPartyInviter(),
59
    mGuilds(),
60
    mParties(),
61
    mPartyAcceptDialog(nullptr),
62
    mAttackFilter(nullptr),
63
    mPickupFilter(nullptr),
64
    // TRANSLATORS: here P is title for visible players tab in social window
65
2
    mPlayers(new SocialPlayersTab(this, _("P"),
66


8
        fromBool(getOptionBool("showtabbackground"), Opaque))),
67
    mNavigation(new SocialNavigationTab(this,
68


8
        fromBool(getOptionBool("showtabbackground"), Opaque))),
69
    // TRANSLATORS: here F is title for friends tab in social window
70
2
    mFriends(new SocialFriendsTab(this, _("F"),
71


8
        fromBool(getOptionBool("showtabbackground"), Opaque))),
72
    // TRANSLATORS: social window button
73

4
    mMenuButton(new Button(this, _("Menu"), "menu", this)),
74

2
    mCountLabel(new Label(this, "1000 / 1000")),
75

2
    mTabs(CREATEWIDGETR(TabbedArea, this)),
76
    mMap(nullptr),
77
    mLastUpdateTime(0),
78
    mPartyId(0),
79
    mNeedUpdate(false),
80




68
    mProcessedPortals(false)
81
{
82
2
}
83
84
2
void SocialWindow::postInit()
85
{
86
2
    Window::postInit();
87
10
    setWindowName("Social");
88
2
    setVisible(Visible_false);
89
4
    setSaveVisible(true);
90
2
    setResizable(true);
91
4
    setSaveVisible(true);
92
2
    setCloseButton(true);
93
2
    setStickyButtonLock(true);
94
95
2
    setMinWidth(120);
96
2
    setMinHeight(55);
97
2
    setDefaultSize(590, 200, 180, 300);
98
2
    if (setupWindow != nullptr)
99
        setupWindow->registerWindowForReset(this);
100
101
2
    place(0, 0, mMenuButton);
102
2
    place(0, 1, mCountLabel);
103
2
    place(0, 2, mTabs, 2, 4);
104
105
2
    widgetResized(Event(nullptr));
106
107
2
    loadWindowState();
108
109
2
    mTabs->addTab(mPlayers, mPlayers->mScroll);
110
2
    mTabs->addTab(mFriends, mFriends->mScroll);
111
2
    mTabs->addTab(mNavigation, mNavigation->mScroll);
112
4
    mTabs->setSelectable(false);
113
6
    mTabs->getTabContainer()->setSelectable(false);
114
6
    mTabs->getWidgetContainer()->setSelectable(false);
115
116

8
    if (config.getBoolValue("enableAttackFilter"))
117
    {
118
2
        mAttackFilter = new SocialAttackTab(this,
119


8
            fromBool(getOptionBool("showtabbackground"), Opaque));
120
2
        mTabs->addTab(mAttackFilter, mAttackFilter->mScroll);
121
    }
122
    else
123
    {
124
        mAttackFilter = nullptr;
125
    }
126
127

8
    if (config.getBoolValue("enablePickupFilter"))
128
    {
129
2
        mPickupFilter = new SocialPickupTab(this,
130


8
            fromBool(getOptionBool("showtabbackground"), Opaque));
131
2
        mTabs->addTab(mPickupFilter, mPickupFilter->mScroll);
132
    }
133
    else
134
    {
135
        mPickupFilter = nullptr;
136
    }
137
138

4
    if ((localPlayer != nullptr) && (localPlayer->getParty() != nullptr))
139
        addTab(localPlayer->getParty());
140
141

2
    if ((localPlayer != nullptr) && (localPlayer->getGuild() != nullptr))
142
        addTab(localPlayer->getGuild());
143
144
4
    enableVisibleSound(true);
145
2
    updateButtons();
146
4
    playerRelations.addListener(this);
147
2
}
148
149
16
SocialWindow::~SocialWindow()
150
{
151
4
    playerRelations.removeListener(this);
152
2
    if (mGuildAcceptDialog != nullptr)
153
    {
154
        mGuildAcceptDialog->close();
155
        mGuildAcceptDialog->scheduleDelete();
156
        mGuildAcceptDialog = nullptr;
157
158
        mGuildInvited = 0;
159
    }
160
161
2
    if (mPartyAcceptDialog != nullptr)
162
    {
163
        mPartyAcceptDialog->close();
164
        mPartyAcceptDialog->scheduleDelete();
165
        mPartyAcceptDialog = nullptr;
166
167
        mPartyInviter.clear();
168
    }
169
2
    delete2(mPlayers);
170
2
    delete2(mNavigation);
171
2
    delete2(mAttackFilter);
172
2
    delete2(mPickupFilter);
173
2
    delete2(mFriends);
174
6
    FOR_EACH (GuildMap::iterator, it, mGuilds)
175
    {
176
        delete (*it).second;
177
    }
178
4
    mGuilds.clear();
179
6
    FOR_EACH (PartyMap::iterator, it, mParties)
180
    {
181
        delete (*it).second;
182
    }
183
4
    mParties.clear();
184
4
}
185
186
bool SocialWindow::addTab(Guild *const guild)
187
{
188
    if (guild == nullptr)
189
        return false;
190
191
    if (mGuilds.find(guild) != mGuilds.end())
192
        return false;
193
194
    SocialTab *tab = nullptr;
195
    if (guild->getServerGuild())
196
    {
197
        tab = new SocialGuildTab(this, guild,
198
            fromBool(getOptionBool("showtabbackground"), Opaque));
199
    }
200
#ifdef TMWA_SUPPORT
201
    else
202
    {
203
        tab = new SocialGuildTab2(this, guild,
204
            fromBool(getOptionBool("showtabbackground"), Opaque));
205
    }
206
#endif  // TMWA_SUPPORT
207
208
    mGuilds[guild] = tab;
209
    mTabs->addTab(tab, tab->mScroll);
210
211
    updateButtons();
212
213
    return true;
214
}
215
216
bool SocialWindow::removeTab(Guild *const guild)
217
{
218
    const GuildMap::iterator it = mGuilds.find(guild);
219
    if (it == mGuilds.end())
220
        return false;
221
222
    mTabs->removeTab(it->second);
223
    delete it->second;
224
    mGuilds.erase(it);
225
226
    updateButtons();
227
228
    return true;
229
}
230
231
bool SocialWindow::addTab(Party *const party)
232
{
233
    if (party == nullptr)
234
        return false;
235
236
    if (mParties.find(party) != mParties.end())
237
        return false;
238
239
    SocialPartyTab *const tab = new SocialPartyTab(this, party,
240
        fromBool(getOptionBool("showtabbackground"), Opaque));
241
    mParties[party] = tab;
242
243
    mTabs->addTab(tab, tab->mScroll);
244
245
    updateButtons();
246
247
    return true;
248
}
249
250
bool SocialWindow::removeTab(Party *const party)
251
{
252
    const PartyMap::iterator it = mParties.find(party);
253
    if (it == mParties.end())
254
        return false;
255
256
    mTabs->removeTab(it->second);
257
    delete it->second;
258
    mParties.erase(it);
259
260
    updateButtons();
261
262
    return true;
263
}
264
265
void SocialWindow::action(const ActionEvent &event)
266
{
267
    const std::string &eventId = event.getId();
268
269
    if (event.getSource() == mPartyAcceptDialog)
270
    {
271
        if (eventId == "yes")
272
        {
273
            if (localChatTab != nullptr)
274
            {
275
                localChatTab->chatLog(
276
                    // TRANSLATORS: chat message
277
                    strprintf(_("Accepted party invite from %s."),
278
                    mPartyInviter.c_str()),
279
                    ChatMsgType::BY_SERVER);
280
            }
281
            partyHandler->inviteResponse(mPartyId, true);
282
        }
283
        else if (eventId == "no")
284
        {
285
            if (localChatTab != nullptr)
286
            {
287
                localChatTab->chatLog(
288
                    // TRANSLATORS: chat message
289
                    strprintf(_("Rejected party invite from %s."),
290
                    mPartyInviter.c_str()),
291
                    ChatMsgType::BY_SERVER);
292
            }
293
            partyHandler->inviteResponse(mPartyId, false);
294
        }
295
296
        mPartyInviter.clear();
297
        mPartyAcceptDialog = nullptr;
298
    }
299
    else if (event.getSource() == mGuildAcceptDialog)
300
    {
301
        if (eventId == "yes")
302
        {
303
            if (localChatTab != nullptr)
304
            {
305
                localChatTab->chatLog(
306
                    // TRANSLATORS: chat message
307
                    strprintf(_("Accepted guild invite from %s."),
308
                    mPartyInviter.c_str()),
309
                    ChatMsgType::BY_SERVER);
310
            }
311
#ifdef TMWA_SUPPORT
312
            if (guildManager == nullptr || !GuildManager::getEnableGuildBot())
313
                guildHandler->inviteResponse(mGuildInvited, true);
314
            else
315
                GuildManager::inviteResponse(true);
316
#else  // TMWA_SUPPORT
317
318
            guildHandler->inviteResponse(mGuildInvited, true);
319
#endif  // TMWA_SUPPORT
320
        }
321
        else if (eventId == "no")
322
        {
323
            if (localChatTab != nullptr)
324
            {
325
                localChatTab->chatLog(
326
                    // TRANSLATORS: chat message
327
                    strprintf(_("Rejected guild invite from %s."),
328
                    mPartyInviter.c_str()),
329
                    ChatMsgType::BY_SERVER);
330
            }
331
#ifdef TMWA_SUPPORT
332
            if (guildManager == nullptr || !GuildManager::getEnableGuildBot())
333
                guildHandler->inviteResponse(mGuildInvited, false);
334
            else
335
                GuildManager::inviteResponse(false);
336
#else  // TMWA_SUPPORT
337
338
            guildHandler->inviteResponse(mGuildInvited, false);
339
#endif  // TMWA_SUPPORT
340
        }
341
342
        mGuildInvited = 0;
343
        mGuildAcceptDialog = nullptr;
344
    }
345
    else if (eventId == "party")
346
    {
347
        popupMenu->showPartyPopup();
348
    }
349
    else if (eventId == "guild")
350
    {
351
        popupMenu->showGuildPopup();
352
    }
353
    else if (eventId == "attack")
354
    {
355
        popupMenu->showAttackPopup();
356
    }
357
    else if (eventId == "friends")
358
    {
359
        popupMenu->showFriendsPopup();
360
    }
361
    else if (eventId == "navigation")
362
    {
363
        popupMenu->showNavigationPopup();
364
    }
365
    else if (eventId == "pickup")
366
    {
367
        popupMenu->showPickupPopup();
368
    }
369
    else if (eventId == "players")
370
    {
371
        popupMenu->showPlayersPopup();
372
    }
373
}
374
375
void SocialWindow::showGuildInvite(const std::string &restrict guildName,
376
                                   const int guildId,
377
                                   const std::string &restrict inviterName)
378
{
379
    // check there isnt already an invite showing
380
    if (mGuildInvited != 0)
381
    {
382
        if (localChatTab != nullptr)
383
        {
384
            // TRANSLATORS: chat message
385
            localChatTab->chatLog(_("Received guild request, but one already "
386
                "exists."),
387
                ChatMsgType::BY_SERVER);
388
        }
389
        return;
390
    }
391
392
    const std::string msg = strprintf(
393
        // TRANSLATORS: chat message
394
        _("%s has invited you to join the guild %s."),
395
        inviterName.c_str(), guildName.c_str());
396
397
    if (localChatTab != nullptr)
398
        localChatTab->chatLog(msg, ChatMsgType::BY_SERVER);
399
400
    CREATEWIDGETV(mGuildAcceptDialog, ConfirmDialog,
401
        // TRANSLATORS: guild invite message
402
        _("Accept Guild Invite"),
403
        msg,
404
        SOUND_REQUEST,
405
        false,
406
        Modal_false,
407
        this);
408
    mGuildAcceptDialog->addActionListener(this);
409
    mGuildInvited = guildId;
410
}
411
412
void SocialWindow::showPartyInvite(const std::string &restrict partyName,
413
                                   const std::string &restrict inviter,
414
                                   const int partyId)
415
{
416
    // check there isnt already an invite showing
417
    if (!mPartyInviter.empty())
418
    {
419
        if (localChatTab != nullptr)
420
        {
421
            // TRANSLATORS: chat message
422
            localChatTab->chatLog(_("Received party request, but one already "
423
                "exists."),
424
                ChatMsgType::BY_SERVER);
425
        }
426
        return;
427
    }
428
429
    std::string msg;
430
    if (inviter.empty())
431
    {
432
        if (partyName.empty())
433
        {
434
            // TRANSLATORS: party invite message
435
            msg = _("You have been invited you to join a party.");
436
        }
437
        else
438
        {
439
            // TRANSLATORS: party invite message
440
            msg = strprintf(_("You have been invited to join the %s party."),
441
                partyName.c_str());
442
        }
443
    }
444
    else
445
    {
446
        if (partyName.empty())
447
        {
448
            // TRANSLATORS: party invite message
449
            msg = strprintf(_("%s has invited you to join their party."),
450
                inviter.c_str());
451
        }
452
        else
453
        {
454
            // TRANSLATORS: party invite message
455
            msg = strprintf(_("%s has invited you to join the %s party."),
456
                inviter.c_str(), partyName.c_str());
457
        }
458
    }
459
460
    if (localChatTab != nullptr)
461
        localChatTab->chatLog(msg, ChatMsgType::BY_SERVER);
462
463
    // show invite
464
    CREATEWIDGETV(mPartyAcceptDialog, ConfirmDialog,
465
        // TRANSLATORS: party invite message
466
        _("Accept Party Invite"),
467
        msg,
468
        SOUND_REQUEST,
469
        false,
470
        Modal_false,
471
        this);
472
    mPartyAcceptDialog->addActionListener(this);
473
    mPartyInviter = inviter;
474
    mPartyId = partyId;
475
}
476
477
void SocialWindow::updateActiveList()
478
{
479
    mNeedUpdate = true;
480
}
481
482
void SocialWindow::slowLogic()
483
{
484
    BLOCK_START("SocialWindow::slowLogic")
485
    const time_t nowTime = cur_time;
486
    if (mNeedUpdate && nowTime - mLastUpdateTime > 1)
487
    {
488
        mPlayers->updateList();
489
        mFriends->updateList();
490
        mNeedUpdate = false;
491
        mLastUpdateTime = nowTime;
492
    }
493
    else if (nowTime - mLastUpdateTime > 5)
494
    {
495
        mPlayers->updateList();
496
        mNeedUpdate = false;
497
        mLastUpdateTime = nowTime;
498
    }
499
    BLOCK_END("SocialWindow::slowLogic")
500
}
501
502
void SocialWindow::updateAvatar(const std::string &name)
503
{
504
    mPlayers->updateAvatar(name);
505
}
506
507
void SocialWindow::resetDamage(const std::string &name)
508
{
509
    mPlayers->resetDamage(name);
510
}
511
512
void SocialWindow::updateButtons()
513
{
514
//    if (!mTabs)
515
//        return;
516
517
//    const bool hasTabs = mTabs->getNumberOfTabs() > 0;
518
}
519
520
void SocialWindow::updatePortals()
521
{
522
    if (mNavigation != nullptr)
523
        mNavigation->updateList();
524
}
525
526
void SocialWindow::updatePortalNames()
527
{
528
    if (mNavigation != nullptr)
529
        static_cast<SocialNavigationTab*>(mNavigation)->updateNames();
530
}
531
532
void SocialWindow::selectPortal(const unsigned num)
533
{
534
    if (mNavigation != nullptr)
535
        mNavigation->selectIndex(num);
536
}
537
538
int SocialWindow::getPortalIndex(const int x, const int y)
539
{
540
    if (mNavigation != nullptr)
541
    {
542
        return static_cast<SocialNavigationTab*>(
543
            mNavigation)->getPortalIndex(x, y);
544
    }
545
    return -1;
546
}
547
548
void SocialWindow::addPortal(const int x, const int y)
549
{
550
    if (mNavigation != nullptr)
551
        static_cast<SocialNavigationTab*>(mNavigation)->addPortal(x, y);
552
}
553
554
void SocialWindow::removePortal(const int x, const int y)
555
{
556
    if (mNavigation != nullptr)
557
        static_cast<SocialNavigationTab*>(mNavigation)->removePortal(x, y);
558
}
559
560
void SocialWindow::nextTab()
561
{
562
    if (mTabs != nullptr)
563
        mTabs->selectNextTab();
564
}
565
566
void SocialWindow::prevTab()
567
{
568
    if (mTabs != nullptr)
569
        mTabs->selectPrevTab();
570
}
571
572
void SocialWindow::updateAttackFilter()
573
{
574
    if (mAttackFilter != nullptr)
575
        mAttackFilter->updateList();
576
}
577
578
void SocialWindow::updatePickupFilter()
579
{
580
    if (mPickupFilter != nullptr)
581
        mPickupFilter->updateList();
582
}
583
584
void SocialWindow::updateParty()
585
{
586
    if (localPlayer == nullptr)
587
        return;
588
589
    Party *const party = localPlayer->getParty();
590
    if (party != nullptr)
591
    {
592
        const PartyMap::iterator it = mParties.find(party);
593
        if (it != mParties.end())
594
        {
595
            SocialTab *const tab = (*it).second;
596
            tab->buildCounter(0, 0);
597
        }
598
    }
599
}
600
601
4
void SocialWindow::widgetResized(const Event &event)
602
{
603
4
    Window::widgetResized(event);
604
4
    if (mTabs != nullptr)
605
4
        mTabs->adjustSize();
606
4
}
607
608
2
void SocialWindow::updateCounter(const SocialTab *const tab,
609
                                 const std::string &counter)
610
{
611
4
    if (mTabs->getSelectedTab() == tab)
612
    {
613
2
        mCountLabel->setCaption(counter);
614
2
        mCountLabel->adjustSize();
615
    }
616
2
}
617
618
void SocialWindow::updateMenu(const SocialTab *const tab,
619
                              const std::string &menu)
620
{
621

4
    if (mTabs->getSelectedTab() == tab)
622
2
        mMenuButton->setActionEventId(menu);
623
}
624
625
void SocialWindow::updateGuildCounter(const int online, const int total)
626
{
627
    if (localPlayer == nullptr)
628
        return;
629
630
    Guild *const guild = localPlayer->getGuild();
631
    if (guild != nullptr)
632
    {
633
        const GuildMap::iterator it = mGuilds.find(guild);
634
        if (it != mGuilds.end())
635
        {
636
            SocialTab *const tab = (*it).second;
637
            tab->buildCounter(online, total);
638
        }
639
    }
640
}
641
642
void SocialWindow::updatedPlayer(const std::string &name A_UNUSED)
643
{
644
    mNeedUpdate = true;
645
}
646
647
void SocialWindow::updateAll()
648
{
649
    mNeedUpdate = true;
650

6
}
651
652
#ifdef USE_PROFILER
653
void SocialWindow::logicChildren()
654
{
655
    BLOCK_START("SocialWindow::logicChildren")
656
    BasicContainer::logicChildren();
657
    BLOCK_END("SocialWindow::logicChildren")
658
}
659
#endif  // USE_PROFILER