+bool wxRegKey::RenameValue(const wxChar *szValueOld, const wxChar *szValueNew)
+{
+ bool ok = TRUE;
+ if ( HasValue(szValueNew) ) {
+ wxLogError(_("Registry value '%s' already exists."), szValueNew);
+
+ ok = FALSE;
+ }
+
+ if ( !ok ||
+ !CopyValue(szValueOld, *this, szValueNew) ||
+ !DeleteValue(szValueOld) ) {
+ wxLogError(_("Failed to rename registry value '%s' to '%s'."),
+ szValueOld, szValueNew);
+
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+bool wxRegKey::CopyValue(const wxChar *szValue,
+ wxRegKey& keyDst,
+ const wxChar *szValueNew)
+{
+ if ( !szValueNew ) {
+ // by default, use the same name
+ szValueNew = szValue;
+ }
+
+ switch ( GetValueType(szValue) ) {
+ case Type_String:
+ {
+ wxString strVal;
+ return QueryValue(szValue, strVal) &&
+ keyDst.SetValue(szValueNew, strVal);
+ }
+
+ case Type_Dword:
+ /* case Type_Dword_little_endian: == Type_Dword */
+ {
+ long dwVal;
+ return QueryValue(szValue, &dwVal) &&
+ keyDst.SetValue(szValueNew, dwVal);
+ }
+
+ // these types are unsupported because I am not sure about how
+ // exactly they should be copied and because they shouldn't
+ // occur among the application keys (supposedly created with
+ // this class)
+#ifdef __WIN32__
+ case Type_None:
+ case Type_Expand_String:
+ case Type_Binary:
+ case Type_Dword_big_endian:
+ case Type_Link:
+ case Type_Multi_String:
+ case Type_Resource_list:
+ case Type_Full_resource_descriptor:
+ case Type_Resource_requirements_list:
+#endif // Win32
+ default:
+ wxLogError(_("Can't copy values of unsupported type %d."),
+ GetValueType(szValue));
+ return FALSE;
+ }
+}
+
+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(szNewName);
+ bool ok = keyDst.Create(FALSE /* fail if alredy exists */);
+ if ( ok ) {
+ ok = Copy(keyDst);
+
+ // we created the dest key but copying to it failed - delete it
+ if ( !ok ) {
+ (void)keyDst.DeleteSelf();
+ }
+ }
+
+ return ok;
+}
+
+bool wxRegKey::Copy(wxRegKey& keyDst)
+{
+ bool ok = TRUE;
+
+ // copy all sub keys to the new location
+ wxString strKey;
+ long lIndex;
+ bool bCont = GetFirstKey(strKey, lIndex);
+ while ( ok && bCont ) {
+ wxRegKey key(*this, strKey);
+ wxString keyName;
+ keyName << GetFullName(&keyDst) << REG_SEPARATOR << strKey;
+ ok = key.Copy((const char*) keyName);
+
+ if ( ok )
+ bCont = GetNextKey(strKey, lIndex);
+ }
+
+ // copy all values
+ wxString strVal;
+ bCont = GetFirstValue(strVal, lIndex);
+ while ( ok && bCont ) {
+ ok = CopyValue(strVal, keyDst);
+
+ if ( !ok ) {
+ wxLogSysError(m_dwLastError,
+ _("Failed to copy registry value '%s'"),
+ strVal.c_str());
+ }
+ else {
+ bCont = GetNextValue(strVal, lIndex);
+ }
+ }
+
+ if ( !ok ) {
+ wxLogError(_("Failed to copy the contents of registry key '%s' to "
+ "'%s'."), GetFullName(this), GetFullName(&keyDst));
+ }
+
+ return ok;
+}
+