~ubuntu-branches/ubuntu/saucy/webbrowser-app/saucy-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2013-07-31 10:35:29 UTC
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: package-import@ubuntu.com-20130731103529-y3pnb53u111mpqep
Tags: upstream-0.22+13.10.20130731.1
ImportĀ upstreamĀ versionĀ 0.22+13.10.20130731.1

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.0
21
 
import Ubuntu.Components 0.1
22
 
import Ubuntu.Components.Extras.Browser 0.1
23
 
import Ubuntu.Unity.Action 1.0 as UnityActions
24
 
 
25
 
FocusScope {
26
 
    id: browser
27
 
 
28
 
    property bool chromeless: false
29
 
    property real qtwebkitdpr
30
 
    property bool developerExtrasEnabled: false
31
 
    // necessary so that all widgets (including popovers) follow that
32
 
    property alias automaticOrientation: orientationHelper.automaticOrientation
33
 
 
34
 
    property alias currentIndex: tabsModel.currentIndex
35
 
    property alias currentWebview: tabsModel.currentWebview
36
 
    property string title: currentWebview ? currentWebview.title : ""
37
 
 
38
 
    focus: true
39
 
 
40
 
    UnityActions.ActionManager {
41
 
        actions: [
42
 
            UnityActions.Action {
43
 
                text: i18n.tr("Goto")
44
 
                // TRANSLATORS: This is a free-form list of keywords associated to the 'Goto' action.
45
 
                // Keywords may actually be sentences, and must be separated by semi-colons.
46
 
                keywords: i18n.tr("Address;URL;www")
47
 
                parameterType: UnityActions.Action.String
48
 
                onTriggered: currentWebview.url = value
49
 
            },
50
 
            UnityActions.Action {
51
 
                text: i18n.tr("Back")
52
 
                // TRANSLATORS: This is a free-form list of keywords associated to the 'Back' action.
53
 
                // Keywords may actually be sentences, and must be separated by semi-colons.
54
 
                keywords: i18n.tr("Older Page")
55
 
                enabled: currentWebview ? currentWebview.canGoBack : false
56
 
                onTriggered: currentWebview.goBack()
57
 
            },
58
 
            UnityActions.Action {
59
 
                text: i18n.tr("Forward")
60
 
                // TRANSLATORS: This is a free-form list of keywords associated to the 'Forward' action.
61
 
                // Keywords may actually be sentences, and must be separated by semi-colons.
62
 
                keywords: i18n.tr("Newer Page")
63
 
                enabled: currentWebview ? currentWebview.canGoForward : false
64
 
                onTriggered: currentWebview.goForward()
65
 
            },
66
 
            UnityActions.Action {
67
 
                text: i18n.tr("Reload")
68
 
                // TRANSLATORS: This is a free-form list of keywords associated to the 'Reload' action.
69
 
                // Keywords may actually be sentences, and must be separated by semi-colons.
70
 
                keywords: i18n.tr("Leave Page")
71
 
                enabled: currentWebview != null
72
 
                onTriggered: currentWebview.reload()
73
 
            },
74
 
            UnityActions.Action {
75
 
                text: i18n.tr("Bookmark")
76
 
                // TRANSLATORS: This is a free-form list of keywords associated to the 'Bookmark' action.
77
 
                // Keywords may actually be sentences, and must be separated by semi-colons.
78
 
                keywords: i18n.tr("Add This Page to Bookmarks")
79
 
                enabled: false // TODO: implement bookmarks
80
 
            },
81
 
            UnityActions.Action {
82
 
                text: i18n.tr("New Tab")
83
 
                // TRANSLATORS: This is a free-form list of keywords associated to the 'New Tab' action.
84
 
                // Keywords may actually be sentences, and must be separated by semi-colons.
85
 
                keywords: i18n.tr("Open a New Tab")
86
 
                onTriggered: browser.newTab("", true)
87
 
            }
88
 
        ]
89
 
    }
90
 
 
91
 
    OrientationHelper {
92
 
        id: orientationHelper
93
 
 
94
 
        Item {
95
 
            id: webviewContainer
96
 
            anchors {
97
 
                left: parent.left
98
 
                right: parent.right
99
 
                top: parent.top
100
 
                bottom: osk.top
101
 
            }
102
 
            visible: !activityView.visible
103
 
        }
104
 
 
105
 
        ErrorSheet {
106
 
            anchors.fill: webviewContainer
107
 
            visible: currentWebview ? (currentWebview.lastLoadRequestStatus == WebView.LoadFailedStatus) : false
108
 
            url: currentWebview ? currentWebview.url : ""
109
 
            onRefreshClicked: currentWebview.reload()
110
 
        }
111
 
 
112
 
        ActivityView {
113
 
            id: activityView
114
 
 
115
 
            anchors.fill: parent
116
 
            visible: false
117
 
            tabsModel: tabsModel
118
 
            historyModel: historyModel
119
 
 
120
 
            onHistoryEntryRequested: {
121
 
                currentWebview.url = url
122
 
                visible = false
123
 
            }
124
 
            onNewTabRequested: {
125
 
                browser.newTab("", true)
126
 
                visible = false
127
 
            }
128
 
            onSwitchToTabRequested: {
129
 
                browser.switchToTab(index)
130
 
                visible = false
131
 
            }
132
 
            onCloseTabRequested: {
133
 
                browser.closeTab(index)
134
 
                if (tabsModel.count == 0) {
135
 
                    newTabRequested()
136
 
                }
137
 
            }
138
 
        }
139
 
 
140
 
        Loader {
141
 
            id: panel
142
 
 
143
 
            property Item chrome: item ? item.contents[0] : null
144
 
 
145
 
            sourceComponent: browser.chromeless ? undefined : panelComponent
146
 
 
147
 
            anchors {
148
 
                left: parent.left
149
 
                right: parent.right
150
 
                bottom: (item && item.opened) ? osk.top : parent.bottom
151
 
            }
152
 
 
153
 
            Component {
154
 
                id: panelComponent
155
 
 
156
 
                Panel {
157
 
                    anchors {
158
 
                        left: parent ? parent.left : undefined
159
 
                        right: parent ? parent.right : undefined
160
 
                        bottom: parent ? parent.bottom : undefined
161
 
                    }
162
 
                    height: units.gu(8)
163
 
 
164
 
                    opened: true
165
 
                    onOpenedChanged: {
166
 
                        if (!opened) {
167
 
                            Qt.inputMethod.hide()
168
 
                        }
169
 
                    }
170
 
 
171
 
                    Chrome {
172
 
                        anchors.fill: parent
173
 
 
174
 
                        url: currentWebview ? currentWebview.url : ""
175
 
 
176
 
                        loading: currentWebview ? currentWebview.loading || (currentWebview.loadProgress == 0) : false
177
 
                        loadProgress: currentWebview ? currentWebview.loadProgress : 0
178
 
 
179
 
                        canGoBack: currentWebview ? currentWebview.canGoBack : false
180
 
                        onGoBackClicked: currentWebview.goBack()
181
 
 
182
 
                        canGoForward: currentWebview ? currentWebview.canGoForward : false
183
 
                        onGoForwardClicked: currentWebview.goForward()
184
 
 
185
 
                        onUrlValidated: currentWebview.url = url
186
 
 
187
 
                        property bool stopped: false
188
 
                        onLoadingChanged: {
189
 
                            if (loading) {
190
 
                                if (panel.item) {
191
 
                                    panel.item.opened = true
192
 
                                }
193
 
                            } else if (stopped) {
194
 
                                stopped = false
195
 
                            } else if (!addressBar.activeFocus) {
196
 
                                if (panel.item) {
197
 
                                    panel.item.opened = false
198
 
                                }
199
 
                                if (currentWebview) {
200
 
                                    currentWebview.forceActiveFocus()
201
 
                                }
202
 
                            }
203
 
                        }
204
 
 
205
 
                        onRequestReload: currentWebview.reload()
206
 
                        onRequestStop: {
207
 
                            stopped = true
208
 
                            currentWebview.stop()
209
 
                        }
210
 
 
211
 
                        onToggleTabsClicked: {
212
 
                            activityView.visible = !activityView.visible
213
 
                            if (activityView.visible) {
214
 
                                currentWebview.forceActiveFocus()
215
 
                                panel.item.opened = false
216
 
                            }
217
 
                        }
218
 
                    }
219
 
                }
220
 
            }
221
 
        }
222
 
 
223
 
        Suggestions {
224
 
            opacity: (panel.chrome && (panel.item.state == "spread") &&
225
 
                      panel.chrome.addressBar.activeFocus && (count > 0)) ? 1.0 : 0.0
226
 
            Behavior on opacity {
227
 
                UbuntuNumberAnimation {}
228
 
            }
229
 
            enabled: opacity > 0
230
 
            anchors {
231
 
                bottom: panel.top
232
 
                horizontalCenter: parent.horizontalCenter
233
 
            }
234
 
            width: panel.width - units.gu(5)
235
 
            height: Math.min(contentHeight, panel.y - units.gu(2))
236
 
            model: historyMatches
237
 
            onSelected: {
238
 
                currentWebview.url = url
239
 
                currentWebview.forceActiveFocus()
240
 
            }
241
 
        }
242
 
 
243
 
        KeyboardRectangle {
244
 
            id: osk
245
 
        }
246
 
    }
247
 
 
248
 
    HistoryModel {
249
 
        id: historyModel
250
 
        databasePath: dataLocation + "/history.sqlite"
251
 
    }
252
 
 
253
 
    HistoryMatchesModel {
254
 
        id: historyMatches
255
 
        sourceModel: historyModel
256
 
        query: panel.chrome ? panel.chrome.addressBar.text : ""
257
 
    }
258
 
 
259
 
    TabsModel {
260
 
        id: tabsModel
261
 
    }
262
 
 
263
 
    Component {
264
 
        id: webviewComponent
265
 
 
266
 
        UbuntuWebView {
267
 
            id: webview
268
 
 
269
 
            anchors.fill: parent
270
 
 
271
 
            enabled: !activityView.visible
272
 
            visible: tabsModel.currentWebview === webview
273
 
 
274
 
            devicePixelRatio: browser.qtwebkitdpr
275
 
 
276
 
            experimental.preferences.developerExtrasEnabled: browser.developerExtrasEnabled
277
 
 
278
 
            property int lastLoadRequestStatus: -1
279
 
            onLoadingChanged: {
280
 
                lastLoadRequestStatus = loadRequest.status
281
 
                if (loadRequest.status === WebView.LoadSucceededStatus) {
282
 
                    historyModel.add(webview.url, webview.title, webview.icon)
283
 
                }
284
 
            }
285
 
 
286
 
            onNewTabRequested: browser.newTab(url, true)
287
 
        }
288
 
    }
289
 
 
290
 
    function newTab(url, setCurrent) {
291
 
        var webview = webviewComponent.createObject(webviewContainer, {"url": url})
292
 
        var index = tabsModel.add(webview)
293
 
        if (setCurrent) {
294
 
            tabsModel.currentIndex = index
295
 
            if (!browser.chromeless) {
296
 
                if (!url) {
297
 
                    panel.chrome.addressBar.forceActiveFocus()
298
 
                    panel.item.opened = true
299
 
                }
300
 
            }
301
 
        }
302
 
    }
303
 
 
304
 
    function closeTab(index) {
305
 
        var webview = tabsModel.remove(index)
306
 
        if (webview) {
307
 
            webview.destroy()
308
 
        }
309
 
    }
310
 
 
311
 
    function switchToTab(index) {
312
 
        tabsModel.currentIndex = index
313
 
        currentWebview.forceActiveFocus()
314
 
    }
315
 
}