-
Notifications
You must be signed in to change notification settings - Fork 2
/
WebPuppeteerSys.cpp
163 lines (133 loc) · 4.51 KB
/
WebPuppeteerSys.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "WebPuppeteerSys.hpp"
#include "WebPuppeteerTab.hpp"
#include "WebPuppeteer.hpp"
#include <QEventLoop>
#include <QTimer>
#include <QNetworkReply>
#include <QDateTime>
#include <QFile>
#include <QMessageBox>
#include <QProcess>
WebPuppeteerSys::WebPuppeteerSys(WebPuppeteer *_parent): QObject(_parent) {
parent = _parent;
tmp_e = NULL;
}
void WebPuppeteerSys::log(const QString &msg) {
qDebug("%s", qPrintable(msg));
}
void WebPuppeteerSys::sleep(int msecs) {
QEventLoop e;
QTimer::singleShot(msecs, &e, SLOT(quit()));
e.exec();
}
QScriptValue WebPuppeteerSys::getenv(const QString &var) const {
return QString::fromUtf8(qgetenv(qPrintable(var)));
}
QScriptValue WebPuppeteerSys::newTab() {
// open a new "browser tab", return it
WebPuppeteerTab *tab = new WebPuppeteerTab(parent);
return parent->engine().newQObject(tab, QScriptEngine::ScriptOwnership, QScriptEngine::ExcludeChildObjects | QScriptEngine::ExcludeSuperClassContents);
}
QScriptValue WebPuppeteerSys::get(const QString &url) {
QNetworkRequest req(url);
QNetworkReply *rep = net.get(req);
QEventLoop e;
connect(rep, SIGNAL(finished()), &e, SLOT(quit()));
e.exec();
if (rep->error() != QNetworkReply::NoError) {
qDebug("GET error: %s", qPrintable(rep->errorString()));
rep->deleteLater();
return parent->engine().currentContext()->throwError(QScriptContext::UnknownError, rep->errorString());
}
rep->deleteLater();
return parent->engine().newVariant((rep->readAll()));
}
QScriptValue WebPuppeteerSys::post(const QString &url, const QString &post, const QString content_type) {
QByteArray data_post;
data_post = post.toUtf8();
QNetworkRequest req(url);
req.setHeader(QNetworkRequest::ContentTypeHeader, content_type);
//req.setRawHeader("Rest-Key", api_key.toLatin1());
QNetworkReply *rep = net.post(req, data_post);
QEventLoop e;
connect(rep, SIGNAL(finished()), &e, SLOT(quit()));
e.exec();
if (rep->error() != QNetworkReply::NoError) {
qDebug("GET error: %s (body: %s)", qPrintable(rep->errorString()), qPrintable(rep->readAll()));
rep->deleteLater();
return parent->engine().currentContext()->throwError(QScriptContext::UnknownError, rep->errorString());
}
rep->deleteLater();
return parent->engine().newVariant((rep->readAll()));
}
void WebPuppeteerSys::alert(QString string) {
QMessageBox::information(NULL, "WebPuppeteer alert", string);
}
void WebPuppeteerSys::alertcb(QString string, QScriptValue cb) {
tmp_cb = cb;
QMessageBox mb(QMessageBox::Information, "WebPuppeteer alert", string, QMessageBox::Ok);
QEventLoop e;
QTimer t;
tmp_e = &e;
t.setSingleShot(false);
t.setInterval(250);
t.start();
connect(&mb, SIGNAL(buttonClicked(QAbstractButton*)), &e, SLOT(quit()));
connect(&t, SIGNAL(timeout()), this, SLOT(alertcb_cb()));
mb.show();
e.exec();
tmp_e = NULL;
}
void WebPuppeteerSys::alertcb_cb() {
if (tmp_e == NULL) return;
if (tmp_cb.call().toBool()) tmp_e->quit();
}
bool WebPuppeteerSys::confirm(QString string) {
return QMessageBox::question(NULL, "WebPuppeteer question", string, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes;
}
void WebPuppeteerSys::quit() {
parent->exit(0);
}
void WebPuppeteerSys::abort() {
parent->exit(1);
}
QString WebPuppeteerSys::fileGetContents(QString filename) {
QFile f(filename);
if (!f.open(QIODevice::ReadOnly)) return QString();
return QString::fromUtf8(f.readAll());
}
QString WebPuppeteerSys::fileGetContentsB64(QString filename) {
QFile f(filename);
if (!f.open(QIODevice::ReadOnly)) return QString();
return QString::fromLatin1(f.readAll().toBase64());
}
bool WebPuppeteerSys::filePutContents(QString filename, QString data) {
QFile f(filename);
if (!f.open(QIODevice::WriteOnly)) return false;
f.write(data.toUtf8());
f.close();
return true;
}
bool WebPuppeteerSys::filePutContentsB64(QString filename, QString data) {
QFile f(filename);
if (!f.open(QIODevice::WriteOnly)) return false;
f.write(QByteArray::fromBase64(data.toLatin1()));
f.close();
return true;
}
QScriptValue WebPuppeteerSys::include(QString filename) {
QFile f(filename);
if (!f.exists()) {
return parent->engine().currentContext()->throwError(QScriptContext::RangeError, "Include file not found");
}
if (!f.open(QIODevice::ReadOnly)) {
return parent->engine().currentContext()->throwError(QScriptContext::UnknownError, "Could not open file for reading");
}
return parent->engine().evaluate(QString::fromUtf8(f.readAll()), filename);
}
QString WebPuppeteerSys::exec(QString str) {
QProcess p;
p.start(str);
p.waitForFinished();
return QString::fromUtf8(p.readAll());
}