]>
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"
30 #include "wx/config.h"
34 #include "wx/msw/registry.h"
35 #include "wx/msw/regconf.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // we put our data in HKLM\SOFTWARE_KEY\appname
42 #define SOFTWARE_KEY wxString("Software\\")
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // get the value if the key is opened and it exists
49 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
51 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
54 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
56 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
59 // ============================================================================
61 // ============================================================================
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // create the config object which stores its data under HKCU\vendor\app and, if
68 // style & wxCONFIG_USE_GLOBAL_FILE, under HKLM\vendor\app
69 wxRegConfig::wxRegConfig(const wxString
& appName
, const wxString
& vendorName
,
70 const wxString
& strLocal
, const wxString
& strGlobal
,
72 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
)
76 bool bDoUseGlobal
= (style
& wxCONFIG_USE_GLOBAL_FILE
) != 0;
78 // the convention is to put the programs keys under <vendor>\<appname>
79 // (but it can be overriden by specifying the pathes explicitly in strLocal
81 if ( strLocal
.IsEmpty() || (strGlobal
.IsEmpty() && bDoUseGlobal
) )
83 if ( vendorName
.IsEmpty() )
86 strRoot
= wxTheApp
->GetVendorName();
93 // no '\\' needed if no vendor name
94 if ( !strRoot
.IsEmpty() )
99 if ( appName
.IsEmpty() )
101 wxCHECK_RET( wxTheApp
, T("No application name in wxRegConfig ctor!") );
102 strRoot
<< wxTheApp
->GetAppName();
109 //else: we don't need to do all the complicated stuff above
111 wxString str
= strLocal
.IsEmpty() ? strRoot
: strLocal
;
112 m_keyLocalRoot
.SetName(wxRegKey::HKCU
, SOFTWARE_KEY
+ str
);
113 m_keyLocal
.SetName(m_keyLocalRoot
, "");
117 str
= strGlobal
.IsEmpty() ? strRoot
: strGlobal
;
118 m_keyGlobalRoot
.SetName(wxRegKey::HKLM
, SOFTWARE_KEY
+ str
);
119 m_keyGlobal
.SetName(m_keyGlobalRoot
, "");
122 // Create() will Open() if key already exists
123 m_keyLocalRoot
.Create();
125 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
128 // OTOH, this key may perfectly not exist, so suppress error messages the call
129 // to Open() might generate
133 m_keyGlobalRoot
.Open();
137 wxRegConfig::~wxRegConfig()
139 // nothing to do - key will be closed in their dtors
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
145 void wxRegConfig::SetPath(const wxString
& strPath
)
147 wxArrayString aParts
;
149 // because GetPath() returns "" when we're at root, we must understand
150 // empty string as "/"
151 if ( strPath
.IsEmpty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
153 wxSplitPath(aParts
, strPath
);
156 // relative path, combine with current one
157 wxString strFullPath
= GetPath();
158 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
159 wxSplitPath(aParts
, strFullPath
);
162 // recombine path parts in one variable
165 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
166 strRegPath
<< '\\' << aParts
[n
];
167 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
170 // change current key(s)
171 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
172 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
179 // ----------------------------------------------------------------------------
180 // enumeration (works only with current group)
181 // ----------------------------------------------------------------------------
184 We want to enumerate all local keys/values after the global ones, but, of
185 course, we don't want to repeat a key which appears locally as well as
188 We use the 15th bit of lIndex for distinction between global and local.
191 #define LOCAL_MASK 0x8000
192 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
194 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
197 return GetNextGroup(str
, lIndex
);
200 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
202 // are we already enumerating local entries?
203 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
204 // try to find a global entry which doesn't appear locally
206 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
207 // no more global entries
208 lIndex
|= LOCAL_MASK
;
211 } while( m_keyLocal
.HasSubKey(str
) );
214 // much easier with local entries: get the next one we find
215 // (don't forget to clear our flag bit and set it again later)
216 lIndex
&= ~LOCAL_MASK
;
217 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
218 lIndex
|= LOCAL_MASK
;
223 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
226 return GetNextEntry(str
, lIndex
);
229 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
231 // are we already enumerating local entries?
232 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
233 // try to find a global entry which doesn't appear locally
235 if ( !m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
236 // no more global entries
237 lIndex
|= LOCAL_MASK
;
240 } while( m_keyLocal
.HasValue(str
) );
243 // much easier with local entries: get the next one we find
244 // (don't forget to clear our flag bit and set it again later)
245 lIndex
&= ~LOCAL_MASK
;
246 bool bOk
= m_keyLocal
.GetNextValue(str
, lIndex
);
247 lIndex
|= LOCAL_MASK
;
252 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive
) const
259 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
263 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
269 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
276 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
280 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
286 // ----------------------------------------------------------------------------
287 // tests for existence
288 // ----------------------------------------------------------------------------
290 bool wxRegConfig::HasGroup(const wxString
& key
) const
292 wxConfigPathChanger
path(this, key
);
294 wxString
strName(path
.Name());
296 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
299 bool wxRegConfig::HasEntry(const wxString
& key
) const
301 wxConfigPathChanger
path(this, key
);
303 wxString
strName(path
.Name());
305 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
308 wxConfigBase::EntryType
wxRegConfig::GetEntryType(const wxString
& key
) const
310 wxConfigPathChanger
path(this, key
);
312 wxString
strName(path
.Name());
315 if ( m_keyLocal
.HasValue(strName
) )
316 isNumeric
= m_keyLocal
.IsNumericValue(strName
);
317 else if ( m_keyGlobal
.HasValue(strName
) )
318 isNumeric
= m_keyGlobal
.IsNumericValue(strName
);
320 return wxConfigBase::Type_Unknown
;
322 return isNumeric
? wxConfigBase::Type_Integer
: wxConfigBase::Type_String
;
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
329 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
331 wxConfigPathChanger
path(this, key
);
333 bool bQueryGlobal
= TRUE
;
335 // if immutable key exists in global key we must check that it's not
336 // overriden by the local key with the same name
337 if ( IsImmutable(path
.Name()) ) {
338 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
339 if ( m_keyLocal
.HasValue(path
.Name()) ) {
340 wxLogWarning(T("User value for immutable key '%s' ignored."),
341 path
.Name().c_str());
343 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
347 // don't waste time - it's not there anyhow
348 bQueryGlobal
= FALSE
;
352 // first try local key
353 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
354 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
357 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
364 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
365 const wxString
& szDefault
) const
367 wxConfigPathChanger
path(this, key
);
369 bool bQueryGlobal
= TRUE
;
371 // if immutable key exists in global key we must check that it's not
372 // overriden by the local key with the same name
373 if ( IsImmutable(path
.Name()) ) {
374 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
375 if ( m_keyLocal
.HasValue(path
.Name()) ) {
376 wxLogWarning(T("User value for immutable key '%s' ignored."),
377 path
.Name().c_str());
383 // don't waste time - it's not there anyhow
384 bQueryGlobal
= FALSE
;
388 // first try local key
389 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
390 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
391 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
395 if ( IsRecordingDefaults() ) {
396 ((wxRegConfig
*)this)->Write(key
, szDefault
);
403 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
408 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
410 wxConfigPathChanger
path(this, key
);
412 bool bQueryGlobal
= TRUE
;
414 // if immutable key exists in global key we must check that it's not
415 // overriden by the local key with the same name
416 if ( IsImmutable(path
.Name()) ) {
417 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
418 if ( m_keyLocal
.HasValue(path
.Name()) ) {
419 wxLogWarning(T("User value for immutable key '%s' ignored."),
420 path
.Name().c_str());
426 // don't waste time - it's not there anyhow
427 bQueryGlobal
= FALSE
;
431 // first try local key
432 if ( TryGetValue(m_keyLocal
, path
.Name(), plResult
) ||
433 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
439 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
441 wxConfigPathChanger
path(this, key
);
443 if ( IsImmutable(path
.Name()) ) {
444 wxLogError(T("Can't change immutable entry '%s'."), path
.Name().c_str());
448 return m_keyLocal
.SetValue(path
.Name(), szValue
);
451 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
453 wxConfigPathChanger
path(this, key
);
455 if ( IsImmutable(path
.Name()) ) {
456 wxLogError(T("Can't change immutable entry '%s'."), path
.Name().c_str());
460 return m_keyLocal
.SetValue(path
.Name(), lValue
);
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
469 // check that the old entry exists...
470 if ( !HasEntry(oldName
) )
473 // and that the new one doesn't
474 if ( HasEntry(newName
) )
477 // delete the old entry and create the new one - but do in the reverse
478 // order to not lose the data if Create() fails
481 if ( m_keyLocal
.IsNumericValue(oldName
) )
484 ok
= m_keyLocal
.QueryValue(oldName
, &val
) &&
485 m_keyLocal
.SetValue(newName
, val
);
490 ok
= m_keyLocal
.QueryValue(oldName
, val
) &&
491 m_keyLocal
.SetValue(newName
, val
);
497 if ( !m_keyLocal
.DeleteValue(oldName
) )
499 m_keyLocal
.DeleteValue(newName
);
507 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
509 // check that the old group exists...
510 if ( !HasGroup(oldName
) )
513 // and that the new one doesn't
514 if ( HasGroup(newName
) )
517 // TODO there is no way to rename a registry key - we must do a deep copy
519 wxFAIL_MSG(T("Registry key renaming not implemented"));
524 // ----------------------------------------------------------------------------
526 // ----------------------------------------------------------------------------
527 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
529 wxConfigPathChanger
path(this, value
);
531 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
534 if ( m_keyLocal
.IsEmpty() ) {
535 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
536 SetPath(".."); // changes m_keyLocal
537 return m_keyLocal
.DeleteKey(strKey
);
543 bool wxRegConfig::DeleteGroup(const wxString
& key
)
545 wxConfigPathChanger
path(this, key
);
547 return m_keyLocal
.DeleteKey(path
.Name());
550 bool wxRegConfig::DeleteAll()
555 bool bOk
= m_keyLocalRoot
.DeleteSelf();
557 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
558 // otherwise we will delete HKEY_CLASSES_ROOT recursively
559 if ( bOk
&& m_keyGlobalRoot
.IsOpened() )
560 bOk
= m_keyGlobalRoot
.DeleteSelf();