2
* Copyright 2013 Canonical Ltd.
4
* This file is part of webbrowser-app.
6
* webbrowser-app is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 3.
10
* webbrowser-app is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
import QtWebKit.experimental 1.0
22
import Ubuntu.Components 0.1
23
import Ubuntu.Components.Extras.Browser 0.1
24
import Ubuntu.Components.Popups 0.1
28
\inqmlmodule Ubuntu.Components.Extras.Browser 0.1
30
\brief Custom Ubuntu WebView extending QtWebKit’s WebView
32
This version of UbuntuWebView is deprecated and shouldn’t be used in new
33
code. Use version 0.2 or higher instead.
38
signal newTabRequested(url url)
41
property real devicePixelRatio: QtWebKitDPR
42
onDevicePixelRatioChanged: {
43
// Do not make this patch to QtWebKit a hard requirement.
44
if (_webview.experimental.hasOwnProperty('devicePixelRatio')) {
45
_webview.experimental.devicePixelRatio = devicePixelRatio
50
interactive: !selection.visible
51
maximumFlickVelocity: height * 5
54
* Client overridable function called before the default treatment of a
55
* valid navigation request. This function can stop the navigation request
56
* if it sets the 'action' field of the request to IgnoreRequest.
59
function navigationRequestedDelegate(request) { }
66
* This function can be overridden by client applications that embed an
67
* UbuntuWebView to provide a static overridden UA string.
68
* If not overridden, the default UA string and the default override
69
* mechanism will be used.
71
function getUAString() {
72
// Note that this function used to accept a 'url' parameter to allow
73
// embedders to implement a custom override mechanism. It was removed
74
// after observing that no application was using it, and to simplify
75
// the API. Embedders willing to provide a custom override mechanism
76
// can always override (at their own risk) the onNavigationRequested
80
experimental.userAgent: (_webview.getUAString() === undefined) ? userAgent.defaultUA : _webview.getUAString()
81
onNavigationRequested: {
82
request.action = WebView.AcceptRequest;
84
navigationRequestedDelegate (request);
85
if (request.action === WebView.IgnoreRequest)
88
var staticUA = _webview.getUAString()
89
if (staticUA === undefined) {
90
_webview.experimental.userAgent = userAgent.getUAString(request.url)
92
_webview.experimental.userAgent = staticUA
96
experimental.preferences.navigatorQtObjectEnabled: true
97
experimental.userScripts: [Qt.resolvedUrl("hyperlinks.js"),
98
Qt.resolvedUrl("selection01.js")]
99
experimental.onMessageReceived: {
102
data = JSON.parse(message.data)
104
console.debug('DEBUG:', message.data)
107
if ('event' in data) {
108
if (data.event === 'longpress') {
109
if (('img' in data) || ('href' in data)) {
110
contextualData.clear()
112
contextualData.img = data.img
114
if ('href' in data) {
115
contextualData.href = data.href
116
contextualData.title = data.title
118
contextualRectangle.position(data)
119
PopupUtils.open(contextualPopover, contextualRectangle)
123
if ((data.event === 'longpress') || (data.event === 'selectionadjusted')) {
124
selection.clearData()
125
selection.createData()
126
if ('html' in data) {
127
selection.mimedata.html = data.html
129
// FIXME: push the text and image data in the order
130
// they appear in the selected block.
131
if ('text' in data) {
132
selection.mimedata.text = data.text
134
if ('images' in data) {
135
// TODO: download and cache the images locally
136
// (grab them from the webview’s cache, if possible),
137
// and forward local URLs.
138
selection.mimedata.urls = data.images
140
selection.show(data.left, data.top, data.width, data.height)
141
} else if (data.event === 'newtab') {
142
newTabRequested(data.url)
147
experimental.itemSelector: ItemSelector01 {}
149
property alias selection: selection
150
property ActionList selectionActions
157
property Item __popover: null
158
property var mimedata: null
162
ActionSelectionPopover {
163
grabDismissAreaEvents: false
164
actions: selectionActions
168
function createData() {
169
if (mimedata === null) {
170
mimedata = Clipboard.newData()
174
function clearData() {
175
if (mimedata !== null) {
181
function actionTriggered() {
182
selection.visible = false
185
function __showPopover() {
186
__popover = PopupUtils.open(selectionPopover, selection.rect)
187
var actions = __popover.actions.actions
188
for (var i in actions) {
189
actions[i].onTriggered.connect(actionTriggered)
193
function show(x, y, width, height) {
194
var scale = _webview.experimental.test.contentsScale * _webview.experimental.test.devicePixelRatio
195
rect.x = x * scale + _webview.contentX
196
rect.y = y * scale + _webview.contentY
197
rect.width = width * scale
198
rect.height = height * scale
204
if (!visible && (__popover != null)) {
205
PopupUtils.close(__popover)
211
var message = new Object
212
message.query = 'adjustselection'
213
var rect = selection.rect
214
var scale = _webview.experimental.test.contentsScale * _webview.experimental.test.devicePixelRatio
215
message.left = (rect.x - _webview.contentX) / scale
216
message.right = (rect.x + rect.width - _webview.contentX) / scale
217
message.top = (rect.y - _webview.contentY) / scale
218
message.bottom = (rect.y + rect.height - _webview.contentY) / scale
219
_webview.experimental.postMessage(JSON.stringify(message))
223
Clipboard.push(mimedata)
229
id: contextualRectangle
233
function position(data) {
234
var scale = _webview.experimental.test.contentsScale * _webview.experimental.test.devicePixelRatio
235
x = data.left * scale
237
width = data.width * scale
238
height = data.height * scale
241
property QtObject contextualData: QtObject {
243
property string title
253
property ActionList contextualActions
255
id: contextualPopover
256
ActionSelectionPopover {
257
actions: contextualActions
262
parent: _webview.parent
263
flickableItem: _webview
264
align: Qt.AlignTrailing
268
parent: _webview.parent
269
flickableItem: _webview
270
align: Qt.AlignBottom