Mercurial > hg > svcore
comparison data/fileio/FileSource.cpp @ 742:c10cb8782576 coreaudio_tests
Merge from branch "default"
author | Chris Cannam |
---|---|
date | Sun, 01 Jul 2012 11:53:00 +0100 |
parents | 579b2da21e7a |
children | 3b2409646cc0 |
comparison
equal
deleted
inserted
replaced
666:4efa7429cd85 | 742:c10cb8782576 |
---|---|
75 m_http(0), | 75 m_http(0), |
76 m_localFile(0), | 76 m_localFile(0), |
77 m_preferredContentType(preferredContentType), | 77 m_preferredContentType(preferredContentType), |
78 m_ok(false), | 78 m_ok(false), |
79 m_lastStatus(0), | 79 m_lastStatus(0), |
80 m_resource(fileOrUrl.startsWith(':')), | |
80 m_remote(isRemote(fileOrUrl)), | 81 m_remote(isRemote(fileOrUrl)), |
81 m_done(false), | 82 m_done(false), |
82 m_leaveLocalFile(false), | 83 m_leaveLocalFile(false), |
83 m_reporter(reporter), | 84 m_reporter(reporter), |
84 m_refCounted(false) | 85 m_refCounted(false) |
85 { | 86 { |
86 #ifdef DEBUG_FILE_SOURCE | 87 if (m_resource) { // qrc file |
87 std::cerr << "FileSource::FileSource(" << fileOrUrl.toStdString() << ")" << std::endl; | 88 m_url = QUrl("qrc" + fileOrUrl); |
89 } | |
90 | |
91 if (m_url.toString() == "") { | |
92 m_url = QUrl(fileOrUrl, QUrl::TolerantMode); | |
93 } | |
94 | |
95 #ifdef DEBUG_FILE_SOURCE | |
96 std::cerr << "FileSource::FileSource(" << fileOrUrl << "): url <" << m_url.toString() << ">" << std::endl; | |
88 incCount(m_url.toString()); | 97 incCount(m_url.toString()); |
89 #endif | 98 #endif |
90 | 99 |
91 if (!canHandleScheme(m_url)) { | 100 if (!canHandleScheme(m_url)) { |
92 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString().toStdString() << "\"" << std::endl; | 101 SVDEBUG << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl; |
93 m_errorString = tr("Unsupported scheme in URL"); | 102 m_errorString = tr("Unsupported scheme in URL"); |
94 return; | 103 return; |
95 } | 104 } |
96 | 105 |
97 init(); | 106 init(); |
98 | 107 |
99 if (!isRemote() && | 108 if (!isRemote() && |
100 !isAvailable()) { | 109 !isAvailable()) { |
101 #ifdef DEBUG_FILE_SOURCE | 110 #ifdef DEBUG_FILE_SOURCE |
102 std::cerr << "FileSource::FileSource: Failed to open local file with URL \"" << m_url.toString().toStdString() << "; trying again assuming filename was encoded" << std::endl; | 111 std::cerr << "FileSource::FileSource: Failed to open local file with URL \"" << m_url.toString() << "\"; trying again assuming filename was encoded" << std::endl; |
103 #endif | 112 #endif |
104 m_url = QUrl::fromEncoded(fileOrUrl.toAscii()); | 113 m_url = QUrl::fromEncoded(fileOrUrl.toAscii()); |
105 init(); | 114 init(); |
106 } | 115 } |
107 | 116 |
142 emit statusAvailable(); | 151 emit statusAvailable(); |
143 emit ready(); | 152 emit ready(); |
144 } | 153 } |
145 | 154 |
146 #ifdef DEBUG_FILE_SOURCE | 155 #ifdef DEBUG_FILE_SOURCE |
147 std::cerr << "FileSource::FileSource(string) exiting" << std::endl; | 156 SVDEBUG << "FileSource::FileSource(string) exiting" << endl; |
148 #endif | 157 #endif |
149 } | 158 } |
150 | 159 |
151 FileSource::FileSource(QUrl url, ProgressReporter *reporter) : | 160 FileSource::FileSource(QUrl url, ProgressReporter *reporter) : |
152 m_url(url), | 161 m_url(url), |
153 m_ftp(0), | 162 m_ftp(0), |
154 m_http(0), | 163 m_http(0), |
155 m_localFile(0), | 164 m_localFile(0), |
156 m_ok(false), | 165 m_ok(false), |
157 m_lastStatus(0), | 166 m_lastStatus(0), |
167 m_resource(false), | |
158 m_remote(isRemote(url.toString())), | 168 m_remote(isRemote(url.toString())), |
159 m_done(false), | 169 m_done(false), |
160 m_leaveLocalFile(false), | 170 m_leaveLocalFile(false), |
161 m_reporter(reporter), | 171 m_reporter(reporter), |
162 m_refCounted(false) | 172 m_refCounted(false) |
163 { | 173 { |
164 #ifdef DEBUG_FILE_SOURCE | 174 #ifdef DEBUG_FILE_SOURCE |
165 std::cerr << "FileSource::FileSource(" << url.toString().toStdString() << ") [as url]" << std::endl; | 175 SVDEBUG << "FileSource::FileSource(" << url.toString() << ") [as url]" << endl; |
166 incCount(m_url.toString()); | 176 incCount(m_url.toString()); |
167 #endif | 177 #endif |
168 | 178 |
169 if (!canHandleScheme(m_url)) { | 179 if (!canHandleScheme(m_url)) { |
170 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString().toStdString() << "\"" << std::endl; | 180 SVDEBUG << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl; |
171 m_errorString = tr("Unsupported scheme in URL"); | 181 m_errorString = tr("Unsupported scheme in URL"); |
172 return; | 182 return; |
173 } | 183 } |
174 | 184 |
175 init(); | 185 init(); |
176 | 186 |
177 #ifdef DEBUG_FILE_SOURCE | 187 #ifdef DEBUG_FILE_SOURCE |
178 std::cerr << "FileSource::FileSource(url) exiting" << std::endl; | 188 SVDEBUG << "FileSource::FileSource(url) exiting" << endl; |
179 #endif | 189 #endif |
180 } | 190 } |
181 | 191 |
182 FileSource::FileSource(const FileSource &rf) : | 192 FileSource::FileSource(const FileSource &rf) : |
183 QObject(), | 193 QObject(), |
185 m_ftp(0), | 195 m_ftp(0), |
186 m_http(0), | 196 m_http(0), |
187 m_localFile(0), | 197 m_localFile(0), |
188 m_ok(rf.m_ok), | 198 m_ok(rf.m_ok), |
189 m_lastStatus(rf.m_lastStatus), | 199 m_lastStatus(rf.m_lastStatus), |
200 m_resource(rf.m_resource), | |
190 m_remote(rf.m_remote), | 201 m_remote(rf.m_remote), |
191 m_done(false), | 202 m_done(false), |
192 m_leaveLocalFile(false), | 203 m_leaveLocalFile(false), |
193 m_reporter(rf.m_reporter), | 204 m_reporter(rf.m_reporter), |
194 m_refCounted(false) | 205 m_refCounted(false) |
195 { | 206 { |
196 #ifdef DEBUG_FILE_SOURCE | 207 #ifdef DEBUG_FILE_SOURCE |
197 std::cerr << "FileSource::FileSource(" << m_url.toString().toStdString() << ") [copy ctor]" << std::endl; | 208 SVDEBUG << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]" << endl; |
198 incCount(m_url.toString()); | 209 incCount(m_url.toString()); |
199 #endif | 210 #endif |
200 | 211 |
201 if (!canHandleScheme(m_url)) { | 212 if (!canHandleScheme(m_url)) { |
202 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString().toStdString() << "\"" << std::endl; | 213 SVDEBUG << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl; |
203 m_errorString = tr("Unsupported scheme in URL"); | 214 m_errorString = tr("Unsupported scheme in URL"); |
204 return; | 215 return; |
205 } | 216 } |
206 | 217 |
207 if (!isRemote()) { | 218 if (!isRemote()) { |
208 m_localFilename = rf.m_localFilename; | 219 m_localFilename = rf.m_localFilename; |
209 } else { | 220 } else { |
210 QMutexLocker locker(&m_mapMutex); | 221 QMutexLocker locker(&m_mapMutex); |
211 #ifdef DEBUG_FILE_SOURCE | 222 #ifdef DEBUG_FILE_SOURCE |
212 std::cerr << "FileSource::FileSource(copy ctor): ref count is " | 223 SVDEBUG << "FileSource::FileSource(copy ctor): ref count is " |
213 << m_refCountMap[m_url] << std::endl; | 224 << m_refCountMap[m_url] << endl; |
214 #endif | 225 #endif |
215 if (m_refCountMap[m_url] > 0) { | 226 if (m_refCountMap[m_url] > 0) { |
216 m_refCountMap[m_url]++; | 227 m_refCountMap[m_url]++; |
217 #ifdef DEBUG_FILE_SOURCE | 228 #ifdef DEBUG_FILE_SOURCE |
218 std::cerr << "raised it to " << m_refCountMap[m_url] << std::endl; | 229 std::cerr << "raised it to " << m_refCountMap[m_url] << std::endl; |
226 } | 237 } |
227 | 238 |
228 m_done = true; | 239 m_done = true; |
229 | 240 |
230 #ifdef DEBUG_FILE_SOURCE | 241 #ifdef DEBUG_FILE_SOURCE |
231 std::cerr << "FileSource::FileSource(" << m_url.toString().toStdString() << ") [copy ctor]: note: local filename is \"" << m_localFilename.toStdString() << "\"" << std::endl; | 242 SVDEBUG << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]: note: local filename is \"" << m_localFilename << "\"" << endl; |
232 #endif | 243 #endif |
233 | 244 |
234 #ifdef DEBUG_FILE_SOURCE | 245 #ifdef DEBUG_FILE_SOURCE |
235 std::cerr << "FileSource::FileSource(copy ctor) exiting" << std::endl; | 246 SVDEBUG << "FileSource::FileSource(copy ctor) exiting" << endl; |
236 #endif | 247 #endif |
237 } | 248 } |
238 | 249 |
239 FileSource::~FileSource() | 250 FileSource::~FileSource() |
240 { | 251 { |
241 #ifdef DEBUG_FILE_SOURCE | 252 #ifdef DEBUG_FILE_SOURCE |
242 std::cerr << "FileSource(" << m_url.toString().toStdString() << ")::~FileSource" << std::endl; | 253 std::cerr << "FileSource(" << m_url.toString() << ")::~FileSource" << std::endl; |
243 decCount(m_url.toString()); | 254 decCount(m_url.toString()); |
244 #endif | 255 #endif |
245 | 256 |
246 cleanup(); | 257 cleanup(); |
247 | 258 |
249 } | 260 } |
250 | 261 |
251 void | 262 void |
252 FileSource::init() | 263 FileSource::init() |
253 { | 264 { |
254 if (!isRemote()) { | 265 if (isResource()) { |
266 #ifdef DEBUG_FILE_SOURCE | |
267 std::cerr << "FileSource::init: Is a resource" << std::endl; | |
268 #endif | |
269 QString resourceFile = m_url.toString(); | |
270 resourceFile.replace(QRegExp("^qrc:"), ":"); | |
271 | |
272 if (!QFileInfo(resourceFile).exists()) { | |
273 #ifdef DEBUG_FILE_SOURCE | |
274 std::cerr << "FileSource::init: Resource file of this name does not exist, switching to non-resource URL" << std::endl; | |
275 #endif | |
276 m_url = resourceFile; | |
277 m_resource = false; | |
278 } | |
279 } | |
280 | |
281 if (!isRemote() && !isResource()) { | |
255 #ifdef DEBUG_FILE_SOURCE | 282 #ifdef DEBUG_FILE_SOURCE |
256 std::cerr << "FileSource::init: Not a remote URL" << std::endl; | 283 std::cerr << "FileSource::init: Not a remote URL" << std::endl; |
257 #endif | 284 #endif |
258 bool literal = false; | 285 bool literal = false; |
259 m_localFilename = m_url.toLocalFile(); | 286 m_localFilename = m_url.toLocalFile(); |
264 } | 291 } |
265 m_localFilename = QFileInfo(m_localFilename).absoluteFilePath(); | 292 m_localFilename = QFileInfo(m_localFilename).absoluteFilePath(); |
266 | 293 |
267 #ifdef DEBUG_FILE_SOURCE | 294 #ifdef DEBUG_FILE_SOURCE |
268 std::cerr << "FileSource::init: URL translates to local filename \"" | 295 std::cerr << "FileSource::init: URL translates to local filename \"" |
269 << m_localFilename.toStdString() << "\"" << std::endl; | 296 << m_localFilename << "\" (with literal=" << literal << ")" |
297 << std::endl; | |
270 #endif | 298 #endif |
271 m_ok = true; | 299 m_ok = true; |
272 m_lastStatus = 200; | 300 m_lastStatus = 200; |
273 | 301 |
274 if (!QFileInfo(m_localFilename).exists()) { | 302 if (!QFileInfo(m_localFilename).exists()) { |
305 m_done = true; | 333 m_done = true; |
306 return; | 334 return; |
307 } | 335 } |
308 | 336 |
309 if (m_localFilename == "") return; | 337 if (m_localFilename == "") return; |
338 | |
310 m_localFile = new QFile(m_localFilename); | 339 m_localFile = new QFile(m_localFilename); |
311 m_localFile->open(QFile::WriteOnly); | 340 m_localFile->open(QFile::WriteOnly); |
312 | 341 |
313 QString scheme = m_url.scheme().toLower(); | 342 if (isResource()) { |
314 | 343 |
315 #ifdef DEBUG_FILE_SOURCE | 344 // Absent resource file case was dealt with at the top -- this |
316 std::cerr << "FileSource::init: Don't have local copy of \"" | 345 // is the successful case |
317 << m_url.toString().toStdString() << "\", retrieving" << std::endl; | 346 |
318 #endif | 347 QString resourceFileName = m_url.toString(); |
319 | 348 resourceFileName.replace(QRegExp("^qrc:"), ":"); |
320 if (scheme == "http") { | 349 QFile resourceFile(resourceFileName); |
321 initHttp(); | 350 resourceFile.open(QFile::ReadOnly); |
322 #ifdef DEBUG_FILE_SOURCE | 351 QByteArray ba(resourceFile.readAll()); |
323 std::cerr << "FileSource: initHttp succeeded" << std::endl; | 352 |
324 #endif | 353 #ifdef DEBUG_FILE_SOURCE |
325 } else if (scheme == "ftp") { | 354 std::cerr << "Copying " << ba.size() << " bytes from resource file to cache file" << std::endl; |
326 initFtp(); | 355 #endif |
356 | |
357 qint64 written = m_localFile->write(ba); | |
358 m_localFile->close(); | |
359 delete m_localFile; | |
360 m_localFile = 0; | |
361 | |
362 if (written != ba.size()) { | |
363 #ifdef DEBUG_FILE_SOURCE | |
364 std::cerr << "Copy failed (wrote " << written << " bytes)" << std::endl; | |
365 #endif | |
366 m_ok = false; | |
367 return; | |
368 } else { | |
369 m_ok = true; | |
370 m_lastStatus = 200; | |
371 m_done = true; | |
372 } | |
373 | |
327 } else { | 374 } else { |
328 m_remote = false; | 375 |
329 m_ok = false; | 376 QString scheme = m_url.scheme().toLower(); |
377 | |
378 #ifdef DEBUG_FILE_SOURCE | |
379 std::cerr << "FileSource::init: Don't have local copy of \"" | |
380 << m_url.toString() << "\", retrieving" << std::endl; | |
381 #endif | |
382 | |
383 if (scheme == "http") { | |
384 initHttp(); | |
385 #ifdef DEBUG_FILE_SOURCE | |
386 std::cerr << "FileSource: initHttp succeeded" << std::endl; | |
387 #endif | |
388 } else if (scheme == "ftp") { | |
389 initFtp(); | |
390 } else { | |
391 m_remote = false; | |
392 m_ok = false; | |
393 } | |
330 } | 394 } |
331 | 395 |
332 if (m_ok) { | 396 if (m_ok) { |
333 | 397 |
334 QMutexLocker locker(&m_mapMutex); | 398 QMutexLocker locker(&m_mapMutex); |
353 | 417 |
354 m_remoteLocalMap[m_url] = m_localFilename; | 418 m_remoteLocalMap[m_url] = m_localFilename; |
355 m_refCountMap[m_url]++; | 419 m_refCountMap[m_url]++; |
356 m_refCounted = true; | 420 m_refCounted = true; |
357 | 421 |
358 if (m_reporter) { | 422 if (m_reporter && !m_done) { |
359 m_reporter->setMessage | 423 m_reporter->setMessage |
360 (tr("Downloading %1...").arg(m_url.toString())); | 424 (tr("Downloading %1...").arg(m_url.toString())); |
361 connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled())); | 425 connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled())); |
362 connect(this, SIGNAL(progress(int)), | 426 connect(this, SIGNAL(progress(int)), |
363 m_reporter, SLOT(setProgress(int))); | 427 m_reporter, SLOT(setProgress(int))); |
420 // appropriate... | 484 // appropriate... |
421 | 485 |
422 QString path = "/" + QString(m_url.toEncoded()).section('/', 3); | 486 QString path = "/" + QString(m_url.toEncoded()).section('/', 3); |
423 | 487 |
424 #ifdef DEBUG_FILE_SOURCE | 488 #ifdef DEBUG_FILE_SOURCE |
425 std::cerr << "FileSource: path is \"" | 489 SVDEBUG << "FileSource: path is \"" |
426 << path.toStdString() << "\"" << std::endl; | 490 << path << "\"" << endl; |
427 #endif | 491 #endif |
428 | 492 |
429 if (m_preferredContentType == "") { | 493 if (m_preferredContentType == "") { |
430 m_http->get(path, m_localFile); | 494 m_http->get(path, m_localFile); |
431 } else { | 495 } else { |
432 #ifdef DEBUG_FILE_SOURCE | 496 #ifdef DEBUG_FILE_SOURCE |
433 std::cerr << "FileSource: indicating preferred content type of \"" | 497 std::cerr << "FileSource: indicating preferred content type of \"" |
434 << m_preferredContentType.toStdString() << "\"" << std::endl; | 498 << m_preferredContentType << "\"" << std::endl; |
435 #endif | 499 #endif |
436 QHttpRequestHeader header("GET", path); | 500 QHttpRequestHeader header("GET", path); |
437 header.setValue("Host", m_url.host()); | 501 header.setValue("Host", m_url.host()); |
438 header.setValue("Accept", QString("%1, */*").arg(m_preferredContentType)); | 502 header.setValue("Accept", QString("%1, */*").arg(m_preferredContentType)); |
439 m_http->request(header, 0, m_localFile); | 503 m_http->request(header, 0, m_localFile); |
511 FileSource::canHandleScheme(QUrl url) | 575 FileSource::canHandleScheme(QUrl url) |
512 { | 576 { |
513 // Note that a "scheme" with length 1 is probably a DOS drive letter | 577 // Note that a "scheme" with length 1 is probably a DOS drive letter |
514 QString scheme = url.scheme().toLower(); | 578 QString scheme = url.scheme().toLower(); |
515 return (scheme == "http" || scheme == "ftp" || | 579 return (scheme == "http" || scheme == "ftp" || |
516 scheme == "file" || scheme == "" || scheme.length() == 1); | 580 scheme == "file" || scheme == "qrc" || |
581 scheme == "" || scheme.length() == 1); | |
517 } | 582 } |
518 | 583 |
519 bool | 584 bool |
520 FileSource::isAvailable() | 585 FileSource::isAvailable() |
521 { | 586 { |
522 waitForStatus(); | 587 waitForStatus(); |
523 bool available = true; | 588 bool available = true; |
524 if (!m_ok) available = false; | 589 if (!m_ok) available = false; |
525 else available = (m_lastStatus / 100 == 2); | 590 else available = (m_lastStatus / 100 == 2); |
526 #ifdef DEBUG_FILE_SOURCE | 591 #ifdef DEBUG_FILE_SOURCE |
527 std::cerr << "FileSource::isAvailable: " << (available ? "yes" : "no") | 592 SVDEBUG << "FileSource::isAvailable: " << (available ? "yes" : "no") |
528 << std::endl; | 593 << endl; |
529 #endif | 594 #endif |
530 return available; | 595 return available; |
531 } | 596 } |
532 | 597 |
533 void | 598 void |
541 | 606 |
542 void | 607 void |
543 FileSource::waitForData() | 608 FileSource::waitForData() |
544 { | 609 { |
545 while (m_ok && !m_done) { | 610 while (m_ok && !m_done) { |
546 // std::cerr << "FileSource::waitForData: calling QApplication::processEvents" << std::endl; | 611 // SVDEBUG << "FileSource::waitForData: calling QApplication::processEvents" << endl; |
547 QCoreApplication::processEvents(); | 612 QCoreApplication::processEvents(); |
548 usleep(10000); | 613 usleep(10000); |
549 } | 614 } |
550 } | 615 } |
551 | 616 |
566 { | 631 { |
567 return m_done; | 632 return m_done; |
568 } | 633 } |
569 | 634 |
570 bool | 635 bool |
636 FileSource::isResource() const | |
637 { | |
638 return m_resource; | |
639 } | |
640 | |
641 bool | |
571 FileSource::isRemote() const | 642 FileSource::isRemote() const |
572 { | 643 { |
573 return m_remote; | 644 return m_remote; |
574 } | 645 } |
575 | 646 |
581 | 652 |
582 QString | 653 QString |
583 FileSource::getLocalFilename() const | 654 FileSource::getLocalFilename() const |
584 { | 655 { |
585 return m_localFilename; | 656 return m_localFilename; |
657 } | |
658 | |
659 QString | |
660 FileSource::getBasename() const | |
661 { | |
662 return QFileInfo(m_localFilename).fileName(); | |
586 } | 663 } |
587 | 664 |
588 QString | 665 QString |
589 FileSource::getContentType() const | 666 FileSource::getContentType() const |
590 { | 667 { |
615 | 692 |
616 void | 693 void |
617 FileSource::httpResponseHeaderReceived(const QHttpResponseHeader &resp) | 694 FileSource::httpResponseHeaderReceived(const QHttpResponseHeader &resp) |
618 { | 695 { |
619 #ifdef DEBUG_FILE_SOURCE | 696 #ifdef DEBUG_FILE_SOURCE |
620 std::cerr << "FileSource::httpResponseHeaderReceived" << std::endl; | 697 SVDEBUG << "FileSource::httpResponseHeaderReceived" << endl; |
621 #endif | 698 #endif |
622 | 699 |
623 if (resp.statusCode() / 100 == 3) { | 700 if (resp.statusCode() / 100 == 3) { |
624 QString location = resp.value("Location"); | 701 QString location = resp.value("Location"); |
625 #ifdef DEBUG_FILE_SOURCE | 702 #ifdef DEBUG_FILE_SOURCE |
626 std::cerr << "FileSource::responseHeaderReceived: redirect to \"" | 703 SVDEBUG << "FileSource::responseHeaderReceived: redirect to \"" |
627 << location.toStdString() << "\" received" << std::endl; | 704 << location << "\" received" << endl; |
628 #endif | 705 #endif |
629 if (location != "") { | 706 if (location != "") { |
630 QUrl newUrl(location); | 707 QUrl newUrl(location); |
631 if (newUrl != m_url) { | 708 if (newUrl != m_url) { |
632 cleanup(); | 709 cleanup(); |
649 m_lastStatus = resp.statusCode(); | 726 m_lastStatus = resp.statusCode(); |
650 if (m_lastStatus / 100 >= 4) { | 727 if (m_lastStatus / 100 >= 4) { |
651 m_errorString = QString("%1 %2") | 728 m_errorString = QString("%1 %2") |
652 .arg(resp.statusCode()).arg(resp.reasonPhrase()); | 729 .arg(resp.statusCode()).arg(resp.reasonPhrase()); |
653 #ifdef DEBUG_FILE_SOURCE | 730 #ifdef DEBUG_FILE_SOURCE |
654 std::cerr << "FileSource::responseHeaderReceived: " | 731 SVDEBUG << "FileSource::responseHeaderReceived: " |
655 << m_errorString.toStdString() << std::endl; | 732 << m_errorString << endl; |
656 #endif | 733 #endif |
657 } else { | 734 } else { |
658 #ifdef DEBUG_FILE_SOURCE | 735 #ifdef DEBUG_FILE_SOURCE |
659 std::cerr << "FileSource::responseHeaderReceived: " | 736 SVDEBUG << "FileSource::responseHeaderReceived: " |
660 << m_lastStatus << std::endl; | 737 << m_lastStatus << endl; |
661 #endif | 738 #endif |
662 if (resp.hasContentType()) m_contentType = resp.contentType(); | 739 if (resp.hasContentType()) m_contentType = resp.contentType(); |
663 } | 740 } |
664 emit statusAvailable(); | 741 emit statusAvailable(); |
665 } | 742 } |
675 | 752 |
676 QFtp::Command command = m_ftp->currentCommand(); | 753 QFtp::Command command = m_ftp->currentCommand(); |
677 | 754 |
678 if (!error) { | 755 if (!error) { |
679 #ifdef DEBUG_FILE_SOURCE | 756 #ifdef DEBUG_FILE_SOURCE |
680 std::cerr << "FileSource::ftpCommandFinished: success for command " | 757 SVDEBUG << "FileSource::ftpCommandFinished: success for command " |
681 << command << std::endl; | 758 << command << endl; |
682 #endif | 759 #endif |
683 return; | 760 return; |
684 } | 761 } |
685 | 762 |
686 if (command == QFtp::ConnectToHost) { | 763 if (command == QFtp::ConnectToHost) { |
764 | 841 |
765 void | 842 void |
766 FileSource::deleteCacheFile() | 843 FileSource::deleteCacheFile() |
767 { | 844 { |
768 #ifdef DEBUG_FILE_SOURCE | 845 #ifdef DEBUG_FILE_SOURCE |
769 std::cerr << "FileSource::deleteCacheFile(\"" << m_localFilename.toStdString() << "\")" << std::endl; | 846 SVDEBUG << "FileSource::deleteCacheFile(\"" << m_localFilename << "\")" << endl; |
770 #endif | 847 #endif |
771 | 848 |
772 cleanup(); | 849 cleanup(); |
773 | 850 |
774 if (m_localFilename == "") { | 851 if (m_localFilename == "") { |
801 | 878 |
802 m_fileCreationMutex.lock(); | 879 m_fileCreationMutex.lock(); |
803 | 880 |
804 if (!QFile(m_localFilename).remove()) { | 881 if (!QFile(m_localFilename).remove()) { |
805 #ifdef DEBUG_FILE_SOURCE | 882 #ifdef DEBUG_FILE_SOURCE |
806 std::cerr << "FileSource::deleteCacheFile: ERROR: Failed to delete file \"" << m_localFilename.toStdString() << "\"" << std::endl; | 883 std::cerr << "FileSource::deleteCacheFile: ERROR: Failed to delete file \"" << m_localFilename << "\"" << std::endl; |
807 #endif | 884 #endif |
808 } else { | 885 } else { |
809 #ifdef DEBUG_FILE_SOURCE | 886 #ifdef DEBUG_FILE_SOURCE |
810 std::cerr << "FileSource::deleteCacheFile: Deleted cache file \"" << m_localFilename.toStdString() << "\"" << std::endl; | 887 SVDEBUG << "FileSource::deleteCacheFile: Deleted cache file \"" << m_localFilename << "\"" << endl; |
811 #endif | 888 #endif |
812 m_localFilename = ""; | 889 m_localFilename = ""; |
813 } | 890 } |
814 | 891 |
815 m_fileCreationMutex.unlock(); | 892 m_fileCreationMutex.unlock(); |
822 { | 899 { |
823 { | 900 { |
824 QMutexLocker locker(&m_mapMutex); | 901 QMutexLocker locker(&m_mapMutex); |
825 | 902 |
826 #ifdef DEBUG_FILE_SOURCE | 903 #ifdef DEBUG_FILE_SOURCE |
827 std::cerr << "FileSource::createCacheFile: refcount is " << m_refCountMap[m_url] << std::endl; | 904 SVDEBUG << "FileSource::createCacheFile: refcount is " << m_refCountMap[m_url] << endl; |
828 #endif | 905 #endif |
829 | 906 |
830 if (m_refCountMap[m_url] > 0) { | 907 if (m_refCountMap[m_url] > 0) { |
831 m_refCountMap[m_url]++; | 908 m_refCountMap[m_url]++; |
832 m_localFilename = m_remoteLocalMap[m_url]; | 909 m_localFilename = m_remoteLocalMap[m_url]; |
869 } | 946 } |
870 | 947 |
871 QString filepath(dir.filePath(filename)); | 948 QString filepath(dir.filePath(filename)); |
872 | 949 |
873 #ifdef DEBUG_FILE_SOURCE | 950 #ifdef DEBUG_FILE_SOURCE |
874 std::cerr << "FileSource::createCacheFile: URL is \"" << m_url.toString().toStdString() << "\", dir is \"" << dir.path().toStdString() << "\", base \"" << base.toStdString() << "\", extension \"" << extension.toStdString() << "\", filebase \"" << filename.toStdString() << "\", filename \"" << filepath.toStdString() << "\"" << std::endl; | 951 std::cerr << "FileSource::createCacheFile: URL is \"" << m_url.toString() << "\", dir is \"" << dir.path() << "\", base \"" << base << "\", extension \"" << extension << "\", filebase \"" << filename << "\", filename \"" << filepath << "\"" << std::endl; |
875 #endif | 952 #endif |
876 | 953 |
877 QMutexLocker fcLocker(&m_fileCreationMutex); | 954 QMutexLocker fcLocker(&m_fileCreationMutex); |
878 | 955 |
879 ++m_count; | 956 ++m_count; |
881 if (QFileInfo(filepath).exists() || | 958 if (QFileInfo(filepath).exists() || |
882 !QFile(filepath).open(QFile::WriteOnly)) { | 959 !QFile(filepath).open(QFile::WriteOnly)) { |
883 | 960 |
884 #ifdef DEBUG_FILE_SOURCE | 961 #ifdef DEBUG_FILE_SOURCE |
885 std::cerr << "FileSource::createCacheFile: Failed to create local file \"" | 962 std::cerr << "FileSource::createCacheFile: Failed to create local file \"" |
886 << filepath.toStdString() << "\" for URL \"" | 963 << filepath << "\" for URL \"" |
887 << m_url.toString().toStdString() << "\" (or file already exists): appending suffix instead" << std::endl; | 964 << m_url.toString() << "\" (or file already exists): appending suffix instead" << std::endl; |
888 #endif | 965 #endif |
889 | 966 |
890 if (extension == "") { | 967 if (extension == "") { |
891 filename = QString("%1_%2").arg(base).arg(m_count); | 968 filename = QString("%1_%2").arg(base).arg(m_count); |
892 } else { | 969 } else { |
897 if (QFileInfo(filepath).exists() || | 974 if (QFileInfo(filepath).exists() || |
898 !QFile(filepath).open(QFile::WriteOnly)) { | 975 !QFile(filepath).open(QFile::WriteOnly)) { |
899 | 976 |
900 #ifdef DEBUG_FILE_SOURCE | 977 #ifdef DEBUG_FILE_SOURCE |
901 std::cerr << "FileSource::createCacheFile: ERROR: Failed to create local file \"" | 978 std::cerr << "FileSource::createCacheFile: ERROR: Failed to create local file \"" |
902 << filepath.toStdString() << "\" for URL \"" | 979 << filepath << "\" for URL \"" |
903 << m_url.toString().toStdString() << "\" (or file already exists)" << std::endl; | 980 << m_url.toString() << "\" (or file already exists)" << std::endl; |
904 #endif | 981 #endif |
905 | 982 |
906 return ""; | 983 return ""; |
907 } | 984 } |
908 } | 985 } |
909 | 986 |
910 #ifdef DEBUG_FILE_SOURCE | 987 #ifdef DEBUG_FILE_SOURCE |
911 std::cerr << "FileSource::createCacheFile: url " | 988 std::cerr << "FileSource::createCacheFile: url " |
912 << m_url.toString().toStdString() << " -> local filename " | 989 << m_url.toString() << " -> local filename " |
913 << filepath.toStdString() << std::endl; | 990 << filepath << std::endl; |
914 #endif | 991 #endif |
915 | 992 |
916 m_localFilename = filepath; | 993 m_localFilename = filepath; |
917 | 994 |
918 return false; | 995 return false; |