videodownloader/network/httpdownloadwithinforequst.cpp

96 lines
3.2 KiB
C++
Raw Normal View History

2015-09-08 17:05:59 +02:00
#include "./httpdownloadwithinforequst.h"
#include "./permissionstatus.h"
2015-04-22 19:32:04 +02:00
namespace Network {
/*!
* \class HttpDownloadWithInfoRequst
* \brief The HttpDownloadWithInfoRequst class a base class for HTTP downloads which need to perform
* additional requests before the actual download.
*
* A YoutubeDownload needs to retrieve the download URL for example. These requests are handled by
* the HttpDownloadWithInfoRequst class during initialization.
*/
/*!
* \brief Constructs a new YoutubeDownload for the specified \a url.
*/
2017-05-01 03:22:50 +02:00
HttpDownloadWithInfoRequst::HttpDownloadWithInfoRequst(const QUrl &url, QObject *parent)
: HttpDownload(url, parent)
{
}
2015-04-22 19:32:04 +02:00
HttpDownloadWithInfoRequst::~HttpDownloadWithInfoRequst()
2017-05-01 03:22:50 +02:00
{
}
2015-04-22 19:32:04 +02:00
void HttpDownloadWithInfoRequst::doInit()
{
setTitleFromFilename(initialUrl().host());
bool success;
QString reasonForFail;
// get the info request
m_infoDownload.reset(infoRequestDownload(success, reasonForFail));
2017-05-01 03:22:50 +02:00
if (success) {
2015-04-22 19:32:04 +02:00
// the request could be constructed successfully
2017-05-01 03:22:50 +02:00
if (!m_infoDownload) {
2015-04-22 19:32:04 +02:00
// no request needed (at this time), just call evalVideoInformation()
evalVideoInformation(nullptr, nullptr);
} else {
// setup and initiate the request
m_infoDownload->setParent(parent());
m_infoDownload->setDefaultUserAgentUsed(isDefaultUserAgentUsed());
m_infoDownload->setCustomUserAgent(userAgent());
m_infoDownload->setProxy(proxy());
connect(m_infoDownload.get(), &Download::statusChanged, this, &HttpDownloadWithInfoRequst::infoRequestChangedStatus);
m_infoDownload->init();
}
} else {
reportInitiated(false, reasonForFail);
}
}
void HttpDownloadWithInfoRequst::abortDownload()
{
2017-05-01 03:22:50 +02:00
if (m_infoDownload) {
2015-04-22 19:32:04 +02:00
m_infoDownload->stop();
}
HttpDownload::abortDownload();
}
/*!
* \brief Handles the status of the info request.
*/
void HttpDownloadWithInfoRequst::infoRequestChangedStatus(Download *download)
{
2017-05-01 03:22:50 +02:00
switch (download->status()) {
2015-04-22 19:32:04 +02:00
case DownloadStatus::Failed:
reportInitiated(false, tr("Couldn't retieve the video information. %1").arg(statusInfo()));
break;
case DownloadStatus::Ready:
2017-05-01 03:22:50 +02:00
if (m_infoDownload->isValidOptionChosen()) {
2015-04-22 19:32:04 +02:00
m_infoDownload->setRedirectPermission(m_infoDownload->chosenOption(), PermissionStatus::AlwaysAllowed);
m_infoBuffer.reset(new QBuffer());
2017-05-01 03:22:50 +02:00
if (m_infoBuffer->open(QIODevice::ReadWrite)) {
2015-04-22 19:32:04 +02:00
download->start(m_infoBuffer.get());
} else {
reportInitiated(false, tr("Couldn't initialize buffer to store initialization data."));
}
} else {
reportInitiated(false, tr("The initialization request has no options."));
}
break;
case DownloadStatus::Finished:
2017-05-01 03:22:50 +02:00
if (!m_infoBuffer) {
2015-04-22 19:32:04 +02:00
reportInitiated(false, tr("The initialization data buffer hasn't been initialized."));
} else {
m_infoBuffer->seek(0);
2015-04-22 19:32:04 +02:00
evalVideoInformation(download, m_infoBuffer.get());
m_infoDownload.reset();
}
break;
2017-05-01 03:22:50 +02:00
default:;
2015-04-22 19:32:04 +02:00
}
}
2019-07-20 20:20:58 +02:00
} // namespace Network