1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: registry.cxx,v $
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"
38 #pragma warning(disable : 4786 4350)
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
) :
56 //-----------------------------------------------------
57 /** Create instance and open the specified Registry key
59 @throws RegistryWriteAccessDenyException
60 RegistryAccessDenyException
61 RegistryKeyNotFoundException
63 RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey
) :
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
) :
78 m_IsWriteable(Writeable
)
82 //-----------------------------------------------------
85 RegistryKeyImpl::~RegistryKeyImpl()
92 //############################################
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
106 //-----------------------------------------------------
109 bool RegistryKeyImpl::IsOpen() const
111 return m_hSubKey
!= 0;
114 //-----------------------------------------------------
115 /** Is this one of the root keys
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
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
) :
158 bool operator() (const std::wstring
& value
)
160 return (0 == StrCmpI(value
.c_str(), name_
.c_str()));
166 //-----------------------------------------------------
167 /** Convenience function to determine if the
168 Registry key at hand has the specified
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
) {
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
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
));
216 SetValue((const RegistryValue
&)(RegistryKey
->GetValue(Name
)));
218 SetValue(RegistryKey
->GetValue(Name
));
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
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
);
244 assert(HasValue(NewName
));