GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/setup_mods.cpp Lines: 0 53 0.0 %
Date: 2021-03-17 Branches: 0 70 0.0 %

Line Branch Exec Source
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
23
#include "gui/widgets/tabs/setup_mods.h"
24
25
#include "configuration.h"
26
27
#include "gui/widgets/containerplacer.h"
28
#include "gui/widgets/layouthelper.h"
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
52
Setup_Mods::~Setup_Mods()
53
{
54
}
55
56
void Setup_Mods::apply()
57
{
58
    SetupTabScroll::apply();
59
    saveMods();
60
}
61
62
void Setup_Mods::externalUpdated()
63
{
64
    clear();
65
    loadMods();
66
}
67
68
void Setup_Mods::loadMods()
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,
79
            Separator_false);
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,
93
            MainConfig_true);
94
        if (modsList.find(name) != modsList.end())
95
            item->setValue("1");
96
        else
97
            item->setValue("0");
98
        item->toWidget();
99
    }
100
}
101
102
void Setup_Mods::saveMods() const
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
127
void Setup_Mods::externalUnloaded()
128
{
129
    clear();
130
}