Libksieve

sieveeditorwebengineview.cpp
1 /*
2  Copyright (C) 2016-2020 Laurent Montel <[email protected]>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "sieveeditorwebengineview.h"
19 #include <qtwebenginewidgetsversion.h>
20 
21 #include <KLocalizedString>
22 #include <QContextMenuEvent>
23 #include <QMenu>
24 #include <QFileDialog>
25 #include <QWebEngineDownloadItem>
26 #include <QWebEngineProfile>
27 #include <QWebEngineSettings>
28 #include <QtWebEngineWidgets>
29 using namespace KSieveUi;
30 
31 SieveEditorWebEngineView::SieveEditorWebEngineView(QWidget *parent)
32  : QWebEngineView(parent)
33 {
34  QWebEngineProfile *profile = new QWebEngineProfile(this);
35  QWebEnginePage *page = new QWebEnginePage(profile, this);
36  page->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
37  page->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, false);
38  page->settings()->setAttribute(QWebEngineSettings::AutoLoadImages, true);
39  page->settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
40  page->settings()->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard, false);
41  page->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
42  page->settings()->setAttribute(QWebEngineSettings::XSSAuditingEnabled, false);
43  page->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, false);
44  page->settings()->setAttribute(QWebEngineSettings::ErrorPageEnabled, false);
45  page->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, false);
46  page->settings()->setAttribute(QWebEngineSettings::ScreenCaptureEnabled, false);
47  page->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, false);
48  page->settings()->setAttribute(QWebEngineSettings::WebGLEnabled, false);
49  page->settings()->setAttribute(QWebEngineSettings::PdfViewerEnabled, false);
50 
51  connect(profile, &QWebEngineProfile::downloadRequested, this, &SieveEditorWebEngineView::downloadRequested);
52  setPage(page);
53 }
54 
55 SieveEditorWebEngineView::~SieveEditorWebEngineView()
56 {
57 }
58 
59 void SieveEditorWebEngineView::downloadRequested(QWebEngineDownloadItem *download)
60 {
61  const QString filename = QFileDialog::getSaveFileName(this, i18n("Save Web Page"));
62  if (!filename.isEmpty()) {
63  download->setSavePageFormat(QWebEngineDownloadItem::SingleHtmlSaveFormat);
64 #if QTWEBENGINEWIDGETS_VERSION >= QT_VERSION_CHECK(5, 14, 0)
65  download->setDownloadDirectory(QFileInfo(filename).path());
66  download->setDownloadFileName(QFileInfo(filename).fileName());
67 #else
68  download->setPath(filename);
69 #endif
70  download->accept();
71  } else {
72  download->cancel();
73  }
74 }
75 
76 void SieveEditorWebEngineView::contextMenuEvent(QContextMenuEvent *ev)
77 {
78  QMenu menu;
79  QAction *act = pageAction(QWebEnginePage::Back);
80  if (act->isEnabled()) {
81  menu.addAction(act);
82  }
83  act = pageAction(QWebEnginePage::Forward);
84  if (act->isEnabled()) {
85  menu.addAction(act);
86  }
87 
88  if (!menu.actions().isEmpty()) {
89  QAction *separator = new QAction(&menu);
90  separator->setSeparator(true);
91  menu.addAction(separator);
92  }
93 
94  act = pageAction(QWebEnginePage::Copy);
95  if (act->isEnabled() && hasSelection()) {
96  menu.addAction(act);
97  }
98 
99  if (!menu.actions().isEmpty()) {
100  QAction *separator = new QAction(&menu);
101  separator->setSeparator(true);
102  menu.addAction(separator);
103  }
104  act = pageAction(QWebEnginePage::Reload);
105  if (act->isEnabled()) {
106  menu.addAction(act);
107  }
108 
109  act = pageAction(QWebEnginePage::SelectAll);
110  if (act->isEnabled()) {
111  menu.addAction(act);
112  }
113  act = pageAction(QWebEnginePage::CopyLinkToClipboard);
114  if (act->isEnabled()) {
115  menu.addAction(act);
116  }
117  act = pageAction(QWebEnginePage::SavePage);
118  if (act->isEnabled()) {
119  QAction *separator = new QAction(&menu);
120  separator->setSeparator(true);
121  menu.addAction(separator);
122  menu.addAction(act);
123  }
124  menu.exec(ev->globalPos());
125 }
126 
127 QWebEngineView *SieveEditorWebEngineView::createWindow(QWebEnginePage::WebWindowType type)
128 {
129  Q_UNUSED(type);
130  return this;
131 }
void setSeparator(bool b)
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-2020 The KDE developers.
Generated on Thu Jul 9 2020 23:07:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.