ManaPlus
setup_mods.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009 The Mana World Development Team
4  * Copyright (C) 2011-2019 The ManaPlus Developers
5  * Copyright (C) 2009-2021 Andrei Karas
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 
24 
25 #include "configuration.h"
26 
29 #include "gui/widgets/setupitem.h"
30 #include "gui/widgets/scrollarea.h"
31 
32 #include "resources/db/moddb.h"
33 
34 #include "utils/foreach.h"
35 #include "utils/gettext.h"
36 
37 #include "debug.h"
38 
39 Setup_Mods::Setup_Mods(const Widget2 *const widget) :
40  SetupTabScroll(widget)
41 {
42  // TRANSLATORS: mods tab in settings
43  setName(_("Mods"));
44 
45  LayoutHelper h(this);
46  ContainerPlacer place = h.getPlacer(0, 0);
47  place(0, 0, mScroll, 10, 10);
48 
49  setDimension(Rect(0, 0, 550, 350));
50 }
51 
53 {
54 }
55 
57 {
59  saveMods();
60 }
61 
63 {
64  clear();
65  loadMods();
66 }
67 
69 {
70  std::string modsString = serverConfig.getValue("mods", "");
71  std::set<std::string> modsList;
72  splitToStringSet(modsList, modsString, '|');
73 
74  const ModInfos &mods = ModDB::getAll();
75  if (mods.empty())
76  {
77  // TRANSLATORS: settings label
78  new SetupItemLabel(_("No mods present"), "", this,
80  return;
81  }
82 
83  FOR_EACH (ModInfoCIterator, it, mods)
84  {
85  const ModInfo *const info = (*it).second;
86  if (info == nullptr)
87  continue;
88 
89  std::string name = info->getName();
90  replaceAll(name, "|", "");
91  SetupItem *const item = new SetupItemCheckBox(
92  info->getDescription(), "", "", this, name,
94  if (modsList.find(name) != modsList.end())
95  item->setValue("1");
96  else
97  item->setValue("0");
98  item->toWidget();
99  }
100 }
101 
103 {
104  const ModInfos &mods = ModDB::getAll();
105  if (mods.empty())
106  return;
107 
108  std::string modsString;
109  const std::set<SetupItem*> &modsList = getAllItems();
110  FOR_EACH (std::set<SetupItem*>::const_iterator, it, modsList)
111  {
112  const SetupItem *const item = *it;
113  if (item == nullptr)
114  continue;
115  const std::string val = item->getValue();
116  if (val == "1")
117  {
118  const std::string key = item->getEventName();
119  if (!modsString.empty())
120  modsString.append("|");
121  modsString.append(key);
122  }
123  }
124  serverConfig.setValue("mods", modsString);
125 }
126 
128 {
129  clear();
130 }
std::string getValue(const std::string &key, const std::string &deflt) const
void setValue(const std::string &key, const std::string &value)
ContainerPlacer getPlacer(const int x, const int y)
Definition: rect.h:74
void setValue(const std::string &str)
Definition: setupitem.h:104
std::string getValue() const
Definition: setupitem.h:107
std::string getEventName() const
Definition: setupitem.h:110
virtual void toWidget()=0
ScrollArea * mScroll
const std::set< SetupItem * > & getAllItems() const
void setName(const std::string &name)
Definition: setuptab.h:68
void saveMods() const
Definition: setup_mods.cpp:102
void externalUnloaded()
Definition: setup_mods.cpp:127
Setup_Mods(const Widget2 *const widget)
Definition: setup_mods.cpp:39
void apply()
Definition: setup_mods.cpp:56
void loadMods()
Definition: setup_mods.cpp:68
void externalUpdated()
Definition: setup_mods.cpp:62
void setDimension(const Rect &dimension)
Definition: widget.cpp:169
Configuration serverConfig
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define _(s)
Definition: gettext.h:35
const bool MainConfig_true
Definition: mainconfig.h:30
ModInfos::const_iterator ModInfoCIterator
Definition: modinfo.h:70
std::map< std::string, ModInfo * > ModInfos
Definition: modinfo.h:68
bool info(InputEvent &event)
Definition: commands.cpp:57
const ModInfos & getAll()
Definition: moddb.cpp:116
const bool Separator_false
Definition: separator.h:30
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)
void splitToStringSet(std::set< std::string > &tokens, const std::string &text, const char separator)