ManaPlus
itemlinkhandler.cpp
Go to the documentation of this file.
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 
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 
37 
39 #include "gui/windows/helpwindow.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 
51 
52 #include "debug.h"
53 
54 namespace
55 {
57 } // namespace
58 
60  LinkHandler(),
61  mAllowCommands(true)
62 {
63 }
64 
66 {
67 }
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  {
89  // TRANSLATORS: dialog message
90  _("Run command"),
91  strprintf("/%s %s", cmd.c_str(), args.c_str()),
93  false,
94  Modal_true,
95  nullptr);
97  }
98 }
99 
100 void ItemLinkHandler::handleHelpLink(const std::string &link)
101 {
102  if (helpWindow != nullptr)
103  {
104  helpWindow->loadHelp(link.substr(7));
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  {
121  // TRANSLATORS: dialog message
122  _("Open url"),
123  url,
125  false,
126  Modal_true,
127  nullptr);
129  }
130  else if (button == MouseButton::RIGHT)
131  {
132  if (popupMenu != nullptr)
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 =
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  {
167  }
168  else if (viewport != nullptr)
169  {
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));
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  {
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  {
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 }
#define maxCards
Definition: cards.h:25
void search(const std::string &text)
Definition: helpwindow.cpp:187
void loadHelp(const std::string &helpFile)
Definition: helpwindow.cpp:126
void loadHelpSimple(const std::string &helpFile)
Definition: helpwindow.cpp:139
void setCommand(const std::string &command, const std::string &args)
void executeAction(const InputActionT keyNum)
bool executeRemoteChatCommand(const std::string &cmd, const std::string &args, ChatTab *const tab)
static ItemColor getColorFromCards(const int *const cards)
static void handleHttpLink(const std::string &link, const MouseEvent *const event)
void handleCommandLink(const std::string &link, const std::string &prefix)
static void handleItemLink(const std::string &link)
static void handleSearchLink(const std::string &link)
static void handleHelpLink(const std::string &link)
void handleLink(const std::string &link, MouseEvent *event)
void setItem(const ItemInfo &item, const ItemColor color, const bool showImage, int id, const int *const cards, const ItemOptionsList *const options)
Definition: itempopup.cpp:189
void showLinkPopup(const std::string &link)
Definition: popupmenu.cpp:2194
void position(const int x, const int y)
Definition: popup.cpp:235
bool isPopupVisible() const
Definition: popup.h:174
void selectQuest(const int varId)
std::string linkCommandSymbol
Definition: settings.h:127
int mMouseX
Definition: viewport.h:154
int mMouseY
Definition: viewport.h:155
void setVisible(Visible visible)
Definition: widget.cpp:225
virtual void requestMoveToTop()
Definition: widget.cpp:213
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
Viewport * viewport
Definition: viewport.cpp:36
#define _(s)
Definition: gettext.h:35
HelpWindow * helpWindow
Definition: helpwindow.cpp:54
InputActionRemoteListener inputActionRemoteListener
InputManager inputManager
uint16_t ItemColor
Definition: itemcolor.h:30
ItemPopup * itemPopup
Definition: itempopup.cpp:64
const bool Modal_true
Definition: modal.h:30
MouseButton ::T MouseButtonT
Definition: mousebutton.h:78
bool itemInfo(InputEvent &event)
Definition: commands.cpp:105
bool url(InputEvent &event)
Definition: commands.cpp:64
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
PopupMenu * popupMenu
Definition: popupmenu.cpp:103
QuestsWindow * questsWindow
ConfirmDialog * confirmDlg
Definition: traderecv.cpp:49
Settings settings
Definition: settings.cpp:32
static const std::string SOUND_REQUEST
Definition: sound.h:29
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)
void splitToIntVector(std::vector< int > &tokens, const std::string &text, const char separator)
std::string strprintf(const char *const format,...)
bool parse2Str(const std::string &args, std::string &str1, std::string &str2)
bool strStartWith(const std::string &str1, const std::string &str2)
std::string url
const bool Visible_false
Definition: visible.h:30