ManaPlus
serverslistmodel.h
Go to the documentation of this file.
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 
32 
33 #include "gui/models/listmodel.h"
34 
35 #include "utils/gettext.h"
36 
41 {
42  public:
43  typedef std::pair<int, std::string> VersionString;
44 
45  ServersListModel(ServerInfos *const servers,
46  ServerDialog *const parent) :
47  mServers(servers),
48  mVersionStrings(servers != nullptr ? servers->size() : 0,
49  VersionString(0, "")),
50  mParent(parent)
51  {
52  }
53 
55 
56 
60  {
61  MutexLocker lock = mParent->lock();
62  return CAST_S32(mServers->size());
63  }
64 
68  std::string getElementAt(int elementIndex)
69  override final A_WARN_UNUSED
70  {
71  MutexLocker lock = mParent->lock();
72  const ServerInfo &server = mServers->at(elementIndex);
73  std::string myServer;
74  if (server.freeType == ServerFreeType::NonFree)
75  {
76  // TRANSLATORS: server license comment
77  myServer.append(_("(NON FREE)"));
78  myServer.append(" ");
79  }
80  else if (server.freeType == ServerFreeType::Unknown)
81  {
82  // TRANSLATORS: server license comment
83  myServer.append(_("(UNKNOWN)"));
84  myServer.append(" ");
85  }
86  myServer.append(server.hostname);
87  return myServer;
88  }
89 
93  const ServerInfo &getServer(const int elementIndex) const A_WARN_UNUSED
94  { 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  {
108  gui->getFont()->getWidth(version), version);
109  }
110  }
111 
112  private:
113  typedef STD_VECTOR<VersionString> VersionStrings;
114 
118 };
119 
120 #endif // GUI_MODELS_SERVERSLISTMODEL_H
#define CAST_S32
Definition: cast.h:30
int getWidth(const std::string &text) const
Definition: font.cpp:334
Font * getFont() const
Definition: gui.h:160
MutexLocker lock()
Definition: serverdialog.h:104
std::string hostname
Definition: serverinfo.h:45
ServerFreeTypeT freeType
Definition: serverinfo.h:43
ServersListModel(ServerInfos *const servers, ServerDialog *const parent)
const ServerInfo & getServer(const int elementIndex) const
void setVersionString(const int index, const std::string &version)
VersionStrings mVersionStrings
ServerInfos * mServers
std::vector< VersionString > VersionStrings
std::string getElementAt(int elementIndex)
ServerDialog * mParent
std::pair< int, std::string > VersionString
#define _(s)
Definition: gettext.h:35
Gui * gui
Definition: gui.cpp:111
#define override
Definition: localconsts.h:47
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
#define nullptr
Definition: localconsts.h:45
int size()
Definition: emotedb.cpp:306
std::vector< ServerInfo > ServerInfos
Definition: serverinfo.h:227