GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/helpwindow.cpp Lines: 51 105 48.6 %
Date: 2018-05-19 03:07:18 Branches: 52 206 25.2 %

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
4
HelpWindow::HelpWindow() :
56
    // TRANSLATORS: help window name
57
4
    Window(_("Help"), Modal_false, nullptr, "help.xml"),
58
    LinkHandler(),
59
    ActionListener(),
60
    // TRANSLATORS: help window. button.
61
4
    mDYKButton(new Button(this, _("Did you know..."), "DYK",
62

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

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

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



100
    mTagFileMap()
68
{
69
4
    setMinWidth(300);
70
4
    setMinHeight(220);
71
4
    setContentSize(455, 350);
72
20
    setWindowName("Help");
73
4
    setCloseButton(true);
74
4
    setResizable(true);
75
4
    setStickyButtonLock(true);
76
77
4
    if (setupWindow != nullptr)
78
2
        setupWindow->registerWindowForReset(this);
79
80
4
    setDefaultSize(500, 400, ImagePosition::CENTER, 0, 0);
81
82
8
    mBrowserBox->setOpaque(Opaque_false);
83
84
4
    mBrowserBox->setLinkHandler(this);
85
4
    if (gui != nullptr)
86
8
        mBrowserBox->setFont(gui->getHelpFont());
87
8
    mBrowserBox->setProcessVars(true);
88
8
    mBrowserBox->setEnableImages(true);
89
8
    mBrowserBox->setEnableKeys(true);
90
8
    mBrowserBox->setEnableTabs(true);
91
92
4
    place(4, 3, mDYKButton, 1, 1);
93
8
    place(0, 0, mScrollArea, 5, 3).setPadding(3);
94
95
4
    Layout &layout = getLayout();
96
4
    layout.setRowHeight(0, LayoutType::SET);
97
98
4
    loadWindowState();
99
4
    loadTags();
100
8
    enableVisibleSound(true);
101
8
    widgetResized(Event(nullptr));
102
4
}
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
4
void HelpWindow::loadTags()
165
{
166

20
    std::string helpPath = branding.getStringValue("tagsPath");
167
4
    if (helpPath.empty())
168

20
        helpPath = paths.getStringValue("tags");
169
170
8
    StringVect filesVect;
171

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

368
                mTagFileMap[str.substr(idx + 1)].insert(str.substr(0, idx));
182
        }
183
    }
184
4
}
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

6
}