diff data/fileio/FileSource.cpp @ 357:b92513201610

* better progress reporting in FileSource * fix set-to-default for audio dials with mappers
author Chris Cannam
date Fri, 04 Jan 2008 17:08:10 +0000
parents d02f71281639
children 183ee2a55fc7
line wrap: on
line diff
--- a/data/fileio/FileSource.cpp	Mon Dec 17 12:32:28 2007 +0000
+++ b/data/fileio/FileSource.cpp	Fri Jan 04 17:08:10 2008 +0000
@@ -14,6 +14,8 @@
 */
 
 #include "FileSource.h"
+#include "ProgressPrinter.h"
+
 #include "base/TempDirectory.h"
 #include "base/Exceptions.h"
 
@@ -44,7 +46,7 @@
 QMutex
 FileSource::m_mapMutex;
 
-FileSource::FileSource(QString fileOrUrl, bool showProgress) :
+FileSource::FileSource(QString fileOrUrl, ShowProgressType progressType) :
     m_url(fileOrUrl),
     m_ftp(0),
     m_http(0),
@@ -54,6 +56,8 @@
     m_remote(isRemote(fileOrUrl)),
     m_done(false),
     m_leaveLocalFile(false),
+    m_progressType(progressType),
+    m_progressPrinter(0),
     m_progressDialog(0),
     m_progressShowTimer(this),
     m_refCounted(false)
@@ -68,7 +72,7 @@
         return;
     }
 
-    init(showProgress);
+    init();
 
     if (isRemote() &&
         (fileOrUrl.contains('%') ||
@@ -99,7 +103,7 @@
             m_ok = false;
             m_done = false;
             m_lastStatus = 0;
-            init(showProgress);
+            init();
         }
     }
 
@@ -109,7 +113,7 @@
     }
 }
 
-FileSource::FileSource(QUrl url, bool showProgress) :
+FileSource::FileSource(QUrl url, ShowProgressType progressType) :
     m_url(url),
     m_ftp(0),
     m_http(0),
@@ -119,6 +123,8 @@
     m_remote(isRemote(url.toString())),
     m_done(false),
     m_leaveLocalFile(false),
+    m_progressType(progressType),
+    m_progressPrinter(0),
     m_progressDialog(0),
     m_progressShowTimer(this),
     m_refCounted(false)
@@ -133,7 +139,7 @@
         return;
     }
 
-    init(showProgress);
+    init();
 }
 
 FileSource::FileSource(const FileSource &rf) :
@@ -147,6 +153,8 @@
     m_remote(rf.m_remote),
     m_done(false),
     m_leaveLocalFile(false),
+    m_progressType(rf.m_progressType),
+    m_progressPrinter(0),
     m_progressDialog(0),
     m_progressShowTimer(0),
     m_refCounted(false)
@@ -197,7 +205,7 @@
 }
 
 void
-FileSource::init(bool showProgress)
+FileSource::init()
 {
     if (!isRemote()) {
 #ifdef DEBUG_FILE_SOURCE
@@ -262,6 +270,7 @@
 
     if (scheme == "http") {
         initHttp();
+        std::cerr << "FileSource: initHttp succeeded" << std::endl;
     } else if (scheme == "ftp") {
         initFtp();
     } else {
@@ -295,14 +304,28 @@
         m_refCountMap[m_url]++;
         m_refCounted = true;
 
-        if (showProgress) {
-            m_progressDialog = new QProgressDialog(tr("Downloading %1...").arg(m_url.toString()), tr("Cancel"), 0, 100);
+        switch (m_progressType) {
+
+        case ProgressNone: break;
+
+        case ProgressDialog:
+            m_progressDialog = new QProgressDialog
+                (tr("Downloading %1...").arg(m_url.toString()),
+                 tr("Cancel"), 0, 100);
             m_progressDialog->hide();
             connect(&m_progressShowTimer, SIGNAL(timeout()),
                     this, SLOT(showProgressDialog()));
-            connect(m_progressDialog, SIGNAL(canceled()), this, SLOT(cancelled()));
+            connect(m_progressDialog, SIGNAL(canceled()),
+                    this, SLOT(cancelled()));
             m_progressShowTimer.setSingleShot(true);
             m_progressShowTimer.start(2000);
+            break;
+
+        case ProgressToConsole:
+            m_progressPrinter = new ProgressPrinter(tr("Downloading..."));
+            connect(this, SIGNAL(progress(int)),
+                    m_progressPrinter, SLOT(progress(int)));
+            break;
         }
     }
 }
@@ -420,6 +443,8 @@
     }
     delete m_progressDialog;
     m_progressDialog = 0;
+    delete m_progressPrinter;
+    m_progressPrinter = 0;
     delete m_localFile; // does not actually delete the file
     m_localFile = 0;
 }
@@ -469,6 +494,7 @@
 FileSource::waitForData()
 {
     while (m_ok && !m_done) {
+//        std::cerr << "FileSource::waitForData: calling QApplication::processEvents" << std::endl;
         QApplication::processEvents();
     }
 }
@@ -822,24 +848,3 @@
     return false;
 }
 
-FileSourceProgressPrinter::FileSourceProgressPrinter() :
-    m_lastProgress(0)
-{
-}
-
-FileSourceProgressPrinter::~FileSourceProgressPrinter()
-{
-    if (m_lastProgress > 0 && m_lastProgress != 100) {
-        std::cerr << "\r\n";
-    }
-}
-
-void
-FileSourceProgressPrinter::progress(int progress)
-{
-    if (progress == m_lastProgress) return;
-    if (progress == 100) std::cerr << "\r\n";
-    else std::cerr << "\r" << progress << "%";
-    m_lastProgress = progress;
-}
-