GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/socialwindow.cpp Lines: 77 288 26.7 %
Date: 2018-09-20 Branches: 65 322 20.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2010  The Mana Developers
4
 *  Copyright (C) 2011-2018  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
1
SocialWindow::SocialWindow() :
51
    // TRANSLATORS: social window name
52
1
    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
1
    mPlayers(new SocialPlayersTab(this, _("P"),
66


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


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


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

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

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


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



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

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


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

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


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

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

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

2
    if (mTabs->getSelectedTab() == tab)
644
1
        mMenuButton->setActionEventId(menu);
645
}
646
647
void SocialWindow::updateGuildCounter(const int online, const int total)
648
{
649
    if (localPlayer == nullptr)
650
        return;
651
652
    Guild *const guild = localPlayer->getGuild();
653
    if (guild != nullptr)
654
    {
655
        const GuildMap::iterator it = mGuilds.find(guild);
656
        if (it != mGuilds.end())
657
        {
658
            SocialTab *const tab = (*it).second;
659
            tab->buildCounter(online, total);
660
        }
661
    }
662
}
663
664
void SocialWindow::updatedPlayer(const std::string &name A_UNUSED)
665
{
666
    mNeedUpdate = true;
667
}
668
669
void SocialWindow::updateAll()
670
{
671
    mNeedUpdate = true;
672

3
}
673
674
#ifdef USE_PROFILER
675
void SocialWindow::logicChildren()
676
{
677
    BLOCK_START("SocialWindow::logicChildren")
678
    BasicContainer::logicChildren();
679
    BLOCK_END("SocialWindow::logicChildren")
680
}
681
#endif  // USE_PROFILER