1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _REGISTRY_HXX_
29 #define _REGISTRY_HXX_
30 
31 #ifdef _MSC_VER
32 #pragma warning(push, 1) /* disable warnings within system headers */
33 #endif
34 #include <windows.h>
35 #ifdef _MSC_VER
36 #pragma warning(pop)
37 #endif
38 
39 #include <memory>
40 #include <vector>
41 #include <string>
42 
43 #include "registryvalueimpl.hxx"
44 
45 //---------------------------------------
46 // forward declaration
47 //---------------------------------------
48 
49 class RegistryKeyImpl;
50 
51 //---------------------------------------
52 // typedefs
53 //---------------------------------------
54 
55 typedef std::auto_ptr<RegistryKeyImpl>  	RegistryKey;
56 typedef std::vector<std::wstring>				StringList;
57 typedef std::auto_ptr<StringList>				StringListPtr;
58 
59 //---------------------------------------
60 //
61 //---------------------------------------
62 
63 class RegistryKeyImpl
64 {
65 public:
66 
67 	//############################################
68 	// Destruction
69 	//############################################
70 
71     virtual ~RegistryKeyImpl();
72 
73 
74 	//############################################
75 	// Queries
76 	//############################################
77 
78 
79 	/** The name of the key at hand, maybe empty
80 		if this is any of the root keys
81 	*/
82 	std::wstring GetName() const;
83 
84 	/** The number of sub values of the key at hand
85 
86 		@precond IsOpen = true
87 
88 		@throws
89 	*/
90 	virtual size_t GetSubValueCount() const = 0;
91 
92 	/** The number of sub-keys of the key at hand
93 
94 		@precond IsOpen = true
95 
96 		@throws
97 	*/
98 	virtual size_t GetSubKeyCount() const = 0;
99 
100 	bool IsOpen() const;
101 
102 	/** Do we have write access on the key at hand
103 	*/
104 	bool IsWriteable() const;
105 
106 	/** The StringList will be allocated on the heap,
107 		so this is in fact a transfer of ownership
108 		to the caller
109 
110 		@precond IsOpen = true
111 
112 		@throws RegistryIOException
113 	*/
114 	virtual StringListPtr GetSubKeyNames() const = 0;
115 
116 	/** The StringList will be allocated on the heap,
117 		so this is in fact a transfer of ownership
118 		to the caller
119 
120 		@precond IsOpen = true
121 
122 		@throws RegistryIOException
123 	*/
124 	virtual StringListPtr GetSubValueNames() const = 0;
125 
126 	/** Get the specified registry value
127 
128 		@precond IsOpen = true
129 	*/
130 	virtual RegistryValue GetValue(const std::wstring& Name) const = 0;
131 
132 	/** Get the specified registry value, return the given
133 		default value if value not found
134 
135 		@precond IsOpen = true
136 	*/
137 	virtual RegistryValue GetValue(const std::wstring& Name, const RegistryValue& Default) const = 0;
138 
139 	/** Convenience function to determine if the
140 		Registry key at hand has the specified
141 		value
142 
143 		@precond IsOpen = true
144 
145 		throws RegistryAccessDenyException
146 	*/
147 	bool HasValue(const std::wstring& Name) const;
148 
149 	/** Convenience function to determine if the
150 		Registry key at hand has the specified
151 		sub-key
152 
153 		@precond IsOpen = true
154 
155 		throws RegistryAccessDenyException
156 	*/
157 	bool HasSubKey(const std::wstring& Name) const;
158 
159 
160 	//############################################
161 	// Commands
162 	//############################################
163 
164 
165 	/** Open the registry key, has no effect if
166 		the key is already open
167 
168 		@precond IsOpen = false
169 
170 		@throws RegistryWriteAccessDenyException
171 				RegistryAccessDenyException
172 	*/
173 	virtual void Open(bool Writeable = true) = 0;
174 
175 	/** Close the registry key at hand, further
176 		using it without re-opening may cause
177 		RegistryIOExceptions to be thrown
178 
179 		This is a template method that calls
180 		ImplClose which has to be overwritten
181 		by sub-classes
182 	*/
183 	void Close();
184 
185 	/** Open the specified sub-key of the registry key
186 		at hand
187 
188 		@precond IsOpen = true
189 				 HasSubKey(Name) = true
190 
191 		@throws RegistryIOException
192 				RegistryKeyNotFoundException
193 				RegistryAccessDeniedException
194 	*/
195 	virtual RegistryKey OpenSubKey(const std::wstring& Name, bool Writeable = true) = 0;
196 
197 	/** Creates a new sub-key below the key at hand
198 
199 		@precond IsOpen = true
200 				 IsWriteable = true
201 
202 		@throws  RegistryIOException
203 				 RegistryWriteAccessDenyException
204 	*/
205 	virtual RegistryKey CreateSubKey(const std::wstring& Name) = 0;
206 
207 	/** Deletes a sub-key below the key at hand, the
208 		key must not have sub-keys
209 
210 		@precond IsOpen = true
211 				 IsWriteable = true
212 
213 		@throws  RegistryIOException
214 				 RegistryWriteAccessDenyException
215 	*/
216 	virtual void DeleteSubKey(const std::wstring& Name) = 0;
217 
218 	/** Deletes a sub-key below the key at hand with all
219 		its sub-keys
220 
221 		@precond IsOpen = true
222 				 IsWriteable = true;
223 
224 		@throws  RegistryIOException
225 				 RegistryWriteAccessDenyException
226 	*/
227 	virtual void DeleteSubKeyTree(const std::wstring& Name) = 0;
228 
229 	/** Delete the specified value
230 
231 		@precond IsOpen = true
232 				 IsWriteable = true
233 				 HasValue(Name) = true
234 
235 		@throws	RegistryIOException
236 				RegistryWriteAccessDeniedException
237 				RegistryValueNotFoundException
238 	*/
239 	virtual void DeleteValue(const std::wstring& Name) = 0;
240 
241 	/** Set the specified registry value
242 
243 		@precond IsOpen = true
244 				 IsWriteable = true
245 
246 		@throws  RegistryIOException
247 				 RegistryWriteAccessDenyException
248 	*/
249 	virtual void SetValue(const RegistryValue& Value) = 0;
250 
251 
252 	/** Copies the specified value from RegistryKey to
253 		the registry key at hand, if a value with this
254 		name already exist under the registry key at hand
255 		it will be overwritten
256 
257 		@precond IsOpen = true
258 				 IsWriteable = true
259 				 RegistryKey.HasSubValue(Name) = true
260 
261 		@throws RegistryIOException
262 				RegistryWriteAccessDeniedException
263 				RegistryValueNotFoundException
264 	*/
265 	virtual void CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name);
266 
267 	/** Copies the specified value from RegistryKey to
268 		the registry key at hand under a new name,
269 		if a value with this name already exist there
270 		it will be overwritten
271 
272 		@precond IsOpen = true
273 				 IsWriteable = true
274 				 RegistryKey.HasSubValue(Name) = true
275 
276 		@throws RegistryIOException
277 				RegistryWriteAccessDeniedException
278 				RegistryValueNotFoundException
279 	*/
280 	virtual void CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name, const std::wstring& NewName);
281 
282 	//############################################
283 	// Creation
284 	// only possible through WindowsRegistry class
285 	//############################################
286 
287 
288 protected:
289 	/** Create instance of the specified Registry key
290 
291 		@throws  RegistryWriteAccessDenyException
292 				 RegistryAccessDenyException
293 				 RegistryKeyNotFoundException
294 	*/
295     RegistryKeyImpl(HKEY RootKey, const std::wstring& KeyName);
296 
297 	/** Create instance of the specified Registry key.
298 		RootKey should only one of the predefined
299 		keys HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,
300 		HKEY_LOCAL_MACHINE, HKEY_USERS
301 
302 		@throws  RegistryWriteAccessDenyException
303 				 RegistryAccessDenyException
304 				 RegistryKeyNotFoundException
305 	*/
306 	RegistryKeyImpl(HKEY RootKey);
307 
308 	/** Create an instances of the specified Registry key,
309 		the key is assumed to be already opened.
310 	*/
311 	RegistryKeyImpl(HKEY RootKey, HKEY SubKey, const std::wstring& KeyName, bool Writeable = true);
312 
313 	/** Is this one of the root keys
314 		HKEY_CLASSES_ROOT
315 		HKEY_CURRENT_USER
316 		etc.
317 	*/
318 	bool IsRootKey() const;
319 
320 protected:
321     HKEY			m_hRootKey;
322     HKEY		    m_hSubKey;
323 	std::wstring	m_KeyName;
324 	bool				m_IsWriteable;
325 
326 // prevent copy and assignment
327 private:
328     RegistryKeyImpl(const RegistryKeyImpl&);
329     RegistryKeyImpl& operator=(const RegistryKeyImpl&);
330 
331 //######################################
332 // Friend declarations
333 //######################################
334 
335 friend class WindowsRegistry;
336 };
337 
338 #endif
339