2012-10-24 07:55:29 -04:00
|
|
|
#include "TimeoutCommand.h"
|
|
|
|
#include "Command.h"
|
|
|
|
#include "WebPageManager.h"
|
|
|
|
#include "WebPage.h"
|
|
|
|
#include <QTimer>
|
2012-11-24 05:08:33 -05:00
|
|
|
#include <QApplication>
|
2012-10-24 07:55:29 -04: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 01:47:54 -05:00
|
|
|
m_command->setParent(this);
|
2012-10-24 07:55:29 -04:00
|
|
|
connect(m_timer, SIGNAL(timeout()), this, SLOT(commandTimeout()));
|
|
|
|
connect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutCommand::start() {
|
2012-11-24 05:08:33 -05:00
|
|
|
QApplication::processEvents();
|
2012-10-24 07:55:29 -04:00
|
|
|
if (m_manager->isLoading()) {
|
2012-10-24 08:15:37 -04:00
|
|
|
m_manager->logger() << this->toString() << "waiting for load to finish";
|
2012-10-24 07:55:29 -04: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) {
|
|
|
|
if (success) {
|
|
|
|
startCommand();
|
|
|
|
} else {
|
2012-10-25 23:45:39 -04:00
|
|
|
disconnect(m_timer, SIGNAL(timeout()), this, SLOT(commandTimeout()));
|
|
|
|
disconnect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
|
|
|
disconnect(m_manager, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
2012-12-12 01:47:54 -05:00
|
|
|
emitFinished(false, m_manager->currentPage()->failureString());
|
2012-10-24 07:55:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutCommand::pageLoadingFromCommand() {
|
|
|
|
startTimeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutCommand::commandTimeout() {
|
2012-10-25 23:45:39 -04: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 07:55:29 -04:00
|
|
|
m_manager->currentPage()->triggerAction(QWebPage::Stop);
|
2012-12-12 01:47:54 -05:00
|
|
|
emit finished(new Response(false, QString("timeout"), this));
|
2012-10-24 07:55:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutCommand::commandFinished(Response *response) {
|
2012-10-25 23:45:39 -04:00
|
|
|
disconnect(m_timer, SIGNAL(timeout()), this, SLOT(commandTimeout()));
|
|
|
|
disconnect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
|
|
|
|
disconnect(m_manager, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
|
2012-10-24 07:55:29 -04:00
|
|
|
emit finished(response);
|
|
|
|
}
|
|
|
|
|