cdownloadmanager_moc.cpp 3.7 KB

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