From 44fde0a90fcac30db3f03c002628be0e2475a1a0 Mon Sep 17 00:00:00 2001 From: Mike Barker Date: Mon, 21 Jan 2019 15:01:08 +0000 Subject: [PATCH] Bug 1899 - Windows: previously deleted folders are unnecessarily re-created on next Save --- src/FileNames.cpp | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/FileNames.cpp b/src/FileNames.cpp index 27bbd57dd..49cd44fe5 100644 --- a/src/FileNames.cpp +++ b/src/FileNames.cpp @@ -370,20 +370,39 @@ FilePath FileNames::PathFromAddr(void *addr) return name.GetFullPath(); } -wxFileNameWrapper FileNames::DefaultToDocumentsFolder -(const wxString &preference) +wxFileNameWrapper FileNames::DefaultToDocumentsFolder(const wxString &preference) { wxFileNameWrapper result; - result.AssignHomeDir(); #ifdef __WIN32__ - result.SetPath(gPrefs->Read( - preference, result.GetPath(wxPATH_GET_VOLUME) + "\\Documents\\Audacity")); - // The path might not exist. - // There is no error if the path could not be created. That's OK. - // The dialog that Audacity offers will allow the user to select a valid directory. - result.Mkdir(0755, wxPATH_MKDIR_FULL); + wxFileName defaultPath( wxStandardPaths::Get().GetDocumentsDir(), "" ); + defaultPath.AppendDir( wxGetApp().GetAppName() ); + result.SetPath( gPrefs->Read( preference, defaultPath.GetPath( wxPATH_GET_VOLUME ) ) ); + + // MJB: Bug 1899 & Bug 2007. Only create directory if the result is the default path + bool bIsDefaultPath = result == defaultPath; + if( !bIsDefaultPath ) + { + // IF the prefs directory doesn't exist - (Deleted by our user perhaps?) + // or exists as a file + // THEN fallback to using the default directory. + bIsDefaultPath = !result.DirExists() || result.FileExists(); + if( bIsDefaultPath ) + { + result.SetPath( defaultPath.GetPath( wxPATH_GET_VOLUME ) ); + gPrefs->Write( preference, defaultPath.GetPath( wxPATH_GET_VOLUME ) ); + gPrefs->Flush(); + } + } + if ( bIsDefaultPath ) + { + // The default path might not exist since it is a sub-directory of 'Documents' + // There is no error if the path could not be created. That's OK. + // The dialog that Audacity offers will allow the user to select a valid directory. + result.Mkdir(0755, wxPATH_MKDIR_FULL); + } #else + result.AssignHomeDir(); result.SetPath(gPrefs->Read( preference, result.GetPath() + "/Documents")); #endif