Exemplo n.º 1
0
    std::shared_ptr<const DownloadTask> Downloader::createDownloadFileTask(const std::string& srcUrl,
                                                                           const std::string& storagePath,
                                                                           const std::string& identifier/* = ""*/)
    {
        DownloadTask *task_ = new (std::nothrow) DownloadTask();
        std::shared_ptr<const DownloadTask> task(task_);
        do
        {
            task_->requestURL    = srcUrl;
            task_->storagePath   = storagePath;
            task_->identifier    = identifier;
            if (0 == srcUrl.length() || 0 == storagePath.length())
            {
                if (onTaskError)
                {
                    onTaskError(*task, DownloadTask::ERROR_INVALID_PARAMS, 0, "URL or storage path is empty.");
                }
                task.reset();
                break;
            }
            task_->_coTask.reset(_impl->createCoTask(task));
        } while (0);

        return task;
    }
Exemplo n.º 2
0
    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);
                }
            }
        };
    }
Exemplo n.º 3
0
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.

}