GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/helpwindow.cpp Lines: 50 104 48.1 %
Date: 2017-11-29 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-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/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

8
    mDYKButton(new Button(this, _("Did you know..."), "DYK", this)),
62
    mBrowserBox(new StaticBrowserBox(this, Opaque_true,
63

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

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



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

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

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

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

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

6
}