GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/models/serverslistmodel.h Lines: 19 29 65.5 %
Date: 2017-11-29 Branches: 4 24 16.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#ifndef GUI_MODELS_SERVERSLISTMODEL_H
24
#define GUI_MODELS_SERVERSLISTMODEL_H
25
26
#include "gui/gui.h"
27
28
#include "gui/fonts/font.h"
29
30
#include "gui/windows/serverdialog.h"
31
32
#include "gui/models/listmodel.h"
33
34
#include "utils/gettext.h"
35
36
/**
37
 * Server and Port List Model
38
 */
39
4
class ServersListModel final : public ListModel
40
{
41
    public:
42
        typedef std::pair<int, std::string> VersionString;
43
44
4
        ServersListModel(ServerInfos *const servers,
45
4
                         ServerDialog *const parent) :
46
            mServers(servers),
47
            mVersionStrings(servers != nullptr ? servers->size() : 0,
48
12
                VersionString(0, "")),
49

24
            mParent(parent)
50
        {
51
4
        }
52
53
        A_DELETE_COPY(ServersListModel)
54
55
        /**
56
         * Used to get number of line in the list
57
         */
58
44
        int getNumberOfElements() override final A_WARN_UNUSED
59
        {
60
132
            MutexLocker lock = mParent->lock();
61
132
            return CAST_S32(mServers->size());
62
        }
63
64
        /**
65
         * Used to get an element from the list
66
         */
67
28
        std::string getElementAt(int elementIndex)
68
                                 override final A_WARN_UNUSED
69
        {
70
84
            MutexLocker lock = mParent->lock();
71
56
            const ServerInfo &server = mServers->at(elementIndex);
72
28
            std::string myServer;
73
28
            if (server.freeType == ServerFreeType::NonFree)
74
            {
75
                // TRANSLATORS: server license comment
76
                myServer.append(_("(NON FREE)"));
77
                myServer.append(" ");
78
            }
79
28
            else if (server.freeType == ServerFreeType::Unknown)
80
            {
81
                // TRANSLATORS: server license comment
82
                myServer.append(_("(UNKNOWN)"));
83
                myServer.append(" ");
84
            }
85
56
            myServer.append(server.hostname);
86
28
            return myServer;
87
        }
88
89
        /**
90
         * Used to get the corresponding Server struct
91
         */
92
28
        const ServerInfo &getServer(const int elementIndex) const A_WARN_UNUSED
93
56
        { return mServers->at(elementIndex); }
94
95
        void setVersionString(const int index, const std::string &version)
96
        {
97
            if (index < 0 || index >= CAST_S32(mVersionStrings.size()))
98
            return;
99
100
            if (version.empty() || (gui == nullptr))
101
            {
102
                mVersionStrings[index] = VersionString(0, "");
103
            }
104
            else
105
            {
106
                mVersionStrings[index] = VersionString(
107
                    gui->getFont()->getWidth(version), version);
108
            }
109
        }
110
111
    private:
112
        typedef STD_VECTOR<VersionString> VersionStrings;
113
114
        ServerInfos *mServers;
115
        VersionStrings mVersionStrings;
116
        ServerDialog *mParent;
117
};
118
119
#endif  // GUI_MODELS_SERVERSLISTMODEL_H