]>
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
, "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
& strName
) const
292 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
295 bool wxRegConfig::HasEntry(const wxString
& strName
) const
297 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
306 wxConfigPathChanger
path(this, key
);
308 bool bQueryGlobal
= TRUE
;
310 // if immutable key exists in global key we must check that it's not
311 // overriden by the local key with the same name
312 if ( IsImmutable(path
.Name()) ) {
313 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
314 if ( m_keyLocal
.HasValue(path
.Name()) ) {
315 wxLogWarning("User value for immutable key '%s' ignored.",
316 path
.Name().c_str());
318 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
322 // don't waste time - it's not there anyhow
323 bQueryGlobal
= FALSE
;
327 // first try local key
328 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
329 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
332 // TODO: do we return TRUE? Not in original implementation,
333 // but I don't see why not. -- JACS
334 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
341 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
342 const wxString
& szDefault
) const
344 wxConfigPathChanger
path(this, key
);
346 bool bQueryGlobal
= TRUE
;
348 // if immutable key exists in global key we must check that it's not
349 // overriden by the local key with the same name
350 if ( IsImmutable(path
.Name()) ) {
351 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
352 if ( m_keyLocal
.HasValue(path
.Name()) ) {
353 wxLogWarning("User value for immutable key '%s' ignored.",
354 path
.Name().c_str());
360 // don't waste time - it's not there anyhow
361 bQueryGlobal
= FALSE
;
365 // first try local key
366 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
367 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
368 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
372 if ( IsRecordingDefaults() ) {
373 ((wxRegConfig
*)this)->Write(key
, szDefault
);
380 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
385 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
387 wxConfigPathChanger
path(this, key
);
389 bool bQueryGlobal
= TRUE
;
391 // if immutable key exists in global key we must check that it's not
392 // overriden by the local key with the same name
393 if ( IsImmutable(path
.Name()) ) {
394 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
395 if ( m_keyLocal
.HasValue(path
.Name()) ) {
396 wxLogWarning("User value for immutable key '%s' ignored.",
397 path
.Name().c_str());
403 // don't waste time - it's not there anyhow
404 bQueryGlobal
= FALSE
;
408 // first try local key
409 if ( TryGetValue(m_keyLocal
, path
.Name(), plResult
) ||
410 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
416 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
418 wxConfigPathChanger
path(this, key
);
420 if ( IsImmutable(path
.Name()) ) {
421 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
425 return m_keyLocal
.SetValue(path
.Name(), szValue
);
428 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
430 wxConfigPathChanger
path(this, key
);
432 if ( IsImmutable(path
.Name()) ) {
433 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
437 return m_keyLocal
.SetValue(path
.Name(), lValue
);
440 // ----------------------------------------------------------------------------
442 // ----------------------------------------------------------------------------
444 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
446 // check that the old entry exists...
447 if ( !HasEntry(oldName
) )
450 // and that the new one doesn't
451 if ( HasEntry(newName
) )
454 // delete the old entry and create the new one - but do in the reverse
455 // order to not lose the data if Create() fails
458 if ( m_keyLocal
.IsNumericValue(oldName
) )
461 ok
= m_keyLocal
.QueryValue(oldName
, &val
) &&
462 m_keyLocal
.SetValue(newName
, val
);
467 ok
= m_keyLocal
.QueryValue(oldName
, val
) &&
468 m_keyLocal
.SetValue(newName
, val
);
474 if ( !m_keyLocal
.DeleteValue(oldName
) )
476 m_keyLocal
.DeleteValue(newName
);
484 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
486 // check that the old group exists...
487 if ( !HasGroup(oldName
) )
490 // and that the new one doesn't
491 if ( HasGroup(newName
) )
494 // TODO there is no way to rename a registry key - we must do a deep copy
496 wxFAIL_MSG("Registry key renaming not implemented");
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
504 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
506 wxConfigPathChanger
path(this, value
);
508 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
511 if ( !m_keyLocal
.HasSubkeys() ) {
512 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
513 SetPath(".."); // changes m_keyLocal
514 return m_keyLocal
.DeleteKey(strKey
);
520 bool wxRegConfig::DeleteGroup(const wxString
& key
)
522 wxConfigPathChanger
path(this, key
);
524 return m_keyLocal
.DeleteKey(path
.Name());
527 bool wxRegConfig::DeleteAll()
532 bool bOk
= m_keyLocalRoot
.DeleteSelf();
534 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
535 // otherwise we will delete HKEY_CLASSES_ROOT recursively
536 if ( bOk
&& m_keyGlobalRoot
.IsOpened() )
537 bOk
= m_keyGlobalRoot
.DeleteSelf();