videospatch.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "videospatch.h"
  2. #include <QUrlQuery>
  3. #include <QSet>
  4. #include <QThread>
  5. VideosPatch::VideosPatch(LotroDatManager *mgr, QObject *parent) : Patch("VideosPatch", mgr, parent)
  6. {
  7. }
  8. void VideosPatch::checkForUpdates()
  9. {
  10. QUrlQuery query; // query for building GET-request aka patch-version
  11. foreach (QString db_name, databases_names) {
  12. query.addQueryItem(db_name, "100");
  13. }
  14. QUrl target_url;
  15. target_url.setUrl(Settings::getValue("Network/patch_updates_url").toString());
  16. target_url.setQuery(query);
  17. QByteArray target_array;
  18. Downloader downloader;
  19. downloader.setUrl(target_url);
  20. downloader.targetBytearray = &target_array;
  21. downloader.start();
  22. downloader.waitForDownloaded();
  23. if (target_array.isEmpty()) {
  24. qWarning() << *this << "Cannot check for updates, target_array is empty!";
  25. emit errorOccured(E_CHECKFORUPDATES, this, "");
  26. emit operationFinished(E_CHECKFORUPDATES, this);
  27. return;
  28. }
  29. QStringList patch_info = QString(target_array).split('|');
  30. if (patch_info.size() != databases_names.size()) {
  31. qCritical() << __FUNCTION__ << "Incorrect patches number! Data: " << patch_info;
  32. emit errorOccured(E_CHECKFORUPDATES, this, "");
  33. emit operationFinished(E_CHECKFORUPDATES, this);
  34. return;
  35. }
  36. for (int i = 0; i < databases_names.size(); ++i) {
  37. QStringList patch_data = patch_info[i].split(":::");
  38. if (patch_data.size() != 3) {
  39. qCritical() << __FUNCTION__ << "Incorrect patch entry size! Entry: " << patch_data;
  40. emit errorOccured(E_CHECKFORUPDATES, this, "");
  41. emit operationFinished(E_CHECKFORUPDATES, this);
  42. return;
  43. }
  44. QString patch_filename = Settings::getValue("General/PatchDownloadDir").toString() + "/" + QUrl(patch_data[0]).fileName();
  45. Settings::setValue("PatchDatabases/" + databases_names[i] + "/url", patch_data[0]);
  46. Settings::setValue("PatchDatabases/" + databases_names[i] + "/hashsum", patch_data[1]);
  47. Settings::setValue("PatchDatabases/" + databases_names[i] + "/datetime", patch_data[2]);
  48. Settings::setValue("PatchDatabases/" + databases_names[i] + "/path", patch_filename);
  49. }
  50. emit operationFinished(E_CHECKFORUPDATES, this);
  51. }
  52. void VideosPatch::download()
  53. {
  54. foreach (QString db_name, databases_names) {
  55. QString settings_prefix = "PatchDatabases/" + db_name;
  56. QString target_filename = QApplication::applicationDirPath() + "/" + Settings::getValue(settings_prefix + "/path").toString();
  57. qDebug() << patch_name_ << ": Checking if file " << target_filename << " matches its hashsum";
  58. if (FileSystem::fileHash(target_filename) == Settings::getValue(settings_prefix + "/hashsum").toString()) {
  59. qInfo() << *this << ": file " << target_filename << " is up-to-date, no need to download";
  60. continue;
  61. }
  62. FileSystem::createFilePath(target_filename);
  63. QFile* target_file = new QFile(target_filename);
  64. if (!target_file->open(QIODevice::ReadWrite | QIODevice::Truncate)) {
  65. qWarning() << *this << "Cannot open file " << target_filename;
  66. continue;
  67. }
  68. qInfo() << *this << ": beginning download of file " << target_filename;
  69. Downloader* downloader = new Downloader();
  70. downloader->setUrl(Settings::getValue(settings_prefix + "/url").toUrl());
  71. downloader->targetFile = target_file;
  72. connect(downloader, &Downloader::progressChanged, this, &VideosPatch::onDownloaderProgressChanged);
  73. connect(downloader, &Downloader::downloadFinished, this, &VideosPatch::onDownloaderFinished);
  74. downloaders_.insert(downloader);
  75. downloader->start();
  76. }
  77. if (downloaders_.empty()) {
  78. emit operationFinished(E_DOWNLOAD, this);
  79. }
  80. }
  81. void VideosPatch::install()
  82. {
  83. QThread::msleep(500);
  84. emit operationFinished(E_INSTALL, this);
  85. }
  86. void VideosPatch::activate()
  87. {
  88. QThread::msleep(500);
  89. emit operationFinished(E_ACTIVATE, this);
  90. }
  91. void VideosPatch::onDownloaderProgressChanged(Downloader *, Downloader::Status)
  92. {
  93. Downloader::Status all_downloads_status;
  94. foreach (Downloader* downloader, downloaders_) {
  95. all_downloads_status = all_downloads_status + downloader->getDownloadStatus();
  96. }
  97. emit progressChanged(OperationProgress(all_downloads_status), this);
  98. }
  99. void VideosPatch::onDownloaderFinished(Downloader *ptr)
  100. {
  101. ptr->targetFile->close();
  102. ptr->targetFile->deleteLater();
  103. Downloader::Status all_downloads_status;
  104. for (const Downloader* downloader : downloaders_) {
  105. all_downloads_status = all_downloads_status + downloader->getDownloadStatus();
  106. }
  107. if (!all_downloads_status.running) {
  108. for (Downloader* downloader : downloaders_) {
  109. downloader->deleteLater();
  110. }
  111. downloaders_.clear();
  112. emit operationFinished(E_DOWNLOAD, this);
  113. }
  114. }