~bfiller/webbrowser-app/restrict-arches

« back to all changes in this revision

Viewing changes to src/Ubuntu/Components/Extras/Browser/UbuntuWebView01.qml

  • Committer: Olivier Tilloy
  • Date: 2014-03-17 22:16:59 UTC
  • mfrom: (468.1.18 with-oxide-api)
  • Revision ID: olivier.tilloy@canonical.com-20140317221659-b2jao4k44upufd2n
Version the new UbuntuWebView API as 0.2 (oxide).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * This file is part of webbrowser-app.
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
import QtQuick 2.0
 
20
import QtWebKit 3.1
 
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
 
25
 
 
26
/*!
 
27
    \qmltype MainView
 
28
    \inqmlmodule Ubuntu.Components.Extras.Browser 0.1
 
29
    \obsolete
 
30
    \brief Custom Ubuntu WebView extending QtWebKit’s WebView
 
31
 
 
32
    This version of UbuntuWebView is deprecated and shouldn’t be used in new
 
33
    code. Use version 0.2 or higher instead.
 
34
*/
 
35
WebView {
 
36
    id: _webview
 
37
 
 
38
    signal newTabRequested(url url)
 
39
 
 
40
    QtObject {
 
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
 
46
            }
 
47
        }
 
48
    }
 
49
 
 
50
    interactive: !selection.visible
 
51
    maximumFlickVelocity: height * 5
 
52
 
 
53
    /**
 
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.
 
57
     *
 
58
     */
 
59
    function navigationRequestedDelegate(request) { }
 
60
 
 
61
    UserAgent01 {
 
62
        id: userAgent
 
63
    }
 
64
 
 
65
    /**
 
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.
 
70
     */
 
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
 
77
        // slot.
 
78
        return undefined
 
79
    }
 
80
    experimental.userAgent: (_webview.getUAString() === undefined) ? userAgent.defaultUA : _webview.getUAString()
 
81
    onNavigationRequested: {
 
82
        request.action = WebView.AcceptRequest;
 
83
 
 
84
        navigationRequestedDelegate (request);
 
85
        if (request.action === WebView.IgnoreRequest)
 
86
            return;
 
87
 
 
88
        var staticUA = _webview.getUAString()
 
89
        if (staticUA === undefined) {
 
90
            _webview.experimental.userAgent = userAgent.getUAString(request.url)
 
91
        } else {
 
92
            _webview.experimental.userAgent = staticUA
 
93
        }
 
94
    }
 
95
 
 
96
    experimental.preferences.navigatorQtObjectEnabled: true
 
97
    experimental.userScripts: [Qt.resolvedUrl("hyperlinks.js"),
 
98
                               Qt.resolvedUrl("selection01.js")]
 
99
    experimental.onMessageReceived: {
 
100
        var data = null
 
101
        try {
 
102
            data = JSON.parse(message.data)
 
103
        } catch (error) {
 
104
            console.debug('DEBUG:', message.data)
 
105
            return
 
106
        }
 
107
        if ('event' in data) {
 
108
            if (data.event === 'longpress') {
 
109
                if (('img' in data) || ('href' in data)) {
 
110
                    contextualData.clear()
 
111
                    if ('img' in data) {
 
112
                        contextualData.img = data.img
 
113
                    }
 
114
                    if ('href' in data) {
 
115
                        contextualData.href = data.href
 
116
                        contextualData.title = data.title
 
117
                    }
 
118
                    contextualRectangle.position(data)
 
119
                    PopupUtils.open(contextualPopover, contextualRectangle)
 
120
                    return
 
121
                }
 
122
            }
 
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
 
128
                }
 
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
 
133
                }
 
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
 
139
                }
 
140
                selection.show(data.left, data.top, data.width, data.height)
 
141
            } else if (data.event === 'newtab') {
 
142
                newTabRequested(data.url)
 
143
            }
 
144
        }
 
145
    }
 
146
 
 
147
    experimental.itemSelector: ItemSelector01 {}
 
148
 
 
149
    property alias selection: selection
 
150
    property ActionList selectionActions
 
151
    Selection {
 
152
        id: selection
 
153
 
 
154
        anchors.fill: parent
 
155
        visible: false
 
156
 
 
157
        property Item __popover: null
 
158
        property var mimedata: null
 
159
 
 
160
        Component {
 
161
            id: selectionPopover
 
162
            ActionSelectionPopover {
 
163
                grabDismissAreaEvents: false
 
164
                actions: selectionActions
 
165
            }
 
166
        }
 
167
 
 
168
        function createData() {
 
169
            if (mimedata === null) {
 
170
                mimedata = Clipboard.newData()
 
171
            }
 
172
        }
 
173
 
 
174
        function clearData() {
 
175
            if (mimedata !== null) {
 
176
                delete mimedata
 
177
                mimedata = null
 
178
            }
 
179
        }
 
180
 
 
181
        function actionTriggered() {
 
182
            selection.visible = false
 
183
        }
 
184
 
 
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)
 
190
            }
 
191
        }
 
192
 
 
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
 
199
            visible = true
 
200
            __showPopover()
 
201
        }
 
202
 
 
203
        onVisibleChanged: {
 
204
            if (!visible && (__popover != null)) {
 
205
                PopupUtils.close(__popover)
 
206
                __popover = null
 
207
            }
 
208
        }
 
209
 
 
210
        onResized: {
 
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))
 
220
        }
 
221
 
 
222
        function copy() {
 
223
            Clipboard.push(mimedata)
 
224
            clearData()
 
225
        }
 
226
    }
 
227
 
 
228
    Item {
 
229
        id: contextualRectangle
 
230
 
 
231
        visible: false
 
232
 
 
233
        function position(data) {
 
234
            var scale = _webview.experimental.test.contentsScale * _webview.experimental.test.devicePixelRatio
 
235
            x = data.left * scale
 
236
            y = data.top * scale
 
237
            width = data.width * scale
 
238
            height = data.height * scale
 
239
        }
 
240
    }
 
241
    property QtObject contextualData: QtObject {
 
242
        property url href
 
243
        property string title
 
244
        property url img
 
245
 
 
246
        function clear() {
 
247
            href = ''
 
248
            title = ''
 
249
            img = ''
 
250
        }
 
251
    }
 
252
 
 
253
    property ActionList contextualActions
 
254
    Component {
 
255
        id: contextualPopover
 
256
        ActionSelectionPopover {
 
257
            actions: contextualActions
 
258
        }
 
259
    }
 
260
 
 
261
    Scrollbar {
 
262
        parent: _webview.parent
 
263
        flickableItem: _webview
 
264
        align: Qt.AlignTrailing
 
265
    }
 
266
 
 
267
    Scrollbar {
 
268
        parent: _webview.parent
 
269
        flickableItem: _webview
 
270
        align: Qt.AlignBottom
 
271
    }
 
272
}