~knitzsche/+junk/tabtest

« back to all changes in this revision

Viewing changes to fix-page-tab-issue/src/plugin/Ubuntu/WebApps/CordovaLoader.qml

  • Committer: Kyle Nitzsche
  • Date: 2014-02-28 21:45:04 UTC
  • Revision ID: kyle.nitzsche@canonical.com-20140228214504-nzkax4md3v35r6p2
working test

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014 Canonical Ltd.
 
3
 *
 
4
 * This file is part of ubuntu-html5-ui-toolkit.
 
5
 *
 
6
 * ubuntu-html5-ui-toolkit 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
 * ubuntu-html5-ui-toolkit 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
 
 
21
Item {
 
22
    id: root
 
23
 
 
24
    property string htmlIndexDirectory
 
25
 
 
26
    // Cordova plugin instance
 
27
    property var cordovaInstance: null
 
28
 
 
29
    // the cordova qml instance was created successfully
 
30
    signal created();
 
31
 
 
32
    // an error occured while creating the cordova instance
 
33
    signal creationError();
 
34
 
 
35
    function _tryCreateObject(statement, parent) {
 
36
        var result = null;
 
37
        try {
 
38
            result = Qt.createQmlObject(statement, parent);
 
39
        }
 
40
        catch (e) {};
 
41
 
 
42
        return result;
 
43
    }
 
44
 
 
45
    function _getCordovaObjectCreationStatementFor(version, params) {
 
46
        return 'import CordovaUbuntu '
 
47
                + version
 
48
                + '; CordovaView { '
 
49
                + ' anchors.fill: parent; '
 
50
                + params + ';'
 
51
                + '}';
 
52
    }
 
53
 
 
54
    function _tryCreateCordovaObject(version, params) {
 
55
        var _params = params || '';
 
56
        return _tryCreateObject(_getCordovaObjectCreationStatementFor(version, params), root);
 
57
    }
 
58
 
 
59
    function _ensureCordovaInitDone() {
 
60
        if (cordovaInstance && htmlIndexDirectory.length !== 0) {
 
61
            cordovaInstance.wwwDir = htmlIndexDirectory;
 
62
        }
 
63
    }
 
64
 
 
65
    onHtmlIndexDirectoryChanged: _ensureCordovaInitDone()
 
66
 
 
67
    Component.onCompleted: {
 
68
        // selectively try to load cordova
 
69
        var cordova = null;
 
70
 
 
71
        var candidates = [{version: '3.4', paramString: 'contentFile: "index.html"'}];
 
72
        for (var i = 0; i < candidates.length; ++i) {
 
73
            cordova = _tryCreateCordovaObject(candidates[i].version,
 
74
                                              candidates[i].paramString);
 
75
            if (cordova)
 
76
                break;
 
77
        }
 
78
 
 
79
        if ( ! cordova) {
 
80
            console.error('Cannot create CordovaView object.');
 
81
            creationError();
 
82
            return;
 
83
        }
 
84
 
 
85
        root.cordovaInstance = cordova;
 
86
 
 
87
        _ensureCordovaInitDone();
 
88
 
 
89
        created();
 
90
    }
 
91
}