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-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 
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 
36 
38 #include "gui/windows/helpwindow.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 
50 
51 #include "debug.h"
52 
53 namespace
54 {
56 } // namespace
57 
59  LinkHandler(),
60  mAllowCommands(true)
61 {
62 }
63 
65 {
66 }
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  {
88  // TRANSLATORS: dialog message
89  _("Run command"),
90  strprintf("/%s %s", cmd.c_str(), args.c_str()),
92  false,
93  Modal_true,
94  nullptr);
96  }
97 }
98 
99 void ItemLinkHandler::handleHelpLink(const std::string &link)
100 {
101  if (helpWindow != nullptr)
102  {
103  helpWindow->loadHelp(link.substr(7));
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  {
120  // TRANSLATORS: dialog message
121  _("Open url"),
122  url,
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 =
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  {
166  }
167  else if (viewport != nullptr)
168  {
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));
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  {
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  {
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 }
bool strStartWith(const std::string &str1, const std::string &str2)
#define _(s)
Definition: gettext.h:34
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
static ItemColor getColorFromCards(const int *const cards)
static void handleItemLink(const std::string &link)
std::string linkCommandSymbol
Definition: settings.h:125
QuestsWindow * questsWindow
void loadHelpSimple(const std::string &helpFile)
Definition: helpwindow.cpp:138
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
void setVisible(Visible visible)
Definition: widget.cpp:224
InputActionRemoteListener inputActionRemoteListener
virtual void requestMoveToTop()
Definition: widget.cpp:212
void showLinkPopup(const std::string &link)
Definition: popupmenu.cpp:2194
bool parse2Str(const std::string &args, std::string &str1, std::string &str2)
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)
static const std::string SOUND_REQUEST
Definition: sound.h:28
uint16_t ItemColor
Definition: itemcolor.h:29
bool isPopupVisible() const
Definition: popup.h:173
int mMouseX
Definition: viewport.h:153
void loadHelp(const std::string &helpFile)
Definition: helpwindow.cpp:125
std::string url
void selectQuest(const int varId)
Settings settings
Definition: settings.cpp:31
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
const bool Visible_false
Definition: visible.h:29
PopupMenu * popupMenu
Definition: popupmenu.cpp:102
void position(const int x, const int y)
Definition: popup.cpp:232
static void handleHttpLink(const std::string &link, const MouseEvent *const event)
void search(const std::string &text)
Definition: helpwindow.cpp:186
void setItem(const ItemInfo &item, const ItemColor color, const bool showImage, int id, const int *const cards, const ItemOptionsList *const options)
Definition: itempopup.cpp:188
void handleLink(const std::string &link, MouseEvent *event)
int mMouseY
Definition: viewport.h:154
ConfirmDialog * confirmDlg
Definition: traderecv.cpp:48
bool executeRemoteChatCommand(const std::string &cmd, const std::string &args, ChatTab *const tab)
static void handleHelpLink(const std::string &link)
void setCommand(const std::string &command, const std::string &args)
InputManager inputManager
static void handleSearchLink(const std::string &link)
void splitToIntVector(std::vector< int > &tokens, const std::string &text, const char separator)
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:35
ItemPopup * itemPopup
Definition: itempopup.cpp:63
HelpWindow * helpWindow
Definition: helpwindow.cpp:53
MouseButton ::T MouseButtonT
Definition: mousebutton.h:77
bool url(InputEvent &event)
Definition: commands.cpp:63
void executeAction(const InputActionT keyNum)
void handleCommandLink(const std::string &link, const std::string &prefix)
#define maxCards
Definition: cards.h:24
Viewport * viewport
Definition: viewport.cpp:35
bool itemInfo(InputEvent &event)
Definition: commands.cpp:104