update dev300-m58
[ooovba.git] / svtools / source / control / reginfo.cxx
blob34ce2865560c360ab7974928451ae18591f29896
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: reginfo.cxx,v $
10 * $Revision: 1.8 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include "reginfo.hxx"
35 #include <tools/debug.hxx>
36 #include <osl/thread.h>
38 #define MAXREGVALUE 200
40 // *****************************************************************************
41 #if defined(WIN) || defined(WNT)
43 #include <tools/svwin.h>
45 #define DBG_HDL DBG_ASSERT(pImp->bValidGroup, "Keine Gruppe gesetzt"); \
46 if( !pImp->bValidGroup ) return
48 struct RegInfo_Impl
50 HKEY aGroupHdl;
51 BOOL bValidGroup;
54 RegInfo::RegInfo()
56 pImp=new RegInfo_Impl;
57 pImp->bValidGroup = FALSE;
60 RegInfo::~RegInfo()
62 if(pImp->bValidGroup)
63 RegCloseKey( pImp->aGroupHdl );
64 delete pImp;
67 String RegInfo::GetKeyName( USHORT nKey ) const
69 DBG_HDL String::EmptyString();
70 char aBuffer[MAXREGVALUE];
71 RegEnumKey( pImp->aGroupHdl, nKey, aBuffer, MAXREGVALUE );
72 return String( UniString::CreateFromAscii(aBuffer) );
75 USHORT RegInfo::GetKeyCount() const
77 DBG_HDL 0;
78 #ifdef WNT
79 DWORD nKeys;
80 DWORD Dum1=10, Dum2, Dum3, Dum4, Dum5, Dum6, Dum7;
81 char s[10];
82 FILETIME aDumFileTime;
83 RegQueryInfoKey( pImp->aGroupHdl, s, &Dum1, 0, &nKeys, &Dum2, &Dum3,
84 &Dum4, &Dum5, &Dum6, &Dum7, &aDumFileTime );
85 return (USHORT) nKeys;
86 #else
87 char aBuffer[MAXREGVALUE];
88 USHORT n=0;
89 while(RegEnumKey(
90 pImp->aGroupHdl, n, aBuffer, MAXREGVALUE) == ERROR_SUCCESS)
91 n++;
92 return n;
93 #endif
96 inline String MakeAppGroupString_Impl( const String &rGroup )
98 String aGroup( UniString::CreateFromAscii("SvAppGroups\\") );
99 aGroup+=rGroup;
100 return aGroup;
103 void RegInfo::SetAppGroup( const String& rGroup )
105 aCurrentGroup = MakeAppGroupString_Impl(rGroup);
106 if( pImp->bValidGroup )
108 RegCloseKey( pImp->aGroupHdl );
109 pImp->bValidGroup = FALSE;
111 ByteString aBStr( aCurrentGroup, osl_getThreadTextEncoding() );
112 RegCreateKey( HKEY_CLASSES_ROOT, aBStr.GetBuffer(), &pImp->aGroupHdl );
113 pImp->bValidGroup = TRUE;
116 void RegInfo::DeleteAppGroup( const String &rGroup )
118 String aOldGroup = aCurrentGroup;
119 SetAppGroup( rGroup );
120 DBG_HDL;
121 USHORT nMax = GetKeyCount();
122 for( USHORT n = nMax; n--; )
124 String aKey( GetKeyName( n ));
125 DeleteKey( aKey );
127 RegCloseKey( pImp->aGroupHdl );
129 ByteString aBStr( rGroup, osl_getThreadTextEncoding() );
130 RegDeleteKey( HKEY_CLASSES_ROOT, aBStr.GetBuffer() );
131 pImp->bValidGroup = FALSE;
132 if( rGroup != aOldGroup )
133 SetAppGroup( aOldGroup );
136 BOOL ReadKey_Impl( const String& rKey,
137 HKEY aHdl, String& rResult )
139 char s[MAXREGVALUE];
140 LONG aLen=MAXREGVALUE;
142 ByteString aBStr( rKey, osl_getThreadTextEncoding() );
143 LONG nRes = RegQueryValue( aHdl, aBStr.GetBuffer(), s, &aLen);
144 if(nRes == ERROR_SUCCESS)
146 rResult = UniString::CreateFromAscii(s);
147 return TRUE;
149 else
150 return FALSE;
153 String RegInfo::ReadKey( const String& rKey ) const
155 DBG_HDL String::EmptyString();
156 String aRes;
157 if(ReadKey_Impl( rKey, pImp->aGroupHdl, aRes))
158 return aRes;
159 else
160 return String::EmptyString();
163 String RegInfo::ReadKey( const String& rKey, const String &rDefault ) const
165 DBG_HDL String::EmptyString();
166 String aRes;
167 if(ReadKey_Impl( rKey, pImp->aGroupHdl, aRes))
168 return aRes;
169 else
170 return rDefault;
173 void RegInfo::WriteKey( const String& rKey, const String& rValue )
175 DBG_HDL;
176 ByteString aBStr( rKey, osl_getThreadTextEncoding() );
177 ByteString aBStr1( rValue, osl_getThreadTextEncoding() );
178 RegSetValue( pImp->aGroupHdl, aBStr.GetBuffer(), REG_SZ, aBStr1.GetBuffer(), 0);
182 void RegInfo::DeleteKey( const String& rKey )
184 DBG_HDL;
185 ByteString aBStr( rKey, osl_getThreadTextEncoding() );
186 RegDeleteKey( pImp->aGroupHdl, aBStr.GetBuffer() );
189 // *****************************************************************************
190 #elif defined(OS2)
192 #define INCL_WINSHELLDATA
193 #include <svpm.h>
195 struct RegInfo_Impl
197 char *pKeyList;
198 String aCurrentApp;
199 void BuildKeyList( const String &rGroup );
202 void RegInfo_Impl::BuildKeyList( const String &rGroup )
204 ByteString aBStr( rGroup, osl_getThreadTextEncoding() );
205 USHORT nLen = 0;
208 nLen+=1000;
209 delete[] pKeyList;
210 pKeyList = new char[nLen];
211 *(int *)pKeyList = 0;
213 while( PrfQueryProfileString(
214 HINI_USERPROFILE, (PCSZ)aBStr.GetBuffer(),
215 0, 0, pKeyList, nLen) == nLen);
219 RegInfo::RegInfo()
221 pImp=new RegInfo_Impl;
222 pImp->pKeyList = 0;
225 RegInfo::~RegInfo()
227 delete[] pImp->pKeyList;
228 delete pImp;
231 inline String MakeAppGroupString_Impl( const String &rGroup )
233 String aGroup(UniString::CreateFromAscii("SvAppGroups:"));
234 aGroup+=rGroup;
235 return aGroup;
238 String RegInfo::GetKeyName( USHORT nKey ) const
240 if( !pImp->pKeyList )
241 pImp->BuildKeyList(pImp->aCurrentApp);
243 const char *pc=pImp->pKeyList;
244 for( USHORT n=0; n<nKey; n++ )
245 while(*pc++);
247 return String(UniString::CreateFromAscii(pc));
250 USHORT RegInfo::GetKeyCount() const
252 if( !pImp->pKeyList )
253 pImp->BuildKeyList( pImp->aCurrentApp);
255 const char *pc=pImp->pKeyList;
256 USHORT nRet=0;
257 while(*pc)
259 while(*pc++);
260 nRet++;
262 return nRet;
265 void RegInfo::SetAppGroup( const String& rGroup )
267 delete[] pImp->pKeyList;
268 pImp->pKeyList = 0;
269 aCurrentGroup = rGroup;
270 pImp->aCurrentApp = MakeAppGroupString_Impl( rGroup );
273 void RegInfo::DeleteAppGroup( const String &rGroup )
275 ByteString aBStr( MakeAppGroupString_Impl( rGroup ), osl_getThreadTextEncoding() );
276 PrfWriteProfileString(
277 HINI_USERPROFILE, (PCSZ)aBStr.GetBuffer(), 0, 0);
281 String RegInfo::ReadKey( const String& rKey ) const
283 ULONG ulBufferMax = MAXREGVALUE;
284 char *pBuffer= new char[MAXREGVALUE];
285 ByteString aBStr( pImp->aCurrentApp, osl_getThreadTextEncoding() );
286 ByteString aBStr1( rKey, osl_getThreadTextEncoding() );
287 *pBuffer=0;
288 PrfQueryProfileData(
289 HINI_USERPROFILE, (PCSZ)aBStr.GetBuffer(), (PCSZ)aBStr1.GetBuffer(), pBuffer, &ulBufferMax);
290 String aRet(UniString::CreateFromAscii(pBuffer));
291 delete[] pBuffer;
292 return aRet;
296 String RegInfo::ReadKey( const String& rKey, const String &rDefault ) const
298 String aResult = ReadKey(rKey);
299 if (!aResult.Len())
300 return rDefault;
301 else
302 return aResult;
306 void RegInfo::WriteKey( const String& rKey, const String& rValue )
308 ByteString aBStr( pImp->aCurrentApp, osl_getThreadTextEncoding() );
309 ByteString aBStr1( rKey, osl_getThreadTextEncoding() );
310 PrfWriteProfileData(
311 HINI_USERPROFILE, (PCSZ)aBStr.GetBuffer(), (PCSZ)aBStr1.GetBuffer(), (PVOID)rValue.GetBuffer(), rValue.Len()*2);
314 void RegInfo::DeleteKey( const String& rKey )
316 ByteString aBStr( pImp->aCurrentApp, osl_getThreadTextEncoding() );
317 ByteString aBStr1( rKey, osl_getThreadTextEncoding() );
318 PrfWriteProfileString(
319 HINI_USERPROFILE, (PCSZ)aBStr.GetBuffer(), (PCSZ)rKey.GetBuffer(), 0);
322 // *****************************************************************************
324 #else
326 RegInfo::RegInfo()
331 RegInfo::~RegInfo()
335 String RegInfo::GetKeyName( USHORT ) const
337 return String::EmptyString();
340 USHORT RegInfo::GetKeyCount() const
342 return 0;
345 void RegInfo::SetAppGroup( const String& )
347 return ;
350 void RegInfo::DeleteAppGroup( const String& )
352 return;
355 String RegInfo::ReadKey( const String& ) const
357 return String::EmptyString();
360 String RegInfo::ReadKey( const String&, const String& ) const
362 return String::EmptyString();
365 void RegInfo::WriteKey( const String&, const String& )
367 return;
370 void RegInfo::DeleteKey( const String& )
372 return;
375 #endif