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