GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/socialfriendstab.h Lines: 17 42 40.5 %
Date: 2017-11-29 Branches: 8 36 22.2 %

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