comparison data/fileio/FileSource.cpp @ 392:183ee2a55fc7

* More work to abstract out interactive components used in the data library, so that it does not need to depend on QtGui.
author Chris Cannam
date Fri, 14 Mar 2008 17:14:21 +0000
parents b92513201610
children be49bf95d4a5
comparison
equal deleted inserted replaced
391:5858cc462d0a 392:183ee2a55fc7
12 License, or (at your option) any later version. See the file 12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information. 13 COPYING included with this distribution for more information.
14 */ 14 */
15 15
16 #include "FileSource.h" 16 #include "FileSource.h"
17 #include "ProgressPrinter.h"
18 17
19 #include "base/TempDirectory.h" 18 #include "base/TempDirectory.h"
20 #include "base/Exceptions.h" 19 #include "base/Exceptions.h"
20 #include "base/ProgressReporter.h"
21 21
22 #include <QHttp> 22 #include <QHttp>
23 #include <QFtp> 23 #include <QFtp>
24 #include <QFileInfo> 24 #include <QFileInfo>
25 #include <QDir> 25 #include <QDir>
26 #include <QApplication> 26 #include <QCoreApplication>
27 #include <QProgressDialog>
28 #include <QHttpResponseHeader> 27 #include <QHttpResponseHeader>
29 28
30 #include <iostream> 29 #include <iostream>
31 30
32 #define DEBUG_FILE_SOURCE 1 31 #define DEBUG_FILE_SOURCE 1
44 FileSource::m_remoteLocalMap; 43 FileSource::m_remoteLocalMap;
45 44
46 QMutex 45 QMutex
47 FileSource::m_mapMutex; 46 FileSource::m_mapMutex;
48 47
49 FileSource::FileSource(QString fileOrUrl, ShowProgressType progressType) : 48 FileSource::FileSource(QString fileOrUrl, ProgressReporter *reporter) :
50 m_url(fileOrUrl), 49 m_url(fileOrUrl),
51 m_ftp(0), 50 m_ftp(0),
52 m_http(0), 51 m_http(0),
53 m_localFile(0), 52 m_localFile(0),
54 m_ok(false), 53 m_ok(false),
55 m_lastStatus(0), 54 m_lastStatus(0),
56 m_remote(isRemote(fileOrUrl)), 55 m_remote(isRemote(fileOrUrl)),
57 m_done(false), 56 m_done(false),
58 m_leaveLocalFile(false), 57 m_leaveLocalFile(false),
59 m_progressType(progressType), 58 m_reporter(reporter),
60 m_progressPrinter(0),
61 m_progressDialog(0),
62 m_progressShowTimer(this),
63 m_refCounted(false) 59 m_refCounted(false)
64 { 60 {
65 #ifdef DEBUG_FILE_SOURCE 61 #ifdef DEBUG_FILE_SOURCE
66 std::cerr << "FileSource::FileSource(" << fileOrUrl.toStdString() << ")" << std::endl; 62 std::cerr << "FileSource::FileSource(" << fileOrUrl.toStdString() << ")" << std::endl;
67 #endif 63 #endif
111 emit statusAvailable(); 107 emit statusAvailable();
112 emit ready(); 108 emit ready();
113 } 109 }
114 } 110 }
115 111
116 FileSource::FileSource(QUrl url, ShowProgressType progressType) : 112 FileSource::FileSource(QUrl url, ProgressReporter *reporter) :
117 m_url(url), 113 m_url(url),
118 m_ftp(0), 114 m_ftp(0),
119 m_http(0), 115 m_http(0),
120 m_localFile(0), 116 m_localFile(0),
121 m_ok(false), 117 m_ok(false),
122 m_lastStatus(0), 118 m_lastStatus(0),
123 m_remote(isRemote(url.toString())), 119 m_remote(isRemote(url.toString())),
124 m_done(false), 120 m_done(false),
125 m_leaveLocalFile(false), 121 m_leaveLocalFile(false),
126 m_progressType(progressType), 122 m_reporter(reporter),
127 m_progressPrinter(0),
128 m_progressDialog(0),
129 m_progressShowTimer(this),
130 m_refCounted(false) 123 m_refCounted(false)
131 { 124 {
132 #ifdef DEBUG_FILE_SOURCE 125 #ifdef DEBUG_FILE_SOURCE
133 std::cerr << "FileSource::FileSource(" << url.toString().toStdString() << ") [as url]" << std::endl; 126 std::cerr << "FileSource::FileSource(" << url.toString().toStdString() << ") [as url]" << std::endl;
134 #endif 127 #endif
151 m_ok(rf.m_ok), 144 m_ok(rf.m_ok),
152 m_lastStatus(rf.m_lastStatus), 145 m_lastStatus(rf.m_lastStatus),
153 m_remote(rf.m_remote), 146 m_remote(rf.m_remote),
154 m_done(false), 147 m_done(false),
155 m_leaveLocalFile(false), 148 m_leaveLocalFile(false),
156 m_progressType(rf.m_progressType), 149 m_reporter(rf.m_reporter),
157 m_progressPrinter(0),
158 m_progressDialog(0),
159 m_progressShowTimer(0),
160 m_refCounted(false) 150 m_refCounted(false)
161 { 151 {
162 #ifdef DEBUG_FILE_SOURCE 152 #ifdef DEBUG_FILE_SOURCE
163 std::cerr << "FileSource::FileSource(" << m_url.toString().toStdString() << ") [copy ctor]" << std::endl; 153 std::cerr << "FileSource::FileSource(" << m_url.toString().toStdString() << ") [copy ctor]" << std::endl;
164 #endif 154 #endif
302 292
303 m_remoteLocalMap[m_url] = m_localFilename; 293 m_remoteLocalMap[m_url] = m_localFilename;
304 m_refCountMap[m_url]++; 294 m_refCountMap[m_url]++;
305 m_refCounted = true; 295 m_refCounted = true;
306 296
307 switch (m_progressType) { 297 if (m_reporter) {
308 298 m_reporter->setMessage
309 case ProgressNone: break; 299 (tr("Downloading %1...").arg(m_url.toString()));
310 300 connect(m_reporter, SIGNAL(cancelled()), this, SLOT(cancelled()));
311 case ProgressDialog:
312 m_progressDialog = new QProgressDialog
313 (tr("Downloading %1...").arg(m_url.toString()),
314 tr("Cancel"), 0, 100);
315 m_progressDialog->hide();
316 connect(&m_progressShowTimer, SIGNAL(timeout()),
317 this, SLOT(showProgressDialog()));
318 connect(m_progressDialog, SIGNAL(canceled()),
319 this, SLOT(cancelled()));
320 m_progressShowTimer.setSingleShot(true);
321 m_progressShowTimer.start(2000);
322 break;
323
324 case ProgressToConsole:
325 m_progressPrinter = new ProgressPrinter(tr("Downloading..."));
326 connect(this, SIGNAL(progress(int)), 301 connect(this, SIGNAL(progress(int)),
327 m_progressPrinter, SLOT(progress(int))); 302 m_reporter, SLOT(setProgress(int)));
328 break;
329 } 303 }
330 } 304 }
331 } 305 }
332 306
333 void 307 void
439 QFtp *f = m_ftp; 413 QFtp *f = m_ftp;
440 m_ftp = 0; 414 m_ftp = 0;
441 f->abort(); 415 f->abort();
442 f->deleteLater(); 416 f->deleteLater();
443 } 417 }
444 delete m_progressDialog;
445 m_progressDialog = 0;
446 delete m_progressPrinter;
447 m_progressPrinter = 0;
448 delete m_localFile; // does not actually delete the file 418 delete m_localFile; // does not actually delete the file
449 m_localFile = 0; 419 m_localFile = 0;
450 } 420 }
451 421
452 bool 422 bool
484 void 454 void
485 FileSource::waitForStatus() 455 FileSource::waitForStatus()
486 { 456 {
487 while (m_ok && (!m_done && m_lastStatus == 0)) { 457 while (m_ok && (!m_done && m_lastStatus == 0)) {
488 // std::cerr << "waitForStatus: processing (last status " << m_lastStatus << ")" << std::endl; 458 // std::cerr << "waitForStatus: processing (last status " << m_lastStatus << ")" << std::endl;
489 QApplication::processEvents(); 459 QCoreApplication::processEvents();
490 } 460 }
491 } 461 }
492 462
493 void 463 void
494 FileSource::waitForData() 464 FileSource::waitForData()
495 { 465 {
496 while (m_ok && !m_done) { 466 while (m_ok && !m_done) {
497 // std::cerr << "FileSource::waitForData: calling QApplication::processEvents" << std::endl; 467 // std::cerr << "FileSource::waitForData: calling QApplication::processEvents" << std::endl;
498 QApplication::processEvents(); 468 QCoreApplication::processEvents();
499 } 469 }
500 } 470 }
501 471
502 void 472 void
503 FileSource::setLeaveLocalFile(bool leave) 473 FileSource::setLeaveLocalFile(bool leave)
619 void 589 void
620 FileSource::dataTransferProgress(qint64 done, qint64 total) 590 FileSource::dataTransferProgress(qint64 done, qint64 total)
621 { 591 {
622 int percent = int((double(done) / double(total)) * 100.0 - 0.1); 592 int percent = int((double(done) / double(total)) * 100.0 - 0.1);
623 emit progress(percent); 593 emit progress(percent);
624
625 if (!m_progressDialog) return;
626
627 if (percent > 0) {
628 m_progressDialog->setValue(percent);
629 m_progressDialog->show();
630 }
631 } 594 }
632 595
633 void 596 void
634 FileSource::cancelled() 597 FileSource::cancelled()
635 { 598 {
741 m_fileCreationMutex.unlock(); 704 m_fileCreationMutex.unlock();
742 705
743 m_done = true; 706 m_done = true;
744 } 707 }
745 708
746 void
747 FileSource::showProgressDialog()
748 {
749 if (m_progressDialog) m_progressDialog->show();
750 }
751
752 bool 709 bool
753 FileSource::createCacheFile() 710 FileSource::createCacheFile()
754 { 711 {
755 { 712 {
756 QMutexLocker locker(&m_mapMutex); 713 QMutexLocker locker(&m_mapMutex);