comparison data/fileio/RemoteFile.cpp @ 304:4fc6f49436b3

* Add support for remote files to image layer
author Chris Cannam
date Fri, 05 Oct 2007 15:52:52 +0000
parents 557e00480279
children 96ef9746c560
comparison
equal deleted inserted replaced
303:15b47d30c085 304:4fc6f49436b3
31 RemoteFile::m_count = 0; 31 RemoteFile::m_count = 0;
32 32
33 QMutex 33 QMutex
34 RemoteFile::m_fileCreationMutex; 34 RemoteFile::m_fileCreationMutex;
35 35
36 RemoteFile::RemoteRefCountMap
37 RemoteFile::m_refCountMap;
38
39 RemoteFile::RemoteLocalMap
40 RemoteFile::m_remoteLocalMap;
41
42 QMutex
43 RemoteFile::m_mapMutex;
44
36 RemoteFile::RemoteFile(QUrl url) : 45 RemoteFile::RemoteFile(QUrl url) :
46 m_url(url),
37 m_ftp(0), 47 m_ftp(0),
38 m_http(0), 48 m_http(0),
39 m_localFile(0), 49 m_localFile(0),
40 m_ok(false), 50 m_ok(false),
41 m_lastStatus(0), 51 m_lastStatus(0),
42 m_done(false), 52 m_done(false),
43 m_progressDialog(0), 53 m_progressDialog(0),
44 m_progressShowTimer(this) 54 m_progressShowTimer(this),
55 m_referenced(false)
45 { 56 {
46 if (!canHandleScheme(url)) { 57 if (!canHandleScheme(url)) {
47 std::cerr << "RemoteFile::RemoteFile: ERROR: Unsupported scheme in URL \"" << url.toString().toStdString() << "\"" << std::endl; 58 std::cerr << "RemoteFile::RemoteFile: ERROR: Unsupported scheme in URL \"" << url.toString().toStdString() << "\"" << std::endl;
59 return;
60 }
61
62 QMutexLocker locker(&m_mapMutex);
63
64 std::cerr << "RemoteFile::RemoteFile: refcount is " << m_refCountMap[m_url] << std::endl;
65
66 if (m_refCountMap[m_url] > 0) {
67 m_refCountMap[m_url]++;
68 m_localFilename = m_remoteLocalMap[m_url];
69 std::cerr << "raising it" << std::endl;
70 m_ok = true;
71 m_done = true;
72 m_referenced = true;
48 return; 73 return;
49 } 74 }
50 75
51 m_localFilename = createLocalFile(url); 76 m_localFilename = createLocalFile(url);
52 if (m_localFilename == "") return; 77 if (m_localFilename == "") return;
144 m_ftp->cd(dirpath); 169 m_ftp->cd(dirpath);
145 m_ftp->get(filename, m_localFile); 170 m_ftp->get(filename, m_localFile);
146 } 171 }
147 172
148 if (m_ok) { 173 if (m_ok) {
174
175 m_remoteLocalMap[m_url] = m_localFilename;
176 m_refCountMap[m_url]++;
177 m_referenced = true;
178
149 m_progressDialog = new QProgressDialog(tr("Downloading %1...").arg(url.toString()), tr("Cancel"), 0, 100); 179 m_progressDialog = new QProgressDialog(tr("Downloading %1...").arg(url.toString()), tr("Cancel"), 0, 100);
150 m_progressDialog->hide(); 180 m_progressDialog->hide();
151 connect(&m_progressShowTimer, SIGNAL(timeout()), 181 connect(&m_progressShowTimer, SIGNAL(timeout()),
152 this, SLOT(showProgressDialog())); 182 this, SLOT(showProgressDialog()));
153 connect(m_progressDialog, SIGNAL(canceled()), this, SLOT(cancelled())); 183 connect(m_progressDialog, SIGNAL(canceled()), this, SLOT(cancelled()));
177 f->abort(); 207 f->abort();
178 f->deleteLater(); 208 f->deleteLater();
179 } 209 }
180 delete m_progressDialog; 210 delete m_progressDialog;
181 m_progressDialog = 0; 211 m_progressDialog = 0;
182 delete m_localFile; 212 delete m_localFile; // does not actually delete the file
183 m_localFile = 0; 213 m_localFile = 0;
214 }
215
216 bool
217 RemoteFile::isRemote(QString fileOrUrl)
218 {
219 return (fileOrUrl.startsWith("http:") || fileOrUrl.startsWith("ftp:"));
184 } 220 }
185 221
186 bool 222 bool
187 RemoteFile::canHandleScheme(QUrl url) 223 RemoteFile::canHandleScheme(QUrl url)
188 { 224 {
346 deleteLocalFile(); 382 deleteLocalFile();
347 } 383 }
348 384
349 m_ok = !error; 385 m_ok = !error;
350 m_done = true; 386 m_done = true;
387 emit ready();
351 } 388 }
352 389
353 void 390 void
354 RemoteFile::deleteLocalFile() 391 RemoteFile::deleteLocalFile()
355 { 392 {
356 // std::cerr << "RemoteFile::deleteLocalFile" << std::endl; 393 // std::cerr << "RemoteFile::deleteLocalFile" << std::endl;
357 394
358 cleanup(); 395 cleanup();
359 396
360 if (m_localFilename == "") return; 397 if (m_localFilename == "") return;
398
399 if (m_referenced) {
400
401 QMutexLocker locker(&m_mapMutex);
402 m_referenced = false;
403
404 if (m_refCountMap[m_url] > 0) {
405 m_refCountMap[m_url]--;
406 if (m_refCountMap[m_url] > 0) {
407 m_done = true;
408 return;
409 }
410 }
411 }
361 412
362 m_fileCreationMutex.lock(); 413 m_fileCreationMutex.lock();
363 414
364 if (!QFile(m_localFilename).remove()) { 415 if (!QFile(m_localFilename).remove()) {
365 std::cerr << "RemoteFile::deleteLocalFile: ERROR: Failed to delete file \"" << m_localFilename.toStdString() << "\"" << std::endl; 416 std::cerr << "RemoteFile::deleteLocalFile: ERROR: Failed to delete file \"" << m_localFilename.toStdString() << "\"" << std::endl;