cdownloadmanager_moc.cpp 3.5 KB

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