Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / setup_native / source / win32 / customactions / reg4msdoc / registry.cxx
blobbc277997b92b4c1c3a834cdff3b17805250e6dfb
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"
31 #include <Shlwapi.h>
32 #include <assert.h>
33 #include <algorithm>
35 #ifdef _MSC_VER
36 #pragma warning(disable : 4786 4350)
37 #endif
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) :
47 m_hRootKey(RootKey),
48 m_hSubKey(0),
49 m_KeyName(KeyName),
50 m_IsWriteable(false)
54 //-----------------------------------------------------
55 /** Create instance and open the specified Registry key
57 @throws RegistryWriteAccessDenyException
58 RegistryAccessDenyException
59 RegistryKeyNotFoundException
61 RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey) :
62 m_hRootKey(RootKey),
63 m_hSubKey(0),
64 m_IsWriteable(false)
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) :
73 m_hRootKey(RootKey),
74 m_hSubKey(SubKey),
75 m_KeyName(KeyName),
76 m_IsWriteable(Writeable)
80 //-----------------------------------------------------
81 /**
83 RegistryKeyImpl::~RegistryKeyImpl()
85 if (IsOpen())
86 Close();
90 //############################################
91 // Queries
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
101 return m_KeyName;
104 //-----------------------------------------------------
107 bool RegistryKeyImpl::IsOpen() const
109 return m_hSubKey != 0;
112 //-----------------------------------------------------
113 /** Is this one of the root keys
114 HKEY_CLASSES_ROOT
115 HKEY_CURRENT_USER
116 etc.
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
134 value
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) :
153 name_(Name)
156 bool operator() (const std::wstring& value)
158 return (0 == StrCmpI(value.c_str(), name_.c_str()));
161 std::wstring name_;
164 //-----------------------------------------------------
165 /** Convenience function to determine if the
166 Registry key at hand has the specified
167 sub-key
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) {
189 assert(false);
192 m_hSubKey = 0;
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
203 IsWriteable = 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));
213 #ifdef __MINGW32__
214 SetValue((const RegistryValue&)(RegistryKey->GetValue(Name)));
215 #else
216 SetValue(RegistryKey->GetValue(Name));
217 #endif
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
227 IsWriteable = 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);
240 SetValue(RegVal);
242 assert(HasValue(NewName));
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */