coverdownloaddialog.cpp (3414B)
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 "coverdownloaddialog.h" 5 #include "qthost.h" 6 7 #include "core/game_list.h" 8 9 #include "common/assert.h" 10 11 CoverDownloadDialog::CoverDownloadDialog(QWidget* parent /*= nullptr*/) : QDialog(parent) 12 { 13 m_ui.setupUi(this); 14 setWindowIcon(QtHost::GetAppIcon()); 15 m_ui.coverIcon->setPixmap(QIcon::fromTheme("artboard-2-line").pixmap(32)); 16 updateEnabled(); 17 18 connect(m_ui.start, &QPushButton::clicked, this, &CoverDownloadDialog::onStartClicked); 19 connect(m_ui.close, &QPushButton::clicked, this, &CoverDownloadDialog::onCloseClicked); 20 connect(m_ui.urls, &QTextEdit::textChanged, this, &CoverDownloadDialog::updateEnabled); 21 } 22 23 CoverDownloadDialog::~CoverDownloadDialog() 24 { 25 Assert(!m_thread); 26 } 27 28 void CoverDownloadDialog::closeEvent(QCloseEvent* ev) 29 { 30 cancelThread(); 31 } 32 33 void CoverDownloadDialog::onDownloadStatus(const QString& text) 34 { 35 m_ui.status->setText(text); 36 } 37 38 void CoverDownloadDialog::onDownloadProgress(int value, int range) 39 { 40 // Limit to once every five seconds, otherwise it's way too flickery. 41 // Ideally in the future we'd have some way to invalidate only a single cover. 42 if (m_last_refresh_time.GetTimeSeconds() >= 5.0f) 43 { 44 emit coverRefreshRequested(); 45 m_last_refresh_time.Reset(); 46 } 47 48 if (range != m_ui.progress->maximum()) 49 m_ui.progress->setMaximum(range); 50 m_ui.progress->setValue(value); 51 } 52 53 void CoverDownloadDialog::onDownloadComplete() 54 { 55 emit coverRefreshRequested(); 56 57 if (m_thread) 58 { 59 m_thread->join(); 60 m_thread.reset(); 61 } 62 63 updateEnabled(); 64 65 m_ui.status->setText(tr("Download complete.")); 66 } 67 68 void CoverDownloadDialog::onStartClicked() 69 { 70 if (m_thread) 71 cancelThread(); 72 else 73 startThread(); 74 } 75 76 void CoverDownloadDialog::onCloseClicked() 77 { 78 if (m_thread) 79 cancelThread(); 80 81 done(0); 82 } 83 84 void CoverDownloadDialog::updateEnabled() 85 { 86 const bool running = static_cast<bool>(m_thread); 87 m_ui.start->setText(running ? tr("Stop") : tr("Start")); 88 m_ui.start->setEnabled(running || !m_ui.urls->toPlainText().isEmpty()); 89 m_ui.close->setEnabled(!running); 90 m_ui.urls->setEnabled(!running); 91 } 92 93 void CoverDownloadDialog::startThread() 94 { 95 m_thread = std::make_unique<CoverDownloadThread>(this, m_ui.urls->toPlainText(), m_ui.useSerialFileNames->isChecked()); 96 m_last_refresh_time.Reset(); 97 connect(m_thread.get(), &CoverDownloadThread::statusUpdated, this, &CoverDownloadDialog::onDownloadStatus); 98 connect(m_thread.get(), &CoverDownloadThread::progressUpdated, this, &CoverDownloadDialog::onDownloadProgress); 99 connect(m_thread.get(), &CoverDownloadThread::threadFinished, this, &CoverDownloadDialog::onDownloadComplete); 100 m_thread->start(); 101 updateEnabled(); 102 } 103 104 void CoverDownloadDialog::cancelThread() 105 { 106 if (!m_thread) 107 return; 108 109 m_thread->requestInterruption(); 110 m_thread->join(); 111 m_thread.reset(); 112 } 113 114 CoverDownloadDialog::CoverDownloadThread::CoverDownloadThread(QWidget* parent, const QString& urls, bool use_serials) 115 : QtAsyncProgressThread(parent), m_use_serials(use_serials) 116 { 117 for (const QString& str : urls.split(QChar('\n'))) 118 m_urls.push_back(str.toStdString()); 119 } 120 121 CoverDownloadDialog::CoverDownloadThread::~CoverDownloadThread() = default; 122 123 void CoverDownloadDialog::CoverDownloadThread::runAsync() 124 { 125 GameList::DownloadCovers(m_urls, m_use_serials, this); 126 }