alkimia  8.0.2
alkwebpage.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright 2018 Ralf Habacker <ralf.habacker@freenet.de> *
3  * *
4  * This file is part of libalkimia. *
5  * *
6  * libalkimia is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public License *
8  * as published by the Free Software Foundation; either version 2.1 of *
9  * the License or (at your option) version 3 or any later version. *
10  * *
11  * libalkimia is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program. If not, see <http://www.gnu.org/licenses/> *
18  ***************************************************************************/
19 
20 #include "alkwebpage.h"
21 
22 #include <QWebFrame>
23 #include <QWebElement>
24 #include <QWebInspector>
25 #include <QWebView>
26 #include <QNetworkRequest>
27 
29 {
30 public:
31  QWebInspector *inspector;
32  Private()
33  : inspector(nullptr)
34  {
35  }
36  ~Private()
37  {
38  delete inspector;
39  }
40 
41  void setWebInspectorEnabled(bool enable, QWebPage* page)
42  {
43  page->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, enable);
44  delete inspector;
45  inspector = nullptr;
46  if (enable) {
47  inspector = new QWebInspector();
48  inspector->setPage(page);
49  }
50  }
51 
52  bool webInspectorEnabled(QWebPage *page)
53  {
54  return page->settings()->testAttribute(QWebSettings::DeveloperExtrasEnabled);
55  }
56 };
57 
58 AlkWebPage::AlkWebPage(QWidget *parent)
59  : QWebView(parent)
60  , d(new Private)
61 {
62  page()->settings()->setAttribute(QWebSettings::JavaEnabled, false);
63  page()->settings()->setAttribute(QWebSettings::AutoLoadImages, false);
64  page()->settings()->setAttribute(QWebSettings::PluginsEnabled, false);
65 }
66 
68 {
69  delete d;
70 }
71 
72 void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage)
73 {
74  QNetworkRequest request;
75  request.setUrl(url);
76  if (!acceptLanguage.isEmpty())
77  request.setRawHeader("Accept-Language", acceptLanguage.toLocal8Bit());
78  QWebView::load(request);
79 }
80 
82 {
83  QWebFrame *frame = page()->mainFrame();
84  return frame->toHtml();
85 }
86 
87 QString AlkWebPage::getFirstElement(const QString &symbol)
88 {
89  QWebFrame *frame = page()->mainFrame();
90  QWebElement element = frame->findFirstElement(symbol);
91  return element.toPlainText();
92 }
93 
95 {
96  d->setWebInspectorEnabled(enable, page());
97 }
98 
100 {
101  return d->webInspectorEnabled(page());
102 }
AlkWebPage::webInspectorEnabled
bool webInspectorEnabled()
Definition: alkwebpage.cpp:99
AlkWebPage::toHtml
QString toHtml()
Definition: alkwebpage.cpp:81
AlkWebPage::getFirstElement
QString getFirstElement(const QString &symbol)
Definition: alkwebpage.cpp:87
AlkWebPage::Private::setWebInspectorEnabled
void setWebInspectorEnabled(bool enable, QWebPage *page)
Definition: alkwebpage.cpp:58
AlkWebPage::AlkWebPage
AlkWebPage(QWidget *parent=nullptr)
Definition: alkwebpage.cpp:58
AlkWebPage::Private::~Private
~Private()
Definition: alkwebpage.cpp:53
AlkWebPage::setWebInspectorEnabled
void setWebInspectorEnabled(bool enable)
Definition: alkwebpage.cpp:94
AlkWebPage::d
Private * d
Definition: alkwebpage.h:50
AlkWebPage::Private
Definition: alkwebpage.cpp:28
AlkWebPage::load
void load(const QUrl &url, const QString &acceptLanguage)
Definition: alkwebpage.cpp:72
AlkWebPage::Private::inspector
QWebInspector * inspector
Definition: alkwebpage.cpp:48
AlkWebPage::Private::Private
Private()
Definition: alkwebpage.cpp:49
AlkWebPage::~AlkWebPage
virtual ~AlkWebPage()
Definition: alkwebpage.cpp:67
alkwebpage.h
AlkWebPage::Private::webInspectorEnabled
bool webInspectorEnabled(QWebPage *page)
Definition: alkwebpage.cpp:69