alkimia  8.0.3
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 #if defined(BUILD_WITH_WEBKIT)
23 #include <QWebFrame>
24 #include <QWebElement>
25 #include <QWebInspector>
26 #include <QWebView>
27 #include <QNetworkRequest>
28 
30 {
31 public:
32  QWebInspector *inspector;
33  Private()
34  : inspector(nullptr)
35  {
36  }
37  ~Private()
38  {
39  delete inspector;
40  }
41 
42  void setWebInspectorEnabled(bool enable, QWebPage* page)
43  {
44  page->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, enable);
45  delete inspector;
46  inspector = nullptr;
47  if (enable) {
48  inspector = new QWebInspector();
49  inspector->setPage(page);
50  }
51  }
52 
53  bool webInspectorEnabled(QWebPage *page)
54  {
55  return page->settings()->testAttribute(QWebSettings::DeveloperExtrasEnabled);
56  }
57 };
58 
59 AlkWebPage::AlkWebPage(QWidget *parent)
60  : QWebView(parent)
61  , d(new Private)
62 {
63  page()->settings()->setAttribute(QWebSettings::JavaEnabled, false);
64  page()->settings()->setAttribute(QWebSettings::AutoLoadImages, false);
65  page()->settings()->setAttribute(QWebSettings::PluginsEnabled, false);
66 }
67 
69 {
70  delete d;
71 }
72 
73 QWidget *AlkWebPage::widget()
74 {
75  return this;
76 }
77 
78 void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage)
79 {
80  QNetworkRequest request;
81  request.setUrl(url);
82  if (!acceptLanguage.isEmpty())
83  request.setRawHeader("Accept-Language", acceptLanguage.toLocal8Bit());
84  QWebView::load(request);
85 }
86 
87 QString AlkWebPage::toHtml()
88 {
89  QWebFrame *frame = page()->mainFrame();
90  return frame->toHtml();
91 }
92 
93 QString AlkWebPage::getFirstElement(const QString &symbol)
94 {
95  QWebFrame *frame = page()->mainFrame();
96  QWebElement element = frame->findFirstElement(symbol);
97  return element.toPlainText();
98 }
99 
100 void AlkWebPage::setWebInspectorEnabled(bool enable)
101 {
102  d->setWebInspectorEnabled(enable, page());
103 }
104 
106 {
107  return d->webInspectorEnabled(page());
108 }
109 
110 #else
111 
113 {
114 public:
115 };
116 
117 AlkWebPage::AlkWebPage(QWidget *parent)
118  : QWidget(parent)
119  , d(new Private)
120 {
121 }
122 
124 {
125  delete d;
126 }
127 
129 {
130  return this;
131 }
132 
133 void AlkWebPage::load(const QUrl &url, const QString &acceptLanguage)
134 {
135  Q_UNUSED(url)
136  Q_UNUSED(acceptLanguage)
137 }
138 
139 void AlkWebPage::setUrl(const QUrl &url)
140 {
141  Q_UNUSED(url)
142 }
143 
144 void AlkWebPage::setContent(const QString &s)
145 {
146  Q_UNUSED(s)
147 }
148 
150 {
151  return QString();
152 }
153 
154 QString AlkWebPage::getFirstElement(const QString &symbol)
155 {
156  Q_UNUSED(symbol)
157 
158  return QString();
159 }
160 
162 {
163  Q_UNUSED(enable)
164 }
165 
167 {
168  return false;
169 }
170 
171 #endif
AlkWebPage::webInspectorEnabled
bool webInspectorEnabled()
Definition: alkwebpage.cpp:166
AlkWebPage::toHtml
QString toHtml()
Definition: alkwebpage.cpp:149
AlkWebPage::widget
QWidget * widget()
Definition: alkwebpage.cpp:128
AlkWebPage::getFirstElement
QString getFirstElement(const QString &symbol)
Definition: alkwebpage.cpp:154
AlkWebPage::AlkWebPage
AlkWebPage(QWidget *parent=nullptr)
Definition: alkwebpage.cpp:117
AlkWebPage::setWebInspectorEnabled
void setWebInspectorEnabled(bool enable)
Definition: alkwebpage.cpp:161
AlkWebPage::d
Private * d
Definition: alkwebpage.h:88
AlkWebPage::Private
Definition: alkwebpage.cpp:113
AlkWebPage::setContent
void setContent(const QString &s)
Definition: alkwebpage.cpp:144
AlkWebPage::load
void load(const QUrl &url, const QString &acceptLanguage)
Definition: alkwebpage.cpp:133
AlkWebPage::~AlkWebPage
virtual ~AlkWebPage()
Definition: alkwebpage.cpp:123
alkwebpage.h
AlkWebPage::setUrl
void setUrl(const QUrl &url)
Definition: alkwebpage.cpp:139