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

6
}