GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/socialfriendstab.h Lines: 17 42 40.5 %
Date: 2021-03-17 Branches: 8 38 21.1 %

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
#ifndef GUI_WIDGETS_TABS_SOCIALFRIENDSTAB_H
23
#define GUI_WIDGETS_TABS_SOCIALFRIENDSTAB_H
24
25
#include "gui/widgets/tabs/socialtab.h"
26
27
#include "actormanager.h"
28
29
#include "being/playerrelations.h"
30
31
#include "gui/models/beingslistmodel.h"
32
33
#include "gui/windows/whoisonline.h"
34
35
#include "gui/widgets/tabs/socialfriendsfunctor.h"
36
37
#include "utils/delete2.h"
38
#include "utils/foreach.h"
39
#include "utils/gettext.h"
40
41
#include "localconsts.h"
42
43
class SocialFriendsTab final : public SocialTab
44
{
45
    public:
46
1
        SocialFriendsTab(const Widget2 *const widget,
47
                         const std::string &name,
48
1
                         const Opaque showBackground) :
49
            SocialTab(widget),
50
2
            mBeings(new BeingsListModel),
51
2
            mFriendSorter()
52
        {
53
1
            createControls(mBeings, showBackground);
54
55
1
            getPlayersAvatars();
56
1
            setCaption(name);
57
2
            mMenuAction = "friends";
58
1
        }
59
60
        A_DELETE_COPY(SocialFriendsTab)
61
62
2
        ~SocialFriendsTab() override final
63
2
        {
64
1
            delete2(mList)
65
1
            delete2(mScroll)
66
2
            delete2(mBeings)
67
2
        }
68
69
        void updateList() override final
70
        {
71
            getPlayersAvatars();
72
        }
73
74
1
        void getPlayersAvatars()
75
        {
76
1
            if (actorManager == nullptr)
77
                return;
78
79
            STD_VECTOR<Avatar*> *const avatars = mBeings->getMembers();
80
81
            STD_VECTOR<Avatar*>::iterator ia = avatars->begin();
82
            while (ia != avatars->end())
83
            {
84
                delete *ia;
85
                ++ ia;
86
            }
87
            avatars->clear();
88
89
            const StringVect *const players
90
                = playerRelations.getPlayersByRelation(Relation::FRIEND);
91
92
            const std::set<std::string> &players2
93
                = whoIsOnline->getOnlineNicks();
94
95
            if (players == nullptr)
96
                return;
97
98
            int online = 0;
99
            int total = 0;
100
101
            FOR_EACHP (StringVectCIter, it, players)
102
            {
103
                Avatar *const ava = new Avatar(*it);
104
                if (actorManager->findBeingByName(*it, ActorType::Player) !=
105
                    nullptr || players2.find(*it) != players2.end())
106
                {
107
                    ava->setOnline(true);
108
                    online ++;
109
                }
110
                total ++;
111
                avatars->push_back(ava);
112
            }
113
            std::sort(avatars->begin(), avatars->end(), mFriendSorter);
114
            delete players;
115
116
            // TRANSLATORS: social window label
117
            mCounterString = strprintf(_("Friends: %u/%u"),
118
                CAST_U32(online),
119
                CAST_U32(total));
120
            updateCounter();
121
        }
122
123
    private:
124
        BeingsListModel *mBeings;
125
        SortFriendsFunctor mFriendSorter;
126
};
127
128
#endif  // GUI_WIDGETS_TABS_SOCIALFRIENDSTAB_H