# HG changeset patch # User Chris Cannam # Date 1192721480 0 # Node ID c08c312b23995ff52b3e147e7ceb83e8984e3bdb # Parent 29c356da4ae4db7b726710c335ec5b0c55062fda * Make RemoteFile far more pervasive, and use it for local files as well so that we can handle both transparently. Make it shallow copy with reference counting, so it can be used by value without having to worry about the cache file lifetime. Use RemoteFile for MainWindow file-open functions, etc diff -r 29c356da4ae4 -r c08c312b2399 document/SVFileReader.cpp --- a/document/SVFileReader.cpp Thu Oct 18 10:15:07 2007 +0000 +++ b/document/SVFileReader.cpp Thu Oct 18 15:31:20 2007 +0000 @@ -412,25 +412,18 @@ QString originalPath = attributes.value("file"); QString path = ff->find(FileFinder::AudioFile, originalPath, m_location); - QUrl url(path); - if (RemoteFile::canHandleScheme(url)) { + RemoteFile file(path); + file.waitForStatus(); - RemoteFile rf(url); - rf.wait(); - - if (rf.isOK()) { - - model = new WaveFileModel(rf.getLocalFilename(), path); - if (!model->isOK()) { - delete model; - model = 0; - //!!! and delete local file? - } - } + if (!file.isOK()) { + std::cerr << "SVFileReader::readModel: Failed to retrieve file \"" << path.toStdString() << "\" for wave file model: " << file.getErrorString().toStdString() << std::endl; + } else if (!file.isAvailable()) { + std::cerr << "SVFileReader::readModel: Failed to retrieve file \"" << path.toStdString() << "\" for wave file model: Source unavailable" << std::endl; } else { - model = new WaveFileModel(path); + file.waitForData(); + model = new WaveFileModel(file); if (!model->isOK()) { delete model; model = 0; diff -r 29c356da4ae4 -r c08c312b2399 main/MainWindow.cpp --- a/main/MainWindow.cpp Thu Oct 18 10:15:07 2007 +0000 +++ b/main/MainWindow.cpp Thu Oct 18 15:31:20 2007 +0000 @@ -2525,7 +2525,7 @@ QString path = getOpenFileName(FileFinder::AudioFile); if (path != "") { - if (openAudioFile(path, ReplaceMainModel) == FileOpenFailed) { + if (openAudio(path, ReplaceMainModel) == FileOpenFailed) { QMessageBox::critical(this, tr("Failed to open file"), tr("File open failed
Audio file \"%1\" could not be opened").arg(path)); } @@ -2538,7 +2538,7 @@ QString path = getOpenFileName(FileFinder::AudioFile); if (path != "") { - if (openAudioFile(path, CreateAdditionalModel) == FileOpenFailed) { + if (openAudio(path, CreateAdditionalModel) == FileOpenFailed) { QMessageBox::critical(this, tr("Failed to open file"), tr("File open failed
Audio file \"%1\" could not be opened").arg(path));
}
@@ -2673,7 +2673,7 @@
if (path != "") {
- FileOpenStatus status = openLayerFile(path);
+ FileOpenStatus status = openLayer(path);
if (status == FileOpenFailed) {
QMessageBox::critical(this, tr("Failed to open file"),
@@ -2686,154 +2686,6 @@
}
}
-MainWindow::FileOpenStatus
-MainWindow::openLayerFile(QString path)
-{
- return openLayerFile(path, path);
-}
-
-MainWindow::FileOpenStatus
-MainWindow::openLayerFile(QString path, QString location)
-{
- Pane *pane = m_paneStack->getCurrentPane();
-
- if (!pane) {
- // shouldn't happen, as the menu action should have been disabled
- std::cerr << "WARNING: MainWindow::openLayerFile: no current pane" << std::endl;
- return FileOpenWrongMode;
- }
-
- if (!getMainModel()) {
- // shouldn't happen, as the menu action should have been disabled
- std::cerr << "WARNING: MainWindow::openLayerFile: No main model -- hence no default sample rate available" << std::endl;
- return FileOpenWrongMode;
- }
-
- bool realFile = (location == path);
-
- if (path.endsWith(".svl") || path.endsWith(".xml")) {
-
- PaneCallback callback(this);
- QFile file(path);
-
- if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
- std::cerr << "ERROR: MainWindow::openLayerFile("
- << location.toStdString()
- << "): Failed to open file for reading" << std::endl;
- return FileOpenFailed;
- }
-
- SVFileReader reader(m_document, callback, location);
- reader.setCurrentPane(pane);
-
- QXmlInputSource inputSource(&file);
- reader.parse(inputSource);
-
- if (!reader.isOK()) {
- std::cerr << "ERROR: MainWindow::openLayerFile("
- << location.toStdString()
- << "): Failed to read XML file: "
- << reader.getErrorString().toStdString() << std::endl;
- return FileOpenFailed;
- }
-
- m_recentFiles.addFile(location);
-
- if (realFile) {
- registerLastOpenedFilePath(FileFinder::LayerFile, path); // for file dialog
- }
-
- return FileOpenSucceeded;
-
- } else {
-
- try {
-
- Model *model = DataFileReaderFactory::load
- (path, getMainModel()->getSampleRate());
-
- if (model) {
-
- Layer *newLayer = m_document->createImportedLayer(model);
-
- if (newLayer) {
-
- m_document->addLayerToView(pane, newLayer);
- m_recentFiles.addFile(location);
-
- if (realFile) {
- registerLastOpenedFilePath(FileFinder::LayerFile, path); // for file dialog
- }
-
- return FileOpenSucceeded;
- }
- }
- } catch (DataFileReaderFactory::Exception e) {
- if (e == DataFileReaderFactory::ImportCancelled) {
- return FileOpenCancelled;
- }
- }
- }
-
- return FileOpenFailed;
-}
-
-MainWindow::FileOpenStatus
-MainWindow::openImageFile(QString path)
-{
- return openImageFile(path, path);
-}
-
-MainWindow::FileOpenStatus
-MainWindow::openImageFile(QString path, QString location)
-{
- Pane *pane = m_paneStack->getCurrentPane();
-
- if (!pane) {
- // shouldn't happen, as the menu action should have been disabled
- std::cerr << "WARNING: MainWindow::openImageFile: no current pane" << std::endl;
- return FileOpenWrongMode;
- }
-
- if (!m_document->getMainModel()) {
- return FileOpenWrongMode;
- }
-
- bool newLayer = false;
-
- ImageLayer *il = dynamic_cast Session file \"%1\" could not be opened").arg(path));
}
@@ -3423,7 +3480,7 @@
if (path.isEmpty()) return;
- FileOpenStatus status = openSomeFile(path, AskUser);
+ FileOpenStatus status = open(path, AskUser);
if (status == FileOpenFailed) {
QMessageBox::critical(this, tr("Failed to open file"),
@@ -3453,7 +3510,7 @@
if (text.isEmpty()) return;
- FileOpenStatus status = openURL(QUrl(text));
+ FileOpenStatus status = open(text);
if (status == FileOpenFailed) {
QMessageBox::critical(this, tr("Failed to open location"),
@@ -3479,7 +3536,7 @@
QString path = action->text();
if (path == "") return;
- FileOpenStatus status = openURL(path);
+ FileOpenStatus status = open(path);
if (status == FileOpenFailed) {
QMessageBox::critical(this, tr("Failed to open location"),
@@ -3490,186 +3547,6 @@
}
}
-MainWindow::FileOpenStatus
-MainWindow::openURL(QUrl url, AudioFileOpenMode mode)
-{
- if (url.scheme().toLower() == "file" || url.scheme() == "") {
-
- return openSomeFile(url.toLocalFile(), mode);
-
- } else if (!RemoteFile::canHandleScheme(url)) {
-
- QMessageBox::critical(this, tr("Unsupported scheme in URL"),
- tr("Download failed The URL scheme \"%1\" is not supported")
- .arg(url.scheme()));
- return FileOpenFailed;
-
- } else {
- RemoteFile rf(url);
- rf.wait();
- if (!rf.isOK()) {
- QMessageBox::critical(this, tr("File download failed"),
- tr("Download failed Failed to download URL \"%1\": %2")
- .arg(url.toString()).arg(rf.getErrorString()));
- return FileOpenFailed;
- }
- FileOpenStatus status;
- if ((status = openSomeFile(rf.getLocalFilename(), url.toString(),
- mode)) !=
- FileOpenSucceeded) {
- rf.deleteLocalFile();
- }
- return status;
- }
-}
-
-MainWindow::FileOpenStatus
-MainWindow::openURL(QString ustr, AudioFileOpenMode mode)
-{
- // This function is used when we don't know whether the string is
- // an encoded or human-readable url
-
- QUrl url(ustr);
-
- if (url.scheme().toLower() == "file" || url.scheme() == "") {
-
- FileOpenStatus status = openSomeFile(url.toLocalFile(), mode);
- if (status == FileOpenFailed) {
- url.setEncodedUrl(ustr.toAscii());
- status = openSomeFile(url.toLocalFile(), mode);
- }
- return status;
-
- } else if (!RemoteFile::canHandleScheme(url)) {
-
- QMessageBox::critical(this, tr("Unsupported scheme in URL"),
- tr("Download failed The URL scheme \"%1\" is not supported")
- .arg(url.scheme()));
- return FileOpenFailed;
-
- } else {
- RemoteFile rf(url);
- rf.wait();
- if (!rf.isOK()) {
- // rf was created on the assumption that ustr was
- // human-readable. Let's try again, this time assuming it
- // was already encoded.
- std::cerr << "MainWindow::openURL: Failed to retrieve URL \""
- << ustr.toStdString() << "\" as human-readable URL; "
- << "trying again treating it as encoded URL"
- << std::endl;
- url.setEncodedUrl(ustr.toAscii());
- return openURL(url, mode);
- }
-
- FileOpenStatus status;
- if ((status = openSomeFile(rf.getLocalFilename(), ustr, mode)) !=
- FileOpenSucceeded) {
- rf.deleteLocalFile();
- }
- return status;
- }
-}
-
-MainWindow::FileOpenStatus
-MainWindow::openSomeFile(QString path, AudioFileOpenMode mode)
-{
- return openSomeFile(path, path, mode);
-}
-
-MainWindow::FileOpenStatus
-MainWindow::openSomeFile(QString path, QString location,
- AudioFileOpenMode mode)
-{
- FileOpenStatus status;
-
- bool canImportLayer = (getMainModel() != 0 &&
- m_paneStack != 0 &&
- m_paneStack->getCurrentPane() != 0);
-
- if ((status = openPlaylistFile(path, location, mode)) != FileOpenFailed) {
- return status;
- } else if ((status = openAudioFile(path, location, mode)) != FileOpenFailed) {
- return status;
- } else if (QFileInfo(path).suffix().toLower() == "sv" &&
- (status = openSessionFile(path, location)) != FileOpenFailed) {
- return status;
- } else if (!canImportLayer) {
- return FileOpenWrongMode;
- } else if ((status = openImageFile(path, location)) != FileOpenFailed) {
- return status;
- } else if ((status = openLayerFile(path, location)) != FileOpenFailed) {
- return status;
- } else {
- return FileOpenFailed;
- }
-}
-
-MainWindow::FileOpenStatus
-MainWindow::openSessionFile(QString path)
-{
- return openSessionFile(path, path);
-}
-
-MainWindow::FileOpenStatus
-MainWindow::openSessionFile(QString path, QString location)
-{
- BZipFileDevice bzFile(path);
- if (!bzFile.open(QIODevice::ReadOnly)) {
- std::cerr << "Failed to open session file \"" << location.toStdString()
- << "\": " << bzFile.errorString().toStdString() << std::endl;
- return FileOpenFailed;
- }
-
- if (!checkSaveModified()) return FileOpenCancelled;
-
- QString error;
- closeSession();
- createDocument();
-
- PaneCallback callback(this);
- m_viewManager->clearSelections();
-
- SVFileReader reader(m_document, callback, location);
- QXmlInputSource inputSource(&bzFile);
- reader.parse(inputSource);
-
- if (!reader.isOK()) {
- error = tr("SV XML file read error:\n%1").arg(reader.getErrorString());
- }
-
- bzFile.close();
-
- bool ok = (error == "");
-
- bool realFile = (location == path);
-
- if (ok) {
-
- setWindowTitle(tr("Sonic Visualiser: %1")
- .arg(QFileInfo(location).fileName()));
-
- if (realFile) m_sessionFile = path;
-
- setupMenus();
- CommandHistory::getInstance()->clear();
- CommandHistory::getInstance()->documentSaved();
- m_documentModified = false;
- updateMenuStates();
-
- m_recentFiles.addFile(location);
-
- if (realFile) {
- registerLastOpenedFilePath(FileFinder::SessionFile, path); // for file dialog
- }
-
- } else {
- setWindowTitle(tr("Sonic Visualiser"));
- }
-
- return ok ? FileOpenSucceeded : FileOpenFailed;
-}
-
void
MainWindow::paneDropAccepted(Pane *pane, QStringList uriList)
{
@@ -3677,7 +3554,7 @@
for (QStringList::iterator i = uriList.begin(); i != uriList.end(); ++i) {
- FileOpenStatus status = openURL(*i, ReplaceCurrentPane);
+ FileOpenStatus status = open(*i, ReplaceCurrentPane);
if (status == FileOpenFailed) {
QMessageBox::critical(this, tr("Failed to open dropped URL"),
@@ -5057,7 +4934,7 @@
if (message.getArgCount() == 1 &&
message.getArg(0).canConvert(QVariant::String)) {
QString path = message.getArg(0).toString();
- if (openSomeFile(path, ReplaceMainModel) != FileOpenSucceeded) {
+ if (open(path, ReplaceMainModel) != FileOpenSucceeded) {
std::cerr << "MainWindow::handleOSCMessage: File open failed for path \""
<< path.toStdString() << "\"" << std::endl;
}
@@ -5070,7 +4947,7 @@
if (message.getArgCount() == 1 &&
message.getArg(0).canConvert(QVariant::String)) {
QString path = message.getArg(0).toString();
- if (openSomeFile(path, CreateAdditionalModel) != FileOpenSucceeded) {
+ if (open(path, CreateAdditionalModel) != FileOpenSucceeded) {
std::cerr << "MainWindow::handleOSCMessage: File open failed for path \""
<< path.toStdString() << "\"" << std::endl;
}
@@ -5087,7 +4964,7 @@
}
std::vector