gamelistrefreshthread.cpp (3095B)
1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) 3 4 #include "gamelistrefreshthread.h" 5 #include "qtutils.h" 6 7 #include "core/game_list.h" 8 9 #include "common/log.h" 10 #include "common/progress_callback.h" 11 #include "common/timer.h" 12 13 #include <QtWidgets/QMessageBox> 14 15 AsyncRefreshProgressCallback::AsyncRefreshProgressCallback(GameListRefreshThread* parent) : m_parent(parent) 16 { 17 } 18 19 float AsyncRefreshProgressCallback::timeSinceStart() const 20 { 21 return m_start_time.GetTimeSeconds(); 22 } 23 24 void AsyncRefreshProgressCallback::Cancel() 25 { 26 // Not atomic, but we don't need to cancel immediately. 27 m_cancelled = true; 28 } 29 30 void AsyncRefreshProgressCallback::PushState() 31 { 32 ProgressCallback::PushState(); 33 } 34 35 void AsyncRefreshProgressCallback::PopState() 36 { 37 ProgressCallback::PopState(); 38 39 if (static_cast<int>(m_progress_range) == m_last_range && static_cast<int>(m_progress_value) == m_last_value) 40 return; 41 42 m_last_range = static_cast<int>(m_progress_range); 43 m_last_value = static_cast<int>(m_progress_value); 44 fireUpdate(); 45 } 46 47 void AsyncRefreshProgressCallback::SetStatusText(const std::string_view text) 48 { 49 const QString new_text = QtUtils::StringViewToQString(text); 50 if (new_text == m_status_text) 51 return; 52 53 m_status_text = new_text; 54 fireUpdate(); 55 } 56 57 void AsyncRefreshProgressCallback::SetProgressRange(u32 range) 58 { 59 ProgressCallback::SetProgressRange(range); 60 if (static_cast<int>(m_progress_range) == m_last_range) 61 return; 62 63 m_last_range = static_cast<int>(m_progress_range); 64 fireUpdate(); 65 } 66 67 void AsyncRefreshProgressCallback::SetProgressValue(u32 value) 68 { 69 ProgressCallback::SetProgressValue(value); 70 if (static_cast<int>(m_progress_value) == m_last_value) 71 return; 72 73 m_last_value = static_cast<int>(m_progress_value); 74 fireUpdate(); 75 } 76 77 void AsyncRefreshProgressCallback::ModalError(const std::string_view message) 78 { 79 QMessageBox::critical(nullptr, QStringLiteral("Error"), QtUtils::StringViewToQString(message)); 80 } 81 82 bool AsyncRefreshProgressCallback::ModalConfirmation(const std::string_view message) 83 { 84 return QMessageBox::question(nullptr, QStringLiteral("Question"), QtUtils::StringViewToQString(message)) == 85 QMessageBox::Yes; 86 } 87 88 void AsyncRefreshProgressCallback::ModalInformation(const std::string_view message) 89 { 90 QMessageBox::information(nullptr, QStringLiteral("Information"), QtUtils::StringViewToQString(message)); 91 } 92 93 void AsyncRefreshProgressCallback::fireUpdate() 94 { 95 m_parent->refreshProgress(m_status_text, m_last_value, m_last_range, m_start_time.GetTimeSeconds()); 96 } 97 98 GameListRefreshThread::GameListRefreshThread(bool invalidate_cache) 99 : QThread(), m_progress(this), m_invalidate_cache(invalidate_cache) 100 { 101 } 102 103 GameListRefreshThread::~GameListRefreshThread() = default; 104 105 float GameListRefreshThread::timeSinceStart() const 106 { 107 return m_progress.timeSinceStart(); 108 } 109 110 void GameListRefreshThread::cancel() 111 { 112 m_progress.Cancel(); 113 } 114 115 void GameListRefreshThread::run() 116 { 117 GameList::Refresh(m_invalidate_cache, false, &m_progress); 118 emit refreshComplete(); 119 }