1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-10-13 22:21:11 +02:00

Simplify public interface of FileHistory

This commit is contained in:
Paul Licameli
2020-05-01 13:07:06 -04:00
parent 3313b33050
commit 66c5b76573
7 changed files with 48 additions and 65 deletions

View File

@@ -863,18 +863,18 @@ void AudacityApp::OnMRUClear(wxCommandEvent& WXUNUSED(event))
void AudacityApp::OnMRUFile(wxCommandEvent& event) { void AudacityApp::OnMRUFile(wxCommandEvent& event) {
int n = event.GetId() - FileHistory::ID_RECENT_FIRST; int n = event.GetId() - FileHistory::ID_RECENT_FIRST;
auto &history = FileHistory::Global(); auto &history = FileHistory::Global();
const auto &fullPathStr = history.GetHistoryFile(n); const auto &fullPathStr = history[ n ];
// Try to open only if not already open. // Try to open only if not already open.
// Test IsAlreadyOpen() here even though AudacityProject::MRUOpen() also now checks, // Test IsAlreadyOpen() here even though AudacityProject::MRUOpen() also now checks,
// because we don't want to RemoveFileFromHistory() just because it already exists, // because we don't want to Remove() just because it already exists,
// and AudacityApp::OnMacOpenFile() calls MRUOpen() directly. // and AudacityApp::OnMacOpenFile() calls MRUOpen() directly.
// that method does not return the bad result. // that method does not return the bad result.
// PRL: Don't call SafeMRUOpen // PRL: Don't call SafeMRUOpen
// -- if open fails for some exceptional reason of resource exhaustion that // -- if open fails for some exceptional reason of resource exhaustion that
// the user can correct, leave the file in history. // the user can correct, leave the file in history.
if (!ProjectFileManager::IsAlreadyOpen(fullPathStr) && !MRUOpen(fullPathStr)) if (!ProjectFileManager::IsAlreadyOpen(fullPathStr) && !MRUOpen(fullPathStr))
history.RemoveFileFromHistory(n); history.Remove(n);
} }
void AudacityApp::OnTimer(wxTimerEvent& WXUNUSED(event)) void AudacityApp::OnTimer(wxTimerEvent& WXUNUSED(event))
@@ -1415,7 +1415,6 @@ bool AudacityApp::OnInit()
auto &recentFiles = FileHistory::Global(); auto &recentFiles = FileHistory::Global();
recentFiles.UseMenu(recentMenu); recentFiles.UseMenu(recentMenu);
recentFiles.AddFilesToMenu(recentMenu);
SetExitOnFrameDelete(false); SetExitOnFrameDelete(false);

View File

@@ -778,7 +778,7 @@ bool ProjectFileManager::SaveAs(const wxString & newFileName, bool bWantSaveCopy
success = DoSave(!bOwnsNewAupName || bWantSaveCopy, bWantSaveCopy); success = DoSave(!bOwnsNewAupName || bWantSaveCopy, bWantSaveCopy);
if (success && addToHistory) { if (success && addToHistory) {
FileHistory::Global().AddFileToHistory( project.GetFileName() ); FileHistory::Global().Append( project.GetFileName() );
} }
if (!success || bWantSaveCopy) // bWantSaveCopy doesn't actually change current project. if (!success || bWantSaveCopy) // bWantSaveCopy doesn't actually change current project.
{ {
@@ -954,7 +954,7 @@ will be irreversibly overwritten.").Format( fName, fName );
success = DoSave(!bOwnsNewAupName || bWantSaveCopy, bWantSaveCopy, bLossless); success = DoSave(!bOwnsNewAupName || bWantSaveCopy, bWantSaveCopy, bLossless);
if (success) { if (success) {
FileHistory::Global().AddFileToHistory( project.GetFileName() ); FileHistory::Global().Append( project.GetFileName() );
if( !bHasPath ) if( !bHasPath )
{ {
gPrefs->Write( wxT("/SaveAs/Path"), filename.GetPath()); gPrefs->Write( wxT("/SaveAs/Path"), filename.GetPath());
@@ -1020,7 +1020,7 @@ bool ProjectFileManager::SaveFromTimerRecording(wxFileName fnFile)
bSuccess = DoSave(true, false); bSuccess = DoSave(true, false);
if (bSuccess) { if (bSuccess) {
FileHistory::Global().AddFileToHistory( project.GetFileName() ); FileHistory::Global().Append( project.GetFileName() );
projectFileIO.SetLoadedFromAup( true ); projectFileIO.SetLoadedFromAup( true );
projectFileIO.SetProjectTitle(); projectFileIO.SetProjectTitle();
} }
@@ -1385,7 +1385,7 @@ void ProjectFileManager::OpenFile(const FilePath &fileNameArg, bool addtohistory
// finished logging errors (if any) before the call to ProjectFSCK() // finished logging errors (if any) before the call to ProjectFSCK()
if (addtohistory) if (addtohistory)
FileHistory::Global().AddFileToHistory(fileName); FileHistory::Global().Append(fileName);
} }
// Use a finally block here, because there are calls to Save() below which // Use a finally block here, because there are calls to Save() below which
@@ -1652,7 +1652,7 @@ bool ProjectFileManager::Import(
if (!success) if (!success)
return false; return false;
FileHistory::Global().AddFileToHistory(fileName); FileHistory::Global().Append(fileName);
// no more errors, commit // no more errors, commit
committed = true; committed = true;

View File

@@ -85,16 +85,15 @@ ExportCLOptions::ExportCLOptions(wxWindow *parent, int WXUNUSED(format))
{ {
mHistory.Load(*gPrefs, wxT("/FileFormats/ExternalProgramHistory")); mHistory.Load(*gPrefs, wxT("/FileFormats/ExternalProgramHistory"));
if (mHistory.GetCount() == 0) { if (mHistory.empty()) {
mHistory.AddFileToHistory(wxT("ffmpeg -i - \"%f.opus\""), false); mHistory.Append(wxT("ffmpeg -i - \"%f.opus\""));
mHistory.AddFileToHistory(wxT("ffmpeg -i - \"%f.wav\""), false); mHistory.Append(wxT("ffmpeg -i - \"%f.wav\""));
mHistory.AddFileToHistory(wxT("ffmpeg -i - \"%f\""), false); mHistory.Append(wxT("ffmpeg -i - \"%f\""));
mHistory.AddFileToHistory(wxT("lame - \"%f\""), false); mHistory.Append(wxT("lame - \"%f\""));
} }
mHistory.AddFileToHistory(gPrefs->Read(wxT("/FileFormats/ExternalProgramExportCommand"), mHistory.Append(gPrefs->Read(wxT("/FileFormats/ExternalProgramExportCommand"),
mHistory.GetHistoryFile(0)), mHistory[ 0 ]));
false);
ShuttleGui S(this, eIsCreatingFromPrefs); ShuttleGui S(this, eIsCreatingFromPrefs);
PopulateOrExchange(S); PopulateOrExchange(S);
@@ -113,14 +112,8 @@ ExportCLOptions::~ExportCLOptions()
/// ///
void ExportCLOptions::PopulateOrExchange(ShuttleGui & S) void ExportCLOptions::PopulateOrExchange(ShuttleGui & S)
{ {
wxArrayStringEx cmds; wxArrayStringEx cmds( mHistory.begin(), mHistory.end() );
wxString cmd; auto cmd = cmds[0];
for (size_t i = 0; i < mHistory.GetCount(); i++) {
cmd = mHistory.GetHistoryFile(i);
cmds.push_back(mHistory.GetHistoryFile(i));
}
cmd = cmds[0];
S.StartVerticalLay(); S.StartVerticalLay();
{ {
@@ -171,7 +164,7 @@ bool ExportCLOptions::TransferDataFromWindow()
wxString cmd = mCmd->GetValue(); wxString cmd = mCmd->GetValue();
mHistory.AddFileToHistory(cmd, false); mHistory.Append(cmd);
mHistory.Save(*gPrefs, wxT("/FileFormats/ExternalProgramHistory")); mHistory.Save(*gPrefs, wxT("/FileFormats/ExternalProgramHistory"));
gPrefs->Write(wxT("/FileFormats/ExternalProgramExportCommand"), cmd); gPrefs->Write(wxT("/FileFormats/ExternalProgramExportCommand"), cmd);

View File

@@ -49,7 +49,7 @@ bool DoImportMIDI( AudacityProject &project, const FilePath &fileName )
); );
ProjectWindow::Get( project ).ZoomAfterImport(pTrack); ProjectWindow::Get( project ).ZoomAfterImport(pTrack);
FileHistory::Global().AddFileToHistory(fileName); FileHistory::Global().Append(fileName);
return true; return true;
} }
else else

View File

@@ -89,7 +89,7 @@ void DoExport( AudacityProject &project, const FileExtension & Format )
} }
else else
{ {
FileHistory::Global().AddFileToHistory(filename); FileHistory::Global().Append(filename);
// We're in batch mode, the file does not exist already. // We're in batch mode, the file does not exist already.
// We really can proceed without prompting. // We really can proceed without prompting.
int nChannels = MacroCommands::IsMono( &project ) ? 1 : 2; int nChannels = MacroCommands::IsMono( &project ) ? 1 : 2;
@@ -595,7 +595,6 @@ BaseItemSharedPtr FileMenu()
// Recent Files and Recent Projects menus // Recent Files and Recent Projects menus
auto &history = FileHistory::Global(); auto &history = FileHistory::Global();
history.UseMenu( &theMenu ); history.UseMenu( &theMenu );
history.AddFilesToMenu( &theMenu );
wxWeakRef<wxMenu> recentFilesMenu{ &theMenu }; wxWeakRef<wxMenu> recentFilesMenu{ &theMenu };
wxTheApp->CallAfter( [=] { wxTheApp->CallAfter( [=] {

View File

@@ -73,21 +73,18 @@ void FileHistory::AddFileToHistory(const FilePath & file, bool update)
mHistory.insert(mHistory.begin(), file); mHistory.insert(mHistory.begin(), file);
if (update) { if (update)
AddFilesToMenu(); NotifyMenus();
}
} }
void FileHistory::RemoveFileFromHistory(size_t i, bool update) void FileHistory::Remove( size_t i )
{ {
wxASSERT(i < mHistory.size()); wxASSERT(i < mHistory.size());
if (i < mHistory.size()) { if (i < mHistory.size()) {
mHistory.erase( mHistory.begin() + i ); mHistory.erase( mHistory.begin() + i );
if (update) { NotifyMenus();
AddFilesToMenu();
}
} }
} }
@@ -95,24 +92,7 @@ void FileHistory::Clear()
{ {
mHistory.clear(); mHistory.clear();
AddFilesToMenu(); NotifyMenus();
}
const FilePath &FileHistory::GetHistoryFile(size_t i) const
{
wxASSERT(i < mHistory.size());
if (i < mHistory.size()) {
return mHistory[i];
}
static const FilePath empty{};
return empty;
}
size_t FileHistory::GetCount()
{
return mHistory.size();
} }
void FileHistory::UseMenu(wxMenu *menu) void FileHistory::UseMenu(wxMenu *menu)
@@ -128,6 +108,8 @@ void FileHistory::UseMenu(wxMenu *menu)
else { else {
wxASSERT(false); wxASSERT(false);
} }
NotifyMenu( menu );
} }
void FileHistory::Load(wxConfigBase & config, const wxString & group) void FileHistory::Load(wxConfigBase & config, const wxString & group)
@@ -146,7 +128,7 @@ void FileHistory::Load(wxConfigBase & config, const wxString & group)
config.SetPath(wxT("..")); config.SetPath(wxT(".."));
AddFilesToMenu(); NotifyMenus();
} }
void FileHistory::Save(wxConfigBase & config, const wxString & group) void FileHistory::Save(wxConfigBase & config, const wxString & group)
@@ -164,15 +146,15 @@ void FileHistory::Save(wxConfigBase & config, const wxString & group)
config.SetPath(wxT("")); config.SetPath(wxT(""));
} }
void FileHistory::AddFilesToMenu() void FileHistory::NotifyMenus()
{ {
Compress(); Compress();
for (auto pMenu : mMenus) for (auto pMenu : mMenus)
if (pMenu) if (pMenu)
AddFilesToMenu(pMenu); NotifyMenu(pMenu);
} }
void FileHistory::AddFilesToMenu(wxMenu *menu) void FileHistory::NotifyMenu(wxMenu *menu)
{ {
wxMenuItemList items = menu->GetMenuItems(); wxMenuItemList items = menu->GetMenuItems();
for (auto end = items.end(), iter = items.begin(); iter != end;) for (auto end = items.end(), iter = items.begin(); iter != end;)

View File

@@ -37,20 +37,30 @@ class AUDACITY_DLL_API FileHistory
static FileHistory &Global(); static FileHistory &Global();
void AddFileToHistory(const FilePath & file, bool update = true); void Append( const FilePath &file )
void RemoveFileFromHistory(size_t i, bool update = true); { AddFileToHistory( file, true ); }
void Remove( size_t i );
void Clear(); void Clear();
// Causes this menu to reflect the contents of this FileHistory, now and
// also whenever the history changes.
void UseMenu(wxMenu *menu); void UseMenu(wxMenu *menu);
void Load(wxConfigBase& config, const wxString & group); void Load(wxConfigBase& config, const wxString & group);
void Save(wxConfigBase& config, const wxString & group); void Save(wxConfigBase& config, const wxString & group);
void AddFilesToMenu(); // stl-style accessors
void AddFilesToMenu(wxMenu *menu); using const_iterator = FilePaths::const_iterator;
const_iterator begin() const { return mHistory.begin(); }
size_t GetCount(); const_iterator end() const { return mHistory.end(); }
const FilePath &GetHistoryFile(size_t i) const; const FilePath &operator[] ( size_t ii ) const { return mHistory[ ii ]; }
bool empty() const { return mHistory.empty(); }
private: private:
void AddFileToHistory(const FilePath & file, bool update);
void NotifyMenus();
void NotifyMenu(wxMenu *menu);
void Compress(); void Compress();
size_t mMaxFiles; size_t mMaxFiles;