comparison data/fileio/FileSource.cpp @ 690:1424aa29ae95

Seems to be a bad idea to use plain DEBUG symbol on OS/X (system wants it)
author Chris Cannam
date Tue, 14 Jun 2011 15:26:52 +0100
parents 06f13a3b9e9e
children 579b2da21e7a
comparison
equal deleted inserted replaced
689:573d45e9487b 690:1424aa29ae95
86 if (m_url.toString() == "") { 86 if (m_url.toString() == "") {
87 m_url = QUrl(fileOrUrl, QUrl::TolerantMode); 87 m_url = QUrl(fileOrUrl, QUrl::TolerantMode);
88 } 88 }
89 89
90 #ifdef DEBUG_FILE_SOURCE 90 #ifdef DEBUG_FILE_SOURCE
91 DEBUG << "FileSource::FileSource(" << fileOrUrl << "): url <" << m_url.toString() << ">" << endl; 91 SVDEBUG << "FileSource::FileSource(" << fileOrUrl << "): url <" << m_url.toString() << ">" << endl;
92 incCount(m_url.toString()); 92 incCount(m_url.toString());
93 #endif 93 #endif
94 94
95 if (!canHandleScheme(m_url)) { 95 if (!canHandleScheme(m_url)) {
96 DEBUG << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl; 96 SVDEBUG << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl;
97 m_errorString = tr("Unsupported scheme in URL"); 97 m_errorString = tr("Unsupported scheme in URL");
98 return; 98 return;
99 } 99 }
100 100
101 init(); 101 init();
146 emit statusAvailable(); 146 emit statusAvailable();
147 emit ready(); 147 emit ready();
148 } 148 }
149 149
150 #ifdef DEBUG_FILE_SOURCE 150 #ifdef DEBUG_FILE_SOURCE
151 DEBUG << "FileSource::FileSource(string) exiting" << endl; 151 SVDEBUG << "FileSource::FileSource(string) exiting" << endl;
152 #endif 152 #endif
153 } 153 }
154 154
155 FileSource::FileSource(QUrl url, ProgressReporter *reporter) : 155 FileSource::FileSource(QUrl url, ProgressReporter *reporter) :
156 m_url(url), 156 m_url(url),
164 m_leaveLocalFile(false), 164 m_leaveLocalFile(false),
165 m_reporter(reporter), 165 m_reporter(reporter),
166 m_refCounted(false) 166 m_refCounted(false)
167 { 167 {
168 #ifdef DEBUG_FILE_SOURCE 168 #ifdef DEBUG_FILE_SOURCE
169 DEBUG << "FileSource::FileSource(" << url.toString() << ") [as url]" << endl; 169 SVDEBUG << "FileSource::FileSource(" << url.toString() << ") [as url]" << endl;
170 incCount(m_url.toString()); 170 incCount(m_url.toString());
171 #endif 171 #endif
172 172
173 if (!canHandleScheme(m_url)) { 173 if (!canHandleScheme(m_url)) {
174 DEBUG << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl; 174 SVDEBUG << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl;
175 m_errorString = tr("Unsupported scheme in URL"); 175 m_errorString = tr("Unsupported scheme in URL");
176 return; 176 return;
177 } 177 }
178 178
179 init(); 179 init();
180 180
181 #ifdef DEBUG_FILE_SOURCE 181 #ifdef DEBUG_FILE_SOURCE
182 DEBUG << "FileSource::FileSource(url) exiting" << endl; 182 SVDEBUG << "FileSource::FileSource(url) exiting" << endl;
183 #endif 183 #endif
184 } 184 }
185 185
186 FileSource::FileSource(const FileSource &rf) : 186 FileSource::FileSource(const FileSource &rf) :
187 QObject(), 187 QObject(),
196 m_leaveLocalFile(false), 196 m_leaveLocalFile(false),
197 m_reporter(rf.m_reporter), 197 m_reporter(rf.m_reporter),
198 m_refCounted(false) 198 m_refCounted(false)
199 { 199 {
200 #ifdef DEBUG_FILE_SOURCE 200 #ifdef DEBUG_FILE_SOURCE
201 DEBUG << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]" << endl; 201 SVDEBUG << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]" << endl;
202 incCount(m_url.toString()); 202 incCount(m_url.toString());
203 #endif 203 #endif
204 204
205 if (!canHandleScheme(m_url)) { 205 if (!canHandleScheme(m_url)) {
206 DEBUG << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl; 206 SVDEBUG << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl;
207 m_errorString = tr("Unsupported scheme in URL"); 207 m_errorString = tr("Unsupported scheme in URL");
208 return; 208 return;
209 } 209 }
210 210
211 if (!isRemote()) { 211 if (!isRemote()) {
212 m_localFilename = rf.m_localFilename; 212 m_localFilename = rf.m_localFilename;
213 } else { 213 } else {
214 QMutexLocker locker(&m_mapMutex); 214 QMutexLocker locker(&m_mapMutex);
215 #ifdef DEBUG_FILE_SOURCE 215 #ifdef DEBUG_FILE_SOURCE
216 DEBUG << "FileSource::FileSource(copy ctor): ref count is " 216 SVDEBUG << "FileSource::FileSource(copy ctor): ref count is "
217 << m_refCountMap[m_url] << endl; 217 << m_refCountMap[m_url] << endl;
218 #endif 218 #endif
219 if (m_refCountMap[m_url] > 0) { 219 if (m_refCountMap[m_url] > 0) {
220 m_refCountMap[m_url]++; 220 m_refCountMap[m_url]++;
221 #ifdef DEBUG_FILE_SOURCE 221 #ifdef DEBUG_FILE_SOURCE
230 } 230 }
231 231
232 m_done = true; 232 m_done = true;
233 233
234 #ifdef DEBUG_FILE_SOURCE 234 #ifdef DEBUG_FILE_SOURCE
235 DEBUG << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]: note: local filename is \"" << m_localFilename << "\"" << endl; 235 SVDEBUG << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]: note: local filename is \"" << m_localFilename << "\"" << endl;
236 #endif 236 #endif
237 237
238 #ifdef DEBUG_FILE_SOURCE 238 #ifdef DEBUG_FILE_SOURCE
239 DEBUG << "FileSource::FileSource(copy ctor) exiting" << endl; 239 SVDEBUG << "FileSource::FileSource(copy ctor) exiting" << endl;
240 #endif 240 #endif
241 } 241 }
242 242
243 FileSource::~FileSource() 243 FileSource::~FileSource()
244 { 244 {
255 void 255 void
256 FileSource::init() 256 FileSource::init()
257 { 257 {
258 if (!isRemote()) { 258 if (!isRemote()) {
259 #ifdef DEBUG_FILE_SOURCE 259 #ifdef DEBUG_FILE_SOURCE
260 DEBUG << "FileSource::init: Not a remote URL" << endl; 260 SVDEBUG << "FileSource::init: Not a remote URL" << endl;
261 #endif 261 #endif
262 bool literal = false; 262 bool literal = false;
263 m_localFilename = m_url.toLocalFile(); 263 m_localFilename = m_url.toLocalFile();
264 if (m_localFilename == "") { 264 if (m_localFilename == "") {
265 // QUrl may have mishandled the scheme (e.g. in a DOS path) 265 // QUrl may have mishandled the scheme (e.g. in a DOS path)
267 literal = true; 267 literal = true;
268 } 268 }
269 m_localFilename = QFileInfo(m_localFilename).absoluteFilePath(); 269 m_localFilename = QFileInfo(m_localFilename).absoluteFilePath();
270 270
271 #ifdef DEBUG_FILE_SOURCE 271 #ifdef DEBUG_FILE_SOURCE
272 DEBUG << "FileSource::init: URL translates to local filename \"" 272 SVDEBUG << "FileSource::init: URL translates to local filename \""
273 << m_localFilename << "\" (with literal=" << literal << ")" << endl; 273 << m_localFilename << "\" (with literal=" << literal << ")" << endl;
274 #endif 274 #endif
275 m_ok = true; 275 m_ok = true;
276 m_lastStatus = 200; 276 m_lastStatus = 200;
277 277
278 if (!QFileInfo(m_localFilename).exists()) { 278 if (!QFileInfo(m_localFilename).exists()) {
279 if (literal) { 279 if (literal) {
280 m_lastStatus = 404; 280 m_lastStatus = 404;
281 } else { 281 } else {
282 #ifdef DEBUG_FILE_SOURCE 282 #ifdef DEBUG_FILE_SOURCE
283 DEBUG << "FileSource::init: Local file of this name does not exist, trying URL as a literal filename" << endl; 283 SVDEBUG << "FileSource::init: Local file of this name does not exist, trying URL as a literal filename" << endl;
284 #endif 284 #endif
285 // Again, QUrl may have been mistreating us -- 285 // Again, QUrl may have been mistreating us --
286 // e.g. dropping a part that looks like query data 286 // e.g. dropping a part that looks like query data
287 m_localFilename = m_url.toString(); 287 m_localFilename = m_url.toString();
288 literal = true; 288 literal = true;
296 return; 296 return;
297 } 297 }
298 298
299 if (createCacheFile()) { 299 if (createCacheFile()) {
300 #ifdef DEBUG_FILE_SOURCE 300 #ifdef DEBUG_FILE_SOURCE
301 DEBUG << "FileSource::init: Already have this one" << endl; 301 SVDEBUG << "FileSource::init: Already have this one" << endl;
302 #endif 302 #endif
303 m_ok = true; 303 m_ok = true;
304 if (!QFileInfo(m_localFilename).exists()) { 304 if (!QFileInfo(m_localFilename).exists()) {
305 m_lastStatus = 404; 305 m_lastStatus = 404;
306 } else { 306 } else {
315 m_localFile->open(QFile::WriteOnly); 315 m_localFile->open(QFile::WriteOnly);
316 316
317 QString scheme = m_url.scheme().toLower(); 317 QString scheme = m_url.scheme().toLower();
318 318
319 #ifdef DEBUG_FILE_SOURCE 319 #ifdef DEBUG_FILE_SOURCE
320 DEBUG << "FileSource::init: Don't have local copy of \"" 320 SVDEBUG << "FileSource::init: Don't have local copy of \""
321 << m_url.toString() << "\", retrieving" << endl; 321 << m_url.toString() << "\", retrieving" << endl;
322 #endif 322 #endif
323 323
324 if (scheme == "http") { 324 if (scheme == "http") {
325 initHttp(); 325 initHttp();
341 // someone else has been doing the same thing at the same time, 341 // someone else has been doing the same thing at the same time,
342 // but has got there first 342 // but has got there first
343 cleanup(); 343 cleanup();
344 m_refCountMap[m_url]++; 344 m_refCountMap[m_url]++;
345 #ifdef DEBUG_FILE_SOURCE 345 #ifdef DEBUG_FILE_SOURCE
346 DEBUG << "FileSource::init: Another FileSource has got there first, abandoning our download and using theirs" << endl; 346 SVDEBUG << "FileSource::init: Another FileSource has got there first, abandoning our download and using theirs" << endl;
347 #endif 347 #endif
348 m_localFilename = m_remoteLocalMap[m_url]; 348 m_localFilename = m_remoteLocalMap[m_url];
349 m_refCounted = true; 349 m_refCounted = true;
350 m_ok = true; 350 m_ok = true;
351 if (!QFileInfo(m_localFilename).exists()) { 351 if (!QFileInfo(m_localFilename).exists()) {
424 // appropriate... 424 // appropriate...
425 425
426 QString path = "/" + QString(m_url.toEncoded()).section('/', 3); 426 QString path = "/" + QString(m_url.toEncoded()).section('/', 3);
427 427
428 #ifdef DEBUG_FILE_SOURCE 428 #ifdef DEBUG_FILE_SOURCE
429 DEBUG << "FileSource: path is \"" 429 SVDEBUG << "FileSource: path is \""
430 << path << "\"" << endl; 430 << path << "\"" << endl;
431 #endif 431 #endif
432 432
433 if (m_preferredContentType == "") { 433 if (m_preferredContentType == "") {
434 m_http->get(path, m_localFile); 434 m_http->get(path, m_localFile);
526 waitForStatus(); 526 waitForStatus();
527 bool available = true; 527 bool available = true;
528 if (!m_ok) available = false; 528 if (!m_ok) available = false;
529 else available = (m_lastStatus / 100 == 2); 529 else available = (m_lastStatus / 100 == 2);
530 #ifdef DEBUG_FILE_SOURCE 530 #ifdef DEBUG_FILE_SOURCE
531 DEBUG << "FileSource::isAvailable: " << (available ? "yes" : "no") 531 SVDEBUG << "FileSource::isAvailable: " << (available ? "yes" : "no")
532 << endl; 532 << endl;
533 #endif 533 #endif
534 return available; 534 return available;
535 } 535 }
536 536
545 545
546 void 546 void
547 FileSource::waitForData() 547 FileSource::waitForData()
548 { 548 {
549 while (m_ok && !m_done) { 549 while (m_ok && !m_done) {
550 // DEBUG << "FileSource::waitForData: calling QApplication::processEvents" << endl; 550 // SVDEBUG << "FileSource::waitForData: calling QApplication::processEvents" << endl;
551 QCoreApplication::processEvents(); 551 QCoreApplication::processEvents();
552 usleep(10000); 552 usleep(10000);
553 } 553 }
554 } 554 }
555 555
625 625
626 void 626 void
627 FileSource::httpResponseHeaderReceived(const QHttpResponseHeader &resp) 627 FileSource::httpResponseHeaderReceived(const QHttpResponseHeader &resp)
628 { 628 {
629 #ifdef DEBUG_FILE_SOURCE 629 #ifdef DEBUG_FILE_SOURCE
630 DEBUG << "FileSource::httpResponseHeaderReceived" << endl; 630 SVDEBUG << "FileSource::httpResponseHeaderReceived" << endl;
631 #endif 631 #endif
632 632
633 if (resp.statusCode() / 100 == 3) { 633 if (resp.statusCode() / 100 == 3) {
634 QString location = resp.value("Location"); 634 QString location = resp.value("Location");
635 #ifdef DEBUG_FILE_SOURCE 635 #ifdef DEBUG_FILE_SOURCE
636 DEBUG << "FileSource::responseHeaderReceived: redirect to \"" 636 SVDEBUG << "FileSource::responseHeaderReceived: redirect to \""
637 << location << "\" received" << endl; 637 << location << "\" received" << endl;
638 #endif 638 #endif
639 if (location != "") { 639 if (location != "") {
640 QUrl newUrl(location); 640 QUrl newUrl(location);
641 if (newUrl != m_url) { 641 if (newUrl != m_url) {
659 m_lastStatus = resp.statusCode(); 659 m_lastStatus = resp.statusCode();
660 if (m_lastStatus / 100 >= 4) { 660 if (m_lastStatus / 100 >= 4) {
661 m_errorString = QString("%1 %2") 661 m_errorString = QString("%1 %2")
662 .arg(resp.statusCode()).arg(resp.reasonPhrase()); 662 .arg(resp.statusCode()).arg(resp.reasonPhrase());
663 #ifdef DEBUG_FILE_SOURCE 663 #ifdef DEBUG_FILE_SOURCE
664 DEBUG << "FileSource::responseHeaderReceived: " 664 SVDEBUG << "FileSource::responseHeaderReceived: "
665 << m_errorString << endl; 665 << m_errorString << endl;
666 #endif 666 #endif
667 } else { 667 } else {
668 #ifdef DEBUG_FILE_SOURCE 668 #ifdef DEBUG_FILE_SOURCE
669 DEBUG << "FileSource::responseHeaderReceived: " 669 SVDEBUG << "FileSource::responseHeaderReceived: "
670 << m_lastStatus << endl; 670 << m_lastStatus << endl;
671 #endif 671 #endif
672 if (resp.hasContentType()) m_contentType = resp.contentType(); 672 if (resp.hasContentType()) m_contentType = resp.contentType();
673 } 673 }
674 emit statusAvailable(); 674 emit statusAvailable();
685 685
686 QFtp::Command command = m_ftp->currentCommand(); 686 QFtp::Command command = m_ftp->currentCommand();
687 687
688 if (!error) { 688 if (!error) {
689 #ifdef DEBUG_FILE_SOURCE 689 #ifdef DEBUG_FILE_SOURCE
690 DEBUG << "FileSource::ftpCommandFinished: success for command " 690 SVDEBUG << "FileSource::ftpCommandFinished: success for command "
691 << command << endl; 691 << command << endl;
692 #endif 692 #endif
693 return; 693 return;
694 } 694 }
695 695
774 774
775 void 775 void
776 FileSource::deleteCacheFile() 776 FileSource::deleteCacheFile()
777 { 777 {
778 #ifdef DEBUG_FILE_SOURCE 778 #ifdef DEBUG_FILE_SOURCE
779 DEBUG << "FileSource::deleteCacheFile(\"" << m_localFilename << "\")" << endl; 779 SVDEBUG << "FileSource::deleteCacheFile(\"" << m_localFilename << "\")" << endl;
780 #endif 780 #endif
781 781
782 cleanup(); 782 cleanup();
783 783
784 if (m_localFilename == "") { 784 if (m_localFilename == "") {
815 #ifdef DEBUG_FILE_SOURCE 815 #ifdef DEBUG_FILE_SOURCE
816 std::cerr << "FileSource::deleteCacheFile: ERROR: Failed to delete file \"" << m_localFilename << "\"" << std::endl; 816 std::cerr << "FileSource::deleteCacheFile: ERROR: Failed to delete file \"" << m_localFilename << "\"" << std::endl;
817 #endif 817 #endif
818 } else { 818 } else {
819 #ifdef DEBUG_FILE_SOURCE 819 #ifdef DEBUG_FILE_SOURCE
820 DEBUG << "FileSource::deleteCacheFile: Deleted cache file \"" << m_localFilename << "\"" << endl; 820 SVDEBUG << "FileSource::deleteCacheFile: Deleted cache file \"" << m_localFilename << "\"" << endl;
821 #endif 821 #endif
822 m_localFilename = ""; 822 m_localFilename = "";
823 } 823 }
824 824
825 m_fileCreationMutex.unlock(); 825 m_fileCreationMutex.unlock();
832 { 832 {
833 { 833 {
834 QMutexLocker locker(&m_mapMutex); 834 QMutexLocker locker(&m_mapMutex);
835 835
836 #ifdef DEBUG_FILE_SOURCE 836 #ifdef DEBUG_FILE_SOURCE
837 DEBUG << "FileSource::createCacheFile: refcount is " << m_refCountMap[m_url] << endl; 837 SVDEBUG << "FileSource::createCacheFile: refcount is " << m_refCountMap[m_url] << endl;
838 #endif 838 #endif
839 839
840 if (m_refCountMap[m_url] > 0) { 840 if (m_refCountMap[m_url] > 0) {
841 m_refCountMap[m_url]++; 841 m_refCountMap[m_url]++;
842 m_localFilename = m_remoteLocalMap[m_url]; 842 m_localFilename = m_remoteLocalMap[m_url];
879 } 879 }
880 880
881 QString filepath(dir.filePath(filename)); 881 QString filepath(dir.filePath(filename));
882 882
883 #ifdef DEBUG_FILE_SOURCE 883 #ifdef DEBUG_FILE_SOURCE
884 DEBUG << "FileSource::createCacheFile: URL is \"" << m_url.toString() << "\", dir is \"" << dir.path() << "\", base \"" << base << "\", extension \"" << extension << "\", filebase \"" << filename << "\", filename \"" << filepath << "\"" << endl; 884 SVDEBUG << "FileSource::createCacheFile: URL is \"" << m_url.toString() << "\", dir is \"" << dir.path() << "\", base \"" << base << "\", extension \"" << extension << "\", filebase \"" << filename << "\", filename \"" << filepath << "\"" << endl;
885 #endif 885 #endif
886 886
887 QMutexLocker fcLocker(&m_fileCreationMutex); 887 QMutexLocker fcLocker(&m_fileCreationMutex);
888 888
889 ++m_count; 889 ++m_count;
916 return ""; 916 return "";
917 } 917 }
918 } 918 }
919 919
920 #ifdef DEBUG_FILE_SOURCE 920 #ifdef DEBUG_FILE_SOURCE
921 DEBUG << "FileSource::createCacheFile: url " 921 SVDEBUG << "FileSource::createCacheFile: url "
922 << m_url.toString() << " -> local filename " 922 << m_url.toString() << " -> local filename "
923 << filepath << endl; 923 << filepath << endl;
924 #endif 924 #endif
925 925
926 m_localFilename = filepath; 926 m_localFilename = filepath;