Example #1
0
void ResourceLoader::downloaded(bool success, const QUrl& url, const QString& filepath)
{
    Guard<SpinLock> guard(&_lock);

    const ResourceKey rk(QTextDocument::ImageResource, url);
    map_t::iterator i = _resources.find(rk);
    assert(i != _resources.end());
    Resource& rs = i->second;

    const QString suffix = QFileInfo(url.path()).suffix();
    const bool isgif = (suffix.toLower() == "gif");

    rs.error = !success;
    if (success)
    {
        if (isgif)
        {
            rs.res = QVariant(filepath);
        }
        else
        {
            QImage img = QImageReader(filepath).read();
            rs.res = QVariant(img);
            if (rs.res.isNull() || !rs.res.isValid())
            {
                rs.error = true;
                QFile::remove(filepath);
            }
        }
    }

    if (!rs.error)
    {
        for (size_t i = 0, size = rs.requestList.size(); i < size; ++i)
        {
            IResourceHolder *edit = rs.requestList.at(i);
            assert(NULL != edit);
            _add_resource_to_edit(edit, rk.type, url, rs.res);
        }
    }
    rs.requestList.clear();

    if (NULL != rs.downloader)
        rs.downloader->deleteLater();
    rs.downloader = NULL;

    // 启动新的下载线程
    if (!_download_queue.empty())
    {
        HttpGet *t = _download_queue.front();
        _download_queue.pop();
        assert(NULL != t);
        t->start_download();
    }
    else
    {
        --_downloading;
    }
}