3#include <QStandardPaths>
4#include <QCoreApplication>
6#include <QLoggingCategory>
7#include <QNetworkProxyFactory>
11static Q_LOGGING_CATEGORY(log,
"RabbitCommon.DownloadFile")
13CDownload::CDownload(QObject *parent)
24 m_szError =
"Urls is empty.";
25 qCritical(log) << m_szError;
44 m_szFileName = szFileName;
45 m_bSequence = bSequence;
50 for(
int i = 0; i < m_Url.length(); i++)
56CDownload::~CDownload()
58 QMap<QNetworkReply*, int>::Iterator it;
60 while(it != m_Reply.end())
62 QNetworkReply* r = it.key();
67 foreach(
auto f, m_DownloadFile)
79 QSharedPointer<QFile> file;
82 if(nIndex < 0 || nIndex >= m_DownloadFile.size())
84 m_szError =
"The index["
85 + QString::number(nIndex)
86 +
"] is out of bounds. [0 -"
87 + QString::number(m_DownloadFile.length())
89 qCritical(log) << m_szError;
90 emit sigError(-1, m_szError);
93 file = m_DownloadFile.at(nIndex);
96 m_szError =
"file is null. index: "
97 + QString::number(nIndex);
98 qCritical(log) << m_szError;
99 emit sigError(-2, m_szError);
103 if(nIndex < m_DownloadFile.size())
105 m_szError =
"The index[" + QString::number(nIndex)
106 +
"] is in of bounds. [0 -"
107 + QString::number(m_DownloadFile.length())
109 qCritical(log) << m_szError;
113 = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
114 szTmp = szTmp + QDir::separator() +
"Rabbit"
115 + QDir::separator() + qApp->applicationName()
116 + QDir::separator() + QString::number(nIndex);
120 QString szPath = url.path();
121 if(m_szFileName.isEmpty())
123 m_szFileName = szPath.mid(szPath.lastIndexOf(
"/"));
125 if(m_szFileName.left(1) !=
"/" && m_szFileName.left(1) !=
"\\")
126 m_szFileName = QDir::separator() + m_szFileName;
127 QString szFile = szTmp + m_szFileName;
129 file = QSharedPointer<QFile>(
new QFile(), &QObject::deleteLater);
132 m_szError =
"new QFile fail";
133 qCritical(log) << m_szError;
136 file->setFileName(szFile);
137 m_DownloadFile.insert(nIndex, file);
142 m_szError =
"file is null. index:" + QString::number(nIndex) ;
143 qCritical(log) << m_szError;
147 qInfo(log) <<
"Download file:"
149 <<
"(redirection:" << bRedirection <<
")"
155 if(url.isLocalFile())
157 qDebug(log) <<
"Is local file:" << url;
158 file->setFileName(url.toLocalFile());
159 if(QFile::exists(file->fileName()))
165 QString szErr = tr(
"The file is not exists: ") + file->fileName();
166 qCritical(log) << szErr;
167 emit sigError(-6, szErr);
171 if(!file->open(QIODevice::WriteOnly))
173 m_szError =
"Open file fail: " + file->fileName();
174 qDebug(log) << m_szError;
178 QNetworkRequest request(url);
189 QNetworkReply* pReply = m_NetManager.get(request);
193 m_Reply.insert(pReply, nIndex);
196 check = connect(pReply, SIGNAL(readyRead()),
this, SLOT(slotReadyRead()));
198 check = connect(pReply, SIGNAL(downloadProgress(qint64, qint64)),
199 this, SLOT(slotDownloadProgress(qint64, qint64)));
201 check = connect(pReply,
202 #
if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
203 SIGNAL(errorOccurred(QNetworkReply::NetworkError)),
205 SIGNAL(error(QNetworkReply::NetworkError)),
207 this, SLOT(slotError(QNetworkReply::NetworkError)));
209 check = connect(pReply, SIGNAL(sslErrors(
const QList<QSslError>)),
210 this, SLOT(slotSslError(
const QList<QSslError>)));
212 check = connect(pReply, SIGNAL(finished()),
213 this, SLOT(slotFinished()));
219void CDownload::slotReadyRead()
222 QNetworkReply* pReply =
dynamic_cast<QNetworkReply*
>(this->sender());
223 if(m_Reply.find(pReply) == m_Reply.end())
225 qCritical(log) <<
"The reply isn't exits";
228 QSharedPointer<QFile> file = m_DownloadFile[m_Reply[pReply]];
231 qCritical(log) <<
"The file isn't exits.";
234 if(file && file->isOpen())
236 QByteArray d = pReply->readAll();
242void CDownload::slotFinished()
244 QNetworkReply* pReply =
dynamic_cast<QNetworkReply*
>(this->sender());
245 if(m_Reply.find(pReply) == m_Reply.end())
247 qCritical(log) <<
"The reply isn't exits" << pReply->url();
250 int nIndex = m_Reply[pReply];
251 qInfo(log) <<
"Download finished:" << nIndex << pReply->url();
253 QVariant redirectionTarget;
255 redirectionTarget = pReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
259 if(redirectionTarget.isValid())
261 QUrl u = redirectionTarget.toUrl();
264 qDebug(log) <<
"redirectionTarget:url:" << u;
273 QMap<QNetworkReply*, int>::Iterator it;
274 it = m_Reply.begin();
275 while(it != m_Reply.end())
277 QNetworkReply* r = it.key();
279 it = m_Reply.begin();
283 QSharedPointer<QFile> file = m_DownloadFile[nIndex];
286 qCritical(log) <<
"The file isn't exits. index:" << nIndex;
293void CDownload::slotError(QNetworkReply::NetworkError e)
295 QNetworkReply* pReply =
dynamic_cast<QNetworkReply*
>(this->sender());
296 if(m_Reply.find(pReply) == m_Reply.end())
298 qCritical(log) <<
"Network error: The reply isn't exits."
299 << pReply->url() <<
"NetworkError:" << e;
303 int nIndex = m_Reply[pReply];
304 QString szErr = pReply->errorString();
305 qDebug(log) <<
"Network error[" << e <<
"]:" << szErr
306 <<
"index:" << nIndex << pReply->url();
309 QSharedPointer<QFile> file = m_DownloadFile[nIndex];
312 qCritical(log) <<
"Network error: The file is null. index: "
313 + QString::number(nIndex);
318 if(m_bSequence && (nIndex < m_Url.size() - 1))
327 if(m_szError.isEmpty()) {
328 m_szError =
"Network error: ";
330 m_szError += szErr +
"; ";
331 m_szError +=
"from " + m_Url[nIndex].toString()
332 +
" to " + file->fileName();
334 emit sigError(e, m_szError);
338void CDownload::slotSslError(
const QList<QSslError> e)
340 QNetworkReply* pReply =
dynamic_cast<QNetworkReply*
>(this->sender());
341 if(m_Reply.find(pReply) == m_Reply.end())
343 qCritical(log) <<
"ssl error: The reply isn't exits. QSslError:" << e;
346 int nIndex = m_Reply[pReply];
347 QString szErr = pReply->errorString();
348 qDebug(log) <<
"ssl error[" << e <<
"]:" << szErr
349 <<
"index:" << nIndex << pReply->url();
351 QSharedPointer<QFile> file = m_DownloadFile[nIndex];
354 qCritical(log) <<
"ssl error: The file null. index: "
355 + QString::number(nIndex);
360 if(m_bSequence && (nIndex < m_Url.size() - 1))
370 foreach(QSslError s, e)
371 sErr += s.errorString() +
" ";
373 if(m_szError.isEmpty())
375 m_szError =
"ssl error: ";
377 m_szError += szErr +
"; ";
378 m_szError +=
"from " + m_Url[nIndex].toString()
379 +
" to " + file->fileName();
381 emit sigError(-1, m_szError);
385void CDownload::slotDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
387 QNetworkReply* pReply =
dynamic_cast<QNetworkReply*
>(this->sender());
390 qCritical(log) <<
"slotDownloadProgress: The sender is not reply";
393 if(m_Reply.find(pReply) == m_Reply.end())
395 qCritical(log) <<
"slotDownloadProgress: The reply isn't exits"
399 int nIndex = m_Reply[pReply];
400 QSharedPointer<QFile> file = m_DownloadFile[nIndex];
403 qCritical(log) <<
"slotDownloadProgress: The file null. index: "
404 + QString::number(nIndex);
410 if(m_nBytesReceived < bytesReceived)
412 m_nBytesReceived = bytesReceived;
413 emit sigDownloadProgress(m_nBytesReceived, bytesTotal);
415 qDebug(log) << tr(
"Downloading %1% [%2/%3]")
416 .arg(QString::number(bytesReceived * 100 / bytesTotal))
417 .arg(QString::number(bytesReceived)).arg(QString::number(bytesTotal))
418 << m_Url[nIndex] << file->fileName();
422int CDownload::CloseReply(QNetworkReply *pReply,
bool bAbort)
426 m_Reply.remove(pReply);
427 pReply->disconnect();
428 if(bAbort && pReply->isRunning())
430 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.