X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b32719cc03c7c383513639dfadbaefa16a8e8a09..2d19ad25782b1b370cd26d0f17d45856d201fc70:/src/msw/registry.cpp diff --git a/src/msw/registry.cpp b/src/msw/registry.cpp index 612f021467..a761867e7d 100644 --- a/src/msw/registry.cpp +++ b/src/msw/registry.cpp @@ -43,7 +43,7 @@ #include // for _MAX_PATH #ifndef _MAX_PATH - #define _MAX_PATH 512 + #define _MAX_PATH 512 #endif // our header @@ -430,7 +430,9 @@ bool wxRegKey::RenameValue(const wxChar *szValueOld, const wxChar *szValueNew) ok = FALSE; } - if ( !ok || !CopyValue(szValueOld, *this, szValueNew) ) { + if ( !ok || + !CopyValue(szValueOld, *this, szValueNew) || + !DeleteValue(szValueOld) ) { wxLogError(_("Failed to rename registry value '%s' to '%s'."), szValueOld, szValueNew); @@ -487,10 +489,65 @@ bool wxRegKey::CopyValue(const wxChar *szValue, } } -bool wxRegKey::Copy(const wxString& strNewName) +bool wxRegKey::Rename(const wxChar *szNewName) +{ + wxCHECK_MSG( !!m_strKey, FALSE, _T("registry hives can't be renamed") ); + + if ( !Exists() ) { + wxLogError(_("Registry key '%s' does not exist, cannot rename it."), + GetFullName(this)); + + return FALSE; + } + + // do we stay in the same hive? + bool inSameHive = !wxStrchr(szNewName, REG_SEPARATOR); + + // construct the full new name of the key + wxRegKey keyDst; + + if ( inSameHive ) { + // rename the key to the new name under the same parent + wxString strKey = m_strKey.BeforeLast(REG_SEPARATOR); + if ( !!strKey ) { + // don't add '\\' in the start if strFullNewName is empty + strKey += REG_SEPARATOR; + } + + strKey += szNewName; + + keyDst.SetName(GetStdKeyFromHkey(m_hRootKey), strKey); + } + else { + // this is the full name already + keyDst.SetName(szNewName); + } + + bool ok = keyDst.Create(FALSE /* fail if alredy exists */); + if ( !ok ) { + wxLogError(_("Registry key '%s' already exists."), + GetFullName(&keyDst)); + } + else { + ok = Copy(keyDst) && DeleteSelf(); + } + + if ( !ok ) { + wxLogError(_("Failed to rename the registry key '%s' to '%s'."), + GetFullName(this), GetFullName(&keyDst)); + } + else { + m_hRootKey = keyDst.m_hRootKey; + m_strKey = keyDst.m_strKey; + } + + return ok; +} + +bool wxRegKey::Copy(const wxChar *szNewName) { // create the new key first - wxRegKey keyDst(strNewName); + wxRegKey keyDst(szNewName); bool ok = keyDst.Create(FALSE /* fail if alredy exists */); if ( ok ) { ok = Copy(keyDst); @@ -516,7 +573,7 @@ bool wxRegKey::Copy(wxRegKey& keyDst) wxRegKey key(*this, strKey); wxString keyName; keyName << GetFullName(&keyDst) << REG_SEPARATOR << strKey; - ok = key.Copy(keyName); + ok = key.Copy((const wxChar*) keyName); if ( ok ) bCont = GetNextKey(strKey, lIndex); @@ -539,8 +596,7 @@ bool wxRegKey::Copy(wxRegKey& keyDst) } if ( !ok ) { - wxLogError(_("Failed to copy the contents of registry key '%s' to " - "'%s'."), GetFullName(this), GetFullName(&keyDst)); + wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), GetFullName(this), GetFullName(&keyDst)); } return ok; @@ -564,9 +620,7 @@ bool wxRegKey::DeleteSelf() // key except HKCR (HKCR has some "deleteable" subkeys) if ( m_strKey.IsEmpty() || (m_hRootKey != HKCR && m_strKey.Find(REG_SEPARATOR) == wxNOT_FOUND) ) { - wxLogError(_("Registry key '%s' is needed for normal system operation,\n" - "deleting it will leave your system in unusable state:\n" - "operation aborted."), GetFullName(this)); + wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."), GetFullName(this)); return FALSE; } @@ -652,12 +706,14 @@ bool wxRegKey::HasValue(const wxChar *szValue) const wxLogNull nolog; #ifdef __WIN32__ - if ( CONST_CAST Open() ) { - return RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, - NULL, NULL, NULL) == ERROR_SUCCESS; - } - else - return FALSE; + if ( !CONST_CAST Open() ) + return FALSE; + + LONG dwRet = ::RegQueryValueEx((HKEY) m_hKey, + WXSTRINGCAST szValue, + RESERVED, + NULL, NULL, NULL); + return dwRet == ERROR_SUCCESS; #else // WIN16 // only unnamed value exists return IsEmpty(szValue); @@ -694,10 +750,10 @@ bool wxRegKey::HasSubKey(const wxChar *szKey) const // this function should be silent, so suppress possible messages from Open() wxLogNull nolog; - if ( CONST_CAST Open() ) - return KeyExists(m_hKey, szKey); - else + if ( !CONST_CAST Open() ) return FALSE; + + return KeyExists(m_hKey, szKey); } wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const @@ -822,7 +878,7 @@ bool wxRegKey::SetValue(const wxChar *szValue, const wxString& strValue) #if defined( __WIN32__) && !defined(__TWIN32__) m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ, (RegString)strValue.c_str(), - strValue.Len() + 1); + (strValue.Len() + 1)*sizeof(wxChar)); if ( m_dwLastError == ERROR_SUCCESS ) return TRUE; #else //WIN16 @@ -961,8 +1017,13 @@ bool wxRegKey::IsNumericValue(const wxChar *szValue) const // ============================================================================ // implementation of global private functions // ============================================================================ + bool KeyExists(WXHKEY hRootKey, const wxChar *szKey) { + // don't close this key itself for the case of empty szKey! + if ( wxIsEmpty(szKey) ) + return TRUE; + HKEY hkeyDummy; if ( RegOpenKey( (HKEY) hRootKey, szKey, &hkeyDummy) == ERROR_SUCCESS ) { RegCloseKey(hkeyDummy);