| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/webviewers/webviewer.h" |
| 4 | |
| 5 | #include "gui/dialogs/formmain.h" |
| 6 | #include "miscellaneous/externaltool.h" |
| 7 | #include "miscellaneous/iconfactory.h" |
| 8 | #include "miscellaneous/settings.h" |
| 9 | #include "network-web/webfactory.h" |
| 10 | |
| 11 | #include <QFileIconProvider> |
| 12 | #include <QTimer> |
| 13 | |
| 14 | WebViewer::WebViewer() {} |
| 15 | |
| 16 | WebViewer::~WebViewer() {} |
| 17 | |
| 18 | void WebViewer::processContextMenu(QMenu* specific_menu, QContextMenuEvent* event) { |
| 19 | // Setup the menu. |
| 20 | m_contextMenuData = provideContextMenuData(event); |
| 21 | specific_menu->setAttribute(Qt::WidgetAttribute::WA_DeleteOnClose, true); |
| 22 | initializeCommonMenuItems(); |
| 23 | |
| 24 | // Add common items. |
| 25 | specific_menu->addSeparator(); |
| 26 | specific_menu->addAction(m_actionOpenExternalBrowser.data()); |
| 27 | specific_menu->addAction(m_actionPlayLink.data()); |
| 28 | |
| 29 | m_actionOpenExternalBrowser.data()->setEnabled(m_contextMenuData.m_linkUrl.isValid()); |
| 30 | |
| 31 | #if defined(ENABLE_MEDIAPLAYER) |
| 32 | m_actionPlayLink.data()->setEnabled(m_contextMenuData.m_linkUrl.isValid()); |
| 33 | #endif |
| 34 | |
| 35 | if (m_contextMenuData.m_linkUrl.isValid()) { |
| 36 | QFileIconProvider icon_provider; |
| 37 | QMenu* menu_ext_tools = new QMenu(QObject::tr("Open with external tool"), specific_menu); |
| 38 | auto tools = ExternalTool::toolsFromSettings(); |
| 39 | |
| 40 | menu_ext_tools->setIcon(qApp->icons()->fromTheme(QSL("document-open"))); |
| 41 | |
| 42 | for (const ExternalTool& tool : std::as_const(tools)) { |
| 43 | QAction* act_tool = new QAction(QFileInfo(tool.executable()).fileName(), menu_ext_tools); |
| 44 | |
| 45 | act_tool->setIcon(icon_provider.icon(QFileInfo(tool.executable()))); |
| 46 | act_tool->setToolTip(tool.executable()); |
| 47 | act_tool->setData(QVariant::fromValue(tool)); |
| 48 | menu_ext_tools->addAction(act_tool); |
| 49 | |
| 50 | QObject::connect(act_tool, &QAction::triggered, act_tool, [this, act_tool]() { |
| 51 | act_tool->data().value<ExternalTool>().run(m_contextMenuData.m_linkUrl.toString()); |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | if (menu_ext_tools->actions().isEmpty()) { |
| 56 | QAction* act_not_tools = new QAction("No external tools activated"); |
| 57 | |
| 58 | act_not_tools->setEnabled(false); |
| 59 | menu_ext_tools->addAction(act_not_tools); |
| 60 | } |
| 61 | |
| 62 | specific_menu->addMenu(menu_ext_tools); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void WebViewer::playClickedLinkAsMedia() { |
| 67 | #if defined(ENABLE_MEDIAPLAYER) |
| 68 | auto context_url = m_contextMenuData.m_linkUrl; |
| 69 | |
| 70 | if (context_url.isValid()) { |
| 71 | qApp->mainForm()->tabWidget()->addMediaPlayer(context_url.toString(), true); |
| 72 | } |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | void WebViewer::openClickedLinkInExternalBrowser() { |
| 77 | auto context_url = m_contextMenuData.m_linkUrl; |
| 78 | |
| 79 | if (context_url.isValid()) { |
| 80 | const QUrl resolved_url = (url().isValid() && context_url.isRelative()) ? url().resolved(context_url) : context_url; |
| 81 | |
| 82 | qApp->web()->openUrlInExternalBrowser(resolved_url.toString()); |
| 83 | |
| 84 | if (qApp->settings() |
| 85 | ->value(GROUP(Messages), SETTING(Messages::BringAppToFrontAfterMessageOpenedExternally)) |
| 86 | .toBool()) { |
| 87 | QTimer::singleShot(1000, qApp, []() { |
| 88 | qApp->mainForm()->display(); |
| 89 | }); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void WebViewer::initializeCommonMenuItems() { |
| 95 | if (!m_actionOpenExternalBrowser.isNull()) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | m_actionOpenExternalBrowser.reset(new QAction(qApp->icons()->fromTheme(QSL("document-open")), |
| 100 | QObject::tr("Open in external browser"))); |
| 101 | |
| 102 | m_actionPlayLink.reset(new QAction(qApp->icons()->fromTheme(QSL("player_play"), QSL("media-playback-start")), |
| 103 | QObject::tr("Play in media player"))); |
| 104 | |
| 105 | #if !defined(ENABLE_MEDIAPLAYER) |
| 106 | m_actionPlayLink->setText(m_actionPlayLink->text() + QSL(" ") + QObject::tr("(not supported)")); |
| 107 | m_actionPlayLink->setEnabled(false); |
| 108 | #endif |
| 109 | |
| 110 | QObject::connect(m_actionOpenExternalBrowser.data(), |
| 111 | &QAction::triggered, |
| 112 | m_actionOpenExternalBrowser.data(), |
| 113 | [this]() { |
| 114 | openClickedLinkInExternalBrowser(); |
| 115 | }); |
| 116 | |
| 117 | QObject::connect(m_actionPlayLink.data(), &QAction::triggered, m_actionPlayLink.data(), [this]() { |
| 118 | playClickedLinkAsMedia(); |
| 119 | }); |
| 120 | } |