GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/models/serverslistmodel.h Lines: 19 29 65.5 %
Date: 2021-03-17 Branches: 4 28 14.3 %

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

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