void DownloadManager::startNextDownload() { if( m_downloadQueue.isEmpty() ) { emit downloadQueueEmpty(); return; } if( m_downloadHash.size() < m_downloadQueueSize ){ Downloads nextItem = m_downloadQueue.dequeue(); QNetworkRequest request; request.setUrl( nextItem.m_url ); request.setRawHeader( "USER-AGENT", m_userAgent.toUtf8() ); if( m_partialDownloadPolicy == ContinueDownload && nextItem.m_tempExists ){ nextItem.m_file = new QFile( nextItem.m_tempFile ); if( !nextItem.m_file->open( QIODevice::ReadWrite ) ){ emit status( nextItem.m_url, "Error", nextItem.m_file->errorString(), nextItem.m_destFile ); nextItem.m_file->deleteLater(); startNextDownload(); return; } nextItem.m_file->seek( nextItem.m_file->size() ); nextItem.m_tempSize = nextItem.m_file->size(); request.setRawHeader( "Range", QByteArray( "bytes=" + QByteArray::number( nextItem.m_tempSize ) + "-" ) ); } else { if( nextItem.m_tempExists ){ QFile::remove( nextItem.m_tempFile ); } nextItem.m_file = new QFile( nextItem.m_tempFile ); if( !nextItem.m_file->open( QIODevice::ReadWrite ) ){ emit status( nextItem.m_url, "Error", nextItem.m_file->errorString(), nextItem.m_destFile ); nextItem.m_file->deleteLater(); startNextDownload(); return; } nextItem.m_tempSize = 0; } QNetworkReply *reply = m_networkManager.get( request ); QObject::connect( reply, SIGNAL(downloadProgress( qint64,qint64 )), this, SLOT(downloadProgress( qint64,qint64) )); QObject::connect( reply, SIGNAL( finished() ), this, SLOT( downloadFinished() ) ); QObject::connect( reply, SIGNAL( readyRead() ), this, SLOT( downloadReadyRead() ) ); QObject::connect( reply, SIGNAL( error(QNetworkReply::NetworkError)), this, SLOT(downloadError(QNetworkReply::NetworkError) )); nextItem.m_time.start(); m_downloadHash[reply] = nextItem; m_urlHash[ nextItem.m_url ] = reply; startNextDownload(); } }
/*! * \qmlsignal MangoDownloader::startNextDownload() * Used if you want to download multiple files */ void MangoDownloader::startNextDownload() { if (downloadQueue.isEmpty()) { qDebug() << downloadedCount << " " << totalCount << "files downloaded successfully\n"; emit started(false); emit finished(); return; } QUrl url = downloadQueue.dequeue(); QString filename = saveFileName(mPath); output.setFileName(mPath + filename); if (!output.open(QIODevice::WriteOnly)) { qDebug() << "Problem opening save file for download"; startNextDownload(); return; } QNetworkRequest request(url); currentDownload = manager.get(request); connect(currentDownload, SIGNAL(downloadProgress(qint64,qint64)), SLOT(downloadProgress(qint64,qint64))); connect(currentDownload, SIGNAL(finished()), SLOT(downloadFinished())); connect(currentDownload, SIGNAL(readyRead()), SLOT(downloadReadyRead())); // prepare the output qDebug () << "Downloading " << url.toEncoded().constData() << "........"; downloadTime.start(); }
void DownloadManager::downloadFinished() { QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); if( hasRedirect( reply ) ) return; if ( reply->error() == QNetworkReply::NoError ) { Downloads item = m_downloadHash[reply]; if( !item.m_file ) return; if( item.m_file->size() == 0 ){ item.m_file->remove(); } else { item.m_file->close(); item.m_file->deleteLater(); if ( QFile::exists( item.m_destFile)){ QFile::remove( item.m_destFile ); } QFile::rename( item.m_tempFile, item.m_destFile ); emit status(item.m_url, "Complete", "Download file completed", item.m_url); emit finished( item.m_url, item.m_destFile ); } m_downloadHash.remove( reply ); m_urlHash.remove( item.m_url ); startNextDownload(); } reply->deleteLater(); }
void DownloadManager::stopDownloadImpl( QString const & url, bool pause ) { QNetworkReply *reply = m_urlHash[url]; if( reply ) { QObject::disconnect( reply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( downloadProgress( qint64, qint64 ))); QObject::disconnect( reply, SIGNAL( finished() ), this, SLOT( downloadFinished() )); QObject::disconnect( reply, SIGNAL( readyRead() ), this, SLOT( downloadReadyRead() ) ); QObject::disconnect( reply, SIGNAL( error( QNetworkReply::NetworkError )), this, SLOT( downloadError( QNetworkReply::NetworkError ))); Downloads item = m_downloadHash[ reply ]; reply->abort(); if( !item.m_file) return; item.m_file->write( reply->readAll() ); item.m_file->close(); if ( !pause ) { QFile::remove( item.m_tempFile ); } m_downloadHash.remove( reply ); m_urlHash.remove( url ); startNextDownload(); reply->deleteLater(); } }
void DownloadManager::startNextDownload() { if (downloadQueue.isEmpty()) { printf("%d/%d files downloaded successfully\n", downloadedCount, totalCount); emit finished(); return; } QUrl url = downloadQueue.dequeue(); QString filename = saveFileName(url); output.setFileName(filename); if (!output.open(QIODevice::WriteOnly)) { fprintf(stderr, "Problem opening save file '%s' for download '%s': %s\n", qPrintable(filename), url.toEncoded().constData(), qPrintable(output.errorString())); startNextDownload(); return; // skip this download } QNetworkRequest request(url); currentDownload = manager.get(request); connect(currentDownload, SIGNAL(downloadProgress(qint64,qint64)), SLOT(downloadProgress(qint64,qint64))); connect(currentDownload, SIGNAL(finished()), SLOT(downloadFinished())); connect(currentDownload, SIGNAL(readyRead()), SLOT(downloadReadyRead())); // prepare the output printf("Downloading %s...\n", url.toEncoded().constData()); downloadTime.start(); }
void plugDownloader::startNextDownload() { if (m_download_queue.isEmpty()) { emit downloadFinished(itemList); this->deleteLater(); return; } currentItem = m_download_queue.dequeue(); currentOutput.setFileName(outPath+currentItem.filename); if (!currentOutput.open(QIODevice::WriteOnly)) { qDebug() << "Unable to open file"; startNextDownload(); return; // skip this download } QNetworkRequest request(currentItem.url); currentDownload = manager.get(request); connect(currentDownload, SIGNAL(downloadProgress(qint64,qint64)), SLOT(downloadProgress(qint64,qint64))); connect(currentDownload, SIGNAL(finished()), SLOT(downloadFinished())); connect(currentDownload, SIGNAL(readyRead()), SLOT(downloadReadyRead())); // prepare the output downloadTime.start(); }
void MangoDownloader::append(const QUrl &url) { if (downloadQueue.isEmpty()) QTimer::singleShot(0, this, SLOT(startNextDownload())); downloadQueue.enqueue(url); ++totalCount; }
void QQmlNetwork::append(const QUrl &url) { if (downloadQueue.isEmpty()){ QTimer::singleShot(0, this, SLOT(startNextDownload())); } downloadQueue.enqueue(url); ++totalCount; }
/** * @brief Adds a download and starts downloading * @param elem Element to download * @see DownloadManagerElement */ void DownloadManager::addDownload(DownloadManagerElement elem) { qDebug() << "Entered, url=" << elem.url; if (m_queue.isEmpty()) QTimer::singleShot(0, this, SLOT(startNextDownload())); m_mutex.lock(); m_queue.enqueue(elem); m_mutex.unlock(); }
void DownloadManager::downloadError(QNetworkReply::NetworkError) { QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); Downloads item = m_downloadHash[reply]; emit status( item.m_url, "Error", reply->errorString(), item.m_url ); emit progress( item.m_url, 0, 0, 0, 0, "bytes/sec" ); m_downloadHash.remove(reply); m_urlHash.remove(item.m_url); startNextDownload(); }
void QQmlNetwork::startNextDownload() { if (downloadQueue.isEmpty()) { // qDebug() << downloadedCount << " " << totalCount << "files downloaded successfully\n"; emit finished(); return; } QUrl url = downloadQueue.dequeue(); QFileInfo fileInf(url.toString()); QString fileName = QString("%1%2").arg(m_path).arg(fileInf.fileName()); setSavedFileName(fileName); // qDebug() << fileName; output.setFileName(fileName); if (output.exists()){ if(output.remove()) { qDebug() << "removed the old file"; } } if (!output.open(QIODevice::WriteOnly)) { error("Problem opening save file for download"); startNextDownload(); return; } qDebug() << "GOING TO TRY AND DOWNLOAD " << url; QNetworkRequest request(url); currentDownload = manager.get(request); // FIX ME check for redirection connect(&manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(checkRedirection(QNetworkReply*))); connect(currentDownload, SIGNAL(downloadProgress(qint64,qint64)), SLOT(downloadProgress(qint64,qint64))); connect(currentDownload, SIGNAL(finished()), SLOT(downloadFinished())); connect(currentDownload, SIGNAL(readyRead()), SLOT(downloadReadyRead())); connect(currentDownload,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(setNetworkError(QNetworkReply::NetworkError))); downloadTime.start(); }
void DownloadManager::downloadFinished() { progressBar.clear(); output.close(); if (currentDownload->error()) { // download failed fprintf(stderr, "Failed: %s\n", qPrintable(currentDownload->errorString())); } else { printf("Succeeded.\n"); ++downloadedCount; } currentDownload->deleteLater(); startNextDownload(); }
/** * @brief Stops the current download and prepends it to the queue */ void DownloadManager::downloadTimeout() { if (!m_downloading) return; qWarning() << "Download timed out" << m_currentDownloadElement.url; m_retries++; m_currentReply->abort(); m_currentReply->deleteLater(); if (m_retries <= 2) { qDebug() << "Restarting the download"; m_queue.prepend(m_currentDownloadElement); } else { qDebug() << "Giving up on this file, tried 3 times"; m_retries = 0; } startNextDownload(); }
void DownloadManager::append(const QUrl &url) { /** * If there is no job in the queue at the moment or we do * not process any job currently, then we trigger the processing * of the next job. */ if (m_downloadQueue.isEmpty() && !m_currentDownload) QTimer::singleShot(0, this, SLOT(startNextDownload())); // Enqueue the new URL to the job queue m_downloadQueue.enqueue(url); emit activeDownloadsChanged(); // Increment the total number of jobs ++m_totalCount; }
void MangaDownloadWidget::downloadFinished(int status, QProcess::ExitStatus exitStatus) { switch (exitStatus) { case QProcess::CrashExit: { _messageModel->editMessageWarning("Warning: Downlaod failed. Status code: "+QString::number(status)); break; } case QProcess::NormalExit: { ++_downloadedCount; _chaptersOnWebModel->removeRow(_chaptersQueue.takeFirst().row()); updatedb(); updateChaptersOnPCView(); _messageModel->editMessageSuccess("Downlaod succeeded. Status code: "+QString::number(status)); break; } } startNextDownload(); }
//! [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(); }
void plugDownloader::downloadFinished() { currentOutput.close(); if (currentDownload->error()) { // download failed qDebug() << currentDownload->errorString(); qutim_sdk_0_2::TreeModelItem item; //TODO может стоит сделать локализацию qutim_sdk_0_2::SystemsCity::PluginSystem()->systemNotification(item, currentDownload->errorString()); } else { ++downloadedCount; } currentDownload->deleteLater(); if (currentOutput.exists()) { currentItem.filename = currentOutput.fileName(); itemList.append(currentItem); //fileList.append(currentOutput.fileName()); //в случае неудачного скачивания, но наличия старой версии файла,добавляем её } startNextDownload(); }
/** * @brief Called by the current network reply * Starts the next download if there is one */ void DownloadManager::downloadFinished() { qDebug() << "Entered"; m_downloading = false; m_retries = 0; QByteArray data; if (m_currentReply->error() != QNetworkReply::NoError) { qWarning() << "Network Error" << m_currentReply->errorString(); } else { data = m_currentReply->readAll(); } m_currentDownloadElement.data = data; m_currentReply->deleteLater(); if (m_currentDownloadElement.imageType == TypeActor && !m_currentDownloadElement.movie) m_currentDownloadElement.actor->image = data; else if (m_currentDownloadElement.imageType == TypeShowThumbnail) m_currentDownloadElement.episode->setThumbnailImage(data); else emit downloadFinished(m_currentDownloadElement); startNextDownload(); }
void DownloadManager::startDownloadImpl( QString const & url, QString const & path ) { bool fileExist = false; bool tempExist = false; QString fileName = ""; QString filePath = saveFilename( path == "" ? url : path, fileExist, fileName, tempExist, path == "" ); if ( fileExist && m_existDownloadPolicy == CancelDownloadFile) { emit status( url, "Cancel", "File already exist", filePath ); return; } Downloads item; item.m_url = url; item.m_key = fileName; item.m_destFile = filePath; item.m_tempFile = filePath + ".part"; item.m_tempExists = tempExist; m_downloadQueue.enqueue(item); startNextDownload(); }
void MangaDownloadWidget::downloadChapters(void) { if (_selectLineEdit->text().isEmpty()) { _messageModel->editMessageWarning("Warning: no manga name specified."); _messageModel->editMessageError("Abort."); return; } _selectLineEdit->setEnabled(false); _chaptersOnWebView->setEnabled(false); _selectAllButton->setEnabled(false); _downloadButton->setEnabled(false); _updateButton->setEnabled(false); _stopButton->setEnabled(true); _chaptersProgressBar->show(); _chaptersProgressBar->setValue(0); _chaptersDownloadedLabel->show(); _chaptersDownloadedLabel->setText("Chapter"); _pagesProgressBar->show(); _pagesProgressBar->setValue(0); _pagesDownloadedLabel->show(); _pagesDownloadedLabel->setText("Page"); _messageModel->editMessageInformation("Gathering information on chapters to downlaod..."); QModelIndexList chaptersSelectedIndexes = _chaptersOnWebView->selectionModel()->selectedIndexes(); for (const QModelIndex& chapterIndex: chaptersSelectedIndexes) { _chaptersQueue.enqueue(chapterIndex); QString currChapterUrl = _chaptersOnWebModel->data(chapterIndex, Qt::UserRole).toString(); QString currChapterTitle = _chaptersOnWebModel->data(chapterIndex, Qt::UserRole+1).toString(); _downloadQueue.enqueue(QPair<QString, QString>(currChapterTitle, currChapterUrl)); } _totalCount = _downloadQueue.size(); _downloadedCount = 0; emit downloading(true); startNextDownload(); }
/** * @brief Called by the current network reply * Starts the next download if there is one */ void DownloadManager::downloadFinished() { qDebug() << "Entered"; QNetworkReply *reply = static_cast<QNetworkReply*>(QObject::sender()); m_downloading = false; m_retries = 0; QByteArray data; if (m_currentReply->error() != QNetworkReply::NoError) { qWarning() << "Network Error" << m_currentReply->errorString(); } else { data = m_currentReply->readAll(); } m_currentDownloadElement.data = data; reply->deleteLater(); if (m_currentDownloadElement.imageType == ImageType::Actor && !m_currentDownloadElement.movie) m_currentDownloadElement.actor->image = data; else if (m_currentDownloadElement.imageType == ImageType::TvShowEpisodeThumb && !m_currentDownloadElement.directDownload) m_currentDownloadElement.episode->setThumbnailImage(data); else emit downloadFinished(m_currentDownloadElement); startNextDownload(); }
void plugDownloader::startDownload() { startNextDownload(); }
//! [0] void DownloadManager::startNextDownload() { // If the queue is empty just add a new status message if (m_downloadQueue.isEmpty()) { addStatusMessage(QString("%1/%2 files downloaded successfully").arg(m_downloadedCount).arg(m_totalCount)); return; } // Otherwise dequeue the first job from the queue ... const QUrl url = m_downloadQueue.dequeue(); // ... and determine a local file name where the result can be stored. const QString filename = saveFileName(url); m_savePath = filename; // Open the file with this file name for writing m_output.setFileName(filename); if (!m_output.open(QIODevice::WriteOnly)) { addErrorMessage(QString("Problem opening save file '%1' for download '%2': %3").arg(filename, url.toString(), m_output.errorString())); startNextDownload(); return; // skip this download } // Now create the network request for the URL ... QNetworkRequest request(url); // ... and start the download. m_currentDownload = m_manager.get(request); // Connect against the necessary signals to get informed about progress and status changes connect(m_currentDownload, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64))); connect(m_currentDownload, SIGNAL(finished()), SLOT(downloadFinished())); connect(m_currentDownload, SIGNAL(readyRead()), SLOT(downloadReadyRead())); // Add a status message addStatusMessage(QString("Downloading %1...").arg(url.toString())); // Start the timer so that we can calculate the download speed later on m_downloadTime.start(); // Not allowed yet I guess. Leave it here until it is allowed // QStringList callback; // QMimeDatabase db; // callback << "org.webcat.browser" << "/" << "org.webcat.browser"; // QDBusPendingReply<int> reply = m_transferClient->createDownload(QFileInfo(filename).fileName(), // QString("image://theme/icon-launcher-browser"), // QString("image://theme/icon-launcher-browser"), // filename.toString(), // db.mimeTypeForUrl(url).toString(), // progressTotal().toULongLong(), // callback, // QString("cancelTransfer"), // QString("restartTransfer")); // reply.waitForFinished(); // if (reply.isError()) { // qWarning() << "DownloadManager::recvObserve: failed to get transfer ID!" << reply.error(); // return; // } // int transferId(reply.value()); // m_transferClient->startTransfer(transferId); }