Libksieve

sieveeditorwebengineview.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-only
5*/
6
7#include "sieveeditorwebengineview.h"
8
9#include <KLocalizedString>
10#include <QContextMenuEvent>
11#include <QFileDialog>
12#include <QMenu>
13#include <QWebEngineDownloadRequest>
14#include <QWebEngineProfile>
15#include <QWebEngineSettings>
16using namespace KSieveUi;
17
18SieveEditorWebEngineView::SieveEditorWebEngineView(QWidget *parent)
19 : QWebEngineView(parent)
20{
21 auto profile = new QWebEngineProfile(this);
22 auto page = new QWebEnginePage(profile, this);
23 page->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
24 page->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, false);
25 page->settings()->setAttribute(QWebEngineSettings::AutoLoadImages, true);
26 page->settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
27 page->settings()->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard, false);
28 page->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
29 page->settings()->setAttribute(QWebEngineSettings::XSSAuditingEnabled, false);
30 page->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, false);
31 page->settings()->setAttribute(QWebEngineSettings::ErrorPageEnabled, false);
32 page->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, false);
33 page->settings()->setAttribute(QWebEngineSettings::ScreenCaptureEnabled, false);
34 page->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, false);
35 page->settings()->setAttribute(QWebEngineSettings::WebGLEnabled, false);
36 page->settings()->setAttribute(QWebEngineSettings::PdfViewerEnabled, false);
37 page->settings()->setAttribute(QWebEngineSettings::NavigateOnDropEnabled, false);
38
39 connect(profile, &QWebEngineProfile::downloadRequested, this, &SieveEditorWebEngineView::downloadRequested);
40 setPage(page);
41}
42
43SieveEditorWebEngineView::~SieveEditorWebEngineView() = default;
44
45void SieveEditorWebEngineView::downloadRequested(QWebEngineDownloadRequest *download)
46{
47 const QString filename = QFileDialog::getSaveFileName(this, i18nc("@title:window", "Save Web Page"));
48 if (!filename.isEmpty()) {
49 download->setSavePageFormat(QWebEngineDownloadRequest::SingleHtmlSaveFormat);
50 download->setDownloadDirectory(QFileInfo(filename).path());
51 download->setDownloadFileName(QFileInfo(filename).fileName());
52 download->accept();
53 } else {
54 download->cancel();
55 }
56}
57
58void SieveEditorWebEngineView::contextMenuEvent(QContextMenuEvent *ev)
59{
60 QMenu menu;
61 QAction *act = pageAction(QWebEnginePage::Back);
62 if (act->isEnabled()) {
63 menu.addAction(act);
64 }
65 act = pageAction(QWebEnginePage::Forward);
66 if (act->isEnabled()) {
67 menu.addAction(act);
68 }
69
70 if (!menu.actions().isEmpty()) {
71 auto separator = new QAction(&menu);
72 separator->setSeparator(true);
73 menu.addAction(separator);
74 }
75
76 act = pageAction(QWebEnginePage::Copy);
77 if (act->isEnabled() && hasSelection()) {
78 act->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy")));
79 menu.addAction(act);
80 }
81
82 if (!menu.actions().isEmpty()) {
83 auto separator = new QAction(&menu);
84 separator->setSeparator(true);
85 menu.addAction(separator);
86 }
87 act = pageAction(QWebEnginePage::Reload);
88 if (act->isEnabled()) {
89 menu.addAction(act);
90 }
91
92 act = pageAction(QWebEnginePage::SelectAll);
93 if (act->isEnabled()) {
94 act->setIcon(QIcon::fromTheme(QStringLiteral("edit-select-all")));
95 menu.addAction(act);
96 }
97 act = pageAction(QWebEnginePage::CopyLinkToClipboard);
98 if (act->isEnabled()) {
99 menu.addAction(act);
100 }
101 act = pageAction(QWebEnginePage::SavePage);
102 if (act->isEnabled()) {
103 auto separator = new QAction(&menu);
104 separator->setSeparator(true);
105 menu.addAction(separator);
106 act->setIcon(QIcon::fromTheme(QStringLiteral("document-save")));
107 menu.addAction(act);
108 }
109 menu.exec(ev->globalPos());
110}
111
112QWebEngineView *SieveEditorWebEngineView::createWindow(QWebEnginePage::WebWindowType type)
113{
114 Q_UNUSED(type)
115 return this;
116}
117
118#include "moc_sieveeditorwebengineview.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString path(const QString &relativePath)
bool isEnabled() const const
void setIcon(const QIcon &icon)
const QPoint & globalPos() const const
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
QIcon fromTheme(const QString &name)
bool isEmpty() const const
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * exec()
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QList< QAction * > actions() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.