GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/itemlinkhandler.cpp Lines: 7 78 9.0 %
Date: 2018-06-18 21:15:20 Branches: 2 144 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-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/widgets/itemlinkhandler.h"
24
25
#include "itemcolormanager.h"
26
#include "settings.h"
27
28
#include "const/sound.h"
29
30
#include "gui/viewport.h"
31
32
#include "gui/popups/itempopup.h"
33
#include "gui/popups/popupmenu.h"
34
35
#include "gui/widgets/createwidget.h"
36
37
#include "gui/windows/confirmdialog.h"
38
#include "gui/windows/helpwindow.h"
39
#include "gui/windows/questswindow.h"
40
41
#include "input/inputmanager.h"
42
43
#include "utils/gettext.h"
44
#include "utils/stringutils.h"
45
46
#include "resources/db/itemdb.h"
47
48
#include "listeners/inputactionremotelistener.h"
49
#include "listeners/openurllistener.h"
50
51
#include "debug.h"
52
53
namespace
54
{
55
2
    OpenUrlListener listener;
56
}  // namespace
57
58
14
ItemLinkHandler::ItemLinkHandler() :
59
    LinkHandler(),
60
28
    mAllowCommands(true)
61
{
62
14
}
63
64
14
ItemLinkHandler::~ItemLinkHandler()
65
{
66
14
}
67
68
void ItemLinkHandler::handleCommandLink(const std::string &link,
69
                                        const std::string &prefix)
70
{
71
    std::string cmd;
72
    std::string args;
73
74
    const std::string cmdStr = link.substr(prefix.size());
75
    if (!parse2Str(cmdStr, cmd, args))
76
    {
77
        cmd = cmdStr;
78
        args.clear();
79
    }
80
    if (mAllowCommands)
81
    {
82
        inputManager.executeRemoteChatCommand(cmd, args, nullptr);
83
    }
84
    else
85
    {
86
        inputActionRemoteListener.setCommand(cmd, args);
87
        ConfirmDialog *const confirmDlg = CREATEWIDGETR(ConfirmDialog,
88
            // TRANSLATORS: dialog message
89
            _("Run command"),
90
            strprintf("/%s %s", cmd.c_str(), args.c_str()),
91
            SOUND_REQUEST,
92
            false,
93
            Modal_true,
94
            nullptr);
95
        confirmDlg->addActionListener(&inputActionRemoteListener);
96
    }
97
}
98
99
void ItemLinkHandler::handleHelpLink(const std::string &link)
100
{
101
    if (helpWindow != nullptr)
102
    {
103
        helpWindow->loadHelp(link.substr(7));
104
        helpWindow->requestMoveToTop();
105
    }
106
}
107
108
void ItemLinkHandler::handleHttpLink(const std::string &link,
109
                                     const MouseEvent *const event)
110
{
111
    if (event == nullptr)
112
        return;
113
    std::string url = link;
114
    replaceAll(url, " ", "");
115
    listener.url = url;
116
    const MouseButtonT button = event->getButton();
117
    if (button == MouseButton::LEFT)
118
    {
119
        ConfirmDialog *const confirmDlg = CREATEWIDGETR(ConfirmDialog,
120
            // TRANSLATORS: dialog message
121
            _("Open url"),
122
            url,
123
            SOUND_REQUEST,
124
            false,
125
            Modal_true,
126
            nullptr);
127
        confirmDlg->addActionListener(&listener);
128
    }
129
    else if (button == MouseButton::RIGHT)
130
    {
131
        if (popupMenu != nullptr)
132
            popupMenu->showLinkPopup(url);
133
    }
134
}
135
136
void ItemLinkHandler::handleItemLink(const std::string &link)
137
{
138
    if ((itemPopup == nullptr) || link.empty())
139
        return;
140
141
    const char ch = link[0];
142
    if (ch < '0' || ch > '9')
143
        return;
144
145
    STD_VECTOR<int> str;
146
    splitToIntVector(str, link, ',');
147
    if (str.empty())
148
        return;
149
150
    const int id = str[0];
151
152
    if (id > 0)
153
    {
154
        str.erase(str.begin());
155
        while (str.size() < maxCards)
156
            str.push_back(0);
157
        const ItemColor color =
158
            ItemColorManager::getColorFromCards(&str[0]);
159
160
        const ItemInfo &itemInfo = ItemDB::get(id);
161
        // +++ need add support for options.
162
        itemPopup->setItem(itemInfo, color, true, -1, &str[0], nullptr);
163
        if (itemPopup->isPopupVisible())
164
        {
165
            itemPopup->setVisible(Visible_false);
166
        }
167
        else if (viewport != nullptr)
168
        {
169
            itemPopup->position(viewport->mMouseX,
170
                viewport->mMouseY);
171
        }
172
    }
173
}
174
175
void ItemLinkHandler::handleSearchLink(const std::string &link)
176
{
177
    if (helpWindow != nullptr)
178
    {
179
        helpWindow->search(link.substr(1));
180
        helpWindow->requestMoveToTop();
181
    }
182
}
183
184
void ItemLinkHandler::handleLink(const std::string &link,
185
                                 MouseEvent *const event)
186
{
187
    if (link.empty())
188
        return;
189
190
    if (strStartWith(link, "http://") || strStartWith(link, "https://"))
191
    {
192
        handleHttpLink(link, event);
193
    }
194
    else if (link[0] == '?')
195
    {
196
        handleSearchLink(link);
197
    }
198
    else if (strStartWith(link, "help://"))
199
    {
200
        handleHelpLink(link);
201
    }
202
    else if (strStartWith(link, settings.linkCommandSymbol))
203
    {
204
        handleCommandLink(link, settings.linkCommandSymbol);
205
    }
206
    else if (strStartWith(link, "="))
207
    {
208
        handleCommandLink(link, "=");
209
    }
210
    else if (link == "news")
211
    {
212
        if (helpWindow != nullptr)
213
            helpWindow->loadHelpSimple("news");
214
    }
215
    else if (link == "copyright")
216
    {
217
        inputManager.executeAction(InputAction::WINDOW_ABOUT);
218
    }
219
    else if (link[0] == 'q')
220
    {
221
        questsWindow->selectQuest(atoi(link.substr(1).c_str()));
222
    }
223
    else
224
    {
225
        handleItemLink(link);
226
    }
227

6
}