GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/serverinfowindow.cpp Lines: 61 82 74.4 %
Date: 2017-11-29 Branches: 47 158 29.7 %

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

2
        "browserbox.xml")),
46
2
    mScrollArea(new ScrollArea(this, mBrowserBox,
47




32
        Opaque_true, "serverinfo_background.xml"))
48
{
49
2
    setMinWidth(300);
50
2
    setMinHeight(220);
51
2
    setContentSize(455, 350);
52
10
    setWindowName("ServerInfoWindow");
53
2
    setCloseButton(true);
54
2
    setResizable(true);
55
2
    setStickyButtonLock(true);
56
57
2
    setDefaultSize(500, 400, ImagePosition::CENTER);
58
59
4
    mBrowserBox->setOpaque(Opaque_false);
60
2
    mBrowserBox->setLinkHandler(this);
61
2
    if (gui != nullptr)
62
4
        mBrowserBox->setFont(gui->getHelpFont());
63
4
    mBrowserBox->setProcessVars(true);
64
4
    mBrowserBox->setEnableTabs(true);
65
66
4
    place(0, 0, mScrollArea, 5, 3).setPadding(3);
67
68
2
    Layout &layout = getLayout();
69
2
    layout.setRowHeight(0, LayoutType::SET);
70
71
2
    loadWindowState();
72
4
    enableVisibleSound(true);
73
4
    widgetResized(Event(nullptr));
74
2
}
75
76
2
void ServerInfoWindow::postInit()
77
{
78
2
    Window::postInit();
79
2
    setVisible(Visible_true);
80
2
    showServerInfo();
81
2
}
82
83
6
ServerInfoWindow::~ServerInfoWindow()
84
{
85
4
}
86
87
void ServerInfoWindow::handleLink(const std::string &link,
88
                                  MouseEvent *const event A_UNUSED)
89
{
90
    if (strStartWith(link, "http://") ||
91
        strStartWith(link, "https://"))
92
    {
93
        openBrowser(link);
94
    }
95
}
96
97
2
void ServerInfoWindow::showServerInfo()
98
{
99
2
    const ServerFreeTypeT type = mServerInfo.freeType;
100
2
    if (type == ServerFreeType::Free)
101
    {
102
        // TRANSLATORS: server info comment
103
        mBrowserBox->addRow(_("##BServer with free license."));
104
    }
105
2
    else if (type == ServerFreeType::NonFree)
106
    {
107
        // TRANSLATORS: server info comment
108
        mBrowserBox->addRow(_("##BServer with non free license."));
109
    }
110
2
    else if (type == ServerFreeType::Unknown ||
111
             type == ServerFreeType::NotSet)
112
    {
113
        // TRANSLATORS: server info comment
114

8
        mBrowserBox->addRow(_("##BServer unknown license."));
115
    }
116

8
    mBrowserBox->addRow("");
117

8
    addServerComment(mServerInfo.serverUrl,
118
        // TRANSLATORS: server info comment
119
2
        _("Server"));
120

8
    addServerComment(mServerInfo.supportUrl,
121
        // TRANSLATORS: server info comment
122
2
        _("Support"));
123

8
    addServerComment(mServerInfo.registerUrl,
124
        // TRANSLATORS: server info comment
125
2
        _("Register"));
126
127

8
    addSourcesList(mServerInfo.nonFreeSources,
128
        // TRANSLATORS: server info non free comment
129
2
        _("##BNon free sources"));
130

8
    addSourcesList(mServerInfo.freeSources,
131
        // TRANSLATORS: server info non free comment
132
2
        _("##BFree sources"));
133

8
    addSourcesList(mServerInfo.docs,
134
        // TRANSLATORS: server info documentation comment
135
2
        _("##BDocumentation"));
136
2
    mBrowserBox->updateHeight();
137
2
}
138
139
6
void ServerInfoWindow::addSourcesList(const STD_VECTOR<ServerUrlInfo> &list,
140
                                      const std::string &comment)
141
{
142
6
    if (!list.empty())
143
    {
144
        mBrowserBox->addRow("");
145
        mBrowserBox->addRow(comment);
146
        FOR_EACH (STD_VECTOR<ServerUrlInfo>::const_iterator, it, list)
147
        {
148
            const ServerUrlInfo &info = *it;
149
            addServerComment(info.url,
150
                info.name);
151
        }
152
    }
153
6
}
154
155
6
void ServerInfoWindow::addServerComment(const std::string &url,
156
                                        const std::string &comment)
157
{
158
12
    std::string str;
159
6
    if (!url.empty())
160
    {
161
        if (strStartWith(url, "http://") ||
162
            strStartWith(url, "https://"))
163
        {
164
            str = strprintf("%s: @@%s|@@##0",
165
                comment.c_str(),
166
                url.c_str());
167
        }
168
        else
169
        {
170
            str = strprintf("%s: %s",
171
                comment.c_str(),
172
                url.c_str());
173
        }
174
        mBrowserBox->addRow(str);
175
    }
176
6
}
177
178
void ServerInfoWindow::close()
179
{
180
    Window::close();
181
    if (serverInfoWindow == this)
182
        serverInfoWindow = nullptr;
183
    scheduleDelete();
184
}