use wxStandardPaths and wxFileName in wxFileConfig resulting in big code simplification and cleanup but also in change of the default file locations under Windows (replaces patch 1620492)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44893 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-03-18 17:34:11 +00:00
parent e3b81044ee
commit 466e87bd8c
5 changed files with 132 additions and 151 deletions

View File

@@ -42,9 +42,10 @@
#include "wx/fileconf.h"
#include "wx/filefn.h"
#include "wx/stdpaths.h"
#if defined(__WXMAC__)
#include "wx/mac/private.h" // includes mac headers
#include "wx/filename.h" // for MacSetTypeAndCreator
#endif
#if defined(__WXMSW__)
@@ -260,118 +261,71 @@ public:
// ----------------------------------------------------------------------------
// static functions
// ----------------------------------------------------------------------------
// this function modifies in place the given wxFileName object if it doesn't
// already have an extension
//
// note that it's slightly misnamed under Mac as there it doesn't add an
// extension but modifies the file name instead, so you shouldn't suppose that
// fn.HasExt() is true after it returns
static void AddConfFileExtIfNeeded(wxFileName& fn)
{
if ( !fn.HasExt() )
{
#if defined( __WXMAC__ )
fn.SetName(fn.GetName() + wxT(" Preferences"));
#elif defined( __UNIX__ )
fn.SetExt(wxT(".conf"));
#else // Windows
fn.SetExt(wxT(".ini"));
#endif // UNIX/Win
}
}
wxString wxFileConfig::GetGlobalDir()
{
wxString strDir;
return wxStandardPaths::Get().GetConfigDir();
}
#ifdef __VMS__ // Note if __VMS is defined __UNIX is also defined
strDir = wxT("sys$manager:");
#elif defined(__WXMAC__)
strDir = wxMacFindFolder( (short) kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder ) ;
#elif defined( __UNIX__ )
strDir = wxT("/etc/");
#elif defined(__OS2__)
ULONG aulSysInfo[QSV_MAX] = {0};
UINT drive;
APIRET rc;
wxString wxFileConfig::GetLocalDir(int style)
{
wxUnusedVar(style);
rc = DosQuerySysInfo( 1L, QSV_MAX, (PVOID)aulSysInfo, sizeof(ULONG)*QSV_MAX);
if (rc == 0)
wxStandardPathsBase& stdp = wxStandardPaths::Get();
// it so happens that user data directory is a subdirectory of user config
// directory on all supported platforms, which explains why we use it here
return style & wxCONFIG_USE_SUBDIR ? stdp.GetUserDataDir()
: stdp.GetUserConfigDir();
}
wxFileName wxFileConfig::GetGlobalFile(const wxString& szFile)
{
wxFileName fn(GetGlobalDir(), szFile);
AddConfFileExtIfNeeded(fn);
return fn;
}
wxFileName wxFileConfig::GetLocalFile(const wxString& szFile, int style)
{
wxFileName fn(GetLocalDir(style), szFile);
#ifdef __UNIX__
if ( !(style & wxCONFIG_USE_SUBDIR) )
{
drive = aulSysInfo[QSV_BOOT_DRIVE - 1];
strDir.Printf(wxT("%c:\\OS2\\"), 'A'+drive-1);
// dot-files under Unix start with, well, a dot (but OTOH they usually
// don't have any specific extension)
fn.SetName(wxT('.') + fn.GetName());
}
else // we do append ".conf" extension to config files in subdirectories
#endif // __UNIX__
{
AddConfFileExtIfNeeded(fn);
}
#elif defined(__WXSTUBS__)
wxFAIL_MSG( wxT("TODO") );
#elif defined(__DOS__)
// There's no such thing as global cfg dir in MS-DOS, let's return
// current directory (FIXME_MGL?)
strDir = wxT(".\\");
#elif defined(__WXWINCE__)
strDir = wxT("\\Windows\\");
#else // Windows
wxChar szWinDir[MAX_PATH];
::GetWindowsDirectory(szWinDir, MAX_PATH);
strDir = szWinDir;
strDir << wxT('\\');
#endif // Unix/Windows
return strDir;
}
wxString wxFileConfig::GetLocalDir()
{
wxString strDir;
#if defined(__WXMAC__) || defined(__DOS__)
// no local dir concept on Mac OS 9 or MS-DOS
strDir << GetGlobalDir() ;
#else
wxGetHomeDir(&strDir);
#ifdef __UNIX__
if (
(strDir.Last() != wxT('/'))
#ifdef __VMS
&& (strDir.Last() != wxT(']'))
#endif
)
strDir << wxT('/');
#else
if (strDir.Last() != wxT('\\'))
strDir << wxT('\\');
#endif
#endif
return strDir;
}
wxString wxFileConfig::GetGlobalFileName(const wxString& file)
{
wxString str = GetGlobalDir();
str << file;
if ( wxStrchr(file, wxT('.')) == NULL )
#if defined( __WXMAC__ )
str << wxT(" Preferences") ;
#elif defined( __UNIX__ )
str << wxT(".conf");
#else // Windows
str << wxT(".ini");
#endif // UNIX/Win
return str;
}
wxString wxFileConfig::GetLocalFileName(const wxString& file)
{
#ifdef __VMS__
// On VMS I saw the problem that the home directory was appended
// twice for the configuration file. Does that also happen for
// other platforms?
wxString str = wxT( '.' );
#else
wxString str = GetLocalDir();
#endif
#if defined( __UNIX__ ) && !defined( __VMS ) && !defined( __WXMAC__ )
str << wxT('.');
#endif
str << file;
#if defined(__WINDOWS__) || defined(__DOS__)
if ( wxStrchr(file, wxT('.')) == NULL )
str << wxT(".ini");
#endif
#ifdef __WXMAC__
str << wxT(" Preferences") ;
#endif
return str;
return fn;
}
// ----------------------------------------------------------------------------
@@ -390,9 +344,9 @@ void wxFileConfig::Init()
// It's not an error if (one of the) file(s) doesn't exist.
// parse the global file
if ( !m_strGlobalFile.empty() && wxFile::Exists(m_strGlobalFile) )
if ( m_fnGlobalFile.FileExists() )
{
wxTextFile fileGlobal(m_strGlobalFile);
wxTextFile fileGlobal(m_fnGlobalFile.GetFullPath());
if ( fileGlobal.Open(*m_conv/*ignored in ANSI build*/) )
{
@@ -401,14 +355,14 @@ void wxFileConfig::Init()
}
else
{
wxLogWarning(_("can't open global configuration file '%s'."), m_strGlobalFile.c_str());
wxLogWarning(_("can't open global configuration file '%s'."), m_fnGlobalFile.GetFullPath().c_str());
}
}
// parse the local file
if ( !m_strLocalFile.empty() && wxFile::Exists(m_strLocalFile) )
if ( m_fnLocalFile.FileExists() )
{
wxTextFile fileLocal(m_strLocalFile);
wxTextFile fileLocal(m_fnLocalFile.GetFullPath());
if ( fileLocal.Open(*m_conv/*ignored in ANSI build*/) )
{
Parse(fileLocal, true /* local */);
@@ -416,7 +370,7 @@ void wxFileConfig::Init()
}
else
{
wxLogWarning(_("can't open user configuration file '%s'."), m_strLocalFile.c_str() );
wxLogWarning(_("can't open user configuration file '%s'."), m_fnLocalFile.GetFullPath().c_str() );
}
}
@@ -431,47 +385,34 @@ wxFileConfig::wxFileConfig(const wxString& appName, const wxString& vendorName,
: wxConfigBase(::GetAppName(appName), vendorName,
strLocal, strGlobal,
style),
m_strLocalFile(strLocal), m_strGlobalFile(strGlobal),
m_fnLocalFile(strLocal),
m_fnGlobalFile(strGlobal),
m_conv(conv.Clone())
{
// Make up names for files if empty
if ( m_strLocalFile.empty() && (style & wxCONFIG_USE_LOCAL_FILE) )
{
m_strLocalFile = GetLocalFileName(GetAppName());
#if defined(__UNIX__) && !defined(__VMS)
if ( style & wxCONFIG_USE_SUBDIR )
m_strLocalFile << wxFILE_SEP_PATH << GetAppName() << _T(".conf");
#endif
}
if ( !m_fnLocalFile.IsOk() && (style & wxCONFIG_USE_LOCAL_FILE) )
m_fnLocalFile = GetLocalFile(GetAppName(), style);
if ( m_strGlobalFile.empty() && (style & wxCONFIG_USE_GLOBAL_FILE) )
m_strGlobalFile = GetGlobalFileName(GetAppName());
if ( !m_fnGlobalFile.IsOk() && (style & wxCONFIG_USE_GLOBAL_FILE) )
m_fnGlobalFile = GetGlobalFile(GetAppName());
// Check if styles are not supplied, but filenames are, in which case
// add the correct styles.
if ( !m_strLocalFile.empty() )
if ( m_fnLocalFile.IsOk() )
SetStyle(GetStyle() | wxCONFIG_USE_LOCAL_FILE);
if ( !m_strGlobalFile.empty() )
if ( m_fnGlobalFile.IsOk() )
SetStyle(GetStyle() | wxCONFIG_USE_GLOBAL_FILE);
// if the path is not absolute, prepend the standard directory to it
// UNLESS wxCONFIG_USE_RELATIVE_PATH style is set
// unless explicitly asked not to
if ( !(style & wxCONFIG_USE_RELATIVE_PATH) )
{
if ( !m_strLocalFile.empty() && !wxIsAbsolutePath(m_strLocalFile) )
{
const wxString strLocalOrig = m_strLocalFile;
m_strLocalFile = GetLocalDir();
m_strLocalFile << strLocalOrig;
}
if ( m_fnLocalFile.IsOk() )
m_fnLocalFile.MakeAbsolute(GetLocalDir(style));
if ( !m_strGlobalFile.empty() && !wxIsAbsolutePath(m_strGlobalFile) )
{
const wxString strGlobalOrig = m_strGlobalFile;
m_strGlobalFile = GetGlobalDir();
m_strGlobalFile << strGlobalOrig;
}
if ( m_fnGlobalFile.IsOk() )
m_fnGlobalFile.MakeAbsolute(GetGlobalDir());
}
SetUmask(-1);
@@ -1041,13 +982,13 @@ bool wxFileConfig::DoWriteLong(const wxString& key, long lValue)
bool wxFileConfig::Flush(bool /* bCurrentOnly */)
{
if ( !IsDirty() || !m_strLocalFile )
if ( !IsDirty() || !m_fnLocalFile.GetFullPath() )
return true;
// set the umask if needed
wxCHANGE_UMASK(m_umask);
wxTempFile file(m_strLocalFile);
wxTempFile file(m_fnLocalFile.GetFullPath());
if ( !file.IsOpened() )
{
@@ -1079,7 +1020,7 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */)
ResetDirty();
#if defined(__WXMAC__)
wxFileName(m_strLocalFile).MacSetTypeAndCreator('TEXT', 'ttxt');
m_fnLocalFile.MacSetTypeAndCreator('TEXT', 'ttxt');
#endif // __WXMAC__
return true;
@@ -1205,12 +1146,12 @@ bool wxFileConfig::DeleteAll()
{
CleanUp();
if ( !m_strLocalFile.empty() )
if ( m_fnLocalFile.IsOk() )
{
if ( wxFile::Exists(m_strLocalFile) && wxRemove(m_strLocalFile) == -1 )
if ( m_fnLocalFile.FileExists() && wxRemove(m_fnLocalFile.GetFullPath()) == -1 )
{
wxLogSysError(_("can't delete user configuration file '%s'"),
m_strLocalFile.c_str());
m_fnLocalFile.GetFullPath().c_str());
return false;
}
}