* @brief Handles individual file download completion and triggers the next * download. Auto-opens the project file when all downloads complete. */
| 371 | * download. Auto-opens the project file when all downloads complete. |
| 372 | */ |
| 373 | void Misc::Examples::onFileDownloadReply() |
| 374 | { |
| 375 | auto* reply = qobject_cast<QNetworkReply*>(sender()); |
| 376 | if (!reply) |
| 377 | return; |
| 378 | |
| 379 | reply->deleteLater(); |
| 380 | |
| 381 | if (reply->error() == QNetworkReply::NoError) { |
| 382 | const auto file_name = reply->property("fileName").toString(); |
| 383 | const auto file_path = m_downloadPath + "/" + file_name; |
| 384 | |
| 385 | QFileInfo info(file_path); |
| 386 | QDir().mkpath(info.absolutePath()); |
| 387 | |
| 388 | QFile file(file_path); |
| 389 | if (file.open(QIODevice::WriteOnly)) { |
| 390 | file.write(reply->readAll()); |
| 391 | file.close(); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | --m_pendingDownloads; |
| 396 | m_downloadProgress = static_cast<float>(m_totalDownloads - m_pendingDownloads) |
| 397 | / static_cast<float>(m_totalDownloads); |
| 398 | Q_EMIT downloadProgressChanged(); |
| 399 | |
| 400 | if (!m_downloadQueue.isEmpty()) { |
| 401 | downloadNextFile(); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | m_loading = false; |
| 406 | Q_EMIT loadingChanged(); |
| 407 | |
| 408 | Misc::Utilities::revealFile(m_downloadPath); |
| 409 | |
| 410 | if (m_selectedIndex >= 0 && m_selectedIndex < m_filteredExamples.count()) { |
| 411 | const auto example = m_filteredExamples.at(m_selectedIndex).toMap(); |
| 412 | const auto project_file = example.value("projectFileName").toString(); |
| 413 | if (!project_file.isEmpty()) { |
| 414 | const auto path = m_downloadPath + "/" + project_file; |
| 415 | if (QFile::exists(path)) |
| 416 | Q_EMIT projectFileReady(path); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | //-------------------------------------------------------------------------------------------------- |
| 422 | // Internal helpers |