GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/serverinfowindow.cpp Lines: 62 89 69.7 %
Date: 2021-03-17 Branches: 38 134 28.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2017-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "gui/windows/serverinfowindow.h"
23
24
#include "enums/gui/layouttype.h"
25
26
#include "gui/gui.h"
27
28
#include "gui/widgets/layout.h"
29
#include "gui/widgets/scrollarea.h"
30
#include "gui/widgets/staticbrowserbox.h"
31
32
#include "utils/foreach.h"
33
#include "utils/gettext.h"
34
#include "utils/process.h"
35
36
#include "debug.h"
37
38
ServerInfoWindow *serverInfoWindow = nullptr;
39
40
1
ServerInfoWindow::ServerInfoWindow(ServerInfo &serverInfo) :
41
    // TRANSLATORS: servers dialog name
42
1
    Window(_("Server info"), Modal_false, nullptr, "serverinfo.xml"),
43
    LinkHandler(),
44
    mServerInfo(serverInfo),
45
    mBrowserBox(new StaticBrowserBox(this, Opaque_true,
46

1
        "browserbox.xml")),
47
1
    mScrollArea(new ScrollArea(this, mBrowserBox,
48



16
        Opaque_true, "serverinfo_background.xml"))
49
{
50
1
    setMinWidth(300);
51
1
    setMinHeight(220);
52
1
    setContentSize(455, 350);
53
5
    setWindowName("ServerInfoWindow");
54
1
    setCloseButton(true);
55
1
    setResizable(true);
56
1
    setStickyButtonLock(true);
57
58
1
    setDefaultSize(500, 400, ImagePosition::CENTER, 0, 0);
59
60
2
    mBrowserBox->setOpaque(Opaque_false);
61
1
    mBrowserBox->setLinkHandler(this);
62
1
    if (gui != nullptr)
63
2
        mBrowserBox->setFont(gui->getHelpFont());
64
2
    mBrowserBox->setProcessVars(true);
65
2
    mBrowserBox->setEnableTabs(true);
66
67
2
    place(0, 0, mScrollArea, 5, 3).setPadding(3);
68
69
1
    Layout &layout = getLayout();
70
1
    layout.setRowHeight(0, LayoutType::SET);
71
72
1
    loadWindowState();
73
2
    enableVisibleSound(true);
74
1
    widgetResized(Event(nullptr));
75
1
}
76
77
1
void ServerInfoWindow::postInit()
78
{
79
1
    Window::postInit();
80
1
    setVisible(Visible_true);
81
1
    showServerInfo();
82
1
}
83
84
3
ServerInfoWindow::~ServerInfoWindow()
85
{
86
2
}
87
88
void ServerInfoWindow::handleLink(const std::string &link,
89
                                  MouseEvent *const event A_UNUSED)
90
{
91
    if (strStartWith(link, "http://") ||
92
        strStartWith(link, "https://"))
93
    {
94
        openBrowser(link);
95
    }
96
}
97
98
1
void ServerInfoWindow::showServerInfo()
99
{
100
1
    const ServerFreeTypeT type = mServerInfo.freeType;
101
1
    if (type == ServerFreeType::Free)
102
    {
103
        // TRANSLATORS: server info comment
104
        mBrowserBox->addRow(_("##BServer with free license."),
105
            false);
106
    }
107
1
    else if (type == ServerFreeType::NonFree)
108
    {
109
        // TRANSLATORS: server info comment
110
        mBrowserBox->addRow(_("##BServer with non free license."),
111
            false);
112
    }
113
1
    else if (type == ServerFreeType::Unknown ||
114
             type == ServerFreeType::NotSet)
115
    {
116
        // TRANSLATORS: server info comment
117
4
        mBrowserBox->addRow(_("##BServer unknown license."),
118
1
            false);
119
    }
120
4
    mBrowserBox->addRow("", false);
121
4
    addServerComment(mServerInfo.serverUrl,
122
        // TRANSLATORS: server info comment
123
2
        _("Server"));
124
4
    addServerComment(mServerInfo.supportUrl,
125
        // TRANSLATORS: server info comment
126
2
        _("Support"));
127
4
    addServerComment(mServerInfo.registerUrl,
128
        // TRANSLATORS: server info comment
129
2
        _("Register"));
130
131
4
    addSourcesList(mServerInfo.nonFreeSources,
132
        // TRANSLATORS: server info non free comment
133
2
        _("##BNon free sources"));
134
4
    addSourcesList(mServerInfo.freeSources,
135
        // TRANSLATORS: server info free comment
136
2
        _("##BFree sources"));
137
4
    addSourcesList(mServerInfo.docs,
138
        // TRANSLATORS: server info documentation comment
139
2
        _("##BDocumentation"));
140
1
    mBrowserBox->updateHeight();
141
1
}
142
143
3
void ServerInfoWindow::addSourcesList(const STD_VECTOR<ServerUrlInfo> &list,
144
                                      const std::string &comment)
145
{
146
3
    if (!list.empty())
147
    {
148
        mBrowserBox->addRow("", false);
149
        mBrowserBox->addRow(comment, false);
150
        FOR_EACH (STD_VECTOR<ServerUrlInfo>::const_iterator, it, list)
151
        {
152
            const ServerUrlInfo &info = *it;
153
            addServerComment(info.url,
154
                info.name);
155
        }
156
    }
157
3
}
158
159
3
void ServerInfoWindow::addServerComment(const std::string &url,
160
                                        const std::string &comment)
161
{
162
6
    std::string str;
163
3
    if (!url.empty())
164
    {
165
        if (strStartWith(url, "http://") ||
166
            strStartWith(url, "https://"))
167
        {
168
            str = strprintf("%s: @@%s|@@##0",
169
                comment.c_str(),
170
                url.c_str());
171
        }
172
        else
173
        {
174
            str = strprintf("%s: %s",
175
                comment.c_str(),
176
                url.c_str());
177
        }
178
        mBrowserBox->addRow(str,
179
            false);
180
    }
181
3
}
182
183
void ServerInfoWindow::close()
184
{
185
    Window::close();
186
    if (serverInfoWindow == this)
187
        serverInfoWindow = nullptr;
188
    scheduleDelete();
189
}