UNPKG

4.31 kBtext/x-cView Raw
1/*
2 *
3 * Copyright 2013 Canonical Ltd.
4 *
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 *
22*/
23
24#include <QQuickView>
25#include <QQuickItem>
26
27#include "inappbrowser.h"
28#include <cordova.h>
29
30Inappbrowser::Inappbrowser(Cordova *cordova): CPlugin(cordova), _eventCb(0) {
31}
32
33const char code[] = "\
34var component; \
35function createObject() { \
36 component = Qt.createComponent(%1); \
37 if (component.status == Component.Ready) \
38 finishCreation(); \
39 else \
40 component.statusChanged.connect(finishCreation); \
41} \
42function finishCreation() { \
43 CordovaWrapper.global.inappbrowser = component.createObject(root, \
44 {root: root, cordova: cordova, url1: %2}); \
45} \
46createObject()";
47
48const char EXIT_EVENT[] = "{type: 'exit'}";
49const char LOADSTART_EVENT[] = "{type: 'loadstart'}";
50const char LOADSTOP_EVENT[] = "{type: 'loadstop'}";
51const char LOADERROR_EVENT[] = "{type: 'loaderror'}";
52
53void Inappbrowser::open(int cb, int, const QString &url, const QString &, const QString &) {
54 assert(_eventCb == 0);
55
56 _eventCb = cb;
57
58 QString path = m_cordova->get_app_dir() + "/../qml/InAppBrowser.qml";
59 QString qml = QString(code)
60 .arg(CordovaInternal::format(path)).arg(CordovaInternal::format(url));
61 m_cordova->execQML(qml);
62}
63
64void Inappbrowser::show(int, int) {
65 m_cordova->execQML("CordovaWrapper.global.inappbrowser.visible = true");
66}
67
68void Inappbrowser::hide(int, int) {
69 m_cordova->execQML("CordovaWrapper.global.inappbrowser.visible = false");
70}
71
72void Inappbrowser::close(int, int) {
73 m_cordova->execQML("CordovaWrapper.global.inappbrowser.destroy()");
74 this->callbackWithoutRemove(_eventCb, EXIT_EVENT);
75 _eventCb = 0;
76}
77
78void Inappbrowser::injectStyleFile(int scId, int ecId, const QString& src, bool b) {
79 QString code("(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %1; d.head.appendChild(c);})(document)");
80 code = code.arg(CordovaInternal::format(src));
81
82 injectScriptCode(scId, ecId, code, b);
83}
84
85void Inappbrowser::injectStyleCode(int scId, int ecId, const QString& src, bool b) {
86 QString code("(function(d) { var c = d.createElement('style'); c.innerHTML = %1; d.body.appendChild(c); })(document)");
87 code = code.arg(CordovaInternal::format(src));
88
89 injectScriptCode(scId, ecId, code, b);
90}
91
92void Inappbrowser::injectScriptFile(int scId, int ecId, const QString& src, bool b) {
93 QString code("(function(d) { var c = d.createElement('script'); c.src = %1; d.body.appendChild(c);})(document)");
94 code = code.arg(CordovaInternal::format(src));
95
96 injectScriptCode(scId, ecId, code, b);
97}
98
99void Inappbrowser::injectScriptCode(int scId, int, const QString& code, bool) {
100 m_cordova->execQML(QString("CordovaWrapper.global.inappbrowser.executeJS(%2, %1)").arg(CordovaInternal::format(code)).arg(scId));
101}
102
103void Inappbrowser::loadFinished(bool status) {
104 if (!status) {
105 this->callbackWithoutRemove(_eventCb, LOADSTOP_EVENT);
106 } else {
107 this->callbackWithoutRemove(_eventCb, LOADSTART_EVENT);
108 }
109}