cdownloadmanager_moc.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * cdownloadmanager_moc.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "cdownloadmanager_moc.h"
  12. #include "../vcmiqt/launcherdirs.h"
  13. #include "../../lib/CConfigHandler.h"
  14. CDownloadManager::CDownloadManager()
  15. {
  16. connect(&manager, SIGNAL(finished(QNetworkReply *)),
  17. SLOT(downloadFinished(QNetworkReply *)));
  18. connect(&manager, &QNetworkAccessManager::sslErrors, [](QNetworkReply * reply, const QList<QSslError> & errors) {
  19. if(settings["launcher"]["ignoreSslErrors"].Bool())
  20. reply->ignoreSslErrors();
  21. });
  22. }
  23. void CDownloadManager::downloadFile(const QUrl & url, const QString & file, qint64 bytesTotal)
  24. {
  25. QNetworkRequest request(url);
  26. FileEntry entry;
  27. entry.file.reset(new QFile(QString{QLatin1String{"%1/%2"}}.arg(CLauncherDirs::downloadsPath(), file)));
  28. entry.bytesReceived = 0;
  29. entry.totalSize = bytesTotal;
  30. entry.filename = file;
  31. if(entry.file->open(QIODevice::WriteOnly | QIODevice::Truncate))
  32. {
  33. entry.status = FileEntry::IN_PROGRESS;
  34. entry.reply = manager.get(request);
  35. connect(entry.reply, SIGNAL(downloadProgress(qint64,qint64)),
  36. SLOT(downloadProgressChanged(qint64,qint64)));
  37. }
  38. else
  39. {
  40. entry.status = FileEntry::FAILED;
  41. entry.reply = nullptr;
  42. encounteredErrors += entry.file->errorString();
  43. }
  44. // even if failed - add it into list to report it in finished() call
  45. currentDownloads.push_back(entry);
  46. }
  47. CDownloadManager::FileEntry & CDownloadManager::getEntry(QNetworkReply * reply)
  48. {
  49. assert(reply);
  50. for(auto & entry : currentDownloads)
  51. {
  52. if(entry.reply == reply)
  53. return entry;
  54. }
  55. throw std::runtime_error("Failed to find download entry");
  56. }
  57. void CDownloadManager::downloadFinished(QNetworkReply * reply)
  58. {
  59. FileEntry & file = getEntry(reply);
  60. QVariant possibleRedirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
  61. QUrl qurl = possibleRedirectUrl.toUrl();
  62. if(possibleRedirectUrl.isValid())
  63. {
  64. QString filename;
  65. qint64 totalSize = 0;
  66. for(int i = 0; i< currentDownloads.size(); ++i)
  67. {
  68. if(currentDownloads[i].file == file.file)
  69. {
  70. filename = currentDownloads[i].filename;
  71. totalSize = currentDownloads[i].totalSize;
  72. currentDownloads.removeAt(i);
  73. break;
  74. }
  75. }
  76. downloadFile(qurl, filename, totalSize);
  77. return;
  78. }
  79. if(file.reply->error())
  80. {
  81. encounteredErrors += file.reply->errorString();
  82. file.file->remove();
  83. file.status = FileEntry::FAILED;
  84. }
  85. else
  86. {
  87. file.file->write(file.reply->readAll());
  88. file.file->close();
  89. file.status = FileEntry::FINISHED;
  90. }
  91. bool downloadComplete = true;
  92. for(auto & entry : currentDownloads)
  93. {
  94. if(entry.status == FileEntry::IN_PROGRESS)
  95. {
  96. downloadComplete = false;
  97. break;
  98. }
  99. }
  100. QStringList successful;
  101. QStringList failed;
  102. for(auto & entry : currentDownloads)
  103. {
  104. if(entry.status == FileEntry::FINISHED)
  105. successful += entry.file->fileName();
  106. else
  107. failed += entry.file->fileName();
  108. }
  109. if(downloadComplete)
  110. Q_EMIT finished(successful, failed, encounteredErrors);
  111. file.reply->deleteLater();
  112. file.reply = nullptr;
  113. }
  114. void CDownloadManager::downloadProgressChanged(qint64 bytesReceived, qint64 bytesTotal)
  115. {
  116. auto reply = dynamic_cast<QNetworkReply *>(sender());
  117. FileEntry & entry = getEntry(reply);
  118. entry.file->write(entry.reply->readAll());
  119. entry.bytesReceived = bytesReceived;
  120. if(bytesTotal > entry.totalSize)
  121. entry.totalSize = bytesTotal;
  122. quint64 total = 0;
  123. for(auto & entry : currentDownloads)
  124. total += entry.totalSize > 0 ? entry.totalSize : entry.bytesReceived;
  125. quint64 received = 0;
  126. for(auto & entry : currentDownloads)
  127. received += entry.bytesReceived > 0 ? entry.bytesReceived : 0;
  128. if(received > total)
  129. total = received;
  130. Q_EMIT downloadProgress(received, total);
  131. }
  132. bool CDownloadManager::downloadInProgress(const QUrl & url) const
  133. {
  134. for(auto & entry : currentDownloads)
  135. {
  136. if(entry.reply->url() == url)
  137. return true;
  138. }
  139. return false;
  140. }