GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/setup_mods.cpp Lines: 0 53 0.0 %
Date: 2017-11-29 Branches: 0 72 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  Andrei Karas
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
#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
        if (modsList.find(name) != modsList.end())
94
            item->setValue("1");
95
        else
96
            item->setValue("0");
97
        item->toWidget();
98
    }
99
}
100
101
void Setup_Mods::saveMods() const
102
{
103
    const ModInfos &mods = ModDB::getAll();
104
    if (mods.empty())
105
        return;
106
107
    std::string modsString;
108
    const std::set<SetupItem*> &modsList = getAllItems();
109
    FOR_EACH (std::set<SetupItem*>::const_iterator, it, modsList)
110
    {
111
        const SetupItem *const item = *it;
112
        if (item == nullptr)
113
            continue;
114
        const std::string val = item->getValue();
115
        if (val == "1")
116
        {
117
            const std::string key = item->getEventName();
118
            if (!modsString.empty())
119
                modsString.append("|");
120
            modsString.append(key);
121
        }
122
    }
123
    serverConfig.setValue("mods", modsString);
124
}
125
126
void Setup_Mods::externalUnloaded()
127
{
128
    clear();
129
}