2013-10-16 21:13:03 +04:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright 2013 Canonical Ltd.
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QQuickView>
|
|
|
|
#include <QQuickItem>
|
|
|
|
|
|
|
|
#include "inappbrowser.h"
|
|
|
|
#include <cordova.h>
|
|
|
|
|
|
|
|
Inappbrowser::Inappbrowser(Cordova *cordova): CPlugin(cordova), _eventCb(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const char code[] = "\
|
2014-07-27 01:37:17 +04:00
|
|
|
var component; \
|
2013-10-16 21:13:03 +04:00
|
|
|
function createObject() { \
|
|
|
|
component = Qt.createComponent(%1); \
|
|
|
|
if (component.status == Component.Ready) \
|
|
|
|
finishCreation(); \
|
|
|
|
else \
|
|
|
|
component.statusChanged.connect(finishCreation); \
|
|
|
|
} \
|
|
|
|
function finishCreation() { \
|
2014-07-27 01:37:17 +04:00
|
|
|
CordovaWrapper.global.inappbrowser = component.createObject(root, \
|
2013-10-16 21:13:03 +04:00
|
|
|
{root: root, cordova: cordova, url1: %2}); \
|
|
|
|
} \
|
|
|
|
createObject()";
|
|
|
|
|
2014-10-18 02:36:31 +04:00
|
|
|
const char EXIT_EVENT[] = "{type: 'exit'}";
|
|
|
|
const char LOADSTART_EVENT[] = "{type: 'loadstart'}";
|
|
|
|
const char LOADSTOP_EVENT[] = "{type: 'loadstop'}";
|
|
|
|
const char LOADERROR_EVENT[] = "{type: 'loaderror'}";
|
2013-10-16 21:13:03 +04:00
|
|
|
|
2014-07-27 01:37:17 +04:00
|
|
|
void Inappbrowser::open(int cb, int, const QString &url, const QString &, const QString &) {
|
2013-10-16 21:13:03 +04:00
|
|
|
assert(_eventCb == 0);
|
|
|
|
|
|
|
|
_eventCb = cb;
|
|
|
|
|
|
|
|
QString path = m_cordova->get_app_dir() + "/../qml/InAppBrowser.qml";
|
|
|
|
QString qml = QString(code)
|
|
|
|
.arg(CordovaInternal::format(path)).arg(CordovaInternal::format(url));
|
|
|
|
m_cordova->execQML(qml);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inappbrowser::show(int, int) {
|
2014-07-27 01:37:17 +04:00
|
|
|
m_cordova->execQML("CordovaWrapper.global.inappbrowser.visible = true");
|
2013-10-16 21:13:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inappbrowser::close(int, int) {
|
2014-07-27 01:37:17 +04:00
|
|
|
m_cordova->execQML("CordovaWrapper.global.inappbrowser.destroy()");
|
2013-10-16 21:13:03 +04:00
|
|
|
this->callbackWithoutRemove(_eventCb, EXIT_EVENT);
|
|
|
|
_eventCb = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-18 02:36:31 +04:00
|
|
|
void Inappbrowser::injectStyleFile(int scId, int ecId, const QString& src, bool b) {
|
|
|
|
QString code("(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %1; d.head.appendChild(c);})(document)");
|
|
|
|
code = code.arg(CordovaInternal::format(src));
|
|
|
|
|
|
|
|
injectScriptCode(scId, ecId, code, b);
|
2013-10-16 21:13:03 +04:00
|
|
|
}
|
|
|
|
|
2014-10-18 02:36:31 +04:00
|
|
|
void Inappbrowser::injectStyleCode(int scId, int ecId, const QString& src, bool b) {
|
|
|
|
QString code("(function(d) { var c = d.createElement('style'); c.innerHTML = %1; d.body.appendChild(c); })(document)");
|
|
|
|
code = code.arg(CordovaInternal::format(src));
|
|
|
|
|
|
|
|
injectScriptCode(scId, ecId, code, b);
|
2013-10-16 21:13:03 +04:00
|
|
|
}
|
|
|
|
|
2014-10-18 02:36:31 +04:00
|
|
|
void Inappbrowser::injectScriptFile(int scId, int ecId, const QString& src, bool b) {
|
|
|
|
QString code("(function(d) { var c = d.createElement('script'); c.src = %1; d.body.appendChild(c);})(document)");
|
|
|
|
code = code.arg(CordovaInternal::format(src));
|
|
|
|
|
|
|
|
injectScriptCode(scId, ecId, code, b);
|
2013-10-16 21:13:03 +04:00
|
|
|
}
|
|
|
|
|
2014-10-18 02:36:31 +04:00
|
|
|
void Inappbrowser::injectScriptCode(int scId, int, const QString& code, bool) {
|
|
|
|
m_cordova->execQML(QString("CordovaWrapper.global.inappbrowser.executeJS(%2, %1)").arg(CordovaInternal::format(code)).arg(scId));
|
2013-10-16 21:13:03 +04:00
|
|
|
}
|
|
|
|
|
2014-10-17 01:45:03 +04:00
|
|
|
void Inappbrowser::loadFinished(bool status) {
|
2014-10-18 02:36:31 +04:00
|
|
|
if (!status) {
|
2013-10-16 21:13:03 +04:00
|
|
|
this->callbackWithoutRemove(_eventCb, LOADSTOP_EVENT);
|
2014-10-17 01:45:03 +04:00
|
|
|
} else {
|
|
|
|
this->callbackWithoutRemove(_eventCb, LOADSTART_EVENT);
|
2013-10-16 21:13:03 +04:00
|
|
|
}
|
|
|
|
}
|