Changeset View
Changeset View
Standalone View
Standalone View
processui/scripting.cpp
Show All 37 Lines | |||||
38 | 38 | | |||
39 | #include <KDesktopFile> | 39 | #include <KDesktopFile> | ||
40 | #include <KStandardAction> | 40 | #include <KStandardAction> | ||
41 | #include <KLocalizedString> | 41 | #include <KLocalizedString> | ||
42 | #include <QVBoxLayout> | 42 | #include <QVBoxLayout> | ||
43 | #include <QMessageBox> | 43 | #include <QMessageBox> | ||
44 | #include <QDialogButtonBox> | 44 | #include <QDialogButtonBox> | ||
45 | 45 | | |||
46 | #if HAVE_QTWEBENGINEWIDGETS | 46 | #if WEBENGINE_SCRIPTING_ENABLED | ||
47 | #include <QWebChannel> | 47 | #include <QWebChannel> | ||
48 | #include <QWebEngineSettings> | 48 | #include <QWebEngineSettings> | ||
49 | #include <QWebEngineView> | 49 | #include <QWebEngineView> | ||
50 | #include <QWebEngineProfile> | 50 | #include <QWebEngineProfile> | ||
51 | #include <QWebEngineScriptCollection> | 51 | #include <QWebEngineScriptCollection> | ||
52 | #include <QWebEngineUrlRequestInterceptor> | 52 | #include <QWebEngineUrlRequestInterceptor> | ||
53 | #include <qtwebenginewidgetsversion.h> | 53 | #include <qtwebenginewidgetsversion.h> | ||
54 | #endif | 54 | #endif | ||
55 | 55 | | |||
56 | #if HAVE_QTWEBENGINEWIDGETS | 56 | #if WEBENGINE_SCRIPTING_ENABLED | ||
57 | class RemoteUrlInterceptor : public QWebEngineUrlRequestInterceptor { | 57 | class RemoteUrlInterceptor : public QWebEngineUrlRequestInterceptor { | ||
58 | public: | 58 | public: | ||
59 | RemoteUrlInterceptor(QObject *parent) : QWebEngineUrlRequestInterceptor(parent) {} | 59 | RemoteUrlInterceptor(QObject *parent) : QWebEngineUrlRequestInterceptor(parent) {} | ||
60 | void interceptRequest(QWebEngineUrlRequestInfo &info) override | 60 | void interceptRequest(QWebEngineUrlRequestInfo &info) override | ||
61 | { | 61 | { | ||
62 | // Block non-GET/HEAD requests | 62 | // Block non-GET/HEAD requests | ||
63 | if(!QStringList({QStringLiteral("GET"), QStringLiteral("HEAD")}) | 63 | if(!QStringList({QStringLiteral("GET"), QStringLiteral("HEAD")}) | ||
64 | .contains(QString::fromLatin1(info.requestMethod()))) | 64 | .contains(QString::fromLatin1(info.requestMethod()))) | ||
Show All 11 Lines | 75 | class ScriptingHtmlDialog : public QDialog { | |||
76 | public: | 76 | public: | ||
77 | ScriptingHtmlDialog(QWidget *parent) : QDialog(parent) { | 77 | ScriptingHtmlDialog(QWidget *parent) : QDialog(parent) { | ||
78 | 78 | | |||
79 | QDialogButtonBox *buttonBox = new QDialogButtonBox(this); | 79 | QDialogButtonBox *buttonBox = new QDialogButtonBox(this); | ||
80 | buttonBox->setStandardButtons(QDialogButtonBox::Close); | 80 | buttonBox->setStandardButtons(QDialogButtonBox::Close); | ||
81 | connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); | 81 | connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); | ||
82 | connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); | 82 | connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); | ||
83 | 83 | | |||
84 | #if HAVE_QTWEBENGINEWIDGETS | 84 | #if WEBENGINE_SCRIPTING_ENABLED | ||
85 | QVBoxLayout *layout = new QVBoxLayout; | 85 | QVBoxLayout *layout = new QVBoxLayout; | ||
86 | layout->addWidget(&m_webView); | 86 | layout->addWidget(&m_webView); | ||
87 | layout->addWidget(buttonBox); | 87 | layout->addWidget(buttonBox); | ||
88 | setLayout(layout); | 88 | setLayout(layout); | ||
89 | layout->setContentsMargins(0,0,0,0); | 89 | layout->setContentsMargins(0,0,0,0); | ||
90 | m_webView.settings()->setAttribute(QWebEngineSettings::PluginsEnabled, false); | 90 | m_webView.settings()->setAttribute(QWebEngineSettings::PluginsEnabled, false); | ||
91 | #if QTWEBENGINEWIDGETS_VERSION < QT_VERSION_CHECK(5, 13, 0) | 91 | #if QTWEBENGINEWIDGETS_VERSION < QT_VERSION_CHECK(5, 13, 0) | ||
92 | m_webView.page()->profile()->setRequestInterceptor(new RemoteUrlInterceptor(this)); | 92 | m_webView.page()->profile()->setRequestInterceptor(new RemoteUrlInterceptor(this)); | ||
93 | #else | 93 | #else | ||
94 | m_webView.page()->profile()->setUrlRequestInterceptor(new RemoteUrlInterceptor(this)); | 94 | m_webView.page()->profile()->setUrlRequestInterceptor(new RemoteUrlInterceptor(this)); | ||
95 | #endif | 95 | #endif | ||
96 | #endif | 96 | #endif | ||
97 | } | 97 | } | ||
98 | #if HAVE_QTWEBENGINEWIDGETS | 98 | #if WEBENGINE_SCRIPTING_ENABLED | ||
99 | QWebEngineView *webView() { | 99 | QWebEngineView *webView() { | ||
100 | return &m_webView; | 100 | return &m_webView; | ||
101 | } | 101 | } | ||
102 | protected: | 102 | protected: | ||
103 | QWebEngineView m_webView; | 103 | QWebEngineView m_webView; | ||
104 | #endif | 104 | #endif | ||
105 | }; | 105 | }; | ||
106 | 106 | | |||
Show All 22 Lines | 128 | Scripting::Scripting(KSysGuardProcessList * parent) : QWidget(parent), mProcessList(parent) { | |||
129 | mScriptingHtmlDialog = nullptr; | 129 | mScriptingHtmlDialog = nullptr; | ||
130 | loadContextMenu(); | 130 | loadContextMenu(); | ||
131 | } | 131 | } | ||
132 | void Scripting::runScript(const QString &path, const QString &name) { | 132 | void Scripting::runScript(const QString &path, const QString &name) { | ||
133 | //Record the script name and path for use in the script helper functions | 133 | //Record the script name and path for use in the script helper functions | ||
134 | mScriptPath = path; | 134 | mScriptPath = path; | ||
135 | mScriptName = name; | 135 | mScriptName = name; | ||
136 | 136 | | |||
137 | #if HAVE_QTWEBENGINEWIDGETS | 137 | #if WEBENGINE_SCRIPTING_ENABLED | ||
138 | QUrl fileName = QUrl::fromLocalFile(path + QStringLiteral("index.html")); | 138 | QUrl fileName = QUrl::fromLocalFile(path + QStringLiteral("index.html")); | ||
139 | if(!mScriptingHtmlDialog) { | 139 | if(!mScriptingHtmlDialog) { | ||
140 | mScriptingHtmlDialog = new ScriptingHtmlDialog(this); | 140 | mScriptingHtmlDialog = new ScriptingHtmlDialog(this); | ||
141 | mWebChannel = new QWebChannel(mScriptingHtmlDialog); | 141 | mWebChannel = new QWebChannel(mScriptingHtmlDialog); | ||
142 | connect(mScriptingHtmlDialog, &QDialog::rejected, this, &Scripting::stopAllScripts); | 142 | connect(mScriptingHtmlDialog, &QDialog::rejected, this, &Scripting::stopAllScripts); | ||
143 | // Only show after page loaded to allow for layouting | 143 | // Only show after page loaded to allow for layouting | ||
144 | mScriptingHtmlDialog->connect(mScriptingHtmlDialog->webView(), &QWebEngineView::loadFinished, | 144 | mScriptingHtmlDialog->connect(mScriptingHtmlDialog->webView(), &QWebEngineView::loadFinished, | ||
145 | mScriptingHtmlDialog, &ScriptingHtmlDialog::show); | 145 | mScriptingHtmlDialog, &ScriptingHtmlDialog::show); | ||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Line(s) | 211 | });)JS")); | |||
245 | setupJavascriptObjects(); | 245 | setupJavascriptObjects(); | ||
246 | 246 | | |||
247 | mScriptingHtmlDialog->webView()->load(fileName); | 247 | mScriptingHtmlDialog->webView()->load(fileName); | ||
248 | #else | 248 | #else | ||
249 | QMessageBox::critical(this, i18n("QtWebEngineWidgets not available"), | 249 | QMessageBox::critical(this, i18n("QtWebEngineWidgets not available"), | ||
250 | i18n("KSysGuard library was compiled without QtWebEngineWidgets, please contact your distribution.")); | 250 | i18n("KSysGuard library was compiled without QtWebEngineWidgets, please contact your distribution.")); | ||
251 | #endif | 251 | #endif | ||
252 | } | 252 | } | ||
253 | #if HAVE_QTWEBENGINEWIDGETS | 253 | #if WEBENGINE_SCRIPTING_ENABLED | ||
254 | void Scripting::zoomIn() { | 254 | void Scripting::zoomIn() { | ||
255 | QWebEngineView *webView = mScriptingHtmlDialog->webView(); | 255 | QWebEngineView *webView = mScriptingHtmlDialog->webView(); | ||
256 | webView->setZoomFactor( webView->zoomFactor() * 1.1 ); | 256 | webView->setZoomFactor( webView->zoomFactor() * 1.1 ); | ||
257 | } | 257 | } | ||
258 | void Scripting::zoomOut() { | 258 | void Scripting::zoomOut() { | ||
259 | QWebEngineView *webView = mScriptingHtmlDialog->webView(); | 259 | QWebEngineView *webView = mScriptingHtmlDialog->webView(); | ||
260 | if(webView->zoomFactor() > 0.1) //Prevent it getting too small | 260 | if(webView->zoomFactor() > 0.1) //Prevent it getting too small | ||
261 | webView->setZoomFactor( webView->zoomFactor() / 1.1 ); | 261 | webView->setZoomFactor( webView->zoomFactor() / 1.1 ); | ||
▲ Show 20 Lines • Show All 68 Lines • Show Last 20 Lines |