X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1824493628261b2cb7ceceb431d85c427f1c976b..cb6827a8c645a883a2d2a7b117a50609d706ccce:/src/msw/regconf.cpp?ds=sidebyside diff --git a/src/msw/regconf.cpp b/src/msw/regconf.cpp index 22743d41a0..8943720d8f 100644 --- a/src/msw/regconf.cpp +++ b/src/msw/regconf.cpp @@ -13,31 +13,24 @@ #pragma implementation "regconf.h" #endif -// ============================================================================ -// declarations -// ============================================================================ - -// ---------------------------------------------------------------------------- -// headers -// ---------------------------------------------------------------------------- +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" -#ifdef __GNUG__ - #pragma implementation "regconf.h" +#ifdef __BORLANDC__ +#pragma hdrstop #endif -#include "wx/wxprec.h" - -#ifdef __BORLANDC__ - #pragma hdrstop -#endif //__BORLANDC__ - #ifndef WX_PRECOMP #include #endif //WX_PRECOMP +#include #include #include #include + +#ifndef __WIN16__ + #include #include @@ -71,62 +64,60 @@ bool TryGetValue(const wxRegKey& key, const wxString& str, long *plVal) // ctor/dtor // ---------------------------------------------------------------------------- -#if 0 -wxRegConfig::wxRegConfig(const wxString& strRoot) - : m_keyLocalRoot(wxRegKey::HKCU, SOFTWARE_KEY + strRoot), - m_keyLocal(m_keyLocalRoot, ""), - m_keyGlobalRoot(wxRegKey::HKLM, SOFTWARE_KEY + strRoot), - m_keyGlobal(m_keyGlobalRoot, "") +// create the config object which stores its data under HKCU\vendor\app and, if +// style & wxCONFIG_USE_GLOBAL_FILE, under HKLM\vendor\app +wxRegConfig::wxRegConfig(const wxString& appName, const wxString& vendorName, + const wxString& strLocal, const wxString& strGlobal, + long style) + : wxConfigBase(appName, vendorName, strLocal, strGlobal, style) { - // Create() will Open() if key already exists - m_keyLocalRoot.Create(); + wxString strRoot; - // as it's the same key, Open() shouldn't fail (i.e. no need for Create()) - m_keyLocal.Open(); + bool bDoUseGlobal = (style & wxCONFIG_USE_GLOBAL_FILE) != 0; - wxLogNull nolog; - m_keyGlobalRoot.Open(); -} -#endif - -// TODO: vendor name is ignored, because we can't yet do the test for optional vendor -// name in the constructor body. We need a wxRegKey::Set that takes the same -// args as the constructor. Then we'll set m_keyLocalRoot etc. in the constructor body. - -wxRegConfig::wxRegConfig(const wxString& appName, const wxString& vendorName, - const wxString& strLocal, const wxString& strGlobal, long style) - : wxConfigBase(appName, vendorName, strLocal, strGlobal, style), + // the convention is to put the programs keys under \ + // (but it can be overriden by specifying the pathes explicitly in strLocal + // and/or strGlobal) + if ( strLocal.IsEmpty() || (strGlobal.IsEmpty() && bDoUseGlobal) ) + { + if ( vendorName.IsEmpty() ) + { + if ( wxTheApp ) + strRoot = wxTheApp->GetVendorName(); + } + else + { + strRoot = vendorName; + } - m_keyLocalRoot(wxRegKey::HKCU, SOFTWARE_KEY + appName), - m_keyLocal(m_keyLocalRoot, ""), - m_keyGlobalRoot(wxRegKey::HKLM, SOFTWARE_KEY + appName), - m_keyGlobal(m_keyGlobalRoot, "") -{ - // TODO: really, we should check and supply an app name if one isn't supplied. - // Unfortunately I don't know how to initialise the member wxRegKey - // variables from within the constructor body. -- JACS - // Vadim - we just need an implementation of wxRegKey::Set, - // and then we can uncomment this and remove the constructor lines above. -/* - wxString strRoot(appName); - if (appName.IsEmpty() && wxTheApp) + // no '\\' needed if no vendor name + if ( !strRoot.IsEmpty() ) { - strRoot = wxTheApp->GetAppName(); + strRoot += '\\'; } - wxASSERT( !strRoot.IsEmpty() ); - if (!vendorName.IsEmpty()) + if ( appName.IsEmpty() ) + { + wxCHECK_RET( wxTheApp, "No application name in wxRegConfig ctor!" ); + strRoot << wxTheApp->GetAppName(); + } + else { - strRoot += "\\"; - strRoot += vendorName; + strRoot << appName; } + } + //else: we don't need to do all the complicated stuff above - m_keyLocalRoot.Set(wxRegKey::HKCU, SOFTWARE_KEY + strRoot), - m_keyLocal.Set(m_keyLocalRoot, ""), + wxString str = strLocal.IsEmpty() ? strRoot : strLocal; + m_keyLocalRoot.SetName(wxRegKey::HKCU, SOFTWARE_KEY + str); + m_keyLocal.SetName(m_keyLocalRoot, ""); - m_keyGlobalRoot.Set(wxRegKey::HKLM, SOFTWARE_KEY + strRoot), - m_keyGlobal.Set(m_keyGlobalRoot, "") -*/ + if ( bDoUseGlobal ) + { + str = strGlobal.IsEmpty() ? strRoot : strGlobal; + m_keyGlobalRoot.SetName(wxRegKey::HKLM, SOFTWARE_KEY + str); + m_keyGlobal.SetName(m_keyGlobalRoot, ""); + } // Create() will Open() if key already exists m_keyLocalRoot.Create(); @@ -134,9 +125,13 @@ wxRegConfig::wxRegConfig(const wxString& appName, const wxString& vendorName, // as it's the same key, Open() shouldn't fail (i.e. no need for Create()) m_keyLocal.Open(); - wxLogNull nolog; - m_keyGlobalRoot.Open(); - + // OTOH, this key may perfectly not exist, so suppress error messages the call + // to Open() might generate + if ( bDoUseGlobal ) + { + wxLogNull nolog; + m_keyGlobalRoot.Open(); + } } wxRegConfig::~wxRegConfig() @@ -442,6 +437,67 @@ bool wxRegConfig::Write(const wxString& key, long lValue) return m_keyLocal.SetValue(path.Name(), lValue); } +// ---------------------------------------------------------------------------- +// renaming +// ---------------------------------------------------------------------------- + +bool wxRegConfig::RenameEntry(const wxString& oldName, const wxString& newName) +{ + // check that the old entry exists... + if ( !HasEntry(oldName) ) + return FALSE; + + // and that the new one doesn't + if ( HasEntry(newName) ) + return FALSE; + + // delete the old entry and create the new one - but do in the reverse + // order to not lose the data if Create() fails + + bool ok; + if ( m_keyLocal.IsNumericValue(oldName) ) + { + long val; + ok = m_keyLocal.QueryValue(oldName, &val) && + m_keyLocal.SetValue(newName, val); + } + else + { + wxString val; + ok = m_keyLocal.QueryValue(oldName, val) && + m_keyLocal.SetValue(newName, val); + } + + if ( !ok ) + return FALSE; + + if ( !m_keyLocal.DeleteValue(oldName) ) + { + m_keyLocal.DeleteValue(newName); + + return FALSE; + } + + return TRUE; +} + +bool wxRegConfig::RenameGroup(const wxString& oldName, const wxString& newName) +{ + // check that the old group exists... + if ( !HasGroup(oldName) ) + return FALSE; + + // and that the new one doesn't + if ( HasGroup(newName) ) + return FALSE; + + // TODO there is no way to rename a registry key - we must do a deep copy + // ourselves + wxFAIL_MSG("Registry key renaming not implemented"); + + return FALSE; +} + // ---------------------------------------------------------------------------- // deleting // ---------------------------------------------------------------------------- @@ -453,7 +509,7 @@ bool wxRegConfig::DeleteEntry(const wxString& value, bool bGroupIfEmptyAlso) return FALSE; if ( !m_keyLocal.HasSubkeys() ) { - wxString strKey = GetPath().Right(wxCONFIG_PATH_SEPARATOR); + wxString strKey = GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR); SetPath(".."); // changes m_keyLocal return m_keyLocal.DeleteKey(strKey); } @@ -474,8 +530,15 @@ bool wxRegConfig::DeleteAll() m_keyGlobal.Close(); bool bOk = m_keyLocalRoot.DeleteSelf(); - if ( bOk ) + + // make sure that we opened m_keyGlobalRoot and so it has a reasonable name: + // otherwise we will delete HKEY_CLASSES_ROOT recursively + if ( bOk && m_keyGlobalRoot.IsOpened() ) bOk = m_keyGlobalRoot.DeleteSelf(); return bOk; } + +#endif + // __WIN16__ +