Break dependency cycle introduced at 50f3321...

... This only cuts and pastes some functions from FileNames to another new file
and namespace, and changes namespace qualifiers where they are called.

The cycle resulted because of the need to include widgets/ErrorDialog.h
This commit is contained in:
Paul Licameli 2021-01-14 10:50:06 -05:00
parent 086cacad76
commit 43574553fc
13 changed files with 202 additions and 161 deletions

View File

@ -94,11 +94,11 @@ It handles initialization and termination by subclassing wxApp.
#include "ProjectWindow.h"
#include "Screenshot.h"
#include "Sequence.h"
#include "TempDirectory.h"
#include "Track.h"
#include "prefs/PrefsDialog.h"
#include "Theme.h"
#include "PlatformCompatibility.h"
#include "FileNames.h"
#include "AutoRecoveryDialog.h"
#include "SplashDialog.h"
#include "FFT.h"
@ -1122,11 +1122,11 @@ 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. */
FileNames::SetDefaultTempDir( wxString::Format(
TempDirectory::SetDefaultTempDir( wxString::Format(
wxT("%s/audacity-%s"), envTempDir, wxGetUserId() ) );
} else {
/* On Unix systems, the default temp dir is in /var/tmp. */
FileNames::SetDefaultTempDir( wxString::Format(
TempDirectory::SetDefaultTempDir( wxString::Format(
wxT("/var/tmp/audacity-%s"), wxGetUserId() ) );
}
@ -1199,7 +1199,7 @@ bool AudacityApp::OnInit()
// See bug #1271 for explanation of location
tmpDirLoc = FileNames::MkDir(wxStandardPaths::Get().GetUserLocalDataDir());
FileNames::SetDefaultTempDir( wxString::Format(
TempDirectory::SetDefaultTempDir( wxString::Format(
wxT("%s\\SessionData"), tmpDirLoc ) );
#endif //__WXWSW__
@ -1220,10 +1220,10 @@ 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.
FileNames::SetDefaultTempDir( wxString::Format(
TempDirectory::SetDefaultTempDir( wxString::Format(
wxT("%s/Library/Application Support/audacity/SessionData"), tmpDirLoc) );
//FileNames::SetDefaultTempDir( wxString::Format(
//TempDirectory::SetDefaultTempDir( wxString::Format(
// wxT("%s/audacity-%s"),
// tmpDirLoc,
// wxGetUserId() ) );
@ -1614,8 +1614,8 @@ void SetToExtantDirectory( wxString & result, const wxString & dir ){
bool AudacityApp::InitTempDir()
{
// We need to find a temp directory location.
auto tempFromPrefs = FileNames::TempDir();
auto tempDefaultLoc = FileNames::DefaultTempDir();
auto tempFromPrefs = TempDirectory::TempDir();
auto tempDefaultLoc = TempDirectory::DefaultTempDir();
wxString temp;
@ -1629,7 +1629,7 @@ bool AudacityApp::InitTempDir()
wxLogNull logNo;
// Try temp dir that was stored in prefs first
if( FileNames::IsTempDirectoryNameOK( tempFromPrefs ) )
if( TempDirectory::IsTempDirectoryNameOK( tempFromPrefs ) )
SetToExtantDirectory( temp, tempFromPrefs );
// If that didn't work, try the default location
@ -1652,7 +1652,7 @@ bool AudacityApp::InitTempDir()
if (temp.empty()) {
// Failed
if( !FileNames::IsTempDirectoryNameOK( tempFromPrefs ) ) {
if( !TempDirectory::IsTempDirectoryNameOK( tempFromPrefs ) ) {
AudacityMessageBox(XO(
"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 {
@ -1677,7 +1677,7 @@ bool AudacityApp::InitTempDir()
chmod(OSFILENAME(temp), 0755);
#endif
FileNames::ResetTempDir();
TempDirectory::ResetTempDir();
FileNames::UpdateDefaultPath(FileNames::Operation::Temp, temp);
return true;

View File

@ -11,11 +11,11 @@ Paul Licameli split from AutoRecovery.cpp
#include "AutoRecoveryDialog.h"
#include "ActiveProjects.h"
#include "FileNames.h"
#include "ProjectManager.h"
#include "ProjectFileIO.h"
#include "ProjectFileManager.h"
#include "ShuttleGui.h"
#include "TempDirectory.h"
#include "widgets/AudacityMessageBox.h"
#include "widgets/wxPanelWrapper.h"
@ -146,7 +146,7 @@ void AutoRecoveryDialog::PopulateOrExchange(ShuttleGui &S)
void AutoRecoveryDialog::PopulateList()
{
wxString tempdir = FileNames::TempDir();
wxString tempdir = TempDirectory::TempDir();
wxString pattern = wxT("*.") + FileNames::UnsavedProjectExtension();
FilePaths files;
@ -287,7 +287,7 @@ void AutoRecoveryDialog::OnDiscardSelected(wxCommandEvent &WXUNUSED(evt))
{
file.SetFullName(wxT(""));
wxFileName temp(FileNames::TempDir(), wxT(""));
wxFileName temp(TempDirectory::TempDir(), wxT(""));
if (file == temp)
ProjectFileIO::RemoveProject(fileName);
}

View File

@ -273,6 +273,8 @@ list( APPEND SOURCES
SseMathFuncs.h
Tags.cpp
Tags.h
TempDirectory.cpp
TempDirectory.h
Theme.cpp
Theme.h
ThemeAsCeeCode.h

View File

@ -37,7 +37,6 @@ used throughout Audacity into this one place.
#include "PlatformCompatibility.h"
#include "wxFileNameWrapper.h"
#include "widgets/AudacityMessageBox.h"
#include "widgets/ErrorDialog.h"
#include "widgets/FileDialog/FileDialog.h"
#if defined(__WXMAC__) || defined(__WXGTK__)
@ -197,45 +196,6 @@ wxString FileNames::MkDir(const wxString &Str)
return Str;
}
static wxString &TempDirPath()
{
static wxString path;
return path;
}
/// Returns the directory used for temp files.
/// \todo put a counter in here to see if it gets used a lot.
/// if it does, then maybe we should cache the path name
/// each time.
wxString FileNames::TempDir()
{
auto &path = TempDirPath();
if (gPrefs && path.empty())
path =
gPrefs->Read(PreferenceKey(Operation::Temp, PathType::_None), wxT(""));
if (IsOnFATFileSystem(path))
{
ShowErrorDialog(
nullptr,
XO("Unsuitable"),
XO("The temporary files directory is on a FAT formatted drive.\n"
"Resetting to default location."),
"Error:_Unsuitable_drive"
);
path = DefaultTempDir();
UpdateDefaultPath(FileNames::Operation::Temp, path);
}
return FileNames::MkDir(path);
}
void FileNames::ResetTempDir()
{
TempDirPath().clear();
}
// originally an ExportMultipleDialog method. Append suffix if newName appears in otherNames.
void FileNames::MakeNameUnique(FilePaths &otherNames,
wxFileName &newName)
@ -636,58 +596,6 @@ FileNames::SelectFile(Operation op,
});
}
/** \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
if (FileNames::FATFilesystemDenied(NameCanonical,
XO("The temporary files directory is on a FAT formatted drive.\n"
"Resetting to default location.")))
{
return false;
}
return !(NameCanonical.StartsWith( BadPath ));
}
bool FileNames::IsMidi(const FilePath &fName)
{
const auto extension = fName.AfterLast(wxT('.'));
@ -832,14 +740,6 @@ wxString FileNames::UnsavedProjectExtension()
return wxT("aup3unsaved");
}
wxString FileNames::UnsavedProjectFileName()
{
wxFileName fn(TempDir(),
CreateUniqueName(wxT("New Project"), UnsavedProjectExtension()));
return fn.GetFullPath();
}
// How to detect whether the file system of a path is FAT
// No apparent way to do it with wxWidgets
#if defined(__DARWIN__)
@ -891,25 +791,6 @@ bool FileNames::IsOnFATFileSystem(const FilePath &path)
}
#endif
bool FileNames::FATFilesystemDenied( const FilePath &path,
const TranslatableString &msg,
wxWindow *window /* = nullptr */ )
{
if (FileNames::IsOnFATFileSystem(path))
{
ShowErrorDialog(
window,
XO("Unsuitable"),
XO("%s\n\nFor tips on suitable drives, click the help button.").Format(msg),
"Error:_Unsuitable_drive"
);
return true;
}
return false;
}
wxString FileNames::AbbreviatePath( const wxFileName &fileName )
{
wxString target;

View File

@ -65,12 +65,6 @@ namespace FileNames
bool HardLinkFile( const FilePath& file1, const FilePath& file2);
wxString MkDir(const wxString &Str);
wxString TempDir();
void ResetTempDir();
const FilePath &DefaultTempDir();
void SetDefaultTempDir( const FilePath &tempDir );
bool IsTempDirectoryNameOK( const FilePath & Name );
bool IsMidi(const FilePath &fName);
@ -205,17 +199,11 @@ namespace FileNames
wxString CreateUniqueName(const wxString &prefix,
const wxString &suffix = wxEmptyString);
// Create a filename for an unsaved/temporary project file
wxString UnsavedProjectFileName();
// File extension used for unsaved/temporary project files
wxString UnsavedProjectExtension();
AUDACITY_DLL_API
bool IsOnFATFileSystem(const FilePath &path);
bool FATFilesystemDenied(const FilePath &path,
const TranslatableString &msg,
wxWindow *window = nullptr);
AUDACITY_DLL_API
//! Give enough of the path to identify the device. (On Windows, drive letter plus ':')

View File

@ -13,7 +13,7 @@
#include "Project.h"
#include "KeyboardCapture.h"
#include "FileNames.h"
#include "TempDirectory.h"
#include "./widgets/ErrorDialog.h"
#include <wx/display.h>
@ -129,7 +129,7 @@ AudacityProject::AudacityProject()
// Make sure there is plenty of space for Sqlite files
wxLongLong freeSpace = 0;
auto path = FileNames::TempDir();
auto path = TempDirectory::TempDir();
if (wxGetDiskSpace(path, NULL, &freeSpace)) {
if (freeSpace < wxLongLong(wxLL(100 * 1048576))) {
auto volume = FileNames::AbbreviatePath( path );

View File

@ -20,13 +20,13 @@ Paul Licameli split from AudacityProject.cpp
#include "ActiveProjects.h"
#include "DBConnection.h"
#include "FileNames.h"
#include "Project.h"
#include "ProjectFileIORegistry.h"
#include "ProjectSerializer.h"
#include "ProjectSettings.h"
#include "SampleBlock.h"
#include "Tags.h"
#include "TempDirectory.h"
#include "TimeTrack.h"
#include "ViewInfo.h"
#include "WaveTrack.h"
@ -309,7 +309,7 @@ bool ProjectFileIO::OpenConnection(FilePath fileName /* = {} */)
fileName = GetFileName();
if (fileName.empty())
{
fileName = FileNames::UnsavedProjectFileName();
fileName = TempDirectory::UnsavedProjectFileName();
isTemp = true;
}
}
@ -317,7 +317,7 @@ bool ProjectFileIO::OpenConnection(FilePath fileName /* = {} */)
{
// If this project resides in the temporary directory, then we'll mark it
// as temporary.
wxFileName temp(FileNames::TempDir(), wxT(""));
wxFileName temp(TempDirectory::TempDir(), wxT(""));
wxFileName file(fileName);
file.SetFullName(wxT(""));
if (file == temp)
@ -398,7 +398,7 @@ void ProjectFileIO::DiscardConnection()
if (mPrevTemporary)
{
// This is just a safety check.
wxFileName temp(FileNames::TempDir(), wxT(""));
wxFileName temp(TempDirectory::TempDir(), wxT(""));
wxFileName file(mPrevFileName);
file.SetFullName(wxT(""));
if (file == temp)
@ -2278,7 +2278,7 @@ bool ProjectFileIO::CloseProject()
if (IsTemporary())
{
// This is just a safety check.
wxFileName temp(FileNames::TempDir(), wxT(""));
wxFileName temp(TempDirectory::TempDir(), wxT(""));
wxFileName file(filename);
file.SetFullName(wxT(""));
if (file == temp)

View File

@ -19,7 +19,6 @@ Paul Licameli split from AudacityProject.cpp
#endif
#include <wx/frame.h>
#include "FileNames.h"
#include "Legacy.h"
#include "PlatformCompatibility.h"
#include "Project.h"
@ -34,6 +33,7 @@ Paul Licameli split from AudacityProject.cpp
#include "SelectUtilities.h"
#include "SelectionState.h"
#include "Tags.h"
#include "TempDirectory.h"
#include "TrackPanelAx.h"
#include "TrackPanel.h"
#include "UndoManager.h"
@ -269,7 +269,7 @@ bool ProjectFileManager::DoSave(const FilePath & fileName, const bool fromSaveAs
// Some confirmation dialogs
{
if (FileNames::FATFilesystemDenied(fileName, XO("Projects cannot be saved to FAT drives.")))
if (TempDirectory::FATFilesystemDenied(fileName, XO("Projects cannot be saved to FAT drives.")))
{
return false;
}
@ -606,7 +606,7 @@ bool ProjectFileManager::SaveCopy(const FilePath &fileName /* = wxT("") */)
filename.SetExt(wxT("aup3"));
if (FileNames::FATFilesystemDenied(filename.GetFullPath(), XO("Projects cannot be saved to FAT drives.")))
if (TempDirectory::FATFilesystemDenied(filename.GetFullPath(), XO("Projects cannot be saved to FAT drives.")))
{
if (project.mBatchMode)
{
@ -858,7 +858,7 @@ void ProjectFileManager::OpenFile(const FilePath &fileNameArg, bool addtohistory
auto fileName = PlatformCompatibility::ConvertSlashInFileName(
PlatformCompatibility::GetLongFileName(fileNameArg));
if (FileNames::FATFilesystemDenied(fileName,
if (TempDirectory::FATFilesystemDenied(fileName,
XO("Project resides on FAT formatted drive.\n"
"Copy it to another drive to open it.")))
{

133
src/TempDirectory.cpp Normal file
View File

@ -0,0 +1,133 @@
/**********************************************************************
Audacity: A Digital Audio Editor
TempDirectory.cpp
Paul Licameli split from FileNames.cpp
**********************************************************************/
#include "TempDirectory.h"
#include "FileNames.h"
#include "widgets/ErrorDialog.h"
static wxString &TempDirPath()
{
static wxString path;
return path;
}
/// Returns the directory used for temp files.
/// \todo put a counter in here to see if it gets used a lot.
/// if it does, then maybe we should cache the path name
/// each time.
wxString TempDirectory::TempDir()
{
auto &path = TempDirPath();
if (gPrefs && path.empty())
path =
gPrefs->Read(PreferenceKey(FileNames::Operation::Temp,
FileNames::PathType::_None), wxT(""));
if (FileNames::IsOnFATFileSystem(path))
{
ShowErrorDialog(
nullptr,
XO("Unsuitable"),
XO("The temporary files directory is on a FAT formatted drive.\n"
"Resetting to default location."),
"Error:_Unsuitable_drive"
);
path = DefaultTempDir();
UpdateDefaultPath(FileNames::Operation::Temp, path);
}
return FileNames::MkDir(path);
}
void TempDirectory::ResetTempDir()
{
TempDirPath().clear();
}
/** \brief Default temp directory */
static FilePath sDefaultTempDir;
const FilePath &TempDirectory::DefaultTempDir()
{
return sDefaultTempDir;
}
void TempDirectory::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 TempDirectory::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
if (FATFilesystemDenied(NameCanonical,
XO("The temporary files directory is on a FAT formatted drive.\n"
"Resetting to default location.")))
{
return false;
}
return !(NameCanonical.StartsWith( BadPath ));
}
wxString TempDirectory::UnsavedProjectFileName()
{
wxFileName fn(TempDir(),
FileNames::CreateUniqueName(wxT("New Project"), FileNames::UnsavedProjectExtension()));
return fn.GetFullPath();
}
bool TempDirectory::FATFilesystemDenied( const FilePath &path,
const TranslatableString &msg,
wxWindow *window /* = nullptr */ )
{
if (FileNames::IsOnFATFileSystem(path))
{
ShowErrorDialog(
window,
XO("Unsuitable"),
XO("%s\n\nFor tips on suitable drives, click the help button.").Format(msg),
"Error:_Unsuitable_drive"
);
return true;
}
return false;
}

34
src/TempDirectory.h Normal file
View File

@ -0,0 +1,34 @@
/**********************************************************************
Audacity: A Digital Audio Editor
TempDirectory.h
Paul Licameli split from FileNames.h
**********************************************************************/
#ifndef __AUDACITY_TEMP_DIRECTORY__
#define __AUDACITY_TEMP_DIRECTORY__
#endif
#include "Audacity.h"
#include "audacity/Types.h"
namespace TempDirectory
{
wxString TempDir();
void ResetTempDir();
const FilePath &DefaultTempDir();
void SetDefaultTempDir( const FilePath &tempDir );
bool IsTempDirectoryNameOK( const FilePath & Name );
// Create a filename for an unsaved/temporary project file
wxString UnsavedProjectFileName();
bool FATFilesystemDenied(const FilePath &path,
const TranslatableString &msg,
wxWindow *window = nullptr);
};

View File

@ -63,6 +63,7 @@ effects from this one class.
#include "../../ProjectSettings.h"
#include "../../ShuttleGetDefinition.h"
#include "../../ShuttleGui.h"
#include "../../TempDirectory.h"
#include "../../ViewInfo.h"
#include "../../WaveClip.h"
#include "../../WaveTrack.h"
@ -702,7 +703,7 @@ bool NyquistEffect::Process()
mProps += wxString::Format(wxT("(putprop '*SYSTEM-DIR* \"%s\" 'BASE)\n"), EscapeString(FileNames::BaseDir()));
mProps += wxString::Format(wxT("(putprop '*SYSTEM-DIR* \"%s\" 'DATA)\n"), EscapeString(FileNames::DataDir()));
mProps += wxString::Format(wxT("(putprop '*SYSTEM-DIR* \"%s\" 'HELP)\n"), EscapeString(FileNames::HtmlHelpDir().RemoveLast()));
mProps += wxString::Format(wxT("(putprop '*SYSTEM-DIR* \"%s\" 'TEMP)\n"), EscapeString(FileNames::TempDir()));
mProps += wxString::Format(wxT("(putprop '*SYSTEM-DIR* \"%s\" 'TEMP)\n"), EscapeString(TempDirectory::TempDir()));
mProps += wxString::Format(wxT("(putprop '*SYSTEM-DIR* \"%s\" 'SYS-TEMP)\n"), EscapeString(wxStandardPaths::Get().GetTempDir()));
mProps += wxString::Format(wxT("(putprop '*SYSTEM-DIR* \"%s\" 'DOCUMENTS)\n"), EscapeString(wxStandardPaths::Get().GetDocumentsDir()));
mProps += wxString::Format(wxT("(putprop '*SYSTEM-DIR* \"%s\" 'HOME)\n"), EscapeString(wxGetHomeDir()));

View File

@ -14,6 +14,7 @@
#include "../ProjectSelectionManager.h"
#include "../toolbars/ToolManager.h"
#include "../Screenshot.h"
#include "../TempDirectory.h"
#include "../UndoManager.h"
#include "../commands/CommandContext.h"
#include "../commands/CommandManager.h"
@ -379,7 +380,7 @@ void OnResetConfig(const CommandContext &context)
gPrefs->DeleteAll();
// Directory will be reset on next restart.
FileNames::UpdateDefaultPath(FileNames::Operation::Temp, FileNames::DefaultTempDir());
FileNames::UpdateDefaultPath(FileNames::Operation::Temp, TempDirectory::DefaultTempDir());
gPrefs->Write("/GUI/SyncLockTracks", 0);
gPrefs->Write("/SnapTo", 0 );
ProjectSelectionManager::Get( project ).AS_SetSnapTo( 0 );

View File

@ -32,12 +32,13 @@
#include <wx/filename.h>
#include <wx/utils.h>
#include "../FileNames.h"
#include "../Prefs.h"
#include "../ShuttleGui.h"
#include "../TempDirectory.h"
#include "../widgets/AudacityMessageBox.h"
using namespace FileNames;
using namespace TempDirectory;
class FilesystemValidator : public wxValidator
{
@ -84,7 +85,7 @@ public:
wxString path = tc->GetValue();
path.insert(tc->GetInsertionPoint(), keycode);
if (FileNames::FATFilesystemDenied(path, mMessage)) {
if (FATFilesystemDenied(path, mMessage)) {
evt.Skip(false);
return;
}
@ -398,7 +399,7 @@ bool DirectoriesPrefs::Validate()
}
wxFileName oldDir;
oldDir.SetPath(FileNames::TempDir());
oldDir.SetPath(TempDir());
if (Temp != oldDir) {
AudacityMessageBox(
XO(