Mana
Loading...
Searching...
No Matches
itemlinkhandler.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <sstream>
23#include <string>
24
25#include <SDL.h>
26
27#if SDL_VERSION_ATLEAST(2, 0, 14)
28#include "gui/confirmdialog.h"
29#endif
30#include "gui/itempopup.h"
31#include "gui/okdialog.h"
32#include "gui/viewport.h"
33
35
36#include "client.h"
37#include "resources/iteminfo.h"
38#include "resources/itemdb.h"
39#include "utils/gettext.h"
40#include "utils/stringutils.h"
41
43 : mParent(parent)
44{
45 mItemPopup = std::make_unique<ItemPopup>();
46 mItemPopup->addDeathListener(this);
47}
48
50
51static bool isUrl(const std::string &link)
52{
53 return startsWith(link, "https://") ||
54 startsWith(link, "http://") ||
55 startsWith(link, "ftp://");
56}
57
58void ItemLinkHandler::handleLink(const std::string &link)
59{
60#if SDL_VERSION_ATLEAST(2, 0, 14)
61 // Handle screenshots by constructing full file path
62 if (startsWith(link, "screenshot:"))
63 {
64 std::string filename = link.substr(11); // Remove "screenshot:" prefix
65
66 // Prevent directory traversal attacks or opening malicious files
67 if (filename.find("..") == std::string::npos && endsWith(filename, ".png"))
68 {
69 std::string fileUrl = "file://" + Client::getScreenshotDirectory() + "/" + filename;
70 if (SDL_OpenURL(fileUrl.c_str()) == -1)
71 new OkDialog(_("Open URL Failed"), SDL_GetError(), true, mParent);
72 }
73
74 return;
75 }
76#endif
77
78 if (isUrl(link))
79 {
80 mLink = link;
81
82#if SDL_VERSION_ATLEAST(2, 0, 14)
83 auto confirmDialog = new ConfirmDialog(_("Open URL?"), link, mParent);
84 confirmDialog->addActionListener(this);
85#else
86 new OkDialog(_("Open URL Failed"),
87 _("Opening of URLs requires SDL 2.0.14."), true, mParent);
88#endif
89 return;
90 }
91
92 int id = 0;
93 std::istringstream stream(link);
94 stream >> id;
95
96 if (id > 0)
97 {
98 const ItemInfo &itemInfo = itemDb->get(id);
99 mItemPopup->setItem(itemInfo, true);
100
101 if (mItemPopup->isVisible())
102 mItemPopup->setVisible(false);
103 else
105 }
106}
107
108void ItemLinkHandler::action(const gcn::ActionEvent &actionEvent)
109{
110 if (actionEvent.getId() == "yes")
111 {
112#if SDL_VERSION_ATLEAST(2, 0, 14)
113 if (SDL_OpenURL(mLink.c_str()) == -1)
114 {
115 new OkDialog(_("Open URL Failed"), SDL_GetError(), true, mParent);
116 }
117#endif
118 }
119}
120
121void ItemLinkHandler::death(const gcn::Event &event)
122{
123 // If somebody else killed the PopupUp, make sure we don't also try to delete it
124 if (event.getSource() == mItemPopup.get())
125 mItemPopup.release();
126}
static const std::string & getScreenshotDirectory()
Definition client.h:184
An option dialog.
const ItemInfo & get(int id) const
Definition itemdb.cpp:145
Defines a class for storing generic item infos.
Definition iteminfo.h:100
std::unique_ptr< ItemPopup > mItemPopup
std::string mLink
~ItemLinkHandler() override
void death(const gcn::Event &event) override
void handleLink(const std::string &link) override
void action(const gcn::ActionEvent &actionEvent) override
ItemLinkHandler(Window *parent=nullptr)
An 'Ok' button dialog.
Definition okdialog.h:34
int getMouseY() const
Returns mouse y in pixels.
Definition viewport.h:136
int getMouseX() const
Returns mouse x in pixels.
Definition viewport.h:131
A window.
Definition window.h:59
ItemDB * itemDb
Items info database.
Definition client.cpp:106
Viewport * viewport
Viewport on the map.
Definition game.cpp:115
#define _(s)
Definition gettext.h:38
bool startsWith(std::string_view str, std::string_view prefix)
Returns whether a string starts with a given prefix.
bool endsWith(std::string_view str, std::string_view suffix)
Returns whether a string ends with a given suffix.