cdownloadmanager_moc.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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)
  19. {
  20. filename = file;
  21. QNetworkRequest request(url);
  22. FileEntry entry;
  23. entry.file.reset(new QFile(CLauncherDirs::get().downloadsPath() + '/' + file));
  24. entry.bytesReceived = 0;
  25. entry.totalSize = 0;
  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. assert(0);
  51. static FileEntry errorValue;
  52. return errorValue;
  53. }
  54. void CDownloadManager::downloadFinished(QNetworkReply * reply)
  55. {
  56. FileEntry & file = getEntry(reply);
  57. QVariant possibleRedirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
  58. QUrl qurl = possibleRedirectUrl.toUrl();
  59. if(possibleRedirectUrl.isValid())
  60. {
  61. for(int i = 0; i< currentDownloads.size(); ++i)
  62. {
  63. if(currentDownloads[i].file == file.file)
  64. {
  65. currentDownloads.removeAt(i);
  66. break;
  67. }
  68. }
  69. downloadFile(qurl, filename);
  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. entry.totalSize = bytesTotal;
  114. quint64 total = 0;
  115. for(auto & entry : currentDownloads)
  116. total += entry.totalSize > 0 ? entry.totalSize : 0;
  117. quint64 received = 0;
  118. for(auto & entry : currentDownloads)
  119. received += entry.bytesReceived > 0 ? entry.bytesReceived : 0;
  120. emit downloadProgress(received, total);
  121. }
  122. bool CDownloadManager::downloadInProgress(const QUrl & url)
  123. {
  124. for(auto & entry : currentDownloads)
  125. {
  126. if(entry.reply->url() == url)
  127. return true;
  128. }
  129. return false;
  130. }