1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | #include <QQuickView>
|
25 | #include <QQuickItem>
|
26 |
|
27 | #include "inappbrowser.h"
|
28 | #include <cordova.h>
|
29 |
|
30 | Inappbrowser::Inappbrowser(Cordova *cordova): CPlugin(cordova), _eventCb(0) {
|
31 | }
|
32 |
|
33 | const char code[] = "\
|
34 | var component; \
|
35 | function createObject() { \
|
36 | component = Qt.createComponent(%1); \
|
37 | if (component.status == Component.Ready) \
|
38 | finishCreation(); \
|
39 | else \
|
40 | component.statusChanged.connect(finishCreation); \
|
41 | } \
|
42 | function finishCreation() { \
|
43 | CordovaWrapper.global.inappbrowser = component.createObject(root, \
|
44 | {root: root, cordova: cordova, url1: %2}); \
|
45 | } \
|
46 | createObject()";
|
47 |
|
48 | const char EXIT_EVENT[] = "{type: 'exit'}";
|
49 | const char LOADSTART_EVENT[] = "{type: 'loadstart'}";
|
50 | const char LOADSTOP_EVENT[] = "{type: 'loadstop'}";
|
51 | const char LOADERROR_EVENT[] = "{type: 'loaderror'}";
|
52 |
|
53 | void 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 |
|
64 | void Inappbrowser::show(int, int) {
|
65 | m_cordova->execQML("CordovaWrapper.global.inappbrowser.visible = true");
|
66 | }
|
67 |
|
68 | void Inappbrowser::hide(int, int) {
|
69 | m_cordova->execQML("CordovaWrapper.global.inappbrowser.visible = false");
|
70 | }
|
71 |
|
72 | void Inappbrowser::close(int, int) {
|
73 | m_cordova->execQML("CordovaWrapper.global.inappbrowser.destroy()");
|
74 | this->callbackWithoutRemove(_eventCb, EXIT_EVENT);
|
75 | _eventCb = 0;
|
76 | }
|
77 |
|
78 | void 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 |
|
85 | void 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 |
|
92 | void 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 |
|
99 | void 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 |
|
103 | void Inappbrowser::loadFinished(bool status) {
|
104 | if (!status) {
|
105 | this->callbackWithoutRemove(_eventCb, LOADSTOP_EVENT);
|
106 | } else {
|
107 | this->callbackWithoutRemove(_eventCb, LOADSTART_EVENT);
|
108 | }
|
109 | }
|