diff 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
line wrap: on
line diff
--- a/data/fileio/RemoteFile.cpp	Fri Oct 05 13:26:47 2007 +0000
+++ b/data/fileio/RemoteFile.cpp	Fri Oct 05 15:52:52 2007 +0000
@@ -33,7 +33,17 @@
 QMutex
 RemoteFile::m_fileCreationMutex;
 
+RemoteFile::RemoteRefCountMap
+RemoteFile::m_refCountMap;
+
+RemoteFile::RemoteLocalMap
+RemoteFile::m_remoteLocalMap;
+
+QMutex
+RemoteFile::m_mapMutex;
+
 RemoteFile::RemoteFile(QUrl url) :
+    m_url(url),
     m_ftp(0),
     m_http(0),
     m_localFile(0),
@@ -41,13 +51,28 @@
     m_lastStatus(0),
     m_done(false),
     m_progressDialog(0),
-    m_progressShowTimer(this)
+    m_progressShowTimer(this),
+    m_referenced(false)
 {
     if (!canHandleScheme(url)) {
         std::cerr << "RemoteFile::RemoteFile: ERROR: Unsupported scheme in URL \"" << url.toString().toStdString() << "\"" << std::endl;
         return;
     }
 
+    QMutexLocker locker(&m_mapMutex);
+
+    std::cerr << "RemoteFile::RemoteFile: refcount is " << m_refCountMap[m_url] << std::endl;
+
+    if (m_refCountMap[m_url] > 0) {
+        m_refCountMap[m_url]++;
+        m_localFilename = m_remoteLocalMap[m_url];
+        std::cerr << "raising it" << std::endl;
+        m_ok = true;
+        m_done = true;
+        m_referenced = true;
+        return;
+    }
+
     m_localFilename = createLocalFile(url);
     if (m_localFilename == "") return;
     m_localFile = new QFile(m_localFilename);
@@ -146,6 +171,11 @@
     }
 
     if (m_ok) {
+
+        m_remoteLocalMap[m_url] = m_localFilename;
+        m_refCountMap[m_url]++;
+        m_referenced = true;
+
         m_progressDialog = new QProgressDialog(tr("Downloading %1...").arg(url.toString()), tr("Cancel"), 0, 100);
         m_progressDialog->hide();
         connect(&m_progressShowTimer, SIGNAL(timeout()),
@@ -179,11 +209,17 @@
     }
     delete m_progressDialog;
     m_progressDialog = 0;
-    delete m_localFile;
+    delete m_localFile; // does not actually delete the file
     m_localFile = 0;
 }
 
 bool
+RemoteFile::isRemote(QString fileOrUrl)
+{
+    return (fileOrUrl.startsWith("http:") || fileOrUrl.startsWith("ftp:"));
+}
+
+bool
 RemoteFile::canHandleScheme(QUrl url)
 {
     QString scheme = url.scheme().toLower();
@@ -348,6 +384,7 @@
 
     m_ok = !error;
     m_done = true;
+    emit ready();
 }
 
 void
@@ -359,6 +396,20 @@
 
     if (m_localFilename == "") return;
 
+    if (m_referenced) {
+
+        QMutexLocker locker(&m_mapMutex);
+        m_referenced = false;
+
+        if (m_refCountMap[m_url] > 0) {
+            m_refCountMap[m_url]--;
+            if (m_refCountMap[m_url] > 0) {
+                m_done = true;
+                return;
+            }
+        }
+    }
+
     m_fileCreationMutex.lock();
 
     if (!QFile(m_localFilename).remove()) {