1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "registry.hxx"
36 #pragma warning(disable : 4786 4350)
39 //-----------------------------------------------------
40 /** Create instance and open the specified Registry key
42 @throws RegistryWriteAccessDenyException
43 RegistryAccessDenyException
44 RegistryKeyNotFoundException
46 RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey
, const std::wstring
& KeyName
) :
54 //-----------------------------------------------------
55 /** Create instance and open the specified Registry key
57 @throws RegistryWriteAccessDenyException
58 RegistryAccessDenyException
59 RegistryKeyNotFoundException
61 RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey
) :
68 //-----------------------------------------------------
69 /** Create an instances of the specified Registry key,
70 the key is assumed to be already opened.
72 RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey
, HKEY SubKey
, const std::wstring
& KeyName
, bool Writeable
) :
76 m_IsWriteable(Writeable
)
80 //-----------------------------------------------------
83 RegistryKeyImpl::~RegistryKeyImpl()
90 //############################################
92 //############################################
95 //-----------------------------------------------------
96 /** The name of the key at hand, maybe empty
97 if this is any of the root keys
99 std::wstring
RegistryKeyImpl::GetName() const
104 //-----------------------------------------------------
107 bool RegistryKeyImpl::IsOpen() const
109 return m_hSubKey
!= 0;
112 //-----------------------------------------------------
113 /** Is this one of the root keys
118 bool RegistryKeyImpl::IsRootKey() const
120 return (0 == m_KeyName
.length());
123 //-----------------------------------------------------
124 /** Do we have write access on the key at hand
126 bool RegistryKeyImpl::IsWriteable() const
128 return m_IsWriteable
;
131 //-----------------------------------------------------
132 /** Convenience function to determine if the
133 Registry key at hand has the specified
136 @precond IsOpen = true
138 throws RegistryAccessDenyException
140 bool RegistryKeyImpl::HasValue(const std::wstring
& Name
) const
142 StringListPtr names
= GetSubValueNames();
144 StringList::iterator iter_end
= names
->end();
145 StringList::iterator iter
= std::find(names
->begin(), iter_end
, Name
);
147 return (iter
!= iter_end
);
150 struct CompareNamesCaseInsensitive
152 CompareNamesCaseInsensitive(const std::wstring
& Name
) :
156 bool operator() (const std::wstring
& value
)
158 return (0 == StrCmpI(value
.c_str(), name_
.c_str()));
164 //-----------------------------------------------------
165 /** Convenience function to determine if the
166 Registry key at hand has the specified
169 @precond IsOpen = true
171 throws RegistryAccessDenyException
173 bool RegistryKeyImpl::HasSubKey(const std::wstring
& Name
) const
175 StringListPtr names
= GetSubKeyNames();
177 StringList::iterator iter_end
= names
->end();
178 StringList::iterator iter
= std::find_if(names
->begin(), iter_end
, CompareNamesCaseInsensitive(Name
));
180 return (iter
!= iter_end
);
183 //-----------------------------------------------------
186 void RegistryKeyImpl::Close()
188 if (RegCloseKey(m_hSubKey
) != ERROR_SUCCESS
) {
193 m_IsWriteable
= false;
196 //-----------------------------------------------------
197 /** Copies the specified value from RegistryKey to
198 the registry key at hand, if a value with this
199 name already exist under the registry key at hand
200 it will be overwritten
202 @precond IsOpen = true
204 RegistryKey.HasSubValue(Name) = true
206 @throws RegistryIOException
207 RegistryWriteAccessDeniedException
208 RegistryValueNotFoundException
210 void RegistryKeyImpl::CopyValue(const RegistryKey
& RegistryKey
, const std::wstring
& Name
)
212 assert(RegistryKey
->HasValue(Name
));
214 SetValue((const RegistryValue
&)(RegistryKey
->GetValue(Name
)));
216 SetValue(RegistryKey
->GetValue(Name
));
218 assert(HasValue(Name
));
221 /** Copies the specified value from RegistryKey to
222 the registry key at hand under a new name,
223 if a value with this name already exist there
224 it will be overwritten
226 @precond IsOpen = true
228 RegistryKey.HasSubValue(Name) = true
230 @throws RegistryIOException
231 RegistryWriteAccessDeniedException
232 RegistryValueNotFoundException
234 void RegistryKeyImpl::CopyValue(const RegistryKey
& RegistryKey
, const std::wstring
& Name
, const std::wstring
& NewName
)
236 assert(RegistryKey
->HasValue(Name
));
238 RegistryValue RegVal
= RegistryKey
->GetValue(Name
);
239 RegVal
->SetName(NewName
);
242 assert(HasValue(NewName
));
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */