diff --git a/src/BatchCommands.cpp b/src/BatchCommands.cpp index 930abdeca..ed16d34d4 100644 --- a/src/BatchCommands.cpp +++ b/src/BatchCommands.cpp @@ -328,7 +328,7 @@ wxString BatchCommands::PromptForParamsFor(const wxString & command, const wxStr wxString res = params; - EffectManager::Get().SetBatchProcessing(ID, true); + auto cleanup = EffectManager::Get().SetBatchProcessing(ID); if (EffectManager::Get().SetEffectParameters(ID, params)) { @@ -338,8 +338,6 @@ wxString BatchCommands::PromptForParamsFor(const wxString & command, const wxStr } } - EffectManager::Get().SetBatchProcessing(ID, false); - return res; } @@ -606,7 +604,7 @@ bool BatchCommands::ApplyEffectCommand(const PluginID & ID, const wxString & com bool res = false; - EffectManager::Get().SetBatchProcessing(ID, true); + auto cleanup = EffectManager::Get().SetBatchProcessing(ID); // transfer the parameters to the effect... if (EffectManager::Get().SetEffectParameters(ID, params)) @@ -617,8 +615,6 @@ bool BatchCommands::ApplyEffectCommand(const PluginID & ID, const wxString & com AudacityProject::OnEffectFlags::kDontRepeatLast); } - EffectManager::Get().SetBatchProcessing(ID, false); - return res; } @@ -652,17 +648,15 @@ bool BatchCommands::ApplyCommand(const wxString & command, const wxString & para bool BatchCommands::ApplyCommandInBatchMode(const wxString & command, const wxString ¶ms) { AudacityProject *project = GetActiveProject(); - bool rc; // enter batch mode... bool prevShowMode = project->GetShowId3Dialog(); + auto cleanup = finally( [&] { + // exit batch mode... + project->SetShowId3Dialog(prevShowMode); + } ); - rc = ApplyCommand( command, params ); - - // exit batch mode... - project->SetShowId3Dialog(prevShowMode); - - return rc; + return ApplyCommand( command, params ); } // ApplyChain returns true on success, false otherwise. @@ -670,29 +664,31 @@ bool BatchCommands::ApplyCommandInBatchMode(const wxString & command, const wxSt bool BatchCommands::ApplyChain(const wxString & filename) { mFileName = filename; - unsigned int i; - bool res = true; + + AudacityProject *proj = GetActiveProject(); + bool res = false; + auto cleanup = finally( [&] { + if (!res) { + if(proj) { + // Chain failed or was cancelled; revert to the previous state + proj->RollbackState(); + } + } + } ); mAbort = false; - for (i = 0; i < mCommandChain.GetCount(); i++) { - if (!ApplyCommandInBatchMode(mCommandChain[i], mParamsChain[i]) || mAbort) { - res = false; + size_t i = 0; + for (; i < mCommandChain.GetCount(); i++) { + if (!ApplyCommandInBatchMode(mCommandChain[i], mParamsChain[i]) || mAbort) break; - } } + res = (i == mCommandChain.GetCount()); + if (!res) + return false; + mFileName.Empty(); - AudacityProject *proj = GetActiveProject(); - - if (!res) - { - if(proj) { - // Chain failed or was cancelled; revert to the previous state - proj->RollbackState(); - } - return false; - } // Chain was successfully applied; save the NEW project state wxString longDesc, shortDesc; diff --git a/src/effects/EffectManager.h b/src/effects/EffectManager.h index eb35ceb81..6a7c32f58 100644 --- a/src/effects/EffectManager.h +++ b/src/effects/EffectManager.h @@ -91,7 +91,22 @@ public: bool HasPresets(const PluginID & ID); wxString GetPreset(const PluginID & ID, const wxString & params, wxWindow * parent); wxString GetDefaultPreset(const PluginID & ID); + +private: void SetBatchProcessing(const PluginID & ID, bool start); + struct UnsetBatchProcessing { + PluginID mID; + void operator () (EffectManager *p) const + { if(p) p->SetBatchProcessing(mID, false); } + }; + using BatchProcessingScope = + std::unique_ptr< EffectManager, UnsetBatchProcessing >; +public: + // RAII for the function above + BatchProcessingScope SetBatchProcessing(const PluginID &ID) + { + SetBatchProcessing(ID, true); return BatchProcessingScope{ this, {ID} }; + } /** Allow effects to disable saving the state at run time */ void SetSkipStateFlag(bool flag);