diff --git a/src/AudacityApp.cpp b/src/AudacityApp.cpp index 78764f69f..c48a71e19 100644 --- a/src/AudacityApp.cpp +++ b/src/AudacityApp.cpp @@ -1279,10 +1279,12 @@ bool AudacityApp::OnInit() if (!envTempDir.empty()) { /* On Unix systems, the environment variable TMPDIR may point to an unusual path when /tmp and /var/tmp are not desirable. */ - defaultTempDir.Printf(wxT("%s/audacity-%s"), envTempDir, wxGetUserId()); + FileNames::SetDefaultTempDir( wxString::Format( + wxT("%s/audacity-%s"), envTempDir, wxGetUserId() ) ); } else { /* On Unix systems, the default temp dir is in /var/tmp. */ - defaultTempDir.Printf(wxT("/var/tmp/audacity-%s"), wxGetUserId()); + FileNames::SetDefaultTempDir( wxString::Format( + wxT("/var/tmp/audacity-%s"), wxGetUserId() ) ); } // DA: Path env variable. @@ -1349,8 +1351,8 @@ bool AudacityApp::OnInit() // See bug #1271 for explanation of location tmpDirLoc = FileNames::MkDir(wxStandardPaths::Get().GetUserLocalDataDir()); - defaultTempDir.Printf(wxT("%s\\SessionData"), - tmpDirLoc); + FileNames::SetDefaultTempDir( wxString::Format( + wxT("%s\\SessionData"), tmpDirLoc ) ); #endif //__WXWSW__ #ifdef __WXMAC__ @@ -1368,12 +1370,13 @@ bool AudacityApp::OnInit() // JKC Bug 1220: Using an actual temp directory for session data on Mac was // wrong because it would get cleared out on a reboot. - defaultTempDir.Printf(wxT("%s/Library/Application Support/audacity/SessionData"), - tmpDirLoc); + FileNames::SetDefaultTempDir( wxString::Format( + wxT("%s/Library/Application Support/audacity/SessionData"), tmpDirLoc) ); - //defaultTempDir.Printf(wxT("%s/audacity-%s"), + //FileNames::SetDefaultTempDir( wxString::Format( + // wxT("%s/audacity-%s"), // tmpDirLoc, - // wxGetUserId()); + // wxGetUserId() ) ); #endif //__WXMAC__ // Define languanges for which we have translations, but that are not yet @@ -1675,36 +1678,6 @@ void AudacityApp::OnKeyDown(wxKeyEvent &event) event.Skip(); } -// We now disallow temp directory name that puts it where cleaner apps will -// try to clean out the files. -bool AudacityApp::IsTempDirectoryNameOK( const wxString & Name ){ - if( Name.empty() ) - return false; - - wxFileName tmpFile; - tmpFile.AssignTempFileName(wxT("nn")); - // use Long Path to expand out any abbreviated long substrings. - wxString BadPath = tmpFile.GetLongPath(); - ::wxRemoveFile(tmpFile.GetFullPath()); - -#ifdef __WXMAC__ - // This test is to fix bug 1220 on a 1.x to 2.x to 2.1.3 upgrade. - // It is less permissive than we could be as it stops a path - // with this string ANYWHERE within it rather than excluding just - // the paths that the earlier Audacities used to create. - if( Name.Contains( "/tmp/") ) - return false; - BadPath = BadPath.BeforeLast( '/' ) + "/"; - wxFileName cmpFile( Name ); - wxString NameCanonical = cmpFile.GetLongPath( ) + "/"; -#else - BadPath = BadPath.BeforeLast( '\\' ) + "\\"; - wxFileName cmpFile( Name ); - wxString NameCanonical = cmpFile.GetLongPath( ) + "\\"; -#endif - return !(NameCanonical.StartsWith( BadPath )); -} - // Ensures directory is created and puts the name into result. // result is unchanged if unsuccessful. void SetToExtantDirectory( wxString & result, const wxString & dir ){ @@ -1726,7 +1699,7 @@ bool AudacityApp::InitTempDir() // We need to find a temp directory location. wxString tempFromPrefs = gPrefs->Read(wxT("/Directories/TempDir"), wxT("")); - wxString tempDefaultLoc = wxGetApp().defaultTempDir; + auto tempDefaultLoc = FileNames::DefaultTempDir(); wxString temp; @@ -1740,7 +1713,7 @@ bool AudacityApp::InitTempDir() wxLogNull logNo; // Try temp dir that was stored in prefs first - if( IsTempDirectoryNameOK( tempFromPrefs ) ) + if( FileNames::IsTempDirectoryNameOK( tempFromPrefs ) ) SetToExtantDirectory( temp, tempFromPrefs ); // If that didn't work, try the default location @@ -1763,7 +1736,7 @@ bool AudacityApp::InitTempDir() if (temp.empty()) { // Failed - if( !IsTempDirectoryNameOK( tempFromPrefs ) ) { + if( !FileNames::IsTempDirectoryNameOK( tempFromPrefs ) ) { AudacityMessageBox(_("Audacity could not find a safe place to store temporary files.\nAudacity needs a place where automatic cleanup programs won't delete the temporary files.\nPlease enter an appropriate directory in the preferences dialog.")); } else { AudacityMessageBox(_("Audacity could not find a place to store temporary files.\nPlease enter an appropriate directory in the preferences dialog.")); diff --git a/src/AudacityApp.h b/src/AudacityApp.h index 3f59f00e4..b0c91f44a 100644 --- a/src/AudacityApp.h +++ b/src/AudacityApp.h @@ -123,9 +123,6 @@ class AudacityApp final : public wxApp { * which contains the Audacity program. */ FilePaths audacityPathList; - /** \brief Default temp directory */ - wxString defaultTempDir; - // Useful functions for working with search paths static void AddUniquePathToPathList(const FilePath &path, FilePaths &pathList); @@ -135,7 +132,6 @@ class AudacityApp final : public wxApp { const FilePaths & pathList, FilePaths &results, int flags = wxDIR_FILES); - static bool IsTempDirectoryNameOK( const wxString & Name ); bool GetWindowRectAlreadySaved()const {return mWindowRectAlreadySaved;} void SetWindowRectAlreadySaved(bool alreadySaved) {mWindowRectAlreadySaved = alreadySaved;} diff --git a/src/FileNames.cpp b/src/FileNames.cpp index 43ac3c7d1..9b197a6a6 100644 --- a/src/FileNames.cpp +++ b/src/FileNames.cpp @@ -475,3 +475,47 @@ FileNames::SelectFile(Operation op, wildcard, flags, parent, wxDefaultCoord, wxDefaultCoord); }); } + +/** \brief Default temp directory */ +static FilePath sDefaultTempDir; + +const FilePath &FileNames::DefaultTempDir() +{ + return sDefaultTempDir; +} + +void FileNames::SetDefaultTempDir( const FilePath &tempDir ) +{ + sDefaultTempDir = tempDir; +} + +// We now disallow temp directory name that puts it where cleaner apps will +// try to clean out the files. +bool FileNames::IsTempDirectoryNameOK( const FilePath & Name ) +{ + if( Name.empty() ) + return false; + + wxFileName tmpFile; + tmpFile.AssignTempFileName(wxT("nn")); + // use Long Path to expand out any abbreviated long substrings. + wxString BadPath = tmpFile.GetLongPath(); + ::wxRemoveFile(tmpFile.GetFullPath()); + +#ifdef __WXMAC__ + // This test is to fix bug 1220 on a 1.x to 2.x to 2.1.3 upgrade. + // It is less permissive than we could be as it stops a path + // with this string ANYWHERE within it rather than excluding just + // the paths that the earlier Audacities used to create. + if( Name.Contains( "/tmp/") ) + return false; + BadPath = BadPath.BeforeLast( '/' ) + "/"; + wxFileName cmpFile( Name ); + wxString NameCanonical = cmpFile.GetLongPath( ) + "/"; +#else + BadPath = BadPath.BeforeLast( '\\' ) + "\\"; + wxFileName cmpFile( Name ); + wxString NameCanonical = cmpFile.GetLongPath( ) + "\\"; +#endif + return !(NameCanonical.StartsWith( BadPath )); +} diff --git a/src/FileNames.h b/src/FileNames.h index d75e17b2f..16c7851cb 100644 --- a/src/FileNames.h +++ b/src/FileNames.h @@ -36,6 +36,10 @@ public: static wxString MkDir(const wxString &Str); static wxString TempDir(); + static const FilePath &DefaultTempDir(); + static void SetDefaultTempDir( const FilePath &tempDir ); + static bool IsTempDirectoryNameOK( const FilePath & Name ); + // originally an ExportMultiple method. Append suffix if newName appears in otherNames. static void MakeNameUnique( FilePaths &otherNames, wxFileName &newName); diff --git a/src/prefs/DirectoriesPrefs.cpp b/src/prefs/DirectoriesPrefs.cpp index fead3c0a2..7f7a746bb 100644 --- a/src/prefs/DirectoriesPrefs.cpp +++ b/src/prefs/DirectoriesPrefs.cpp @@ -32,8 +32,8 @@ #include #include +#include "../FileNames.h" #include "../Prefs.h" -#include "../AudacityApp.h" #include "../ShuttleGui.h" #include "../widgets/ErrorDialog.h" @@ -147,14 +147,15 @@ void DirectoriesPrefs::PopulateOrExchange(ShuttleGui & S) void DirectoriesPrefs::OnChooseTempDir(wxCommandEvent & e) { - wxString oldTempDir = gPrefs->Read(wxT("/Directories/TempDir"), wxGetApp().defaultTempDir); + wxString oldTempDir = + gPrefs->Read(wxT("/Directories/TempDir"), FileNames::DefaultTempDir()); // Because we went through InitTempDir() during initialisation, // the old temp directory name in prefs should already be OK. Just in case there is // some way we hadn't thought of for it to be not OK, // we avoid prompting with it in that case and use the suggested default instead. - if( !AudacityApp::IsTempDirectoryNameOK( oldTempDir ) ) - oldTempDir = wxGetApp().defaultTempDir; + if( !FileNames::IsTempDirectoryNameOK( oldTempDir ) ) + oldTempDir = FileNames::DefaultTempDir(); wxDirDialogWrapper dlog(this, _("Choose a location to place the temporary directory"), @@ -182,7 +183,7 @@ void DirectoriesPrefs::OnChooseTempDir(wxCommandEvent & e) // If the default temp dir or user's pref dir don't end in '/' they cause // wxFileName's == operator to construct a wxFileName representing a file // (that doesn't exist) -- hence the constructor calls - if (tmpDirPath != wxFileName(wxGetApp().defaultTempDir, wxT("")) && + if (tmpDirPath != wxFileName(FileNames::DefaultTempDir(), wxT("")) && tmpDirPath != wxFileName(mTempDir->GetValue(), wxT("")) && (dirsInPath.size() == 0 || dirsInPath[dirsInPath.size()-1] != newDirName)) @@ -225,7 +226,7 @@ bool DirectoriesPrefs::Validate() tempDir.SetPath(mTempDir->GetValue()); wxString path{tempDir.GetPath()}; - if( !AudacityApp::IsTempDirectoryNameOK( path ) ) { + if( !FileNames::IsTempDirectoryNameOK( path ) ) { AudacityMessageBox( wxString::Format(_("Directory %s is not suitable (at risk of being cleaned out)"), path),