cdownloadmanager_moc.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. for(int i = 0; i< currentDownloads.size(); ++i)
  66. {
  67. if(currentDownloads[i].file == file.file)
  68. {
  69. filename = currentDownloads[i].filename;
  70. currentDownloads.removeAt(i);
  71. break;
  72. }
  73. }
  74. downloadFile(qurl, filename, file.totalSize);
  75. return;
  76. }
  77. if(file.reply->error())
  78. {
  79. encounteredErrors += file.reply->errorString();
  80. file.file->remove();
  81. file.status = FileEntry::FAILED;
  82. }
  83. else
  84. {
  85. file.file->write(file.reply->readAll());
  86. file.file->close();
  87. file.status = FileEntry::FINISHED;
  88. }
  89. bool downloadComplete = true;
  90. for(auto & entry : currentDownloads)
  91. {
  92. if(entry.status == FileEntry::IN_PROGRESS)
  93. {
  94. downloadComplete = false;
  95. break;
  96. }
  97. }
  98. QStringList successful;
  99. QStringList failed;
  100. for(auto & entry : currentDownloads)
  101. {
  102. if(entry.status == FileEntry::FINISHED)
  103. successful += entry.file->fileName();
  104. else
  105. failed += entry.file->fileName();
  106. }
  107. if(downloadComplete)
  108. emit finished(successful, failed, encounteredErrors);
  109. file.reply->deleteLater();
  110. file.reply = nullptr;
  111. }
  112. void CDownloadManager::downloadProgressChanged(qint64 bytesReceived, qint64 bytesTotal)
  113. {
  114. auto reply = dynamic_cast<QNetworkReply *>(sender());
  115. FileEntry & entry = getEntry(reply);
  116. entry.file->write(entry.reply->readAll());
  117. entry.bytesReceived = bytesReceived;
  118. if(bytesTotal > entry.totalSize)
  119. entry.totalSize = bytesTotal;
  120. quint64 total = 0;
  121. for(auto & entry : currentDownloads)
  122. total += entry.totalSize > 0 ? entry.totalSize : entry.bytesReceived;
  123. quint64 received = 0;
  124. for(auto & entry : currentDownloads)
  125. received += entry.bytesReceived > 0 ? entry.bytesReceived : 0;
  126. if(received > total)
  127. total = received;
  128. emit downloadProgress(received, total);
  129. }
  130. bool CDownloadManager::downloadInProgress(const QUrl & url) const
  131. {
  132. for(auto & entry : currentDownloads)
  133. {
  134. if(entry.reply->url() == url)
  135. return true;
  136. }
  137. return false;
  138. }