Update ooo320-m1
[ooovba.git] / setup_native / source / win32 / customactions / reg4msdoc / registry.cxx
blob6c1e840e8cb7b0bb350521d08db25012190609f9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: registry.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "registry.hxx"
33 #include <Shlwapi.h>
34 #include <assert.h>
35 #include <algorithm>
37 #ifdef _MSC_VER
38 #pragma warning(disable : 4786 4350)
39 #endif
41 //-----------------------------------------------------
42 /** Create instance and open the specified Registry key
44 @throws RegistryWriteAccessDenyException
45 RegistryAccessDenyException
46 RegistryKeyNotFoundException
48 RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey, const std::wstring& KeyName) :
49 m_hRootKey(RootKey),
50 m_hSubKey(0),
51 m_KeyName(KeyName),
52 m_IsWriteable(false)
56 //-----------------------------------------------------
57 /** Create instance and open the specified Registry key
59 @throws RegistryWriteAccessDenyException
60 RegistryAccessDenyException
61 RegistryKeyNotFoundException
63 RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey) :
64 m_hRootKey(RootKey),
65 m_hSubKey(0),
66 m_IsWriteable(false)
70 //-----------------------------------------------------
71 /** Create an instances of the specified Registry key,
72 the key is assumed to be already opened.
74 RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey, HKEY SubKey, const std::wstring& KeyName, bool Writeable) :
75 m_hRootKey(RootKey),
76 m_hSubKey(SubKey),
77 m_KeyName(KeyName),
78 m_IsWriteable(Writeable)
82 //-----------------------------------------------------
83 /**
85 RegistryKeyImpl::~RegistryKeyImpl()
87 if (IsOpen())
88 Close();
92 //############################################
93 // Queries
94 //############################################
97 //-----------------------------------------------------
98 /** The name of the key at hand, maybe empty
99 if this is any of the root keys
101 std::wstring RegistryKeyImpl::GetName() const
103 return m_KeyName;
106 //-----------------------------------------------------
107 /**
109 bool RegistryKeyImpl::IsOpen() const
111 return m_hSubKey != 0;
114 //-----------------------------------------------------
115 /** Is this one of the root keys
116 HKEY_CLASSES_ROOT
117 HKEY_CURRENT_USER
118 etc.
120 bool RegistryKeyImpl::IsRootKey() const
122 return (0 == m_KeyName.length());
125 //-----------------------------------------------------
126 /** Do we have write access on the key at hand
128 bool RegistryKeyImpl::IsWriteable() const
130 return m_IsWriteable;
133 //-----------------------------------------------------
134 /** Convenience function to determine if the
135 Registry key at hand has the specified
136 value
138 @precond IsOpen = true
140 throws RegistryAccessDenyException
142 bool RegistryKeyImpl::HasValue(const std::wstring& Name) const
144 StringListPtr names = GetSubValueNames();
146 StringList::iterator iter_end = names->end();
147 StringList::iterator iter = std::find(names->begin(), iter_end, Name);
149 return (iter != iter_end);
152 struct CompareNamesCaseInsensitive
154 CompareNamesCaseInsensitive(const std::wstring& Name) :
155 name_(Name)
158 bool operator() (const std::wstring& value)
160 return (0 == StrCmpI(value.c_str(), name_.c_str()));
163 std::wstring name_;
166 //-----------------------------------------------------
167 /** Convenience function to determine if the
168 Registry key at hand has the specified
169 sub-key
171 @precond IsOpen = true
173 throws RegistryAccessDenyException
175 bool RegistryKeyImpl::HasSubKey(const std::wstring& Name) const
177 StringListPtr names = GetSubKeyNames();
179 StringList::iterator iter_end = names->end();
180 StringList::iterator iter = std::find_if(names->begin(), iter_end, CompareNamesCaseInsensitive(Name));
182 return (iter != iter_end);
185 //-----------------------------------------------------
188 void RegistryKeyImpl::Close()
190 if (RegCloseKey(m_hSubKey) != ERROR_SUCCESS) {
191 assert(false);
194 m_hSubKey = 0;
195 m_IsWriteable = false;
198 //-----------------------------------------------------
199 /** Copies the specified value from RegistryKey to
200 the registry key at hand, if a value with this
201 name already exist under the registry key at hand
202 it will be overwritten
204 @precond IsOpen = true
205 IsWriteable = true
206 RegistryKey.HasSubValue(Name) = true
208 @throws RegistryIOException
209 RegistryWriteAccessDeniedException
210 RegistryValueNotFoundException
212 void RegistryKeyImpl::CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name)
214 assert(RegistryKey->HasValue(Name));
215 #ifdef __MINGW32__
216 SetValue((const RegistryValue&)(RegistryKey->GetValue(Name)));
217 #else
218 SetValue(RegistryKey->GetValue(Name));
219 #endif
220 assert(HasValue(Name));
223 /** Copies the specified value from RegistryKey to
224 the registry key at hand under a new name,
225 if a value with this name already exist there
226 it will be overwritten
228 @precond IsOpen = true
229 IsWriteable = true
230 RegistryKey.HasSubValue(Name) = true
232 @throws RegistryIOException
233 RegistryWriteAccessDeniedException
234 RegistryValueNotFoundException
236 void RegistryKeyImpl::CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name, const std::wstring& NewName)
238 assert(RegistryKey->HasValue(Name));
240 RegistryValue RegVal = RegistryKey->GetValue(Name);
241 RegVal->SetName(NewName);
242 SetValue(RegVal);
244 assert(HasValue(NewName));