patchlist.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "patchlist.h"
  2. #include <QDebug>
  3. PatchList::PatchList(LotroDatManager *mgr, QObject *parent) : QObject(parent)
  4. {
  5. lotro_mgr_ = mgr;
  6. texts_patch_ = new TextsPatch(mgr);
  7. graphics_patch_ = new GraphicsPatch(mgr);
  8. sounds_patch_ = new SoundsPatch(mgr);
  9. videos_patch_ = new VideosPatch(mgr);
  10. texts_patch_thread_ = new QThread(this);
  11. graphics_patch_thread_ = new QThread(this);
  12. sounds_patch_thread_ = new QThread(this);
  13. videos_patch_thread_ = new QThread(this);
  14. connect(texts_patch_thread_, &QThread::finished, texts_patch_, &TextsPatch::deleteLater);
  15. connect(graphics_patch_thread_, &QThread::finished, graphics_patch_, &GraphicsPatch::deleteLater);
  16. connect(sounds_patch_thread_, &QThread::finished, sounds_patch_, &SoundsPatch::deleteLater);
  17. connect(videos_patch_thread_, &QThread::finished, videos_patch_, &VideosPatch::deleteLater);
  18. texts_patch_->moveToThread(texts_patch_thread_);
  19. graphics_patch_->moveToThread(graphics_patch_thread_);
  20. sounds_patch_->moveToThread(sounds_patch_thread_);
  21. videos_patch_->moveToThread(videos_patch_thread_);
  22. connect(texts_patch_, &TextsPatch::operationStarted, this, &PatchList::onPatchOperationStarted, Qt::QueuedConnection);
  23. connect(graphics_patch_, &GraphicsPatch::operationStarted, this, &PatchList::onPatchOperationStarted, Qt::QueuedConnection);
  24. connect(sounds_patch_, &SoundsPatch::operationStarted, this, &PatchList::onPatchOperationStarted, Qt::QueuedConnection);
  25. connect(videos_patch_, &VideosPatch::operationStarted, this, &PatchList::onPatchOperationStarted, Qt::QueuedConnection);
  26. connect(texts_patch_, &TextsPatch::operationFinished, this, &PatchList::onPatchOperationFinished, Qt::QueuedConnection);
  27. connect(graphics_patch_, &GraphicsPatch::operationFinished, this, &PatchList::onPatchOperationFinished, Qt::QueuedConnection);
  28. connect(sounds_patch_, &SoundsPatch::operationFinished, this, &PatchList::onPatchOperationFinished, Qt::QueuedConnection);
  29. connect(videos_patch_, &VideosPatch::operationFinished, this, &PatchList::onPatchOperationFinished, Qt::QueuedConnection);
  30. connect(texts_patch_, &TextsPatch::downloadStatusChanged, this, &PatchList::onPatchDownloadStatusChanged, Qt::QueuedConnection);
  31. connect(graphics_patch_, &GraphicsPatch::downloadStatusChanged, this, &PatchList::onPatchDownloadStatusChanged, Qt::QueuedConnection);
  32. connect(sounds_patch_, &SoundsPatch::downloadStatusChanged, this, &PatchList::onPatchDownloadStatusChanged, Qt::QueuedConnection);
  33. connect(videos_patch_, &VideosPatch::downloadStatusChanged, this, &PatchList::onPatchDownloadStatusChanged, Qt::QueuedConnection);
  34. connect(texts_patch_, &TextsPatch::installStatusChanged, this, &PatchList::onPatchInstallStatusChanged, Qt::QueuedConnection);
  35. connect(graphics_patch_, &GraphicsPatch::installStatusChanged, this, &PatchList::onPatchInstallStatusChanged, Qt::QueuedConnection);
  36. connect(sounds_patch_, &SoundsPatch::installStatusChanged, this, &PatchList::onPatchInstallStatusChanged, Qt::QueuedConnection);
  37. connect(videos_patch_, &VideosPatch::installStatusChanged, this, &PatchList::onPatchInstallStatusChanged, Qt::QueuedConnection);
  38. connect(lotro_mgr_, &LotroDatManager::operationFinished, this, &PatchList::onLotroManagerOperationFinished, Qt::QueuedConnection);
  39. texts_patch_thread_->start();
  40. graphics_patch_thread_->start();
  41. sounds_patch_thread_->start();
  42. videos_patch_thread_->start();
  43. patch_update_timer.setInterval(10 * 60 * 1000); // 10 minutes
  44. connect(&patch_update_timer, &QTimer::timeout, this, &PatchList::update);
  45. }
  46. QList<Patch *> PatchList::getPatchList()
  47. {
  48. return {texts_patch_, graphics_patch_, sounds_patch_, videos_patch_};
  49. }
  50. void PatchList::onPatchOperationStarted(QString operation_name, Patch *patch)
  51. {
  52. patch_update_timer.stop();
  53. if (active_operations_num_ == 0) {
  54. emit patchOperationsStarted();
  55. }
  56. ++active_operations_num_;
  57. qDebug() << "Operation " << operation_name << " started of patchset " << patch->getPatchName();
  58. }
  59. void PatchList::onPatchOperationFinished(QString operation_name, Patch *patch, bool result)
  60. {
  61. qDebug() << "Operation " << operation_name << " finished of patchset " << patch->getPatchName() << ", result = " << result;
  62. --active_operations_num_;
  63. if (result && operation_name == "checkForUpdates") {
  64. QMetaObject::invokeMethod(patch, &Patch::download, Qt::QueuedConnection);
  65. return;
  66. }
  67. // if (result && operation_name == "download") {
  68. // QMetaObject::invokeMethod(patch, &Patch::install, Qt::QueuedConnection);
  69. // return;
  70. // }
  71. // if (result && operation_name == "install") {
  72. // QMetaObject::invokeMethod(patch, &Patch::activate, Qt::QueuedConnection);
  73. // return;
  74. // }
  75. if (active_operations_num_ == 0) {
  76. if (auto_updates_enabled_) {
  77. patch_update_timer.start();
  78. }
  79. qDebug() << __FUNCTION__ << "All patch operations successfully finished!";
  80. emit patchOperationsFinished();
  81. }
  82. }
  83. void PatchList::onPatchDownloadStatusChanged(Patch *patch, Downloader::Status status)
  84. {
  85. patches_download_status_[patch] = status;
  86. Downloader::Status total_status;
  87. foreach (Downloader::Status st, patches_download_status_) {
  88. total_status = total_status + st;
  89. }
  90. emit downloadTotalStatusChanged(total_status);
  91. }
  92. void PatchList::onPatchInstallStatusChanged(Patch *patch, Patch::InstallationStatus status)
  93. {
  94. patches_installation_status_[patch] = status;
  95. Patch::InstallationStatus total_status;
  96. foreach (Patch::InstallationStatus st, patches_installation_status_) {
  97. total_status = total_status + st;
  98. }
  99. emit installTotalStatusChanged(total_status);
  100. }
  101. void PatchList::onLotroManagerOperationFinished(QString operation_name, QVector<QVariant>, bool result)
  102. {
  103. if (operation_name == "initializeManager") {
  104. --active_operations_num_;
  105. if (active_operations_num_ == 0) {
  106. emit patchOperationsFinished();
  107. }
  108. // if (result == true) {
  109. update();
  110. // }
  111. }
  112. }
  113. void PatchList::startAutoUpdate()
  114. {
  115. auto_updates_enabled_ = true;
  116. patch_update_timer.start();
  117. }
  118. void PatchList::initialize() {
  119. ++active_operations_num_;
  120. emit patchOperationsStarted();
  121. QMetaObject::invokeMethod(lotro_mgr_, &LotroDatManager::initializeManager, Qt::QueuedConnection);
  122. }
  123. void PatchList::update()
  124. {
  125. if (active_operations_num_ != 0) {
  126. qDebug() << __FUNCTION__ << "Tried to start update, while other operations weren't finished yet!";
  127. return;
  128. }
  129. qDebug() << __FUNCTION__ << "Starting update!";
  130. QMetaObject::invokeMethod(texts_patch_, &TextsPatch::checkForUpdates, Qt::QueuedConnection);
  131. QMetaObject::invokeMethod(graphics_patch_, &GraphicsPatch::checkForUpdates, Qt::QueuedConnection);
  132. QMetaObject::invokeMethod(sounds_patch_, &SoundsPatch::checkForUpdates, Qt::QueuedConnection);
  133. QMetaObject::invokeMethod(videos_patch_, &VideosPatch::checkForUpdates, Qt::QueuedConnection);
  134. }