From 6359d7d23f4137a35dfd241a0daa22c6a885e426 Mon Sep 17 00:00:00 2001 From: Silarn Date: Sun, 6 May 2018 17:21:35 -0500 Subject: Fixes for downloads and installs --- src/downloadmanager.cpp | 49 +++++++++++++++++++++++++-------------------- src/downloadmanager.h | 4 ++-- src/installationmanager.cpp | 43 +++++++++++++++++---------------------- src/installationmanager.h | 7 ++----- 4 files changed, 49 insertions(+), 54 deletions(-) diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp index 0da5ced3..2ab56fab 100644 --- a/src/downloadmanager.cpp +++ b/src/downloadmanager.cpp @@ -145,8 +145,10 @@ void DownloadManager::DownloadInfo::setName(QString newName, bool renameFile) QString oldMetaFileName = QString("%1.meta").arg(m_FileName); m_FileName = QFileInfo(newName).fileName(); if ((m_State == DownloadManager::STATE_STARTED) || - (m_State == DownloadManager::STATE_DOWNLOADING)) { + (m_State == DownloadManager::STATE_DOWNLOADING) || + (m_State == DownloadManager::STATE_PAUSED)) { newName.append(UNFINISHED); + oldMetaFileName = QString("%1%2.meta").arg(m_FileName).arg(UNFINISHED); } if (renameFile) { if ((newName != m_Output.fileName()) && !m_Output.rename(newName)) { @@ -154,10 +156,9 @@ void DownloadManager::DownloadInfo::setName(QString newName, bool renameFile) return; } - QFile metaFile(oldMetaFileName); - if (metaFile.exists()) { + QFile metaFile(QFileInfo(newName).path() + "/" + oldMetaFileName); + if (metaFile.exists()) metaFile.rename(newName.mid(0).append(".meta")); - } } if (!m_Output.isOpen()) { // can't set file name if it's open @@ -457,7 +458,9 @@ void DownloadManager::startDownload(QNetworkReply *reply, DownloadInfo *newDownl else setState(newDownload, STATE_DOWNLOADING); } - } + } else + connect(newDownload->m_Reply, SIGNAL(finished()), this, SLOT(downloadFinished())); + QCoreApplication::processEvents(); @@ -465,8 +468,8 @@ void DownloadManager::startDownload(QNetworkReply *reply, DownloadInfo *newDownl downloadFinished(indexByName(newDownload->m_FileName)); return; } - } - connect(newDownload->m_Reply, SIGNAL(finished()), this, SLOT(downloadFinished())); + } else + connect(newDownload->m_Reply, SIGNAL(finished()), this, SLOT(downloadFinished())); } @@ -496,11 +499,11 @@ void DownloadManager::addNXMDownload(const QString &url) for (DownloadInfo *download : m_ActiveDownloads) { if (download->m_FileInfo->modID == nxmInfo.modId() && download->m_FileInfo->fileID == nxmInfo.fileId()) { if (download->m_State == STATE_DOWNLOADING || download->m_State == STATE_PAUSED || download->m_State == STATE_STARTED) { - qDebug("download requested is already started (mod: %s, file: %s)", qPrintable(download->m_FileInfo->modName), + qDebug("download requested is already started (mod: %s, file: %s)", qPrintable(download->m_FileInfo->modID), qPrintable(download->m_FileInfo->fileName)); - QMessageBox::information(nullptr, tr("Already Started"), tr("There is already a download started for this file (%2).") - .arg(download->m_FileInfo->modName).arg(download->m_FileInfo->name), QMessageBox::Ok); + QMessageBox::information(nullptr, tr("Already Started"), tr("There is already a download started for this file (mod: %1, file: %2).") + .arg(download->m_FileInfo->modName).arg(download->m_FileInfo->fileName), QMessageBox::Ok); return; } } @@ -702,6 +705,8 @@ void DownloadManager::resumeDownloadInt(int index) std::get<0>(info->m_SpeedDiff) = 0; std::get<1>(info->m_SpeedDiff) = 0; std::get<2>(info->m_SpeedDiff) = 0; + std::get<3>(info->m_SpeedDiff) = 0; + std::get<4>(info->m_SpeedDiff) = 0; qDebug("resume at %lld bytes", info->m_ResumePos); QByteArray rangeHeader = "bytes=" + QByteArray::number(info->m_ResumePos) + "-"; request.setRawHeader("Range", rangeHeader); @@ -1105,11 +1110,17 @@ void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) int oldProgress = info->m_Progress.first; info->m_Progress.first = ((info->m_ResumePos + bytesReceived) * 100) / (info->m_ResumePos + bytesTotal); + int elapsed = info->m_StartTime.elapsed(); + std::get<0>(info->m_SpeedDiff) = bytesReceived - std::get<2>(info->m_SpeedDiff); + std::get<1>(info->m_SpeedDiff) = elapsed - std::get<3>(info->m_SpeedDiff); + std::get<2>(info->m_SpeedDiff) = bytesReceived; + std::get<3>(info->m_SpeedDiff) = elapsed; + + double calc = ((double)std::get<0>(info->m_SpeedDiff)) / (((double)(std::get<1>(info->m_SpeedDiff)) / 5000.0)); + std::get<4>(info->m_SpeedDiff) = ((calc*0.5) + (std::get<4>(info->m_SpeedDiff)*1.5)) / 2; + // calculate the download speed - double speed = (bytesReceived - std::get<1>(info->m_SpeedDiff)) * 1000.0 / // Calculate with the data transferred in the last 5 seconds... - (std::get<1>(info->m_SpeedDiff) ? // If we are past 5 seconds - (5 * 1000) + (info->m_StartTime.elapsed() - std::get<2>(info->m_SpeedDiff)) : // Divide by 5 seconds + the diff between chunks - info->m_StartTime.elapsed()); // Else just use the current elapsed time + double speed = (std::get<4>(info->m_SpeedDiff) * 1000.0) / (5 * 1000); QString unit; if (speed < 1024) { @@ -1126,12 +1137,6 @@ void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) info->m_Progress.second = QString::fromLatin1("%1% - %2 %3").arg(info->m_Progress.first).arg(speed, 3, 'f', 1).arg(unit); - if (info->m_StartTime.elapsed() >= 5 * 1000) { - std::get<1>(info->m_SpeedDiff) = std::get<1>(info->m_SpeedDiff) + bytesReceived - std::get<0>(info->m_SpeedDiff); - std::get<2>(info->m_SpeedDiff) = info->m_StartTime.elapsed(); - } - std::get<0>(info->m_SpeedDiff) = bytesReceived; - TaskProgressManager::instance().updateProgress(info->m_TaskProgressId, bytesReceived, bytesTotal); emit update(index); } @@ -1551,7 +1556,7 @@ void DownloadManager::downloadFinished(int index) QString newName = getFileNameFromNetworkReply(reply); QString oldName = QFileInfo(info->m_Output).fileName(); - if (!newName.isEmpty() && (newName != oldName)) { + if (!newName.isEmpty() && (oldName.isEmpty())) { info->setName(getDownloadFileName(newName), true); } else { info->setName(m_OutputDirectory + "/" + info->m_FileName, true); // don't rename but remove the ".unfinished" extension @@ -1594,7 +1599,7 @@ void DownloadManager::metaDataChanged() DownloadInfo *info = findDownload(this->sender(), &index); if (info != nullptr) { QString newName = getFileNameFromNetworkReply(info->m_Reply); - if (!newName.isEmpty() && (newName != info->m_FileName)) { + if (!newName.isEmpty() && (info->m_FileName.isEmpty())) { info->setName(getDownloadFileName(newName), true); refreshAlphabeticalTranslation(); if (!info->m_Output.isOpen() && !info->m_Output.open(QIODevice::WriteOnly | QIODevice::Append)) { diff --git a/src/downloadmanager.h b/src/downloadmanager.h index 6e3f9c2c..98f5e468 100644 --- a/src/downloadmanager.h +++ b/src/downloadmanager.h @@ -76,7 +76,7 @@ private: QTime m_StartTime; qint64 m_PreResumeSize; std::pair m_Progress; - std::tuple m_SpeedDiff; + std::tuple m_SpeedDiff; DownloadState m_State; int m_CurrentUrl; QStringList m_Urls; @@ -114,7 +114,7 @@ private: private: static unsigned int s_NextDownloadID; private: - DownloadInfo() : m_TotalSize(0), m_ReQueried(false), m_Hidden(false), m_SpeedDiff(std::tuple(0,0,0)) {} + DownloadInfo() : m_TotalSize(0), m_ReQueried(false), m_Hidden(false), m_SpeedDiff(std::tuple(0,0,0,0,0)) {} }; public: diff --git a/src/installationmanager.cpp b/src/installationmanager.cpp index 27c83787..b7ec3d68 100644 --- a/src/installationmanager.cpp +++ b/src/installationmanager.cpp @@ -95,9 +95,6 @@ InstallationManager::InstallationManager() if (!m_ArchiveHandler->isValid()) { throw MyException(getErrorString(m_ArchiveHandler->getLastError())); } - - connect(this, SIGNAL(progressUpdate(float)), this, (SLOT(doProgressUpdate(float))), Qt::QueuedConnection); - connect(this, SIGNAL(progressUpdate(const QString)), this, (SLOT(doProgressFileUpdate(const QString))), Qt::QueuedConnection); } InstallationManager::~InstallationManager() @@ -203,9 +200,12 @@ bool InstallationManager::unpackSingleFile(const QString &fileName) ); }); do { - ::Sleep(50); + if (m_Progress != m_InstallationProgress->value()) + m_InstallationProgress->setValue(m_Progress); + if (m_ProgressFile != m_InstallationProgress->labelText()) + m_InstallationProgress->setLabelText(m_ProgressFile); QCoreApplication::processEvents(); - } while (!future.isFinished()); + } while (!future.isFinished() || m_InstallationProgress->isVisible()); bool res = future.result(); return res; @@ -301,9 +301,12 @@ QStringList InstallationManager::extractFiles(const QStringList &filesOrig, bool ); }); do { - ::Sleep(50); + if (m_Progress != m_InstallationProgress->value()) + m_InstallationProgress->setValue(m_Progress); + if (m_ProgressFile != m_InstallationProgress->labelText()) + m_InstallationProgress->setLabelText(m_ProgressFile); QCoreApplication::processEvents(); - } while (!future.isFinished()); + } while (!future.isFinished() || m_InstallationProgress->isVisible()); if (!future.result()) { throw MyException(QString("extracting failed (%1)").arg(m_ArchiveHandler->getLastError())); } @@ -430,21 +433,9 @@ DirectoryTree::Node *InstallationManager::getSimpleArchiveBase(DirectoryTree *da void InstallationManager::updateProgress(float percentage) -{ - emit progressUpdate(percentage); -} - - -void InstallationManager::updateProgressFile(QString const &fileName) -{ - emit progressUpdate(fileName); -} - - -void InstallationManager::doProgressUpdate(float percentage) { if (m_InstallationProgress != nullptr) { - m_InstallationProgress->setValue(static_cast(percentage * 100.0)); + m_Progress = static_cast(percentage * 100.0); if (m_InstallationProgress->wasCanceled()) { m_ArchiveHandler->cancel(); @@ -453,11 +444,10 @@ void InstallationManager::doProgressUpdate(float percentage) } } -void InstallationManager::doProgressFileUpdate(QString const fileName) + +void InstallationManager::updateProgressFile(QString const &fileName) { - if (m_InstallationProgress != nullptr) { - m_InstallationProgress->setLabelText(fileName); - } + m_ProgressFile = fileName; } @@ -610,7 +600,10 @@ bool InstallationManager::doInstall(GuessedValue &modName, QString game ); }); do { - ::Sleep(50); + if (m_Progress != m_InstallationProgress->value()) + m_InstallationProgress->setValue(m_Progress); + if (m_ProgressFile != m_InstallationProgress->labelText()) + m_InstallationProgress->setLabelText(m_ProgressFile); QCoreApplication::processEvents(); } while (!future.isFinished()); if (!future.result()) { diff --git a/src/installationmanager.h b/src/installationmanager.h index 84ccd1a4..e17aee17 100644 --- a/src/installationmanager.h +++ b/src/installationmanager.h @@ -182,11 +182,6 @@ signals: void progressUpdate(float percentage); void progressUpdate(QString const fileName); -private slots: - - void doProgressUpdate(float percentage); - void doProgressFileUpdate(const QString fileName); - private: struct ByPriority { @@ -217,6 +212,8 @@ private: QString m_CurrentFile; QProgressDialog *m_InstallationProgress { nullptr }; + int m_Progress; + QString m_ProgressFile; std::set m_TempFilesToDelete; -- cgit v1.3.1