//! [1] void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) { // Update the properties with the new progress values m_progressTotal = bytesTotal; m_progressValue = bytesReceived; emit progressTotalChanged(); emit progressValueChanged(); // Calculate the download speed ... double speed = bytesReceived * 1000.0 / m_downloadTime.elapsed(); QString unit; if (speed < 1024) { unit = "bytes/sec"; } else if (speed < 1024 * 1024) { speed /= 1024; unit = "kB/s"; } else { speed /= 1024 * 1024; unit = "MB/s"; } // ... and update the progress message. m_progressMessage = QString("%1 %2").arg(speed, 3, 'f', 1).arg(unit); emit progressMessageChanged(); }
void AdBlockDownloader::onDownloadProgress(qint64 bytesreceived, qint64 bytestotal) { if(!this->_downloading) { this->_downloading = true; emit downloadingChanged(); } this->_progressvalue = bytesreceived; if(this->_progresstotal != bytestotal) { this->_progresstotal = bytestotal; emit progressTotalChanged(); } emit progressValueChanged(); }
//! [2] void DownloadManager::downloadFinished() { // Reset the progress information when the download has finished m_progressTotal = 0; m_progressValue = 0; m_progressMessage.clear(); emit progressValueChanged(); emit progressTotalChanged(); emit progressMessageChanged(); // Close the file where the data have been written m_output.close(); // Add a status or error message if (m_currentDownload->error()) { addErrorMessage(QString("Failed: %1").arg(m_currentDownload->errorString())); } else if (m_currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 302) { m_output.remove(); QUrl redirecturl = m_currentDownload->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); if(redirecturl.isValid() && (redirecturl != m_currentDownload->url())) /* Avoid Fake/Redirect Loop */ { downloadUrl(redirecturl.toString()); } } else { addStatusMessage("Succeeded."); ++m_downloadedCount; } /** * We can't call 'delete m_currentDownload' here, because this method might have been invoked directly as result of a signal * emission of the network reply object. */ m_currentDownload->deleteLater(); m_currentDownload = 0; emit activeDownloadsChanged(); basename = ""; // Trigger the execution of the next job startNextDownload(); }