1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "registry.hxx" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include <Shlwapi.h> 31*cdf0e10cSrcweir #include <assert.h> 32*cdf0e10cSrcweir #include <algorithm> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir #ifdef _MSC_VER 35*cdf0e10cSrcweir #pragma warning(disable : 4786 4350) 36*cdf0e10cSrcweir #endif 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir //----------------------------------------------------- 39*cdf0e10cSrcweir /** Create instance and open the specified Registry key 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir @throws RegistryWriteAccessDenyException 42*cdf0e10cSrcweir RegistryAccessDenyException 43*cdf0e10cSrcweir RegistryKeyNotFoundException 44*cdf0e10cSrcweir */ 45*cdf0e10cSrcweir RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey, const std::wstring& KeyName) : 46*cdf0e10cSrcweir m_hRootKey(RootKey), 47*cdf0e10cSrcweir m_hSubKey(0), 48*cdf0e10cSrcweir m_KeyName(KeyName), 49*cdf0e10cSrcweir m_IsWriteable(false) 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir } 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir //----------------------------------------------------- 54*cdf0e10cSrcweir /** Create instance and open the specified Registry key 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir @throws RegistryWriteAccessDenyException 57*cdf0e10cSrcweir RegistryAccessDenyException 58*cdf0e10cSrcweir RegistryKeyNotFoundException 59*cdf0e10cSrcweir */ 60*cdf0e10cSrcweir RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey) : 61*cdf0e10cSrcweir m_hRootKey(RootKey), 62*cdf0e10cSrcweir m_hSubKey(0), 63*cdf0e10cSrcweir m_IsWriteable(false) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir //----------------------------------------------------- 68*cdf0e10cSrcweir /** Create an instances of the specified Registry key, 69*cdf0e10cSrcweir the key is assumed to be already opened. 70*cdf0e10cSrcweir */ 71*cdf0e10cSrcweir RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey, HKEY SubKey, const std::wstring& KeyName, bool Writeable) : 72*cdf0e10cSrcweir m_hRootKey(RootKey), 73*cdf0e10cSrcweir m_hSubKey(SubKey), 74*cdf0e10cSrcweir m_KeyName(KeyName), 75*cdf0e10cSrcweir m_IsWriteable(Writeable) 76*cdf0e10cSrcweir { 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir //----------------------------------------------------- 80*cdf0e10cSrcweir /** 81*cdf0e10cSrcweir */ 82*cdf0e10cSrcweir RegistryKeyImpl::~RegistryKeyImpl() 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir if (IsOpen()) 85*cdf0e10cSrcweir Close(); 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir //############################################ 90*cdf0e10cSrcweir // Queries 91*cdf0e10cSrcweir //############################################ 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir //----------------------------------------------------- 95*cdf0e10cSrcweir /** The name of the key at hand, maybe empty 96*cdf0e10cSrcweir if this is any of the root keys 97*cdf0e10cSrcweir */ 98*cdf0e10cSrcweir std::wstring RegistryKeyImpl::GetName() const 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir return m_KeyName; 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir //----------------------------------------------------- 104*cdf0e10cSrcweir /** 105*cdf0e10cSrcweir */ 106*cdf0e10cSrcweir bool RegistryKeyImpl::IsOpen() const 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir return m_hSubKey != 0; 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir //----------------------------------------------------- 112*cdf0e10cSrcweir /** Is this one of the root keys 113*cdf0e10cSrcweir HKEY_CLASSES_ROOT 114*cdf0e10cSrcweir HKEY_CURRENT_USER 115*cdf0e10cSrcweir etc. 116*cdf0e10cSrcweir */ 117*cdf0e10cSrcweir bool RegistryKeyImpl::IsRootKey() const 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir return (0 == m_KeyName.length()); 120*cdf0e10cSrcweir } 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir //----------------------------------------------------- 123*cdf0e10cSrcweir /** Do we have write access on the key at hand 124*cdf0e10cSrcweir */ 125*cdf0e10cSrcweir bool RegistryKeyImpl::IsWriteable() const 126*cdf0e10cSrcweir { 127*cdf0e10cSrcweir return m_IsWriteable; 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir //----------------------------------------------------- 131*cdf0e10cSrcweir /** Convenience function to determine if the 132*cdf0e10cSrcweir Registry key at hand has the specified 133*cdf0e10cSrcweir value 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir @precond IsOpen = true 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir throws RegistryAccessDenyException 138*cdf0e10cSrcweir */ 139*cdf0e10cSrcweir bool RegistryKeyImpl::HasValue(const std::wstring& Name) const 140*cdf0e10cSrcweir { 141*cdf0e10cSrcweir StringListPtr names = GetSubValueNames(); 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir StringList::iterator iter_end = names->end(); 144*cdf0e10cSrcweir StringList::iterator iter = std::find(names->begin(), iter_end, Name); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir return (iter != iter_end); 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir struct CompareNamesCaseInsensitive 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir CompareNamesCaseInsensitive(const std::wstring& Name) : 152*cdf0e10cSrcweir name_(Name) 153*cdf0e10cSrcweir {} 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir bool operator() (const std::wstring& value) 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir return (0 == StrCmpI(value.c_str(), name_.c_str())); 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir std::wstring name_; 161*cdf0e10cSrcweir }; 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir //----------------------------------------------------- 164*cdf0e10cSrcweir /** Convenience function to determine if the 165*cdf0e10cSrcweir Registry key at hand has the specified 166*cdf0e10cSrcweir sub-key 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir @precond IsOpen = true 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir throws RegistryAccessDenyException 171*cdf0e10cSrcweir */ 172*cdf0e10cSrcweir bool RegistryKeyImpl::HasSubKey(const std::wstring& Name) const 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir StringListPtr names = GetSubKeyNames(); 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir StringList::iterator iter_end = names->end(); 177*cdf0e10cSrcweir StringList::iterator iter = std::find_if(names->begin(), iter_end, CompareNamesCaseInsensitive(Name)); 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir return (iter != iter_end); 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir //----------------------------------------------------- 183*cdf0e10cSrcweir /** 184*cdf0e10cSrcweir */ 185*cdf0e10cSrcweir void RegistryKeyImpl::Close() 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir if (RegCloseKey(m_hSubKey) != ERROR_SUCCESS) { 188*cdf0e10cSrcweir assert(false); 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir m_hSubKey = 0; 192*cdf0e10cSrcweir m_IsWriteable = false; 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir //----------------------------------------------------- 196*cdf0e10cSrcweir /** Copies the specified value from RegistryKey to 197*cdf0e10cSrcweir the registry key at hand, if a value with this 198*cdf0e10cSrcweir name already exist under the registry key at hand 199*cdf0e10cSrcweir it will be overwritten 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir @precond IsOpen = true 202*cdf0e10cSrcweir IsWriteable = true 203*cdf0e10cSrcweir RegistryKey.HasSubValue(Name) = true 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir @throws RegistryIOException 206*cdf0e10cSrcweir RegistryWriteAccessDeniedException 207*cdf0e10cSrcweir RegistryValueNotFoundException 208*cdf0e10cSrcweir */ 209*cdf0e10cSrcweir void RegistryKeyImpl::CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir assert(RegistryKey->HasValue(Name)); 212*cdf0e10cSrcweir #ifdef __MINGW32__ 213*cdf0e10cSrcweir SetValue((const RegistryValue&)(RegistryKey->GetValue(Name))); 214*cdf0e10cSrcweir #else 215*cdf0e10cSrcweir SetValue(RegistryKey->GetValue(Name)); 216*cdf0e10cSrcweir #endif 217*cdf0e10cSrcweir assert(HasValue(Name)); 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir /** Copies the specified value from RegistryKey to 221*cdf0e10cSrcweir the registry key at hand under a new name, 222*cdf0e10cSrcweir if a value with this name already exist there 223*cdf0e10cSrcweir it will be overwritten 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir @precond IsOpen = true 226*cdf0e10cSrcweir IsWriteable = true 227*cdf0e10cSrcweir RegistryKey.HasSubValue(Name) = true 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir @throws RegistryIOException 230*cdf0e10cSrcweir RegistryWriteAccessDeniedException 231*cdf0e10cSrcweir RegistryValueNotFoundException 232*cdf0e10cSrcweir */ 233*cdf0e10cSrcweir void RegistryKeyImpl::CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name, const std::wstring& NewName) 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir assert(RegistryKey->HasValue(Name)); 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir RegistryValue RegVal = RegistryKey->GetValue(Name); 238*cdf0e10cSrcweir RegVal->SetName(NewName); 239*cdf0e10cSrcweir SetValue(RegVal); 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir assert(HasValue(NewName)); 242*cdf0e10cSrcweir } 243