]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/regconf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/regconf.cpp
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "regconf.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/string.h"
33 #include "wx/config.h"
37 #include "wx/msw/registry.h"
38 #include "wx/msw/regconf.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // we put our data in HKLM\SOFTWARE_KEY\appname
45 #define SOFTWARE_KEY wxString("Software\\")
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // get the value if the key is opened and it exists
52 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
54 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
57 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
59 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // create the config object which stores its data under HKCU\vendor\app and, if
71 // style & wxCONFIG_USE_GLOBAL_FILE, under HKLM\vendor\app
72 wxRegConfig::wxRegConfig(const wxString
& appName
, const wxString
& vendorName
,
73 const wxString
& strLocal
, const wxString
& strGlobal
,
75 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
)
79 bool bDoUseGlobal
= (style
& wxCONFIG_USE_GLOBAL_FILE
) != 0;
81 // the convention is to put the programs keys under <vendor>\<appname>
82 // (but it can be overriden by specifying the pathes explicitly in strLocal
84 if ( strLocal
.IsEmpty() || (strGlobal
.IsEmpty() && bDoUseGlobal
) )
86 if ( vendorName
.IsEmpty() )
89 strRoot
= wxTheApp
->GetVendorName();
96 // no '\\' needed if no vendor name
97 if ( !strRoot
.IsEmpty() )
102 if ( appName
.IsEmpty() )
104 wxCHECK_RET( wxTheApp
, wxT("No application name in wxRegConfig ctor!") );
105 strRoot
<< wxTheApp
->GetAppName();
112 //else: we don't need to do all the complicated stuff above
114 wxString str
= strLocal
.IsEmpty() ? strRoot
: strLocal
;
115 m_keyLocalRoot
.SetName(wxRegKey::HKCU
, SOFTWARE_KEY
+ str
);
116 m_keyLocal
.SetName(m_keyLocalRoot
, "");
120 str
= strGlobal
.IsEmpty() ? strRoot
: strGlobal
;
121 m_keyGlobalRoot
.SetName(wxRegKey::HKLM
, SOFTWARE_KEY
+ str
);
122 m_keyGlobal
.SetName(m_keyGlobalRoot
, "");
125 // Create() will Open() if key already exists
126 m_keyLocalRoot
.Create();
128 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
131 // OTOH, this key may perfectly not exist, so suppress error messages the call
132 // to Open() might generate
136 m_keyGlobalRoot
.Open();
140 wxRegConfig::~wxRegConfig()
142 // nothing to do - key will be closed in their dtors
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
148 void wxRegConfig::SetPath(const wxString
& strPath
)
150 wxArrayString aParts
;
152 // because GetPath() returns "" when we're at root, we must understand
153 // empty string as "/"
154 if ( strPath
.IsEmpty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
156 wxSplitPath(aParts
, strPath
);
159 // relative path, combine with current one
160 wxString strFullPath
= GetPath();
161 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
162 wxSplitPath(aParts
, strFullPath
);
165 // recombine path parts in one variable
166 wxString strOldPath
= m_strPath
, strRegPath
;
168 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
169 strRegPath
<< '\\' << aParts
[n
];
170 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
173 if ( m_strPath
== strOldPath
)
176 // as we create the registry key when SetPath(key) is done, we can be left
177 // with plenty of empty keys if this was only done to try to read some value
178 // which, in fact, doesn't exist - to prevent this from happening we
179 // automatically delete the old key if it was empty
180 if ( m_keyLocal
.IsEmpty() )
182 m_keyLocal
.DeleteSelf();
185 // change current key(s)
186 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
187 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
194 // ----------------------------------------------------------------------------
195 // enumeration (works only with current group)
196 // ----------------------------------------------------------------------------
199 We want to enumerate all local keys/values after the global ones, but, of
200 course, we don't want to repeat a key which appears locally as well as
203 We use the 15th bit of lIndex for distinction between global and local.
206 #define LOCAL_MASK 0x8000
207 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
209 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
212 return GetNextGroup(str
, lIndex
);
215 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
217 // are we already enumerating local entries?
218 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
219 // try to find a global entry which doesn't appear locally
220 while ( m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
221 if ( !m_keyLocal
.HasSubKey(str
) ) {
222 // ok, found one - return it
227 // no more global entries
228 lIndex
|= LOCAL_MASK
;
231 // much easier with local entries: get the next one we find
232 // (don't forget to clear our flag bit and set it again later)
233 lIndex
&= ~LOCAL_MASK
;
234 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
235 lIndex
|= LOCAL_MASK
;
240 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
243 return GetNextEntry(str
, lIndex
);
246 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
248 // are we already enumerating local entries?
249 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
250 // try to find a global entry which doesn't appear locally
252 if ( !m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
253 // no more global entries
254 lIndex
|= LOCAL_MASK
;
257 } while( m_keyLocal
.HasValue(str
) );
260 // much easier with local entries: get the next one we find
261 // (don't forget to clear our flag bit and set it again later)
262 lIndex
&= ~LOCAL_MASK
;
263 bool bOk
= m_keyLocal
.GetNextValue(str
, lIndex
);
264 lIndex
|= LOCAL_MASK
;
269 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive
) const
276 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
280 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
286 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
293 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
297 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
303 // ----------------------------------------------------------------------------
304 // tests for existence
305 // ----------------------------------------------------------------------------
307 bool wxRegConfig::HasGroup(const wxString
& key
) const
309 wxConfigPathChanger
path(this, key
);
311 wxString
strName(path
.Name());
313 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
316 bool wxRegConfig::HasEntry(const wxString
& key
) const
318 wxConfigPathChanger
path(this, key
);
320 wxString
strName(path
.Name());
322 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
325 wxConfigBase::EntryType
wxRegConfig::GetEntryType(const wxString
& key
) const
327 wxConfigPathChanger
path(this, key
);
329 wxString
strName(path
.Name());
332 if ( m_keyLocal
.HasValue(strName
) )
333 isNumeric
= m_keyLocal
.IsNumericValue(strName
);
334 else if ( m_keyGlobal
.HasValue(strName
) )
335 isNumeric
= m_keyGlobal
.IsNumericValue(strName
);
337 return wxConfigBase::Type_Unknown
;
339 return isNumeric
? wxConfigBase::Type_Integer
: wxConfigBase::Type_String
;
342 // ----------------------------------------------------------------------------
344 // ----------------------------------------------------------------------------
346 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
348 wxConfigPathChanger
path(this, key
);
350 bool bQueryGlobal
= TRUE
;
352 // if immutable key exists in global key we must check that it's not
353 // overriden by the local key with the same name
354 if ( IsImmutable(path
.Name()) ) {
355 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
356 if ( m_keyLocal
.HasValue(path
.Name()) ) {
357 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
358 path
.Name().c_str());
360 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
364 // don't waste time - it's not there anyhow
365 bQueryGlobal
= FALSE
;
369 // first try local key
370 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
371 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
374 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
381 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
382 const wxString
& szDefault
) const
384 wxConfigPathChanger
path(this, key
);
386 bool bQueryGlobal
= TRUE
;
388 // if immutable key exists in global key we must check that it's not
389 // overriden by the local key with the same name
390 if ( IsImmutable(path
.Name()) ) {
391 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
392 if ( m_keyLocal
.HasValue(path
.Name()) ) {
393 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
394 path
.Name().c_str());
400 // don't waste time - it's not there anyhow
401 bQueryGlobal
= FALSE
;
405 // first try local key
406 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
407 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
408 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
412 if ( IsRecordingDefaults() ) {
413 ((wxRegConfig
*)this)->Write(key
, szDefault
);
420 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
425 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
427 wxConfigPathChanger
path(this, key
);
429 bool bQueryGlobal
= TRUE
;
431 // if immutable key exists in global key we must check that it's not
432 // overriden by the local key with the same name
433 if ( IsImmutable(path
.Name()) ) {
434 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
435 if ( m_keyLocal
.HasValue(path
.Name()) ) {
436 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
437 path
.Name().c_str());
443 // don't waste time - it's not there anyhow
444 bQueryGlobal
= FALSE
;
448 // first try local key
449 if ( TryGetValue(m_keyLocal
, path
.Name(), plResult
) ||
450 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
456 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
458 wxConfigPathChanger
path(this, key
);
460 if ( IsImmutable(path
.Name()) ) {
461 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
465 return m_keyLocal
.SetValue(path
.Name(), szValue
);
468 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
470 wxConfigPathChanger
path(this, key
);
472 if ( IsImmutable(path
.Name()) ) {
473 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
477 return m_keyLocal
.SetValue(path
.Name(), lValue
);
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
484 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
486 // check that the old entry exists...
487 if ( !HasEntry(oldName
) )
490 // and that the new one doesn't
491 if ( HasEntry(newName
) )
494 // delete the old entry and create the new one - but do in the reverse
495 // order to not lose the data if Create() fails
498 if ( m_keyLocal
.IsNumericValue(oldName
) )
501 ok
= m_keyLocal
.QueryValue(oldName
, &val
) &&
502 m_keyLocal
.SetValue(newName
, val
);
507 ok
= m_keyLocal
.QueryValue(oldName
, val
) &&
508 m_keyLocal
.SetValue(newName
, val
);
514 if ( !m_keyLocal
.DeleteValue(oldName
) )
516 m_keyLocal
.DeleteValue(newName
);
524 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
526 // check that the old group exists...
527 if ( !HasGroup(oldName
) )
530 // and that the new one doesn't
531 if ( HasGroup(newName
) )
534 // TODO there is no way to rename a registry key - we must do a deep copy
536 wxFAIL_MSG(wxT("Registry key renaming not implemented"));
541 // ----------------------------------------------------------------------------
543 // ----------------------------------------------------------------------------
544 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
546 wxConfigPathChanger
path(this, value
);
548 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
551 if ( m_keyLocal
.IsEmpty() ) {
552 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
553 SetPath(".."); // changes m_keyLocal
554 return m_keyLocal
.DeleteKey(strKey
);
560 bool wxRegConfig::DeleteGroup(const wxString
& key
)
562 wxConfigPathChanger
path(this, key
);
564 return m_keyLocal
.DeleteKey(path
.Name());
567 bool wxRegConfig::DeleteAll()
572 bool bOk
= m_keyLocalRoot
.DeleteSelf();
574 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
575 // otherwise we will delete HKEY_CLASSES_ROOT recursively
576 if ( bOk
&& m_keyGlobalRoot
.IsOpened() )
577 bOk
= m_keyGlobalRoot
.DeleteSelf();