comparison data/fileio/FileSource.cpp @ 843:e802e550a1f2

Drop std:: from cout, cerr, endl -- pull these in through Debug.h
author Chris Cannam
date Tue, 26 Nov 2013 13:35:08 +0000
parents fcee7e040ab4
children f5cd33909744
comparison
equal deleted inserted replaced
842:23d3a6eca5c3 843:e802e550a1f2
57 if (urlExtantCountMap.find(url) == urlExtantCountMap.end()) { 57 if (urlExtantCountMap.find(url) == urlExtantCountMap.end()) {
58 urlExtantCountMap[url] = 1; 58 urlExtantCountMap[url] = 1;
59 } else { 59 } else {
60 ++urlExtantCountMap[url]; 60 ++urlExtantCountMap[url];
61 } 61 }
62 std::cerr << "FileSource: Now " << urlExtantCountMap[url] << " for this url, " << extantCount << " total" << std::endl; 62 cerr << "FileSource: Now " << urlExtantCountMap[url] << " for this url, " << extantCount << " total" << endl;
63 } 63 }
64 static void decCount(QString url) { 64 static void decCount(QString url) {
65 --extantCount; 65 --extantCount;
66 --urlExtantCountMap[url]; 66 --urlExtantCountMap[url];
67 std::cerr << "FileSource: Now " << urlExtantCountMap[url] << " for this url, " << extantCount << " total" << std::endl; 67 cerr << "FileSource: Now " << urlExtantCountMap[url] << " for this url, " << extantCount << " total" << endl;
68 } 68 }
69 #endif 69 #endif
70 70
71 static QThreadStorage<QNetworkAccessManager *> nms; 71 static QThreadStorage<QNetworkAccessManager *> nms;
72 72
93 if (m_url.toString() == "") { 93 if (m_url.toString() == "") {
94 m_url = QUrl(fileOrUrl, QUrl::TolerantMode); 94 m_url = QUrl(fileOrUrl, QUrl::TolerantMode);
95 } 95 }
96 96
97 #ifdef DEBUG_FILE_SOURCE 97 #ifdef DEBUG_FILE_SOURCE
98 std::cerr << "FileSource::FileSource(" << fileOrUrl << "): url <" << m_url.toString() << ">" << std::endl; 98 cerr << "FileSource::FileSource(" << fileOrUrl << "): url <" << m_url.toString() << ">" << endl;
99 incCount(m_url.toString()); 99 incCount(m_url.toString());
100 #endif 100 #endif
101 101
102 if (!canHandleScheme(m_url)) { 102 if (!canHandleScheme(m_url)) {
103 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << std::endl; 103 cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl;
104 m_errorString = tr("Unsupported scheme in URL"); 104 m_errorString = tr("Unsupported scheme in URL");
105 return; 105 return;
106 } 106 }
107 107
108 init(); 108 init();
109 109
110 if (!isRemote() && 110 if (!isRemote() &&
111 !isAvailable()) { 111 !isAvailable()) {
112 #ifdef DEBUG_FILE_SOURCE 112 #ifdef DEBUG_FILE_SOURCE
113 std::cerr << "FileSource::FileSource: Failed to open local file with URL \"" << m_url.toString() << "\"; trying again assuming filename was encoded" << std::endl; 113 cerr << "FileSource::FileSource: Failed to open local file with URL \"" << m_url.toString() << "\"; trying again assuming filename was encoded" << endl;
114 #endif 114 #endif
115 m_url = QUrl::fromEncoded(fileOrUrl.toLatin1()); 115 m_url = QUrl::fromEncoded(fileOrUrl.toLatin1());
116 #ifdef DEBUG_FILE_SOURCE 116 #ifdef DEBUG_FILE_SOURCE
117 std::cerr << "FileSource::FileSource: URL is now \"" << m_url.toString() << "\"" << std::endl; 117 cerr << "FileSource::FileSource: URL is now \"" << m_url.toString() << "\"" << endl;
118 #endif 118 #endif
119 init(); 119 init();
120 } 120 }
121 121
122 if (isRemote() && 122 if (isRemote() &&
128 if (!isAvailable()) { 128 if (!isAvailable()) {
129 129
130 // The URL was created on the assumption that the string 130 // The URL was created on the assumption that the string
131 // was human-readable. Let's try again, this time 131 // was human-readable. Let's try again, this time
132 // assuming it was already encoded. 132 // assuming it was already encoded.
133 std::cerr << "FileSource::FileSource: Failed to retrieve URL \"" 133 cerr << "FileSource::FileSource: Failed to retrieve URL \""
134 << fileOrUrl.toStdString() 134 << fileOrUrl.toStdString()
135 << "\" as human-readable URL; " 135 << "\" as human-readable URL; "
136 << "trying again treating it as encoded URL" 136 << "trying again treating it as encoded URL"
137 << std::endl; 137 << endl;
138 138
139 // even though our cache file doesn't exist (because the 139 // even though our cache file doesn't exist (because the
140 // resource was 404), we still need to ensure we're no 140 // resource was 404), we still need to ensure we're no
141 // longer associating a filename with this url in the 141 // longer associating a filename with this url in the
142 // refcount map -- or createCacheFile will think we've 142 // refcount map -- or createCacheFile will think we've
156 emit statusAvailable(); 156 emit statusAvailable();
157 emit ready(); 157 emit ready();
158 } 158 }
159 159
160 #ifdef DEBUG_FILE_SOURCE 160 #ifdef DEBUG_FILE_SOURCE
161 std::cerr << "FileSource::FileSource(string) exiting" << std::endl; 161 cerr << "FileSource::FileSource(string) exiting" << endl;
162 #endif 162 #endif
163 } 163 }
164 164
165 FileSource::FileSource(QUrl url, ProgressReporter *reporter) : 165 FileSource::FileSource(QUrl url, ProgressReporter *reporter) :
166 m_url(url), 166 m_url(url),
174 m_leaveLocalFile(false), 174 m_leaveLocalFile(false),
175 m_reporter(reporter), 175 m_reporter(reporter),
176 m_refCounted(false) 176 m_refCounted(false)
177 { 177 {
178 #ifdef DEBUG_FILE_SOURCE 178 #ifdef DEBUG_FILE_SOURCE
179 std::cerr << "FileSource::FileSource(" << url.toString() << ") [as url]" << std::endl; 179 cerr << "FileSource::FileSource(" << url.toString() << ") [as url]" << endl;
180 incCount(m_url.toString()); 180 incCount(m_url.toString());
181 #endif 181 #endif
182 182
183 if (!canHandleScheme(m_url)) { 183 if (!canHandleScheme(m_url)) {
184 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << std::endl; 184 cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl;
185 m_errorString = tr("Unsupported scheme in URL"); 185 m_errorString = tr("Unsupported scheme in URL");
186 return; 186 return;
187 } 187 }
188 188
189 init(); 189 init();
190 190
191 #ifdef DEBUG_FILE_SOURCE 191 #ifdef DEBUG_FILE_SOURCE
192 std::cerr << "FileSource::FileSource(url) exiting" << std::endl; 192 cerr << "FileSource::FileSource(url) exiting" << endl;
193 #endif 193 #endif
194 } 194 }
195 195
196 FileSource::FileSource(const FileSource &rf) : 196 FileSource::FileSource(const FileSource &rf) :
197 QObject(), 197 QObject(),
206 m_leaveLocalFile(false), 206 m_leaveLocalFile(false),
207 m_reporter(rf.m_reporter), 207 m_reporter(rf.m_reporter),
208 m_refCounted(false) 208 m_refCounted(false)
209 { 209 {
210 #ifdef DEBUG_FILE_SOURCE 210 #ifdef DEBUG_FILE_SOURCE
211 std::cerr << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]" << std::endl; 211 cerr << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]" << endl;
212 incCount(m_url.toString()); 212 incCount(m_url.toString());
213 #endif 213 #endif
214 214
215 if (!canHandleScheme(m_url)) { 215 if (!canHandleScheme(m_url)) {
216 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << std::endl; 216 cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << endl;
217 m_errorString = tr("Unsupported scheme in URL"); 217 m_errorString = tr("Unsupported scheme in URL");
218 return; 218 return;
219 } 219 }
220 220
221 if (!isRemote()) { 221 if (!isRemote()) {
222 m_localFilename = rf.m_localFilename; 222 m_localFilename = rf.m_localFilename;
223 } else { 223 } else {
224 QMutexLocker locker(&m_mapMutex); 224 QMutexLocker locker(&m_mapMutex);
225 #ifdef DEBUG_FILE_SOURCE 225 #ifdef DEBUG_FILE_SOURCE
226 std::cerr << "FileSource::FileSource(copy ctor): ref count is " 226 cerr << "FileSource::FileSource(copy ctor): ref count is "
227 << m_refCountMap[m_url] << std::endl; 227 << m_refCountMap[m_url] << endl;
228 #endif 228 #endif
229 if (m_refCountMap[m_url] > 0) { 229 if (m_refCountMap[m_url] > 0) {
230 m_refCountMap[m_url]++; 230 m_refCountMap[m_url]++;
231 #ifdef DEBUG_FILE_SOURCE 231 #ifdef DEBUG_FILE_SOURCE
232 std::cerr << "raised it to " << m_refCountMap[m_url] << std::endl; 232 cerr << "raised it to " << m_refCountMap[m_url] << endl;
233 #endif 233 #endif
234 m_localFilename = m_remoteLocalMap[m_url]; 234 m_localFilename = m_remoteLocalMap[m_url];
235 m_refCounted = true; 235 m_refCounted = true;
236 } else { 236 } else {
237 m_ok = false; 237 m_ok = false;
240 } 240 }
241 241
242 m_done = true; 242 m_done = true;
243 243
244 #ifdef DEBUG_FILE_SOURCE 244 #ifdef DEBUG_FILE_SOURCE
245 std::cerr << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]: note: local filename is \"" << m_localFilename << "\"" << std::endl; 245 cerr << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]: note: local filename is \"" << m_localFilename << "\"" << endl;
246 #endif 246 #endif
247 247
248 #ifdef DEBUG_FILE_SOURCE 248 #ifdef DEBUG_FILE_SOURCE
249 std::cerr << "FileSource::FileSource(copy ctor) exiting" << std::endl; 249 cerr << "FileSource::FileSource(copy ctor) exiting" << endl;
250 #endif 250 #endif
251 } 251 }
252 252
253 FileSource::~FileSource() 253 FileSource::~FileSource()
254 { 254 {
255 #ifdef DEBUG_FILE_SOURCE 255 #ifdef DEBUG_FILE_SOURCE
256 std::cerr << "FileSource(" << m_url.toString() << ")::~FileSource" << std::endl; 256 cerr << "FileSource(" << m_url.toString() << ")::~FileSource" << endl;
257 decCount(m_url.toString()); 257 decCount(m_url.toString());
258 #endif 258 #endif
259 259
260 cleanup(); 260 cleanup();
261 261
272 } 272 }
273 } 273 }
274 274
275 if (isResource()) { 275 if (isResource()) {
276 #ifdef DEBUG_FILE_SOURCE 276 #ifdef DEBUG_FILE_SOURCE
277 std::cerr << "FileSource::init: Is a resource" << std::endl; 277 cerr << "FileSource::init: Is a resource" << endl;
278 #endif 278 #endif
279 QString resourceFile = m_url.toString(); 279 QString resourceFile = m_url.toString();
280 resourceFile.replace(QRegExp("^qrc:"), ":"); 280 resourceFile.replace(QRegExp("^qrc:"), ":");
281 281
282 if (!QFileInfo(resourceFile).exists()) { 282 if (!QFileInfo(resourceFile).exists()) {
283 #ifdef DEBUG_FILE_SOURCE 283 #ifdef DEBUG_FILE_SOURCE
284 std::cerr << "FileSource::init: Resource file of this name does not exist, switching to non-resource URL" << std::endl; 284 cerr << "FileSource::init: Resource file of this name does not exist, switching to non-resource URL" << endl;
285 #endif 285 #endif
286 m_url = resourceFile; 286 m_url = resourceFile;
287 m_resource = false; 287 m_resource = false;
288 } 288 }
289 } 289 }
290 290
291 if (!isRemote() && !isResource()) { 291 if (!isRemote() && !isResource()) {
292 #ifdef DEBUG_FILE_SOURCE 292 #ifdef DEBUG_FILE_SOURCE
293 std::cerr << "FileSource::init: Not a remote URL" << std::endl; 293 cerr << "FileSource::init: Not a remote URL" << endl;
294 #endif 294 #endif
295 bool literal = false; 295 bool literal = false;
296 m_localFilename = m_url.toLocalFile(); 296 m_localFilename = m_url.toLocalFile();
297 297
298 if (m_localFilename == "") { 298 if (m_localFilename == "") {
299 // QUrl may have mishandled the scheme (e.g. in a DOS path) 299 // QUrl may have mishandled the scheme (e.g. in a DOS path)
300 m_localFilename = m_rawFileOrUrl; 300 m_localFilename = m_rawFileOrUrl;
301 #ifdef DEBUG_FILE_SOURCE 301 #ifdef DEBUG_FILE_SOURCE
302 std::cerr << "FileSource::init: Trying literal local filename \"" 302 cerr << "FileSource::init: Trying literal local filename \""
303 << m_localFilename << "\"" << std::endl; 303 << m_localFilename << "\"" << endl;
304 #endif 304 #endif
305 literal = true; 305 literal = true;
306 } 306 }
307 m_localFilename = QFileInfo(m_localFilename).absoluteFilePath(); 307 m_localFilename = QFileInfo(m_localFilename).absoluteFilePath();
308 308
309 #ifdef DEBUG_FILE_SOURCE 309 #ifdef DEBUG_FILE_SOURCE
310 std::cerr << "FileSource::init: URL translates to absolute filename \"" 310 cerr << "FileSource::init: URL translates to absolute filename \""
311 << m_localFilename << "\" (with literal=" << literal << ")" 311 << m_localFilename << "\" (with literal=" << literal << ")"
312 << std::endl; 312 << endl;
313 #endif 313 #endif
314 m_ok = true; 314 m_ok = true;
315 m_lastStatus = 200; 315 m_lastStatus = 200;
316 316
317 if (!QFileInfo(m_localFilename).exists()) { 317 if (!QFileInfo(m_localFilename).exists()) {
318 if (literal) { 318 if (literal) {
319 m_lastStatus = 404; 319 m_lastStatus = 404;
320 } else { 320 } else {
321 #ifdef DEBUG_FILE_SOURCE 321 #ifdef DEBUG_FILE_SOURCE
322 std::cerr << "FileSource::init: Local file of this name does not exist, trying URL as a literal filename" << std::endl; 322 cerr << "FileSource::init: Local file of this name does not exist, trying URL as a literal filename" << endl;
323 #endif 323 #endif
324 // Again, QUrl may have been mistreating us -- 324 // Again, QUrl may have been mistreating us --
325 // e.g. dropping a part that looks like query data 325 // e.g. dropping a part that looks like query data
326 m_localFilename = m_rawFileOrUrl; 326 m_localFilename = m_rawFileOrUrl;
327 literal = true; 327 literal = true;
335 return; 335 return;
336 } 336 }
337 337
338 if (createCacheFile()) { 338 if (createCacheFile()) {
339 #ifdef DEBUG_FILE_SOURCE 339 #ifdef DEBUG_FILE_SOURCE
340 std::cerr << "FileSource::init: Already have this one" << std::endl; 340 cerr << "FileSource::init: Already have this one" << endl;
341 #endif 341 #endif
342 m_ok = true; 342 m_ok = true;
343 if (!QFileInfo(m_localFilename).exists()) { 343 if (!QFileInfo(m_localFilename).exists()) {
344 m_lastStatus = 404; 344 m_lastStatus = 404;
345 } else { 345 } else {
364 QFile resourceFile(resourceFileName); 364 QFile resourceFile(resourceFileName);
365 resourceFile.open(QFile::ReadOnly); 365 resourceFile.open(QFile::ReadOnly);
366 QByteArray ba(resourceFile.readAll()); 366 QByteArray ba(resourceFile.readAll());
367 367
368 #ifdef DEBUG_FILE_SOURCE 368 #ifdef DEBUG_FILE_SOURCE
369 std::cerr << "Copying " << ba.size() << " bytes from resource file to cache file" << std::endl; 369 cerr << "Copying " << ba.size() << " bytes from resource file to cache file" << endl;
370 #endif 370 #endif
371 371
372 qint64 written = m_localFile->write(ba); 372 qint64 written = m_localFile->write(ba);
373 m_localFile->close(); 373 m_localFile->close();
374 delete m_localFile; 374 delete m_localFile;
375 m_localFile = 0; 375 m_localFile = 0;
376 376
377 if (written != ba.size()) { 377 if (written != ba.size()) {
378 #ifdef DEBUG_FILE_SOURCE 378 #ifdef DEBUG_FILE_SOURCE
379 std::cerr << "Copy failed (wrote " << written << " bytes)" << std::endl; 379 cerr << "Copy failed (wrote " << written << " bytes)" << endl;
380 #endif 380 #endif
381 m_ok = false; 381 m_ok = false;
382 return; 382 return;
383 } else { 383 } else {
384 m_ok = true; 384 m_ok = true;
389 } else { 389 } else {
390 390
391 QString scheme = m_url.scheme().toLower(); 391 QString scheme = m_url.scheme().toLower();
392 392
393 #ifdef DEBUG_FILE_SOURCE 393 #ifdef DEBUG_FILE_SOURCE
394 std::cerr << "FileSource::init: Don't have local copy of \"" 394 cerr << "FileSource::init: Don't have local copy of \""
395 << m_url.toString() << "\", retrieving" << std::endl; 395 << m_url.toString() << "\", retrieving" << endl;
396 #endif 396 #endif
397 397
398 if (scheme == "http" || scheme == "https" || scheme == "ftp") { 398 if (scheme == "http" || scheme == "https" || scheme == "ftp") {
399 initRemote(); 399 initRemote();
400 #ifdef DEBUG_FILE_SOURCE 400 #ifdef DEBUG_FILE_SOURCE
401 std::cerr << "FileSource: initRemote returned" << std::endl; 401 cerr << "FileSource: initRemote returned" << endl;
402 #endif 402 #endif
403 } else { 403 } else {
404 m_remote = false; 404 m_remote = false;
405 m_ok = false; 405 m_ok = false;
406 } 406 }
414 // someone else has been doing the same thing at the same time, 414 // someone else has been doing the same thing at the same time,
415 // but has got there first 415 // but has got there first
416 cleanup(); 416 cleanup();
417 m_refCountMap[m_url]++; 417 m_refCountMap[m_url]++;
418 #ifdef DEBUG_FILE_SOURCE 418 #ifdef DEBUG_FILE_SOURCE
419 std::cerr << "FileSource::init: Another FileSource has got there first, abandoning our download and using theirs" << std::endl; 419 cerr << "FileSource::init: Another FileSource has got there first, abandoning our download and using theirs" << endl;
420 #endif 420 #endif
421 m_localFilename = m_remoteLocalMap[m_url]; 421 m_localFilename = m_remoteLocalMap[m_url];
422 m_refCounted = true; 422 m_refCounted = true;
423 m_ok = true; 423 m_ok = true;
424 if (!QFileInfo(m_localFilename).exists()) { 424 if (!QFileInfo(m_localFilename).exists()) {
450 QNetworkRequest req; 450 QNetworkRequest req;
451 req.setUrl(m_url); 451 req.setUrl(m_url);
452 452
453 if (m_preferredContentType != "") { 453 if (m_preferredContentType != "") {
454 #ifdef DEBUG_FILE_SOURCE 454 #ifdef DEBUG_FILE_SOURCE
455 std::cerr << "FileSource: indicating preferred content type of \"" 455 cerr << "FileSource: indicating preferred content type of \""
456 << m_preferredContentType << "\"" << std::endl; 456 << m_preferredContentType << "\"" << endl;
457 #endif 457 #endif
458 req.setRawHeader 458 req.setRawHeader
459 ("Accept", 459 ("Accept",
460 QString("%1, */*").arg(m_preferredContentType).toLatin1()); 460 QString("%1, */*").arg(m_preferredContentType).toLatin1());
461 } 461 }
519 waitForStatus(); 519 waitForStatus();
520 bool available = true; 520 bool available = true;
521 if (!m_ok) available = false; 521 if (!m_ok) available = false;
522 else available = (m_lastStatus / 100 == 2); 522 else available = (m_lastStatus / 100 == 2);
523 #ifdef DEBUG_FILE_SOURCE 523 #ifdef DEBUG_FILE_SOURCE
524 std::cerr << "FileSource::isAvailable: " << (available ? "yes" : "no") 524 cerr << "FileSource::isAvailable: " << (available ? "yes" : "no")
525 << std::endl; 525 << endl;
526 #endif 526 #endif
527 return available; 527 return available;
528 } 528 }
529 529
530 void 530 void
531 FileSource::waitForStatus() 531 FileSource::waitForStatus()
532 { 532 {
533 while (m_ok && (!m_done && m_lastStatus == 0)) { 533 while (m_ok && (!m_done && m_lastStatus == 0)) {
534 // std::cerr << "waitForStatus: processing (last status " << m_lastStatus << ")" << std::endl; 534 // cerr << "waitForStatus: processing (last status " << m_lastStatus << ")" << endl;
535 QCoreApplication::processEvents(); 535 QCoreApplication::processEvents();
536 } 536 }
537 } 537 }
538 538
539 void 539 void
540 FileSource::waitForData() 540 FileSource::waitForData()
541 { 541 {
542 while (m_ok && !m_done) { 542 while (m_ok && !m_done) {
543 // std::cerr << "FileSource::waitForData: calling QApplication::processEvents" << std::endl; 543 // cerr << "FileSource::waitForData: calling QApplication::processEvents" << endl;
544 QCoreApplication::processEvents(); 544 QCoreApplication::processEvents();
545 usleep(10000); 545 usleep(10000);
546 } 546 }
547 } 547 }
548 548
624 624
625 void 625 void
626 FileSource::metaDataChanged() 626 FileSource::metaDataChanged()
627 { 627 {
628 #ifdef DEBUG_FILE_SOURCE 628 #ifdef DEBUG_FILE_SOURCE
629 std::cerr << "FileSource::metaDataChanged" << std::endl; 629 cerr << "FileSource::metaDataChanged" << endl;
630 #endif 630 #endif
631 631
632 if (!m_reply) { 632 if (!m_reply) {
633 std::cerr << "WARNING: FileSource::metaDataChanged() called without a reply object being known to us" << std::endl; 633 cerr << "WARNING: FileSource::metaDataChanged() called without a reply object being known to us" << endl;
634 return; 634 return;
635 } 635 }
636 636
637 int status = 637 int status =
638 m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); 638 m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
639 639
640 if (status / 100 == 3) { 640 if (status / 100 == 3) {
641 QString location = m_reply->header 641 QString location = m_reply->header
642 (QNetworkRequest::LocationHeader).toString(); 642 (QNetworkRequest::LocationHeader).toString();
643 #ifdef DEBUG_FILE_SOURCE 643 #ifdef DEBUG_FILE_SOURCE
644 std::cerr << "FileSource::metaDataChanged: redirect to \"" 644 cerr << "FileSource::metaDataChanged: redirect to \""
645 << location << "\" received" << std::endl; 645 << location << "\" received" << endl;
646 #endif 646 #endif
647 if (location != "") { 647 if (location != "") {
648 QUrl newUrl(location); 648 QUrl newUrl(location);
649 if (newUrl != m_url) { 649 if (newUrl != m_url) {
650 cleanup(); 650 cleanup();
669 m_errorString = QString("%1 %2") 669 m_errorString = QString("%1 %2")
670 .arg(status) 670 .arg(status)
671 .arg(m_reply->attribute 671 .arg(m_reply->attribute
672 (QNetworkRequest::HttpReasonPhraseAttribute).toString()); 672 (QNetworkRequest::HttpReasonPhraseAttribute).toString());
673 #ifdef DEBUG_FILE_SOURCE 673 #ifdef DEBUG_FILE_SOURCE
674 std::cerr << "FileSource::metaDataChanged: " 674 cerr << "FileSource::metaDataChanged: "
675 << m_errorString << std::endl; 675 << m_errorString << endl;
676 #endif 676 #endif
677 } else { 677 } else {
678 #ifdef DEBUG_FILE_SOURCE 678 #ifdef DEBUG_FILE_SOURCE
679 std::cerr << "FileSource::metaDataChanged: " 679 cerr << "FileSource::metaDataChanged: "
680 << m_lastStatus << std::endl; 680 << m_lastStatus << endl;
681 #endif 681 #endif
682 m_contentType = 682 m_contentType =
683 m_reply->header(QNetworkRequest::ContentTypeHeader).toString(); 683 m_reply->header(QNetworkRequest::ContentTypeHeader).toString();
684 } 684 }
685 emit statusAvailable(); 685 emit statusAvailable();
706 FileSource::replyFinished() 706 FileSource::replyFinished()
707 { 707 {
708 emit progress(100); 708 emit progress(100);
709 709
710 #ifdef DEBUG_FILE_SOURCE 710 #ifdef DEBUG_FILE_SOURCE
711 std::cerr << "FileSource::replyFinished()" << std::endl; 711 cerr << "FileSource::replyFinished()" << endl;
712 #endif 712 #endif
713 713
714 if (m_done) return; 714 if (m_done) return;
715 715
716 bool error = (m_lastStatus / 100 >= 4); 716 bool error = (m_lastStatus / 100 >= 4);
728 } 728 }
729 } 729 }
730 730
731 if (error) { 731 if (error) {
732 #ifdef DEBUG_FILE_SOURCE 732 #ifdef DEBUG_FILE_SOURCE
733 std::cerr << "FileSource::done: error is " << error << ", deleting cache file" << std::endl; 733 cerr << "FileSource::done: error is " << error << ", deleting cache file" << endl;
734 #endif 734 #endif
735 deleteCacheFile(); 735 deleteCacheFile();
736 } 736 }
737 737
738 m_ok = !error; 738 m_ok = !error;
754 754
755 void 755 void
756 FileSource::deleteCacheFile() 756 FileSource::deleteCacheFile()
757 { 757 {
758 #ifdef DEBUG_FILE_SOURCE 758 #ifdef DEBUG_FILE_SOURCE
759 std::cerr << "FileSource::deleteCacheFile(\"" << m_localFilename << "\")" << std::endl; 759 cerr << "FileSource::deleteCacheFile(\"" << m_localFilename << "\")" << endl;
760 #endif 760 #endif
761 761
762 cleanup(); 762 cleanup();
763 763
764 if (m_localFilename == "") { 764 if (m_localFilename == "") {
765 return; 765 return;
766 } 766 }
767 767
768 if (!isRemote()) { 768 if (!isRemote()) {
769 #ifdef DEBUG_FILE_SOURCE 769 #ifdef DEBUG_FILE_SOURCE
770 std::cerr << "not a cache file" << std::endl; 770 cerr << "not a cache file" << endl;
771 #endif 771 #endif
772 return; 772 return;
773 } 773 }
774 774
775 if (m_refCounted) { 775 if (m_refCounted) {
778 m_refCounted = false; 778 m_refCounted = false;
779 779
780 if (m_refCountMap[m_url] > 0) { 780 if (m_refCountMap[m_url] > 0) {
781 m_refCountMap[m_url]--; 781 m_refCountMap[m_url]--;
782 #ifdef DEBUG_FILE_SOURCE 782 #ifdef DEBUG_FILE_SOURCE
783 std::cerr << "reduced ref count to " << m_refCountMap[m_url] << std::endl; 783 cerr << "reduced ref count to " << m_refCountMap[m_url] << endl;
784 #endif 784 #endif
785 if (m_refCountMap[m_url] > 0) { 785 if (m_refCountMap[m_url] > 0) {
786 m_done = true; 786 m_done = true;
787 return; 787 return;
788 } 788 }
791 791
792 m_fileCreationMutex.lock(); 792 m_fileCreationMutex.lock();
793 793
794 if (!QFile(m_localFilename).remove()) { 794 if (!QFile(m_localFilename).remove()) {
795 #ifdef DEBUG_FILE_SOURCE 795 #ifdef DEBUG_FILE_SOURCE
796 std::cerr << "FileSource::deleteCacheFile: ERROR: Failed to delete file \"" << m_localFilename << "\"" << std::endl; 796 cerr << "FileSource::deleteCacheFile: ERROR: Failed to delete file \"" << m_localFilename << "\"" << endl;
797 #endif 797 #endif
798 } else { 798 } else {
799 #ifdef DEBUG_FILE_SOURCE 799 #ifdef DEBUG_FILE_SOURCE
800 std::cerr << "FileSource::deleteCacheFile: Deleted cache file \"" << m_localFilename << "\"" << std::endl; 800 cerr << "FileSource::deleteCacheFile: Deleted cache file \"" << m_localFilename << "\"" << endl;
801 #endif 801 #endif
802 m_localFilename = ""; 802 m_localFilename = "";
803 } 803 }
804 804
805 m_fileCreationMutex.unlock(); 805 m_fileCreationMutex.unlock();
812 { 812 {
813 { 813 {
814 QMutexLocker locker(&m_mapMutex); 814 QMutexLocker locker(&m_mapMutex);
815 815
816 #ifdef DEBUG_FILE_SOURCE 816 #ifdef DEBUG_FILE_SOURCE
817 std::cerr << "FileSource::createCacheFile: refcount is " << m_refCountMap[m_url] << std::endl; 817 cerr << "FileSource::createCacheFile: refcount is " << m_refCountMap[m_url] << endl;
818 #endif 818 #endif
819 819
820 if (m_refCountMap[m_url] > 0) { 820 if (m_refCountMap[m_url] > 0) {
821 m_refCountMap[m_url]++; 821 m_refCountMap[m_url]++;
822 m_localFilename = m_remoteLocalMap[m_url]; 822 m_localFilename = m_remoteLocalMap[m_url];
823 #ifdef DEBUG_FILE_SOURCE 823 #ifdef DEBUG_FILE_SOURCE
824 std::cerr << "raised it to " << m_refCountMap[m_url] << std::endl; 824 cerr << "raised it to " << m_refCountMap[m_url] << endl;
825 #endif 825 #endif
826 m_refCounted = true; 826 m_refCounted = true;
827 return true; 827 return true;
828 } 828 }
829 } 829 }
831 QDir dir; 831 QDir dir;
832 try { 832 try {
833 dir = TempDirectory::getInstance()->getSubDirectoryPath("download"); 833 dir = TempDirectory::getInstance()->getSubDirectoryPath("download");
834 } catch (DirectoryCreationFailed f) { 834 } catch (DirectoryCreationFailed f) {
835 #ifdef DEBUG_FILE_SOURCE 835 #ifdef DEBUG_FILE_SOURCE
836 std::cerr << "FileSource::createCacheFile: ERROR: Failed to create temporary directory: " << f.what() << std::endl; 836 cerr << "FileSource::createCacheFile: ERROR: Failed to create temporary directory: " << f.what() << endl;
837 #endif 837 #endif
838 return ""; 838 return "";
839 } 839 }
840 840
841 QString filepart = m_url.path().section('/', -1, -1, 841 QString filepart = m_url.path().section('/', -1, -1,
859 } 859 }
860 860
861 QString filepath(dir.filePath(filename)); 861 QString filepath(dir.filePath(filename));
862 862
863 #ifdef DEBUG_FILE_SOURCE 863 #ifdef DEBUG_FILE_SOURCE
864 std::cerr << "FileSource::createCacheFile: URL is \"" << m_url.toString() << "\", dir is \"" << dir.path() << "\", base \"" << base << "\", extension \"" << extension << "\", filebase \"" << filename << "\", filename \"" << filepath << "\"" << std::endl; 864 cerr << "FileSource::createCacheFile: URL is \"" << m_url.toString() << "\", dir is \"" << dir.path() << "\", base \"" << base << "\", extension \"" << extension << "\", filebase \"" << filename << "\", filename \"" << filepath << "\"" << endl;
865 #endif 865 #endif
866 866
867 QMutexLocker fcLocker(&m_fileCreationMutex); 867 QMutexLocker fcLocker(&m_fileCreationMutex);
868 868
869 ++m_count; 869 ++m_count;
870 870
871 if (QFileInfo(filepath).exists() || 871 if (QFileInfo(filepath).exists() ||
872 !QFile(filepath).open(QFile::WriteOnly)) { 872 !QFile(filepath).open(QFile::WriteOnly)) {
873 873
874 #ifdef DEBUG_FILE_SOURCE 874 #ifdef DEBUG_FILE_SOURCE
875 std::cerr << "FileSource::createCacheFile: Failed to create local file \"" 875 cerr << "FileSource::createCacheFile: Failed to create local file \""
876 << filepath << "\" for URL \"" 876 << filepath << "\" for URL \""
877 << m_url.toString() << "\" (or file already exists): appending suffix instead" << std::endl; 877 << m_url.toString() << "\" (or file already exists): appending suffix instead" << endl;
878 #endif 878 #endif
879 879
880 if (extension == "") { 880 if (extension == "") {
881 filename = QString("%1_%2").arg(base).arg(m_count); 881 filename = QString("%1_%2").arg(base).arg(m_count);
882 } else { 882 } else {
886 886
887 if (QFileInfo(filepath).exists() || 887 if (QFileInfo(filepath).exists() ||
888 !QFile(filepath).open(QFile::WriteOnly)) { 888 !QFile(filepath).open(QFile::WriteOnly)) {
889 889
890 #ifdef DEBUG_FILE_SOURCE 890 #ifdef DEBUG_FILE_SOURCE
891 std::cerr << "FileSource::createCacheFile: ERROR: Failed to create local file \"" 891 cerr << "FileSource::createCacheFile: ERROR: Failed to create local file \""
892 << filepath << "\" for URL \"" 892 << filepath << "\" for URL \""
893 << m_url.toString() << "\" (or file already exists)" << std::endl; 893 << m_url.toString() << "\" (or file already exists)" << endl;
894 #endif 894 #endif
895 895
896 return ""; 896 return "";
897 } 897 }
898 } 898 }
899 899
900 #ifdef DEBUG_FILE_SOURCE 900 #ifdef DEBUG_FILE_SOURCE
901 std::cerr << "FileSource::createCacheFile: url " 901 cerr << "FileSource::createCacheFile: url "
902 << m_url.toString() << " -> local filename " 902 << m_url.toString() << " -> local filename "
903 << filepath << std::endl; 903 << filepath << endl;
904 #endif 904 #endif
905 905
906 m_localFilename = filepath; 906 m_localFilename = filepath;
907 907
908 return false; 908 return false;