Downloader::Downloader(const DownloaderHints& hints) { DLLOG("Construct Downloader %p", this); _impl.reset(new DownloaderImpl(hints)); _impl->onTaskProgress = [this](const DownloadTask& task, int64_t bytesReceived, int64_t totalBytesReceived, int64_t totalBytesExpected, std::function<int64_t(void *buffer, int64_t len)>& transferDataToBuffer) { if (onTaskProgress) { onTaskProgress(task, bytesReceived, totalBytesReceived, totalBytesExpected); } }; _impl->onTaskFinish = [this](const DownloadTask& task, int errorCode, int errorCodeInternal, const std::string& errorStr, std::vector<unsigned char>& data) { if (DownloadTask::ERROR_NO_ERROR != errorCode) { if (onTaskError) { onTaskError(task, errorCode, errorCodeInternal, errorStr); } return; } // success callback if (task.storagePath.length()) { if (onFileTaskSuccess) { onFileTaskSuccess(task); } } else { // data task if (onDataTaskSuccess) { onDataTaskSuccess(task, data); } } }; }
void Tasks::exec(Task* t, bool foreground) { // setup signals connect(t, SIGNAL(onStart()), this, SLOT(onTaskStart()), Qt::AutoConnection); connect(t, SIGNAL(onProgress(TaskStatus)), this, SLOT(onTaskProgress(TaskStatus)), Qt::AutoConnection); connect(t, SIGNAL(onDone()), this, SLOT(onTaskDone()), Qt::AutoConnection); connect(t, SIGNAL(onError(QString)), this, SLOT(onTaskError(QString)), Qt::AutoConnection); // run (will trigger start, progress, done) t->run(); // done //TaskStatus ts("", 1); //emit onTaskProgress(ts); // the task will be deleted, after he sent the onDone() signal // AND this signal has been processed by the event loop. // see "Tasks::onTaskDone()" below. }