GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/helpwindow.cpp Lines: 51 107 47.7 %
Date: 2018-09-20 Branches: 50 196 25.5 %

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-2018  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/windows/helpwindow.h"
24
25
#include "configuration.h"
26
27
#include "enums/gui/layouttype.h"
28
29
#include "fs/paths.h"
30
31
#include "fs/virtfs/tools.h"
32
33
#include "gui/gui.h"
34
35
#include "gui/windows/setupwindow.h"
36
37
#include "gui/widgets/button.h"
38
#include "gui/widgets/staticbrowserbox.h"
39
#include "gui/widgets/layout.h"
40
#include "gui/widgets/scrollarea.h"
41
42
#include "input/inputmanager.h"
43
44
#include "utils/foreach.h"
45
#include "utils/gettext.h"
46
#include "utils/process.h"
47
48
#include "utils/translation/podict.h"
49
#include "utils/translation/translationmanager.h"
50
51
#include "debug.h"
52
53
HelpWindow *helpWindow = nullptr;
54
55
2
HelpWindow::HelpWindow() :
56
    // TRANSLATORS: help window name
57
2
    Window(_("Help"), Modal_false, nullptr, "help.xml"),
58
    LinkHandler(),
59
    ActionListener(),
60
    // TRANSLATORS: help window. button.
61
2
    mDYKButton(new Button(this, _("Did you know..."), "DYK",
62

2
        BUTTON_SKIN, this)),
63
    mBrowserBox(new StaticBrowserBox(this, Opaque_true,
64

2
        "browserbox.xml")),
65
2
    mScrollArea(new ScrollArea(this, mBrowserBox,
66

4
        Opaque_true, "help_background.xml")),
67



50
    mTagFileMap()
68
{
69
2
    setMinWidth(300);
70
2
    setMinHeight(220);
71
2
    setContentSize(455, 350);
72
10
    setWindowName("Help");
73
2
    setCloseButton(true);
74
2
    setResizable(true);
75
2
    setStickyButtonLock(true);
76
77
2
    if (setupWindow != nullptr)
78
1
        setupWindow->registerWindowForReset(this);
79
80
2
    setDefaultSize(500, 400, ImagePosition::CENTER, 0, 0);
81
82
4
    mBrowserBox->setOpaque(Opaque_false);
83
84
2
    mBrowserBox->setLinkHandler(this);
85
2
    if (gui != nullptr)
86
4
        mBrowserBox->setFont(gui->getHelpFont());
87
4
    mBrowserBox->setProcessVars(true);
88
4
    mBrowserBox->setEnableImages(true);
89
4
    mBrowserBox->setEnableKeys(true);
90
4
    mBrowserBox->setEnableTabs(true);
91
92
2
    place(4, 3, mDYKButton, 1, 1);
93
4
    place(0, 0, mScrollArea, 5, 3).setPadding(3);
94
95
2
    Layout &layout = getLayout();
96
2
    layout.setRowHeight(0, LayoutType::SET);
97
98
2
    loadWindowState();
99
2
    loadTags();
100
4
    enableVisibleSound(true);
101
2
    widgetResized(Event(nullptr));
102
2
}
103
104
void HelpWindow::action(const ActionEvent &event)
105
{
106
    if (event.getId() == "DYK")
107
        inputManager.executeAction(InputAction::WINDOW_DIDYOUKNOW);
108
}
109
110
void HelpWindow::handleLink(const std::string &link,
111
                            MouseEvent *const event A_UNUSED)
112
{
113
    if (!strStartWith(link, "http://") && !strStartWith(link, "https://"))
114
    {
115
        // need use separate variable
116
        const std::string helpFile = link;  // NOLINT
117
        loadHelp(helpFile);
118
    }
119
    else
120
    {
121
        openBrowser(link);
122
    }
123
}
124
125
void HelpWindow::loadHelp(const std::string &helpFile)
126
{
127
    if (!checkPath(helpFile))
128
        return;
129
    mBrowserBox->clearRows();
130
    loadFile("header");
131
    loadFile(helpFile);
132
    loadFile("footer");
133
    mScrollArea->setVerticalScrollAmount(0);
134
    mBrowserBox->updateHeight();
135
    setVisible(Visible_true);
136
}
137
138
void HelpWindow::loadHelpSimple(const std::string &helpFile)
139
{
140
    if (!checkPath(helpFile))
141
        return;
142
    mBrowserBox->clearRows();
143
    loadFile(helpFile);
144
    mScrollArea->setVerticalScrollAmount(0);
145
    mBrowserBox->updateHeight();
146
    setVisible(Visible_true);
147
}
148
149
void HelpWindow::loadFile(std::string file)
150
{
151
    trim(file);
152
    std::string helpPath = branding.getStringValue("helpPath");
153
    if (helpPath.empty())
154
        helpPath = paths.getStringValue("help");
155
156
    StringVect lines;
157
    TranslationManager::translateFile(pathJoin(helpPath, file).append(".txt"),
158
        translator, lines);
159
160
    for (size_t i = 0, sz = lines.size(); i < sz; ++i)
161
        mBrowserBox->addRow(lines[i], false);
162
}
163
164
2
void HelpWindow::loadTags()
165
{
166
10
    std::string helpPath = branding.getStringValue("tagsPath");
167
2
    if (helpPath.empty())
168

10
        helpPath = paths.getStringValue("tags");
169
170
4
    StringVect filesVect;
171

8
    VirtFs::getFilesInDir(helpPath, filesVect, ".idx");
172
34
    FOR_EACH (StringVectCIter, itVect, filesVect)
173
    {
174
48
        StringVect lines;
175
24
        VirtFs::loadTextFile(*itVect, lines);
176
166
        FOR_EACH (StringVectCIter, it, lines)
177
        {
178
46
            const std::string &str = *it;
179
46
            const size_t idx = str.find('|');
180
46
            if (idx != std::string::npos)
181

184
                mTagFileMap[str.substr(idx + 1)].insert(str.substr(0, idx));
182
        }
183
    }
184
2
}
185
186
void HelpWindow::search(const std::string &text0)
187
{
188
    std::string text = text0;
189
    trim(text);
190
    toLower(text);
191
    if (mTagFileMap.find(text) == mTagFileMap.end())
192
    {
193
        loadHelp("searchnotfound");
194
    }
195
    else
196
    {
197
        const HelpNames &names = mTagFileMap[text];
198
        if (names.size() == 1)
199
        {
200
            loadHelp(*names.begin());
201
        }
202
        else
203
        {
204
            if (translator == nullptr)
205
                return;
206
            mBrowserBox->clearRows();
207
            loadFile("header");
208
            loadFile("searchmany");
209
            FOR_EACH (HelpNamesCIter, it, names)
210
            {
211
                const char *const str = (*it).c_str();
212
                mBrowserBox->addRow(strprintf(" -> @@%s|%[email protected]@", str,
213
                    translator->getChar(str)),
214
                    false);
215
            }
216
            loadFile("footer");
217
            mScrollArea->setVerticalScrollAmount(0);
218
            mBrowserBox->updateHeight();
219
            setVisible(Visible_true);
220
        }
221
    }
222

3
}