Skip to content

Commit

Permalink
Added support for downloading unsupported content.
Browse files Browse the repository at this point in the history
Unsupported content calls the onFilePicker callback, if a filename is
returned, the download begins. onDownloadFinished is called when the
download completes.

ariya#10052
  • Loading branch information
mrampersad committed Sep 12, 2014
1 parent dca6f77 commit 55230b6
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
46 changes: 46 additions & 0 deletions src/downloader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "downloader.h"

#include <QNetworkReply>
#include <QUrl>
#include <QDateTime>
#include <QFile>
#include <QDebug>

#include "phantom.h"

Downloader::Downloader(QObject *parent, QFile *file, QNetworkReply *reply) :
QObject(parent), m_file(file), m_reply(reply)
{
qDebug() << "Downloader: Downloader";

m_file->setParent(this);
m_reply->setParent(this);

connect(reply, SIGNAL(readyRead()), this, SLOT(handleReadyRead()), Qt::QueuedConnection);
connect(reply, SIGNAL(finished()), this, SLOT(handleFinished()), Qt::QueuedConnection);
}

void Downloader::startDownload() {
handleReadyRead();

if( m_reply->isFinished() ) {
handleFinished();
}
}

void Downloader::handleReadyRead()
{
qDebug() << "Downloader: handleReadyRead" << m_reply->bytesAvailable();

m_file->write(m_reply->readAll());
}

void Downloader::handleFinished()
{
qDebug() << "Downloader: handleFinished";

m_file->close();
this->deleteLater();

emit downloadFinished(true);
}
28 changes: 28 additions & 0 deletions src/downloader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef DOWNLOADER_H
#define DOWNLOADER_H

#include <QObject>
#include <QNetworkReply>
#include <QFile>

class Downloader : public QObject
{
Q_OBJECT

public:
Downloader(QObject *parent, QFile *file, QNetworkReply *reply);
void startDownload();

public slots:
void handleReadyRead();
void handleFinished();

signals:
void downloadFinished(bool ok);

private:
QNetworkReply *m_reply;
QFile *m_file;
};

#endif // DOWNLOADER_H
2 changes: 2 additions & 0 deletions src/modules/webpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ function decorateNewPage(opts, page) {

definePageSignalHandler(page, handlers, "onLoadFinished", "loadFinished");

definePageSignalHandler(page, handlers, "onDownloadFinished", "downloadFinished");

definePageSignalHandler(page, handlers, "onUrlChanged", "urlChanged");

definePageSignalHandler(page, handlers, "onNavigationRequested", "navigationRequested");
Expand Down
4 changes: 3 additions & 1 deletion src/phantomjs.pro
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ HEADERS += \
phantom.h \
callback.h \
webpage.h \
downloader.h \
webserver.h \
consts.h \
utils.h \
Expand All @@ -39,6 +40,7 @@ HEADERS += \
SOURCES += phantom.cpp \
callback.cpp \
webpage.cpp \
downloader.cpp \
webserver.cpp \
main.cpp \
utils.cpp \
Expand Down Expand Up @@ -74,7 +76,7 @@ linux*|mac|openbsd* {
SOURCES += breakpad/src/client/minidump_file_writer.cc \
breakpad/src/common/convert_UTF.c \
breakpad/src/common/md5.cc \
breakpad/src/common/string_conversion.cc
breakpad/src/common/string_conversion.cc
}

linux* {
Expand Down
27 changes: 25 additions & 2 deletions src/webpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

#include "phantom.h"
#include "networkaccessmanager.h"
#include "downloader.h"
#include "utils.h"
#include "config.h"
#include "consts.h"
Expand Down Expand Up @@ -368,6 +369,7 @@ WebPage::WebPage(QObject *parent, const QUrl &baseUrl)
connect(m_customWebPage, SIGNAL(windowCloseRequested()), this, SLOT(close()), Qt::QueuedConnection);
connect(m_customWebPage, SIGNAL(loadProgress(int)), this, SLOT(updateLoadingProgress(int)));
connect(m_customWebPage, SIGNAL(repaintRequested(QRect)), this, SLOT(handleRepaintRequested(QRect)), Qt::QueuedConnection);
connect(m_customWebPage, SIGNAL(unsupportedContent(QNetworkReply *)), SLOT(handleUnsupportedContent(QNetworkReply *)), Qt::QueuedConnection);


// Start with transparent background.
Expand Down Expand Up @@ -699,7 +701,7 @@ QVariant WebPage::evaluateJavaScript(const QString &code)
return evalResult;
}

QString WebPage::filePicker(const QString &oldFile)
QString WebPage::filePicker(const QString &oldFile, bool mustExist)
{
qDebug() << "WebPage - filePicker" << "- old file:" << oldFile;

Expand All @@ -710,7 +712,7 @@ QString WebPage::filePicker(const QString &oldFile)
QString filePath = res.toString();
qDebug() << "WebPage - filePicker" << "- new file:" << filePath;
// Return this value only if the file actually exists
if (QFile::exists(filePath)) {
if (mustExist == false || QFile::exists(filePath)) {
return filePath;
}
}
Expand Down Expand Up @@ -758,6 +760,12 @@ void WebPage::finish(bool ok)
emit loadFinished(status);
}

void WebPage::downloadFinish(bool ok)
{
QString status = ok ? "success" : "fail";
emit downloadFinished(status);
}

void WebPage::setCustomHeaders(const QVariantMap &headers)
{
m_networkAccessManager->setCustomHeaders(headers);
Expand Down Expand Up @@ -1624,6 +1632,21 @@ void WebPage::handleRepaintRequested(const QRect &dirtyRect)
emit repaintRequested(dirtyRect.x(), dirtyRect.y(), dirtyRect.width(), dirtyRect.height());
}

void WebPage::handleUnsupportedContent(QNetworkReply *reply)
{
qDebug() << "WebPage - handleUnsupportedContent: " << reply;

QString chosenFile = filePicker(QString::null, false);
if (chosenFile != QString::null ) {
QFile *i = new QFile(chosenFile, this);
i->open(QIODevice::WriteOnly);
Downloader *d = new Downloader(this, i, reply);
connect(d, SIGNAL(downloadFinished(bool)), SLOT(downloadFinish(bool)), Qt::QueuedConnection);
d->startDownload();
} else {
reply->deleteLater();
}
}

void WebPage::stopJavaScript()
{
Expand Down
5 changes: 4 additions & 1 deletion src/webpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ public slots:
void initialized();
void loadStarted();
void loadFinished(const QString &status);
void downloadFinished(const QString &status);
void javaScriptAlertSent(const QString &msg);
void javaScriptConsoleMessageSent(const QString &message);
void javaScriptErrorSent(const QString &msg, int lineNumber, const QString &sourceID, const QString &stack);
Expand All @@ -500,9 +501,11 @@ public slots:

private slots:
void finish(bool ok);
void downloadFinish(bool ok);
void setupFrame(QWebFrame *frame = NULL);
void updateLoadingProgress(int progress);
void handleRepaintRequested(const QRect &dirtyRect);
void handleUnsupportedContent(QNetworkReply *reply);

private:
QImage renderImage();
Expand All @@ -518,7 +521,7 @@ private slots:
*/
void changeCurrentFrame(QWebFrame * const frame);

QString filePicker(const QString &oldFile);
QString filePicker(const QString &oldFile, bool mustExist = true);
bool javaScriptConfirm(const QString &msg);
bool javaScriptPrompt(const QString &msg, const QString &defaultValue, QString *result);
void javascriptInterrupt();
Expand Down

0 comments on commit 55230b6

Please sign in to comment.