2011-02-18 22:53:06 -05:00
|
|
|
#include "WebPage.h"
|
2011-02-25 22:57:55 -05:00
|
|
|
#include "JavascriptInvocation.h"
|
2011-06-08 05:36:45 -04:00
|
|
|
#include "NetworkAccessManager.h"
|
2011-09-24 17:25:15 -04:00
|
|
|
#include "NetworkCookieJar.h"
|
2011-09-15 11:22:10 -04:00
|
|
|
#include "UnsupportedContentHandler.h"
|
2011-02-25 22:32:22 -05:00
|
|
|
#include <QResource>
|
2011-02-26 13:02:43 -05:00
|
|
|
#include <iostream>
|
2011-02-18 22:53:06 -05:00
|
|
|
|
|
|
|
WebPage::WebPage(QObject *parent) : QWebPage(parent) {
|
2011-09-15 11:22:10 -04:00
|
|
|
setForwardUnsupportedContent(true);
|
2011-08-12 15:49:40 -04:00
|
|
|
loadJavascript();
|
2011-08-12 15:26:26 -04:00
|
|
|
setUserStylesheet();
|
2011-08-12 15:49:40 -04:00
|
|
|
|
|
|
|
m_loading = false;
|
2011-08-23 11:18:08 -04:00
|
|
|
this->setCustomNetworkAccessManager();
|
2011-08-12 15:49:40 -04:00
|
|
|
|
|
|
|
connect(this, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
|
|
|
|
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
|
|
|
|
connect(this, SIGNAL(frameCreated(QWebFrame *)),
|
|
|
|
this, SLOT(frameCreated(QWebFrame *)));
|
2011-09-15 11:22:10 -04:00
|
|
|
connect(this, SIGNAL(unsupportedContent(QNetworkReply*)),
|
|
|
|
this, SLOT(handleUnsupportedContent(QNetworkReply*)));
|
2011-08-12 15:49:40 -04:00
|
|
|
}
|
|
|
|
|
2011-08-23 11:18:08 -04:00
|
|
|
void WebPage::setCustomNetworkAccessManager() {
|
|
|
|
NetworkAccessManager *manager = new NetworkAccessManager();
|
2011-09-24 17:25:15 -04:00
|
|
|
manager->setCookieJar(new NetworkCookieJar());
|
2011-08-23 11:18:08 -04:00
|
|
|
this->setNetworkAccessManager(manager);
|
|
|
|
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
|
|
|
|
}
|
|
|
|
|
2011-08-12 15:49:40 -04:00
|
|
|
void WebPage::loadJavascript() {
|
2011-02-25 22:32:22 -05:00
|
|
|
QResource javascript(":/capybara.js");
|
2011-05-03 18:12:10 -04:00
|
|
|
if (javascript.isCompressed()) {
|
|
|
|
QByteArray uncompressedBytes(qUncompress(javascript.data(), javascript.size()));
|
|
|
|
m_capybaraJavascript = QString(uncompressedBytes);
|
|
|
|
} else {
|
|
|
|
char * javascriptString = new char[javascript.size() + 1];
|
|
|
|
strcpy(javascriptString, (const char *)javascript.data());
|
|
|
|
javascriptString[javascript.size()] = 0;
|
|
|
|
m_capybaraJavascript = javascriptString;
|
|
|
|
}
|
2011-04-19 03:16:25 -04:00
|
|
|
}
|
|
|
|
|
2011-08-12 15:26:26 -04:00
|
|
|
void WebPage::setUserStylesheet() {
|
|
|
|
QString data = QString("* { font-family: 'Arial' ! important; }").toUtf8().toBase64();
|
|
|
|
QUrl url = QUrl(QString("data:text/css;charset=utf-8;base64,") + data);
|
|
|
|
settings()->setUserStyleSheetUrl(url);
|
|
|
|
}
|
|
|
|
|
2011-06-08 05:36:45 -04:00
|
|
|
QString WebPage::userAgentForUrl(const QUrl &url ) const {
|
2011-06-25 07:31:19 -04:00
|
|
|
if (!m_userAgent.isEmpty()) {
|
|
|
|
return m_userAgent;
|
2011-06-08 05:36:45 -04:00
|
|
|
} else {
|
|
|
|
return QWebPage::userAgentForUrl(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-25 07:31:19 -04:00
|
|
|
void WebPage::setUserAgent(QString userAgent) {
|
|
|
|
m_userAgent = userAgent;
|
2011-06-08 05:36:45 -04:00
|
|
|
}
|
|
|
|
|
2011-04-19 03:16:25 -04:00
|
|
|
void WebPage::frameCreated(QWebFrame * frame) {
|
|
|
|
connect(frame, SIGNAL(javaScriptWindowObjectCleared()),
|
|
|
|
this, SLOT(injectJavascriptHelpers()));
|
2011-02-25 22:32:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebPage::injectJavascriptHelpers() {
|
2011-04-19 03:16:25 -04:00
|
|
|
QWebFrame* frame = qobject_cast<QWebFrame *>(QObject::sender());
|
|
|
|
frame->evaluateJavaScript(m_capybaraJavascript);
|
2011-02-18 22:53:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebPage::shouldInterruptJavaScript() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-25 22:57:55 -05:00
|
|
|
QVariant WebPage::invokeCapybaraFunction(const char *name, QStringList &arguments) {
|
|
|
|
QString qname(name);
|
|
|
|
QString objectName("CapybaraInvocation");
|
|
|
|
JavascriptInvocation invocation(qname, arguments);
|
2011-04-19 03:16:25 -04:00
|
|
|
currentFrame()->addToJavaScriptWindowObject(objectName, &invocation);
|
2011-02-25 22:57:55 -05:00
|
|
|
QString javascript = QString("Capybara.invoke()");
|
2011-04-19 03:16:25 -04:00
|
|
|
return currentFrame()->evaluateJavaScript(javascript);
|
2011-02-25 22:57:55 -05:00
|
|
|
}
|
|
|
|
|
2011-02-25 23:39:29 -05:00
|
|
|
QVariant WebPage::invokeCapybaraFunction(QString &name, QStringList &arguments) {
|
|
|
|
return invokeCapybaraFunction(name.toAscii().data(), arguments);
|
|
|
|
}
|
|
|
|
|
2011-02-26 13:02:43 -05:00
|
|
|
void WebPage::javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID) {
|
|
|
|
if (!sourceID.isEmpty())
|
|
|
|
std::cout << qPrintable(sourceID) << ":" << lineNumber << " ";
|
|
|
|
std::cout << qPrintable(message) << std::endl;
|
|
|
|
}
|
|
|
|
|
2011-03-11 12:07:30 -05:00
|
|
|
void WebPage::javaScriptAlert(QWebFrame *frame, const QString &message) {
|
|
|
|
Q_UNUSED(frame);
|
|
|
|
std::cout << "ALERT: " << qPrintable(message) << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebPage::javaScriptConfirm(QWebFrame *frame, const QString &message) {
|
|
|
|
Q_UNUSED(frame);
|
|
|
|
Q_UNUSED(message);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebPage::javaScriptPrompt(QWebFrame *frame, const QString &message, const QString &defaultValue, QString *result) {
|
|
|
|
Q_UNUSED(frame)
|
|
|
|
Q_UNUSED(message)
|
|
|
|
Q_UNUSED(defaultValue)
|
|
|
|
Q_UNUSED(result)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-09 00:08:30 -05:00
|
|
|
void WebPage::loadStarted() {
|
|
|
|
m_loading = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebPage::loadFinished(bool success) {
|
|
|
|
m_loading = false;
|
2011-09-15 11:22:10 -04:00
|
|
|
emit pageFinished(success);
|
2011-03-09 00:08:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebPage::isLoading() const {
|
|
|
|
return m_loading;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString WebPage::failureString() {
|
2011-05-05 10:30:43 -04:00
|
|
|
return QString("Unable to load URL: ") + currentFrame()->requestedUrl().toString();
|
2011-03-09 00:08:30 -05:00
|
|
|
}
|
|
|
|
|
2011-05-26 15:05:16 -04:00
|
|
|
bool WebPage::render(const QString &fileName) {
|
|
|
|
QFileInfo fileInfo(fileName);
|
|
|
|
QDir dir;
|
|
|
|
dir.mkpath(fileInfo.absolutePath());
|
|
|
|
|
|
|
|
QSize viewportSize = this->viewportSize();
|
|
|
|
QSize pageSize = this->mainFrame()->contentsSize();
|
|
|
|
if (pageSize.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage buffer(pageSize, QImage::Format_ARGB32);
|
|
|
|
buffer.fill(qRgba(255, 255, 255, 0));
|
|
|
|
|
|
|
|
QPainter p(&buffer);
|
|
|
|
p.setRenderHint( QPainter::Antialiasing, true);
|
|
|
|
p.setRenderHint( QPainter::TextAntialiasing, true);
|
|
|
|
p.setRenderHint( QPainter::SmoothPixmapTransform, true);
|
|
|
|
|
|
|
|
this->setViewportSize(pageSize);
|
|
|
|
this->mainFrame()->render(&p);
|
|
|
|
p.end();
|
|
|
|
this->setViewportSize(viewportSize);
|
|
|
|
|
|
|
|
return buffer.save(fileName);
|
|
|
|
}
|
2011-07-18 17:16:57 -04:00
|
|
|
|
|
|
|
QString WebPage::chooseFile(QWebFrame *parentFrame, const QString &suggestedFile) {
|
|
|
|
Q_UNUSED(parentFrame);
|
|
|
|
Q_UNUSED(suggestedFile);
|
|
|
|
|
2011-07-20 15:58:23 -04:00
|
|
|
return getLastAttachedFileName();
|
2011-07-18 17:16:57 -04:00
|
|
|
}
|
|
|
|
|
2011-07-20 15:58:23 -04:00
|
|
|
bool WebPage::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output) {
|
2011-09-09 15:52:34 -04:00
|
|
|
Q_UNUSED(option);
|
2011-07-20 15:58:23 -04:00
|
|
|
if (extension == ChooseMultipleFilesExtension) {
|
|
|
|
QStringList names = QStringList() << getLastAttachedFileName();
|
|
|
|
static_cast<ChooseMultipleFilesExtensionReturn*>(output)->fileNames = names;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString WebPage::getLastAttachedFileName() {
|
|
|
|
return currentFrame()->evaluateJavaScript(QString("Capybara.lastAttachedFile")).toString();
|
|
|
|
}
|
2011-08-12 16:50:43 -04:00
|
|
|
|
|
|
|
void WebPage::replyFinished(QNetworkReply *reply) {
|
2011-08-23 11:18:08 -04:00
|
|
|
if (reply->url() == this->currentFrame()->url()) {
|
2011-08-23 03:02:37 -04:00
|
|
|
QStringList headers;
|
2011-08-26 10:15:41 -04:00
|
|
|
m_lastStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
2011-08-23 03:02:37 -04:00
|
|
|
QList<QByteArray> list = reply->rawHeaderList();
|
2011-08-19 11:57:39 -04:00
|
|
|
|
2011-08-23 03:02:37 -04:00
|
|
|
int length = list.size();
|
|
|
|
for(int i = 0; i < length; i++) {
|
|
|
|
headers << list.at(i)+": "+reply->rawHeader(list.at(i));
|
|
|
|
}
|
2011-08-19 11:57:39 -04:00
|
|
|
|
2011-08-23 03:02:37 -04:00
|
|
|
m_pageHeaders = headers.join("\n");
|
|
|
|
}
|
2011-08-12 16:50:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int WebPage::getLastStatus() {
|
2011-08-26 10:15:41 -04:00
|
|
|
return m_lastStatus;
|
2011-08-12 16:50:43 -04:00
|
|
|
}
|
2011-08-19 11:57:39 -04:00
|
|
|
|
2011-08-26 10:15:41 -04:00
|
|
|
void WebPage::resetResponseHeaders() {
|
|
|
|
m_lastStatus = 0;
|
|
|
|
m_pageHeaders = QString();
|
2011-08-23 11:18:08 -04:00
|
|
|
}
|
|
|
|
|
2011-08-19 11:57:39 -04:00
|
|
|
QString WebPage::pageHeaders() {
|
|
|
|
return m_pageHeaders;
|
|
|
|
}
|
2011-09-15 11:22:10 -04:00
|
|
|
|
|
|
|
void WebPage::handleUnsupportedContent(QNetworkReply *reply) {
|
|
|
|
UnsupportedContentHandler *handler = new UnsupportedContentHandler(this, reply);
|
|
|
|
Q_UNUSED(handler);
|
|
|
|
}
|