1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00
capybara-webkit/src/WebPageManager.cpp

118 lines
2.8 KiB
C++
Raw Normal View History

2012-03-26 18:41:33 -04:00
#include "WebPageManager.h"
#include "WebPage.h"
2012-06-21 23:34:51 -04:00
#include "NetworkCookieJar.h"
2012-03-26 18:41:33 -04:00
WebPageManager::WebPageManager(QObject *parent) : QObject(parent) {
m_ignoreSslErrors = false;
2012-06-21 23:34:51 -04:00
m_cookieJar = new NetworkCookieJar(this);
2012-06-29 10:41:08 -04:00
m_success = true;
m_loggingEnabled = false;
m_ignoredOutput = new QString();
createPage(this)->setFocus();
2012-03-26 18:41:33 -04:00
}
void WebPageManager::append(WebPage *value) {
m_pages.append(value);
2012-03-26 18:41:33 -04:00
}
2012-06-29 10:41:08 -04:00
QList<WebPage *> WebPageManager::pages() const {
return m_pages;
2012-03-26 18:41:33 -04:00
}
void WebPageManager::setCurrentPage(WebPage *page) {
m_currentPage = page;
}
WebPage *WebPageManager::currentPage() const {
return m_currentPage;
}
WebPage *WebPageManager::createPage(QObject *parent) {
WebPage *page = new WebPage(this, parent);
2012-06-29 10:41:08 -04:00
connect(page, SIGNAL(loadStarted()),
this, SLOT(emitLoadStarted()));
connect(page, SIGNAL(pageFinished(bool)),
this, SLOT(setPageStatus(bool)));
2012-06-29 10:41:08 -04:00
connect(page, SIGNAL(requestCreated(QNetworkReply *)),
this, SLOT(requestCreated(QNetworkReply *)));
connect(page, SIGNAL(replyFinished(QNetworkReply *)),
this, SLOT(replyFinished(QNetworkReply *)));
append(page);
return page;
}
2012-05-14 21:59:28 -04:00
void WebPageManager::emitLoadStarted() {
2012-06-29 10:41:08 -04:00
if (m_started.empty()) {
logger() << "Load started";
2012-05-14 21:59:28 -04:00
emit loadStarted();
2012-06-29 10:41:08 -04:00
}
}
void WebPageManager::requestCreated(QNetworkReply *reply) {
logger() << "Started request to" << reply->url().toString();
2012-06-29 10:41:08 -04:00
m_started += reply;
}
void WebPageManager::replyFinished(QNetworkReply *reply) {
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
logger() << "Received" << status << "from" << reply->url().toString();
2012-06-29 10:41:08 -04:00
m_started.remove(reply);
logger() << m_started.size() << "requests remaining";
if (m_started.empty() && !m_success) {
emitPageFinished();
}
2012-06-29 10:41:08 -04:00
}
void WebPageManager::setPageStatus(bool success) {
logger() << "Page finished with" << success;
2012-06-29 10:41:08 -04:00
m_success = success && m_success;
if (m_started.empty()) {
emitPageFinished();
2012-06-29 10:41:08 -04:00
}
2012-05-14 21:59:28 -04:00
}
void WebPageManager::emitPageFinished() {
logger() << "Load finished";
emit pageFinished(m_success);
m_success = true;
}
void WebPageManager::setIgnoreSslErrors(bool value) {
m_ignoreSslErrors = value;
}
bool WebPageManager::ignoreSslErrors() {
return m_ignoreSslErrors;
}
2012-05-14 21:59:28 -04:00
void WebPageManager::reset() {
2012-06-21 23:34:51 -04:00
m_cookieJar->clearCookies();
2012-05-14 21:59:28 -04:00
m_pages.first()->deleteLater();
m_pages.clear();
createPage(this)->setFocus();
}
2012-06-21 23:34:51 -04:00
NetworkCookieJar *WebPageManager::cookieJar() {
return m_cookieJar;
}
2012-06-29 10:41:08 -04:00
bool WebPageManager::isLoading() const {
foreach(WebPage *page, pages()) {
if (page->isLoading()) {
return true;
}
}
return false;
}
QDebug WebPageManager::logger() const {
if (m_loggingEnabled)
return qDebug();
else
return QDebug(m_ignoredOutput);
}
void WebPageManager::enableLogging() {
m_loggingEnabled = true;
}