changeset 304:4fc6f49436b3

* Add support for remote files to image layer
author Chris Cannam
date Fri, 05 Oct 2007 15:52:52 +0000
parents 15b47d30c085
children fc656505c573
files base/Preferences.cpp data/fileio/CodedAudioFileReader.cpp data/fileio/RemoteFile.cpp data/fileio/RemoteFile.h
diffstat 4 files changed, 71 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/base/Preferences.cpp	Fri Oct 05 13:26:47 2007 +0000
+++ b/base/Preferences.cpp	Fri Oct 05 15:52:52 2007 +0000
@@ -42,7 +42,7 @@
     m_resampleQuality(1),
     m_omitRecentTemps(true),
     m_tempDirRoot(""),
-    m_resampleOnLoad(true)
+    m_resampleOnLoad(false)
 {
     QSettings settings;
     settings.beginGroup("Preferences");
@@ -54,7 +54,7 @@
     m_windowType = WindowType
         (settings.value("window-type", int(HanningWindow)).toInt());
     m_resampleQuality = settings.value("resample-quality", 1).toInt();
-    m_resampleOnLoad = settings.value("resample-on-load", true).toBool();
+    m_resampleOnLoad = settings.value("resample-on-load", false).toBool();
     m_backgroundMode = BackgroundMode
         (settings.value("background-mode", int(BackgroundFromTheme)).toInt());
     settings.endGroup();
--- a/data/fileio/CodedAudioFileReader.cpp	Fri Oct 05 13:26:47 2007 +0000
+++ b/data/fileio/CodedAudioFileReader.cpp	Fri Oct 05 15:52:52 2007 +0000
@@ -303,6 +303,11 @@
                  ratio,
                  final);
 
+            for (size_t i = 0; i < out; ++i) {
+                if (m_resampleBuffer[i] >  1.f) m_resampleBuffer[i] =  1.f;
+                if (m_resampleBuffer[i] < -1.f) m_resampleBuffer[i] = -1.f;
+            }
+
             buffer = m_resampleBuffer;
             sz = out;
         }
--- 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()) {
--- a/data/fileio/RemoteFile.h	Fri Oct 05 13:26:47 2007 +0000
+++ b/data/fileio/RemoteFile.h	Fri Oct 05 15:52:52 2007 +0000
@@ -21,6 +21,8 @@
 #include <QString>
 #include <QTimer>
 
+#include <map>
+
 class QFtp;
 class QHttp;
 class QFile;
@@ -47,6 +49,7 @@
 
     void deleteLocalFile();
 
+    static bool isRemote(QString fileOrUrl);
     static bool canHandleScheme(QUrl url);
 
 signals:
@@ -63,6 +66,7 @@
     void cancelled();
 
 protected:
+    QUrl m_url;
     QFtp *m_ftp;
     QHttp *m_http;
     QFile *m_localFile;
@@ -74,6 +78,13 @@
     QProgressDialog *m_progressDialog;
     QTimer m_progressShowTimer;
 
+    typedef std::map<QUrl, int> RemoteRefCountMap;
+    typedef std::map<QUrl, QString> RemoteLocalMap;
+    static RemoteRefCountMap m_refCountMap;
+    static RemoteLocalMap m_remoteLocalMap;
+    static QMutex m_mapMutex;
+    bool m_referenced;
+
     void cleanup();
 
     QString createLocalFile(QUrl url);