comparison data/fileio/FileSource.cpp @ 520:e340b2fb9471

* Make FileSource able to indicate its preferred content type (e.g. application/rdf+xml in preference to text/html for rdf content) * Temp directory on Windows -- make $HOME expand to homedrive+homepath instead of user profile location (which may be networked)
author Chris Cannam
date Thu, 11 Dec 2008 15:18:10 +0000
parents 3376dc26dece
children 3f68eab92857
comparison
equal deleted inserted replaced
519:21f86744d38e 520:e340b2fb9471
28 #include <QHttpResponseHeader> 28 #include <QHttpResponseHeader>
29 29
30 #include <iostream> 30 #include <iostream>
31 #include <cstdlib> 31 #include <cstdlib>
32 32
33 //#define DEBUG_FILE_SOURCE 1 33 #define DEBUG_FILE_SOURCE 1
34 34
35 int 35 int
36 FileSource::m_count = 0; 36 FileSource::m_count = 0;
37 37
38 QMutex 38 QMutex
45 FileSource::m_remoteLocalMap; 45 FileSource::m_remoteLocalMap;
46 46
47 QMutex 47 QMutex
48 FileSource::m_mapMutex; 48 FileSource::m_mapMutex;
49 49
50 FileSource::FileSource(QString fileOrUrl, ProgressReporter *reporter) : 50 FileSource::FileSource(QString fileOrUrl, ProgressReporter *reporter,
51 QString preferredContentType) :
51 m_url(fileOrUrl), 52 m_url(fileOrUrl),
52 m_ftp(0), 53 m_ftp(0),
53 m_http(0), 54 m_http(0),
54 m_localFile(0), 55 m_localFile(0),
56 m_preferredContentType(preferredContentType),
55 m_ok(false), 57 m_ok(false),
56 m_lastStatus(0), 58 m_lastStatus(0),
57 m_remote(isRemote(fileOrUrl)), 59 m_remote(isRemote(fileOrUrl)),
58 m_done(false), 60 m_done(false),
59 m_leaveLocalFile(false), 61 m_leaveLocalFile(false),
274 << m_url.toString().toStdString() << "\", retrieving" << std::endl; 276 << m_url.toString().toStdString() << "\", retrieving" << std::endl;
275 #endif 277 #endif
276 278
277 if (scheme == "http") { 279 if (scheme == "http") {
278 initHttp(); 280 initHttp();
281 #ifdef DEBUG_FILE_SOURCE
279 std::cerr << "FileSource: initHttp succeeded" << std::endl; 282 std::cerr << "FileSource: initHttp succeeded" << std::endl;
283 #endif
280 } else if (scheme == "ftp") { 284 } else if (scheme == "ftp") {
281 initFtp(); 285 initFtp();
282 } else { 286 } else {
283 m_remote = false; 287 m_remote = false;
284 m_ok = false; 288 m_ok = false;
379 #ifdef DEBUG_FILE_SOURCE 383 #ifdef DEBUG_FILE_SOURCE
380 std::cerr << "FileSource: path is \"" 384 std::cerr << "FileSource: path is \""
381 << path.toStdString() << "\"" << std::endl; 385 << path.toStdString() << "\"" << std::endl;
382 #endif 386 #endif
383 387
384 m_http->get(path, m_localFile); 388 if (m_preferredContentType == "") {
389 m_http->get(path, m_localFile);
390 } else {
391 #ifdef DEBUG_FILE_SOURCE
392 std::cerr << "FileSource: indicating preferred content type of \""
393 << m_preferredContentType.toStdString() << "\"" << std::endl;
394 #endif
395 QHttpRequestHeader header("GET", path);
396 header.setValue("Host", m_url.host());
397 header.setValue("Accept", QString("%1, */*").arg(m_preferredContentType));
398 m_http->request(header, 0, m_localFile);
399 }
385 } 400 }
386 401
387 void 402 void
388 FileSource::initFtp() 403 FileSource::initFtp()
389 { 404 {
558 } 573 }
559 574
560 void 575 void
561 FileSource::httpResponseHeaderReceived(const QHttpResponseHeader &resp) 576 FileSource::httpResponseHeaderReceived(const QHttpResponseHeader &resp)
562 { 577 {
578 #ifdef DEBUG_FILE_SOURCE
563 std::cerr << "FileSource::httpResponseHeaderReceived" << std::endl; 579 std::cerr << "FileSource::httpResponseHeaderReceived" << std::endl;
580 #endif
564 581
565 if (resp.statusCode() / 100 == 3) { 582 if (resp.statusCode() / 100 == 3) {
566 QString location = resp.value("Location"); 583 QString location = resp.value("Location");
567 #ifdef DEBUG_FILE_SOURCE 584 #ifdef DEBUG_FILE_SOURCE
568 std::cerr << "FileSource::responseHeaderReceived: redirect to \"" 585 std::cerr << "FileSource::responseHeaderReceived: redirect to \""
693 #endif 710 #endif
694 deleteCacheFile(); 711 deleteCacheFile();
695 } 712 }
696 713
697 m_ok = !error; 714 m_ok = !error;
715 if (m_localFile) m_localFile->flush();
698 m_done = true; 716 m_done = true;
699 emit ready(); 717 emit ready();
700 } 718 }
701 719
702 void 720 void