cdownloadmanager_moc.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "StdInc.h"
  2. #include "cdownloadmanager_moc.h"
  3. #include "../launcherdirs.h"
  4. CDownloadManager::CDownloadManager()
  5. {
  6. connect(&manager, SIGNAL(finished(QNetworkReply*)),
  7. SLOT(downloadFinished(QNetworkReply*)));
  8. }
  9. void CDownloadManager::downloadFile(const QUrl &url, const QString &file)
  10. {
  11. QNetworkRequest request(url);
  12. FileEntry entry;
  13. entry.file.reset(new QFile(CLauncherDirs::get().downloadsPath() + '/' + file));
  14. entry.bytesReceived = 0;
  15. entry.totalSize = 0;
  16. if (entry.file->open(QIODevice::WriteOnly | QIODevice::Truncate))
  17. {
  18. entry.status = FileEntry::IN_PROGRESS;
  19. entry.reply = manager.get(request);
  20. connect(entry.reply, SIGNAL(downloadProgress(qint64, qint64)),
  21. SLOT(downloadProgressChanged(qint64, qint64)));
  22. }
  23. else
  24. {
  25. entry.status = FileEntry::FAILED;
  26. entry.reply = nullptr;
  27. encounteredErrors += entry.file->errorString();
  28. }
  29. // even if failed - add it into list to report it in finished() call
  30. currentDownloads.push_back(entry);
  31. }
  32. CDownloadManager::FileEntry & CDownloadManager::getEntry(QNetworkReply * reply)
  33. {
  34. assert(reply);
  35. for (auto & entry : currentDownloads)
  36. {
  37. if (entry.reply == reply)
  38. return entry;
  39. }
  40. assert(0);
  41. static FileEntry errorValue;
  42. return errorValue;
  43. }
  44. void CDownloadManager::downloadFinished(QNetworkReply *reply)
  45. {
  46. FileEntry & file = getEntry(reply);
  47. if (file.reply->error())
  48. {
  49. encounteredErrors += file.reply->errorString();
  50. file.file->remove();
  51. file.status = FileEntry::FAILED;
  52. }
  53. else
  54. {
  55. file.file->write(file.reply->readAll());
  56. file.file->close();
  57. file.status = FileEntry::FINISHED;
  58. }
  59. bool downloadComplete = true;
  60. for (auto & entry : currentDownloads)
  61. {
  62. if (entry.status == FileEntry::IN_PROGRESS)
  63. {
  64. downloadComplete = false;
  65. break;
  66. }
  67. }
  68. QStringList successful;
  69. QStringList failed;
  70. for (auto & entry : currentDownloads)
  71. {
  72. if (entry.status == FileEntry::FINISHED)
  73. successful += entry.file->fileName();
  74. else
  75. failed += entry.file->fileName();
  76. }
  77. if (downloadComplete)
  78. emit finished(successful, failed, encounteredErrors);
  79. file.reply->deleteLater();
  80. file.reply = nullptr;
  81. }
  82. void CDownloadManager::downloadProgressChanged(qint64 bytesReceived, qint64 bytesTotal)
  83. {
  84. auto reply = dynamic_cast<QNetworkReply *>(sender());
  85. FileEntry & entry = getEntry(reply);
  86. entry.file->write(entry.reply->readAll());
  87. entry.bytesReceived = bytesReceived;
  88. entry.totalSize = bytesTotal;
  89. quint64 total = 0;
  90. for (auto & entry : currentDownloads)
  91. total += entry.totalSize > 0 ? entry.totalSize : 0;
  92. quint64 received = 0;
  93. for (auto & entry : currentDownloads)
  94. received += entry.bytesReceived > 0 ? entry.bytesReceived : 0;
  95. emit downloadProgress(received, total);
  96. }
  97. bool CDownloadManager::downloadInProgress(const QUrl &url)
  98. {
  99. for (auto & entry : currentDownloads)
  100. {
  101. if (entry.reply->url() == url)
  102. return true;
  103. }
  104. return false;
  105. }