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) {
int n = event.GetId() - FileHistory::ID_RECENT_FIRST;
auto &history = FileHistory::Global();
const auto &fullPathStr = history.GetHistoryFile(n);
const auto &fullPathStr = history[ n ];
// Try to open only if not already open.
// 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.
// that method does not return the bad result.
// PRL: Don't call SafeMRUOpen
// -- if open fails for some exceptional reason of resource exhaustion that
// the user can correct, leave the file in history.
if (!ProjectFileManager::IsAlreadyOpen(fullPathStr) && !MRUOpen(fullPathStr))
history.RemoveFileFromHistory(n);
history.Remove(n);
}
void AudacityApp::OnTimer(wxTimerEvent& WXUNUSED(event))
@ -1415,7 +1415,6 @@ bool AudacityApp::OnInit()
auto &recentFiles = FileHistory::Global();
recentFiles.UseMenu(recentMenu);
recentFiles.AddFilesToMenu(recentMenu);
SetExitOnFrameDelete(false);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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