2012-10-24 11:55:29 +00:00
|
|
|
#include "TimeoutCommand.h"
|
|
|
|
#include "Command.h"
|
|
|
|
#include "WebPageManager.h"
|
|
|
|
#include "WebPage.h"
|
2013-02-02 22:32:54 +00:00
|
|
|
#include "ErrorMessage.h"
|
2012-10-24 11:55:29 +00:00
|
|
|
#include <QTimer>
|
2012-11-24 10:08:33 +00:00
|
|
|
#include <QApplication>
|
2012-10-24 11:55:29 +00:00
|
|
|
|
|
|
|
TimeoutCommand::TimeoutCommand(Command *command, WebPageManager *manager, QObject *parent) : Command(parent) {
|
|
|
|
m_command = command;
|
|
|
|
m_manager = manager;
|
|
|
|
m_timer = new QTimer(this);
|
|
|
|
m_timer->setSingleShot(true);
|
2012-12-12 06:47:54 +00:00
|
|
|
m_command->setParent(this);
|
2012-10-24 11:55:29 +00:00
|
|
|
connect(m_timer, SIGNAL(timeout()), this, SLOT(commandTimeout()));
|
|
|
|
connect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutCommand::start() {
|
2012-11-24 10:08:33 +00:00
|
|
|
QApplication::processEvents();
|
2012-10-24 11:55:29 +00:00
|
|
|
if (m_manager->isLoading()) {
|
2012-10-24 12:15:37 +00:00
|
|
|
m_manager->logger() << this->toString() << "waiting for load to finish";
|
2012-10-24 11:55:29 +00:00
|
|
|
connect(m_manager, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
|
|
|
startTimeout();
|
|
|
|
} else {
|
|
|
|
startCommand();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutCommand::startCommand() {
|
|
|
|
connect(m_command, SIGNAL(finished(Response *)), this, SLOT(commandFinished(Response *)));
|
|
|
|
m_command->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutCommand::startTimeout() {
|
|
|
|
int timeout = m_manager->getTimeout();
|
|
|
|
if (timeout > 0) {
|
|
|
|
m_timer->start(timeout * 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutCommand::pendingLoadFinished(bool success) {
|
2012-12-19 03:46:24 +00:00
|
|
|
disconnect(m_manager, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
2012-10-24 11:55:29 +00:00
|
|
|
if (success) {
|
|
|
|
startCommand();
|
|
|
|
} else {
|
2012-10-26 03:45:39 +00:00
|
|
|
disconnect(m_timer, SIGNAL(timeout()), this, SLOT(commandTimeout()));
|
|
|
|
disconnect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
2013-02-11 23:31:41 +00:00
|
|
|
finish(false, new ErrorMessage(m_manager->currentPage()->failureString()));
|
2012-10-24 11:55:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutCommand::pageLoadingFromCommand() {
|
|
|
|
startTimeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutCommand::commandTimeout() {
|
2012-10-26 03:45:39 +00:00
|
|
|
disconnect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
|
|
|
disconnect(m_manager, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
|
|
|
disconnect(m_command, SIGNAL(finished(Response *)), this, SLOT(commandFinished(Response *)));
|
2012-10-24 11:55:29 +00:00
|
|
|
m_manager->currentPage()->triggerAction(QWebPage::Stop);
|
2013-02-02 22:32:54 +00:00
|
|
|
QString message = QString("Request timed out after %1 second(s)").arg(m_manager->getTimeout());
|
2013-05-21 11:58:46 +00:00
|
|
|
finish(false, new ErrorMessage("TimeoutError", message));
|
2012-10-24 11:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutCommand::commandFinished(Response *response) {
|
2012-10-26 03:45:39 +00:00
|
|
|
disconnect(m_timer, SIGNAL(timeout()), this, SLOT(commandTimeout()));
|
|
|
|
disconnect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
2012-10-24 11:55:29 +00:00
|
|
|
emit finished(response);
|
|
|
|
}
|
|
|
|
|