cdownloadmanager_moc.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. if(entry.file->open(QIODevice::WriteOnly | QIODevice::Truncate))
  26. {
  27. entry.status = FileEntry::IN_PROGRESS;
  28. entry.reply = manager.get(request);
  29. connect(entry.reply, SIGNAL(downloadProgress(qint64,qint64)),
  30. SLOT(downloadProgressChanged(qint64,qint64)));
  31. }
  32. else
  33. {
  34. entry.status = FileEntry::FAILED;
  35. entry.reply = nullptr;
  36. encounteredErrors += entry.file->errorString();
  37. }
  38. // even if failed - add it into list to report it in finished() call
  39. currentDownloads.push_back(entry);
  40. }
  41. CDownloadManager::FileEntry & CDownloadManager::getEntry(QNetworkReply * reply)
  42. {
  43. assert(reply);
  44. for(auto & entry : currentDownloads)
  45. {
  46. if(entry.reply == reply)
  47. return entry;
  48. }
  49. assert(0);
  50. static FileEntry errorValue;
  51. return errorValue;
  52. }
  53. void CDownloadManager::downloadFinished(QNetworkReply * reply)
  54. {
  55. FileEntry & file = getEntry(reply);
  56. if(file.reply->error())
  57. {
  58. encounteredErrors += file.reply->errorString();
  59. file.file->remove();
  60. file.status = FileEntry::FAILED;
  61. }
  62. else
  63. {
  64. file.file->write(file.reply->readAll());
  65. file.file->close();
  66. file.status = FileEntry::FINISHED;
  67. }
  68. bool downloadComplete = true;
  69. for(auto & entry : currentDownloads)
  70. {
  71. if(entry.status == FileEntry::IN_PROGRESS)
  72. {
  73. downloadComplete = false;
  74. break;
  75. }
  76. }
  77. QStringList successful;
  78. QStringList failed;
  79. for(auto & entry : currentDownloads)
  80. {
  81. if(entry.status == FileEntry::FINISHED)
  82. successful += entry.file->fileName();
  83. else
  84. failed += entry.file->fileName();
  85. }
  86. if(downloadComplete)
  87. emit finished(successful, failed, encounteredErrors);
  88. file.reply->deleteLater();
  89. file.reply = nullptr;
  90. }
  91. void CDownloadManager::downloadProgressChanged(qint64 bytesReceived, qint64 bytesTotal)
  92. {
  93. auto reply = dynamic_cast<QNetworkReply *>(sender());
  94. FileEntry & entry = getEntry(reply);
  95. entry.file->write(entry.reply->readAll());
  96. entry.bytesReceived = bytesReceived;
  97. entry.totalSize = bytesTotal;
  98. quint64 total = 0;
  99. for(auto & entry : currentDownloads)
  100. total += entry.totalSize > 0 ? entry.totalSize : 0;
  101. quint64 received = 0;
  102. for(auto & entry : currentDownloads)
  103. received += entry.bytesReceived > 0 ? entry.bytesReceived : 0;
  104. emit downloadProgress(received, total);
  105. }
  106. bool CDownloadManager::downloadInProgress(const QUrl & url)
  107. {
  108. for(auto & entry : currentDownloads)
  109. {
  110. if(entry.reply->url() == url)
  111. return true;
  112. }
  113. return false;
  114. }