GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/itemlinkhandler.cpp Lines: 7 79 8.9 %
Date: 2021-03-17 Branches: 2 138 1.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "gui/widgets/itemlinkhandler.h"
25
26
#include "itemcolormanager.h"
27
#include "settings.h"
28
29
#include "const/sound.h"
30
31
#include "gui/viewport.h"
32
33
#include "gui/popups/itempopup.h"
34
#include "gui/popups/popupmenu.h"
35
36
#include "gui/widgets/createwidget.h"
37
38
#include "gui/windows/confirmdialog.h"
39
#include "gui/windows/helpwindow.h"
40
#include "gui/windows/questswindow.h"
41
42
#include "input/inputmanager.h"
43
44
#include "utils/gettext.h"
45
#include "utils/stringutils.h"
46
47
#include "resources/db/itemdb.h"
48
49
#include "listeners/inputactionremotelistener.h"
50
#include "listeners/openurllistener.h"
51
52
#include "debug.h"
53
54
namespace
55
{
56
1
    OpenUrlListener listener;
57
}  // namespace
58
59
7
ItemLinkHandler::ItemLinkHandler() :
60
    LinkHandler(),
61
14
    mAllowCommands(true)
62
{
63
7
}
64
65
7
ItemLinkHandler::~ItemLinkHandler()
66
{
67
7
}
68
69
void ItemLinkHandler::handleCommandLink(const std::string &link,
70
                                        const std::string &prefix)
71
{
72
    std::string cmd;
73
    std::string args;
74
75
    const std::string cmdStr = link.substr(prefix.size());
76
    if (!parse2Str(cmdStr, cmd, args))
77
    {
78
        cmd = cmdStr;
79
        args.clear();
80
    }
81
    if (mAllowCommands)
82
    {
83
        inputManager.executeRemoteChatCommand(cmd, args, nullptr);
84
    }
85
    else
86
    {
87
        inputActionRemoteListener.setCommand(cmd, args);
88
        ConfirmDialog *const confirmDlg = CREATEWIDGETR(ConfirmDialog,
89
            // TRANSLATORS: dialog message
90
            _("Run command"),
91
            strprintf("/%s %s", cmd.c_str(), args.c_str()),
92
            SOUND_REQUEST,
93
            false,
94
            Modal_true,
95
            nullptr);
96
        confirmDlg->addActionListener(&inputActionRemoteListener);
97
    }
98
}
99
100
void ItemLinkHandler::handleHelpLink(const std::string &link)
101
{
102
    if (helpWindow != nullptr)
103
    {
104
        helpWindow->loadHelp(link.substr(7));
105
        helpWindow->requestMoveToTop();
106
    }
107
}
108
109
void ItemLinkHandler::handleHttpLink(const std::string &link,
110
                                     const MouseEvent *const event)
111
{
112
    if (event == nullptr)
113
        return;
114
    std::string url = link;
115
    replaceAll(url, " ", "");
116
    listener.url = url;
117
    const MouseButtonT button = event->getButton();
118
    if (button == MouseButton::LEFT)
119
    {
120
        ConfirmDialog *const confirmDlg = CREATEWIDGETR(ConfirmDialog,
121
            // TRANSLATORS: dialog message
122
            _("Open url"),
123
            url,
124
            SOUND_REQUEST,
125
            false,
126
            Modal_true,
127
            nullptr);
128
        confirmDlg->addActionListener(&listener);
129
    }
130
    else if (button == MouseButton::RIGHT)
131
    {
132
        if (popupMenu != nullptr)
133
            popupMenu->showLinkPopup(url);
134
    }
135
}
136
137
void ItemLinkHandler::handleItemLink(const std::string &link)
138
{
139
    if ((itemPopup == nullptr) || link.empty())
140
        return;
141
142
    const char ch = link[0];
143
    if (ch < '0' || ch > '9')
144
        return;
145
146
    STD_VECTOR<int> str;
147
    splitToIntVector(str, link, ',');
148
    if (str.empty())
149
        return;
150
151
    const int id = str[0];
152
153
    if (id > 0)
154
    {
155
        str.erase(str.begin());
156
        while (str.size() < maxCards)
157
            str.push_back(0);
158
        const ItemColor color =
159
            ItemColorManager::getColorFromCards(&str[0]);
160
161
        const ItemInfo &itemInfo = ItemDB::get(id);
162
        // +++ need add support for options.
163
        itemPopup->setItem(itemInfo, color, true, -1, &str[0], nullptr);
164
        if (itemPopup->isPopupVisible())
165
        {
166
            itemPopup->setVisible(Visible_false);
167
        }
168
        else if (viewport != nullptr)
169
        {
170
            itemPopup->position(viewport->mMouseX,
171
                viewport->mMouseY);
172
        }
173
    }
174
}
175
176
void ItemLinkHandler::handleSearchLink(const std::string &link)
177
{
178
    if (helpWindow != nullptr)
179
    {
180
        helpWindow->search(link.substr(1));
181
        helpWindow->requestMoveToTop();
182
    }
183
}
184
185
void ItemLinkHandler::handleLink(const std::string &link,
186
                                 MouseEvent *const event)
187
{
188
    if (link.empty())
189
        return;
190
191
    if (strStartWith(link, "http://") || strStartWith(link, "https://"))
192
    {
193
        handleHttpLink(link, event);
194
    }
195
    else if (link[0] == '?')
196
    {
197
        handleSearchLink(link);
198
    }
199
    else if (strStartWith(link, "help://"))
200
    {
201
        handleHelpLink(link);
202
    }
203
    else if (strStartWith(link, settings.linkCommandSymbol))
204
    {
205
        handleCommandLink(link, settings.linkCommandSymbol);
206
    }
207
    else if (strStartWith(link, "="))
208
    {
209
        handleCommandLink(link, "=");
210
    }
211
    else if (link == "news")
212
    {
213
        if (helpWindow != nullptr)
214
            helpWindow->loadHelpSimple("news");
215
    }
216
    else if (link == "copyright")
217
    {
218
        inputManager.executeAction(InputAction::WINDOW_ABOUT);
219
    }
220
    else if (link[0] == 'q')
221
    {
222
        questsWindow->selectQuest(atoi(link.substr(1).c_str()));
223
    }
224
    else
225
    {
226
        handleItemLink(link);
227
    }
228

3
}