6#include <QStandardPaths>
7#include <QCoreApplication>
9#include <QLoggingCategory>
10#include <QNetworkProxyFactory>
14static Q_LOGGING_CATEGORY(log,
"RabbitCommon.DownloadFile")
16CDownload::CDownload(QObject *parent)
27 m_szError =
"Urls is empty.";
28 qCritical(log) << m_szError;
47 m_szFileName = szFileName;
48 m_bSequence = bSequence;
53 for(
int i = 0; i < m_Url.length(); i++)
59CDownload::~CDownload()
61 QMap<QNetworkReply*, int>::Iterator it;
63 while(it != m_Reply.end())
65 QNetworkReply* r = it.key();
70 foreach(
auto f, m_DownloadFile)
82 QSharedPointer<QFile> file;
85 if(nIndex < 0 || nIndex >= m_DownloadFile.size())
87 m_szError =
"The index["
88 + QString::number(nIndex)
89 +
"] is out of bounds. [0 -"
90 + QString::number(m_DownloadFile.length())
92 qCritical(log) << m_szError;
93 emit sigError(-1, m_szError);
96 file = m_DownloadFile.at(nIndex);
99 m_szError =
"file is null. index: "
100 + QString::number(nIndex);
101 qCritical(log) << m_szError;
102 emit sigError(-2, m_szError);
106 if(nIndex < m_DownloadFile.size())
108 m_szError =
"The index[" + QString::number(nIndex)
109 +
"] is in of bounds. [0 -"
110 + QString::number(m_DownloadFile.length())
112 qCritical(log) << m_szError;
116 = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
117 szTmp = szTmp + QDir::separator() +
"Rabbit"
118 + QDir::separator() + qApp->applicationName()
119 + QDir::separator() + QString::number(nIndex);
123 QString szPath = url.path();
124 if(m_szFileName.isEmpty())
126 m_szFileName = szPath.mid(szPath.lastIndexOf(
"/"));
128 if(m_szFileName.left(1) !=
"/" && m_szFileName.left(1) !=
"\\")
129 m_szFileName = QDir::separator() + m_szFileName;
130 QString szFile = szTmp + m_szFileName;
132 file = QSharedPointer<QFile>(
new QFile(), &QObject::deleteLater);
135 m_szError =
"new QFile fail";
136 qCritical(log) << m_szError;
139 file->setFileName(szFile);
140 m_DownloadFile.insert(nIndex, file);
145 m_szError =
"file is null. index:" + QString::number(nIndex) ;
146 qCritical(log) << m_szError;
150 qInfo(log) <<
"Download file:"
152 <<
"(redirection:" << bRedirection <<
")"
158 if(url.isLocalFile())
160 qDebug(log) <<
"Is local file:" << url;
161 file->setFileName(url.toLocalFile());
162 if(QFile::exists(file->fileName()))
168 QString szErr = tr(
"The file is not exists: ") + file->fileName();
169 qCritical(log) << szErr;
170 emit sigError(-6, szErr);
174 if(!file->open(QIODevice::WriteOnly))
176 m_szError =
"Open file fail: " + file->fileName();
177 qDebug(log) << m_szError;
181 QNetworkRequest request(url);
192 QNetworkReply* pReply = m_NetManager.get(request);
196 m_Reply.insert(pReply, nIndex);
199 check = connect(pReply, SIGNAL(readyRead()),
this, SLOT(slotReadyRead()));
201 check = connect(pReply, SIGNAL(downloadProgress(qint64, qint64)),
202 this, SLOT(slotDownloadProgress(qint64, qint64)));
204 check = connect(pReply,
205 #
if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
206 SIGNAL(errorOccurred(QNetworkReply::NetworkError)),
208 SIGNAL(error(QNetworkReply::NetworkError)),
210 this, SLOT(slotError(QNetworkReply::NetworkError)));
212 check = connect(pReply, SIGNAL(sslErrors(
const QList<QSslError>)),
213 this, SLOT(slotSslError(
const QList<QSslError>)));
215 check = connect(pReply, SIGNAL(finished()),
216 this, SLOT(slotFinished()));
222void CDownload::slotReadyRead()
225 QNetworkReply* pReply =
dynamic_cast<QNetworkReply*
>(this->sender());
226 if(m_Reply.find(pReply) == m_Reply.end())
228 qCritical(log) <<
"The reply isn't exits";
231 QSharedPointer<QFile> file = m_DownloadFile[m_Reply[pReply]];
234 qCritical(log) <<
"The file isn't exits.";
237 if(file && file->isOpen())
239 QByteArray d = pReply->readAll();
245void CDownload::slotFinished()
247 QNetworkReply* pReply =
dynamic_cast<QNetworkReply*
>(this->sender());
248 if(m_Reply.find(pReply) == m_Reply.end())
250 qCritical(log) <<
"The reply isn't exits" << pReply->url();
253 int nIndex = m_Reply[pReply];
254 qInfo(log) <<
"Download finished:" << nIndex << pReply->url();
256 QVariant redirectionTarget;
258 redirectionTarget = pReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
262 if(redirectionTarget.isValid())
264 QUrl u = redirectionTarget.toUrl();
267 qDebug(log) <<
"redirectionTarget:url:" << u;
276 QMap<QNetworkReply*, int>::Iterator it;
277 it = m_Reply.begin();
278 while(it != m_Reply.end())
280 QNetworkReply* r = it.key();
282 it = m_Reply.begin();
286 QSharedPointer<QFile> file = m_DownloadFile[nIndex];
289 qCritical(log) <<
"The file isn't exits. index:" << nIndex;
296void CDownload::slotError(QNetworkReply::NetworkError e)
298 QNetworkReply* pReply =
dynamic_cast<QNetworkReply*
>(this->sender());
299 if(m_Reply.find(pReply) == m_Reply.end())
301 qCritical(log) <<
"Network error: The reply isn't exits."
302 << pReply->url() <<
"NetworkError:" << e;
306 int nIndex = m_Reply[pReply];
307 QString szErr = pReply->errorString();
308 qDebug(log) <<
"Network error[" << e <<
"]:" << szErr
309 <<
"index:" << nIndex << pReply->url();
312 QSharedPointer<QFile> file = m_DownloadFile[nIndex];
315 qCritical(log) <<
"Network error: The file is null. index: "
316 + QString::number(nIndex);
321 if(m_bSequence && (nIndex < m_Url.size() - 1))
330 if(m_szError.isEmpty()) {
331 m_szError =
"Network error: ";
333 m_szError += szErr +
"; ";
334 m_szError +=
"from " + m_Url[nIndex].toString()
335 +
" to " + file->fileName();
337 emit sigError(e, m_szError);
341void CDownload::slotSslError(
const QList<QSslError> e)
343 QNetworkReply* pReply =
dynamic_cast<QNetworkReply*
>(this->sender());
344 if(m_Reply.find(pReply) == m_Reply.end())
346 qCritical(log) <<
"ssl error: The reply isn't exits. QSslError:" << e;
349 int nIndex = m_Reply[pReply];
350 QString szErr = pReply->errorString();
351 qDebug(log) <<
"ssl error[" << e <<
"]:" << szErr
352 <<
"index:" << nIndex << pReply->url();
354 QSharedPointer<QFile> file = m_DownloadFile[nIndex];
357 qCritical(log) <<
"ssl error: The file null. index: "
358 + QString::number(nIndex);
363 if(m_bSequence && (nIndex < m_Url.size() - 1))
373 foreach(QSslError s, e)
374 sErr += s.errorString() +
" ";
376 if(m_szError.isEmpty())
378 m_szError =
"ssl error: ";
380 m_szError += szErr +
"; ";
381 m_szError +=
"from " + m_Url[nIndex].toString()
382 +
" to " + file->fileName();
384 emit sigError(-1, m_szError);
388void CDownload::slotDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
390 QNetworkReply* pReply =
dynamic_cast<QNetworkReply*
>(this->sender());
393 qCritical(log) <<
"slotDownloadProgress: The sender is not reply";
396 if(m_Reply.find(pReply) == m_Reply.end())
398 qCritical(log) <<
"slotDownloadProgress: The reply isn't exits"
402 int nIndex = m_Reply[pReply];
403 QSharedPointer<QFile> file = m_DownloadFile[nIndex];
406 qCritical(log) <<
"slotDownloadProgress: The file null. index: "
407 + QString::number(nIndex);
413 if(m_nBytesReceived < bytesReceived)
415 m_nBytesReceived = bytesReceived;
416 emit sigDownloadProgress(m_nBytesReceived, bytesTotal);
418 qDebug(log) << tr(
"Downloading %1% [%2/%3]")
419 .arg(QString::number(bytesReceived * 100 / bytesTotal))
420 .arg(QString::number(bytesReceived)).arg(QString::number(bytesTotal))
421 << m_Url[nIndex] << file->fileName();
425int CDownload::CloseReply(QNetworkReply *pReply,
bool bAbort)
429 m_Reply.remove(pReply);
430 pReply->disconnect();
431 if(bAbort && pReply->isRunning())
433 pReply->deleteLater();
int Start(QVector< QUrl > urls, QString szFileName=QString(), bool bSequence=false)
void sigFinished(const QString szFile)
int DownloadFile(int nIndex, const QUrl &url, bool bRedirection=false)
DownloadFile.