From 0bb6a3d971f0957cedc869a271c51415d6bdf719 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Tue, 20 Dec 2016 16:21:03 -0500 Subject: [PATCH] Exception safety in: importing functions... ... Side effects on preferences may persist even after errors. --- src/Menus.cpp | 17 ++++++---- src/Project.cpp | 75 +++++++++++++++---------------------------- src/import/Import.cpp | 22 +------------ 3 files changed, 38 insertions(+), 76 deletions(-) diff --git a/src/Menus.cpp b/src/Menus.cpp index 52a3b0e9c..413603bc3 100644 --- a/src/Menus.cpp +++ b/src/Menus.cpp @@ -5767,6 +5767,9 @@ void AudacityProject::OnImport() return; } + // PRL: This affects FFmpegImportPlugin::Open which resets the preference + // to false. Should it also be set to true on other paths that reach + // AudacityProject::Import ? gPrefs->Write(wxT("/NewImportingSession"), true); //sort selected files by OD status. Load non OD first so user can edit asap. @@ -5774,6 +5777,14 @@ void AudacityProject::OnImport() selectedFiles.Sort(CompareNoCaseFileName); ODManager::Pauser pauser; + auto cleanup = finally( [&] { + gPrefs->Write(wxT("/LastOpenType"),wxT("")); + + gPrefs->Flush(); + + HandleResize(); // Adjust scrollers for NEW track sizes. + } ); + for (size_t ff = 0; ff < selectedFiles.GetCount(); ff++) { wxString fileName = selectedFiles[ff]; @@ -5782,12 +5793,6 @@ void AudacityProject::OnImport() Import(fileName); } - - gPrefs->Write(wxT("/LastOpenType"),wxT("")); - - gPrefs->Flush(); - - HandleResize(); // Adjust scrollers for NEW track sizes. } void AudacityProject::OnImportLabels() diff --git a/src/Project.cpp b/src/Project.cpp index 355a6e3b7..84ff368ea 100644 --- a/src/Project.cpp +++ b/src/Project.cpp @@ -452,17 +452,16 @@ public: return GuardedCall< bool > ( [&] { //sort by OD non OD. load Non OD first so user can start editing asap. wxArrayString sortednames(filenames); + sortednames.Sort(CompareNoCaseFileName); ODManager::Pauser pauser; - sortednames.Sort(CompareNoCaseFileName); + auto cleanup = finally( [&] { + mProject->HandleResize(); // Adjust scrollers for NEW track sizes. + } ); - for (unsigned int i = 0; i < sortednames.GetCount(); i++) { - - mProject->Import(sortednames[i]); - } - - mProject->HandleResize(); // Adjust scrollers for NEW track sizes. + for (const auto &name : sortednames) + mProject->Import(name); return true; } ); @@ -4081,55 +4080,33 @@ bool AudacityProject::Import(const wxString &fileName, WaveTrackArray* pTrackArr TrackHolders newTracks; wxString errorMessage = wxEmptyString; - // Backup Tags, before the import. Be prepared to roll back changes. - struct TempTags { - TempTags(std::shared_ptr & pTags_) - : pTags(pTags_) - { - oldTags = pTags; - if (oldTags) - pTags = oldTags->Duplicate(); - } + { + // Backup Tags, before the import. Be prepared to roll back changes. + auto cleanup = valueRestorer( mTags, + mTags ? mTags->Duplicate() : decltype(mTags){} ); - ~TempTags() - { - if (oldTags) { - // roll back - pTags = oldTags; - } - } - - void Commit() - { - oldTags.reset(); - } - - std::shared_ptr & pTags; - std::shared_ptr oldTags; - }; - TempTags tempTags(mTags); - - bool success = Importer::Get().Import(fileName, + bool success = Importer::Get().Import(fileName, GetTrackFactory(), newTracks, mTags.get(), errorMessage); - if (!errorMessage.IsEmpty()) { -// Version that goes to internet... -// ShowErrorDialog(this, _("Error Importing"), -// errorMessage, wxT("http://audacity.sourceforge.net/help/faq?s=files&i=wma-proprietary")); -// Version that looks locally for the text. - ShowErrorDialog(this, _("Error Importing"), - errorMessage, wxT("innerlink:wma-proprietary")); + if (!errorMessage.IsEmpty()) { + // Version that goes to internet... + // ShowErrorDialog(this, _("Error Importing"), + // errorMessage, wxT("http://audacity.sourceforge.net/help/faq?s=files&i=wma-proprietary")); + // Version that looks locally for the text. + ShowErrorDialog(this, _("Error Importing"), + errorMessage, wxT("innerlink:wma-proprietary")); + } + if (!success) + return false; + + wxGetApp().AddFileToHistory(fileName); + + // no more errors, commit + cleanup.release(); } - if (!success) - return false; - - wxGetApp().AddFileToHistory(fileName); - - // no more errors - tempTags.Commit(); // for LOF ("list of files") files, do not import the file as if it // were an audio file itself diff --git a/src/import/Import.cpp b/src/import/Import.cpp index 57b6e8459..07dbc2c68 100644 --- a/src/import/Import.cpp +++ b/src/import/Import.cpp @@ -336,7 +336,7 @@ bool Importer::Import(const wxString &fName, wxString &errorMessage) { AudacityProject *pProj = GetActiveProject(); - pProj->mbBusyImporting = true; + auto cleanup = valueRestorer( pProj->mbBusyImporting, true ); wxString extension = fName.AfterLast(wxT('.')); @@ -509,7 +509,6 @@ bool Importer::Import(const wxString &fName, if (ImportDlg.ShowModal() == wxID_CANCEL) { - pProj->mbBusyImporting = false; return false; } } @@ -524,21 +523,18 @@ bool Importer::Import(const wxString &fName, // LOF ("list-of-files") has different semantics if (extension.IsSameAs(wxT("lof"), false)) { - pProj->mbBusyImporting = false; return true; } if (tracks.size() > 0) { // success! - pProj->mbBusyImporting = false; return true; } } if (res == ProgressResult::Cancelled || res == ProgressResult::Failed) { - pProj->mbBusyImporting = false; return false; } @@ -560,7 +556,6 @@ bool Importer::Import(const wxString &fName, errorMessage.Printf(_("This version of Audacity was not compiled with %s support."), unusableImportPlugin-> GetPluginFormatDescription().c_str()); - pProj->mbBusyImporting = false; return false; } } @@ -571,7 +566,6 @@ bool Importer::Import(const wxString &fName, // MIDI files must be imported, not opened if ((extension.IsSameAs(wxT("midi"), false))||(extension.IsSameAs(wxT("mid"), false))) { errorMessage.Printf(_("\"%s\" \nis a MIDI file, not an audio file. \nAudacity cannot open this type of file for playing, but you can\nedit it by clicking File > Import > MIDI."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } #endif @@ -582,87 +576,74 @@ bool Importer::Import(const wxString &fName, if (extension.IsSameAs(wxT("cda"), false)) { /* i18n-hint: %s will be the filename */ errorMessage.Printf(_("\"%s\" is an audio CD track. \nAudacity cannot open audio CDs directly. \nExtract (rip) the CD tracks to an audio format that \nAudacity can import, such as WAV or AIFF."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } // playlist type files if ((extension.IsSameAs(wxT("m3u"), false))||(extension.IsSameAs(wxT("ram"), false))||(extension.IsSameAs(wxT("pls"), false))) { errorMessage.Printf(_("\"%s\" is a playlist file. \nAudacity cannot open this file because it only contains links to other files. \nYou may be able to open it in a text editor and download the actual audio files."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } //WMA files of various forms if ((extension.IsSameAs(wxT("wma"), false))||(extension.IsSameAs(wxT("asf"), false))) { errorMessage.Printf(_("\"%s\" is a Windows Media Audio file. \nAudacity cannot open this type of file due to patent restrictions. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } //AAC files of various forms (probably not encrypted) if ((extension.IsSameAs(wxT("aac"), false))||(extension.IsSameAs(wxT("m4a"), false))||(extension.IsSameAs(wxT("m4r"), false))||(extension.IsSameAs(wxT("mp4"), false))) { errorMessage.Printf(_("\"%s\" is an Advanced Audio Coding file. \nAudacity cannot open this type of file. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } // encrypted itunes files if ((extension.IsSameAs(wxT("m4p"), false))) { errorMessage.Printf(_("\"%s\" is an encrypted audio file. \nThese typically are from an online music store. \nAudacity cannot open this type of file due to the encryption. \nTry recording the file into Audacity, or burn it to audio CD then \nextract the CD track to a supported audio format such as WAV or AIFF."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } // Real Inc. files of various sorts if ((extension.IsSameAs(wxT("ra"), false))||(extension.IsSameAs(wxT("rm"), false))||(extension.IsSameAs(wxT("rpm"), false))) { errorMessage.Printf(_("\"%s\" is a RealPlayer media file. \nAudacity cannot open this proprietary format. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } // Other notes-based formats if ((extension.IsSameAs(wxT("kar"), false))||(extension.IsSameAs(wxT("mod"), false))||(extension.IsSameAs(wxT("rmi"), false))) { errorMessage.Printf(_("\"%s\" is a notes-based file, not an audio file. \nAudacity cannot open this type of file. \nTry converting it to an audio file such as WAV or AIFF and \nthen import it, or record it into Audacity."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } // MusePack files if ((extension.IsSameAs(wxT("mp+"), false))||(extension.IsSameAs(wxT("mpc"), false))||(extension.IsSameAs(wxT("mpp"), false))) { errorMessage.Printf(_("\"%s\" is a Musepack audio file. \nAudacity cannot open this type of file. \nIf you think it might be an mp3 file, rename it to end with \".mp3\" \nand try importing it again. Otherwise you need to convert it to a supported audio \nformat, such as WAV or AIFF."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } // WavPack files if ((extension.IsSameAs(wxT("wv"), false))||(extension.IsSameAs(wxT("wvc"), false))) { errorMessage.Printf(_("\"%s\" is a Wavpack audio file. \nAudacity cannot open this type of file. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } // AC3 files if ((extension.IsSameAs(wxT("ac3"), false))) { errorMessage.Printf(_("\"%s\" is a Dolby Digital audio file. \nAudacity cannot currently open this type of file. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } // Speex files if ((extension.IsSameAs(wxT("spx"), false))) { errorMessage.Printf(_("\"%s\" is an Ogg Speex audio file. \nAudacity cannot currently open this type of file. \nYou need to convert it to a supported audio format, such as WAV or AIFF."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } // Video files of various forms if ((extension.IsSameAs(wxT("mpg"), false))||(extension.IsSameAs(wxT("mpeg"), false))||(extension.IsSameAs(wxT("avi"), false))||(extension.IsSameAs(wxT("wmv"), false))||(extension.IsSameAs(wxT("rv"), false))) { errorMessage.Printf(_("\"%s\" is a video file. \nAudacity cannot currently open this type of file. \nYou need to extract the audio to a supported format, such as WAV or AIFF."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } // Audacity project if (extension.IsSameAs(wxT("aup"), false)) { errorMessage.Printf(_("\"%s\" is an Audacity Project file. \nUse the 'File > Open' command to open Audacity Projects."), fName.c_str()); - pProj->mbBusyImporting = false; return false; } @@ -685,7 +666,6 @@ bool Importer::Import(const wxString &fName, errorMessage.Printf(_("Audacity recognized the type of the file '%s'.\nImporters supposedly supporting such files are:\n%s,\nbut none of them understood this file format."),fName.c_str(), pluglist.c_str()); } - pProj->mbBusyImporting = false; return false; }