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; } }
// Testing Git from Qt int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QStringList args = a.arguments(); if (args.count() != 2) { return 1; } HttpGet getter; if (!getter.getFile(QUrl(args[1]))) return 1; QObject::connect(&getter, SIGNAL(done()), &a, SLOT(quit())); return a.exec(); }
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QTextCodec::setCodecForTr( QTextCodec::codecForName("gb2312")); QStringList args = app.arguments(); if (args.count() !=2){ qDebug()<<QObject::tr("М├ие: wget url")<<endl <<QObject::tr("└§:")<<endl <<QObject::tr(" wget http://www.gnu.org/software/wget/manual/wget.txt"); return -1; } HttpGet httpClient; if (!httpClient.downloadFile(QUrl(args[1]))) return -1; QObject::connect(&httpClient,SIGNAL(done()),&app,SLOT(quit())); return app.exec(); }
int HttpClient::execute(HttpGet& request, IHttpClientListener *listener) { // リスナーが設定されていないときには通信をさせない if (!listener) { return HTTP_FAILED; } HttpRequestListenerImpl *impl = new HttpRequestListenerImpl(request, listener); if (impl) { // GETのリクエストなのでbodyは空にしておく std::string body; // 実際のリクエストを行う int result = GCHttpRequestAsync(request.getUrl(), request.getHeaders(), body, impl); if (!result) { // リクエストに失敗したので、解放する delete impl; } return result; } return HTTP_FAILED; }
void MainWindow::saveBtn_Clicked() { QDir dir(savePath); if (!dir.exists()) { dir.mkdir(savePath); } ui.saveBtn->setEnabled(false); int con = list.count(); for (int i = 0; i < con;i++) { QUrl url(list[i]); QFileInfo fileInfo(url.path()); QString fileName = fileInfo.fileName(); QString str = savePath +"\\"+ fileName; HttpGet* getter = new HttpGet(this); getter->downloadFile(url,str); } ui.saveBtn->setEnabled(true); }