1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
#ifndef JSKITMANAGER_P_H
#define JSKITMANAGER_P_H
#include <QSettings>
#include <QNetworkRequest>
#include <QNetworkReply>
#include "jskitmanager.h"
class JSKitPebble : public QObject
{
Q_OBJECT
LOG4QT_DECLARE_QCLASS_LOGGER
public:
explicit JSKitPebble(const AppInfo &appInfo, JSKitManager *mgr);
Q_INVOKABLE void addEventListener(const QString &type, QJSValue function);
Q_INVOKABLE void removeEventListener(const QString &type, QJSValue function);
Q_INVOKABLE void sendAppMessage(QJSValue message, QJSValue callbackForAck = QJSValue(), QJSValue callbackForNack = QJSValue());
Q_INVOKABLE void showSimpleNotificationOnPebble(const QString &title, const QString &body);
Q_INVOKABLE void openURL(const QUrl &url);
Q_INVOKABLE QJSValue createXMLHttpRequest();
void invokeCallbacks(const QString &type, const QJSValueList &args = QJSValueList());
private:
AppInfo _appInfo;
JSKitManager *_mgr;
QHash<QString, QList<QJSValue>> _callbacks;
};
class JSKitConsole : public QObject
{
Q_OBJECT
LOG4QT_DECLARE_QCLASS_LOGGER
public:
explicit JSKitConsole(JSKitManager *mgr);
Q_INVOKABLE void log(const QString &msg);
};
class JSKitLocalStorage : public QObject
{
Q_OBJECT
Q_PROPERTY(int length READ length NOTIFY lengthChanged)
public:
explicit JSKitLocalStorage(const QUuid &uuid, JSKitManager *mgr);
int length() const;
Q_INVOKABLE QJSValue getItem(const QString &key) const;
Q_INVOKABLE void setItem(const QString &key, const QString &value);
Q_INVOKABLE void removeItem(const QString &key);
Q_INVOKABLE void clear();
signals:
void lengthChanged();
private:
void checkLengthChanged();
static QString getStorageFileFor(const QUuid &uuid);
private:
QSettings *_storage;
int _len;
};
class JSKitXMLHttpRequest : public QObject
{
Q_OBJECT
LOG4QT_DECLARE_QCLASS_LOGGER
Q_PROPERTY(QJSValue onload READ onload WRITE setOnload)
Q_PROPERTY(QJSValue ontimeout READ ontimeout WRITE setOntimeout)
Q_PROPERTY(QJSValue onerror READ onerror WRITE setOnerror)
Q_PROPERTY(unsigned short readyState READ readyState NOTIFY readyStateChanged)
Q_PROPERTY(unsigned short status READ status NOTIFY statusChanged)
Q_PROPERTY(QString responseText READ responseText NOTIFY responseTextChanged)
public:
explicit JSKitXMLHttpRequest(JSKitManager *mgr, QObject *parent = 0);
~JSKitXMLHttpRequest();
enum ReadyStates {
UNSENT = 0,
OPENED = 1,
HEADERS_RECEIVED = 2,
LOADING = 3,
DONE = 4
};
Q_INVOKABLE void open(const QString &method, const QString &url, bool async);
Q_INVOKABLE void setRequestHeader(const QString &header, const QString &value);
Q_INVOKABLE void send(const QString &body);
Q_INVOKABLE void abort();
QJSValue onload() const;
void setOnload(const QJSValue &value);
QJSValue ontimeout() const;
void setOntimeout(const QJSValue &value);
QJSValue onerror() const;
void setOnerror(const QJSValue &value);
unsigned short readyState() const;
unsigned short status() const;
QString responseText() const;
signals:
void readyStateChanged();
void statusChanged();
void responseTextChanged();
private slots:
void handleReplyFinished();
void handleReplyError(QNetworkReply::NetworkError code);
private:
JSKitManager *_mgr;
QNetworkAccessManager *_net;
QString _verb;
QNetworkRequest _request;
QNetworkReply *_reply;
QByteArray _response;
QJSValue _onload;
QJSValue _ontimeout;
QJSValue _onerror;
};
#endif // JSKITMANAGER_P_H
|