Libksieve

sieveeditorwebengineview.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 Laurent Montel <[email protected]>
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 <QWebEngineDownloadItem>
14 #include <QWebEngineProfile>
15 #include <QWebEngineSettings>
16 using namespace KSieveUi;
17 
18 SieveEditorWebEngineView::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 
38  connect(profile, &QWebEngineProfile::downloadRequested, this, &SieveEditorWebEngineView::downloadRequested);
39  setPage(page);
40 }
41 
42 SieveEditorWebEngineView::~SieveEditorWebEngineView()
43 {
44 }
45 
46 void SieveEditorWebEngineView::downloadRequested(QWebEngineDownloadItem *download)
47 {
48  const QString filename = QFileDialog::getSaveFileName(this, i18n("Save Web Page"));
49  if (!filename.isEmpty()) {
50  download->setSavePageFormat(QWebEngineDownloadItem::SingleHtmlSaveFormat);
51  download->setDownloadDirectory(QFileInfo(filename).path());
52  download->setDownloadFileName(QFileInfo(filename).fileName());
53  download->accept();
54  } else {
55  download->cancel();
56  }
57 }
58 
59 void SieveEditorWebEngineView::contextMenuEvent(QContextMenuEvent *ev)
60 {
61  QMenu menu;
62  QAction *act = pageAction(QWebEnginePage::Back);
63  if (act->isEnabled()) {
64  menu.addAction(act);
65  }
66  act = pageAction(QWebEnginePage::Forward);
67  if (act->isEnabled()) {
68  menu.addAction(act);
69  }
70 
71  if (!menu.actions().isEmpty()) {
72  auto separator = new QAction(&menu);
73  separator->setSeparator(true);
74  menu.addAction(separator);
75  }
76 
77  act = pageAction(QWebEnginePage::Copy);
78  if (act->isEnabled() && hasSelection()) {
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  menu.addAction(act);
95  }
96  act = pageAction(QWebEnginePage::CopyLinkToClipboard);
97  if (act->isEnabled()) {
98  menu.addAction(act);
99  }
100  act = pageAction(QWebEnginePage::SavePage);
101  if (act->isEnabled()) {
102  auto separator = new QAction(&menu);
103  separator->setSeparator(true);
104  menu.addAction(separator);
105  menu.addAction(act);
106  }
107  menu.exec(ev->globalPos());
108 }
109 
110 QWebEngineView *SieveEditorWebEngineView::createWindow(QWebEnginePage::WebWindowType type)
111 {
112  Q_UNUSED(type)
113  return this;
114 }
const QPoint & globalPos() const const
QAction * addAction(const QString &text)
bool isEmpty() const const
bool isEmpty() const const
QAction * exec()
QString i18n(const char *text, const TYPE &arg...)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QList< QAction * > actions() const const
bool isEnabled() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Apr 17 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.