]>
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();
141 wxRegConfig::~wxRegConfig()
143 // nothing to do - key will be closed in their dtors
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
149 void wxRegConfig::SetPath(const wxString
& strPath
)
151 wxArrayString aParts
;
153 // because GetPath() returns "" when we're at root, we must understand
154 // empty string as "/"
155 if ( strPath
.IsEmpty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
157 wxSplitPath(aParts
, strPath
);
160 // relative path, combine with current one
161 wxString strFullPath
= GetPath();
162 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
163 wxSplitPath(aParts
, strFullPath
);
166 // recombine path parts in one variable
167 wxString strOldPath
= m_strPath
, strRegPath
;
169 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
170 strRegPath
<< '\\' << aParts
[n
];
171 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
174 if ( m_strPath
== strOldPath
)
177 // as we create the registry key when SetPath(key) is done, we can be left
178 // with plenty of empty keys if this was only done to try to read some value
179 // which, in fact, doesn't exist - to prevent this from happening we
180 // automatically delete the old key if it was empty
181 if ( m_keyLocal
.IsEmpty() )
183 m_keyLocal
.DeleteSelf();
186 // change current key(s)
187 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
188 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
195 // ----------------------------------------------------------------------------
196 // enumeration (works only with current group)
197 // ----------------------------------------------------------------------------
200 We want to enumerate all local keys/values after the global ones, but, of
201 course, we don't want to repeat a key which appears locally as well as
204 We use the 15th bit of lIndex for distinction between global and local.
207 #define LOCAL_MASK 0x8000
208 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
210 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
213 return GetNextGroup(str
, lIndex
);
216 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
218 // are we already enumerating local entries?
219 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
220 // try to find a global entry which doesn't appear locally
221 while ( m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
222 if ( !m_keyLocal
.HasSubKey(str
) ) {
223 // ok, found one - return it
228 // no more global entries
229 lIndex
|= LOCAL_MASK
;
232 // much easier with local entries: get the next one we find
233 // (don't forget to clear our flag bit and set it again later)
234 lIndex
&= ~LOCAL_MASK
;
235 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
236 lIndex
|= LOCAL_MASK
;
241 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
244 return GetNextEntry(str
, lIndex
);
247 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
249 // are we already enumerating local entries?
250 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
251 // try to find a global entry which doesn't appear locally
252 while ( m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
253 if ( !m_keyLocal
.HasValue(str
) ) {
254 // ok, found one - return it
259 // no more global entries
260 lIndex
|= LOCAL_MASK
;
263 // much easier with local entries: get the next one we find
264 // (don't forget to clear our flag bit and set it again later)
265 lIndex
&= ~LOCAL_MASK
;
266 bool bOk
= m_keyLocal
.GetNextValue(str
, lIndex
);
267 lIndex
|= LOCAL_MASK
;
272 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive
) const
279 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
283 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
289 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
296 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
300 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
306 // ----------------------------------------------------------------------------
307 // tests for existence
308 // ----------------------------------------------------------------------------
310 bool wxRegConfig::HasGroup(const wxString
& key
) const
312 wxConfigPathChanger
path(this, key
);
314 wxString
strName(path
.Name());
316 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
319 bool wxRegConfig::HasEntry(const wxString
& key
) const
321 wxConfigPathChanger
path(this, key
);
323 wxString
strName(path
.Name());
325 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
328 wxConfigBase::EntryType
wxRegConfig::GetEntryType(const wxString
& key
) const
330 wxConfigPathChanger
path(this, key
);
332 wxString
strName(path
.Name());
335 if ( m_keyLocal
.HasValue(strName
) )
336 isNumeric
= m_keyLocal
.IsNumericValue(strName
);
337 else if ( m_keyGlobal
.HasValue(strName
) )
338 isNumeric
= m_keyGlobal
.IsNumericValue(strName
);
340 return wxConfigBase::Type_Unknown
;
342 return isNumeric
? wxConfigBase::Type_Integer
: wxConfigBase::Type_String
;
345 // ----------------------------------------------------------------------------
347 // ----------------------------------------------------------------------------
349 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
351 wxConfigPathChanger
path(this, key
);
353 bool bQueryGlobal
= TRUE
;
355 // if immutable key exists in global key we must check that it's not
356 // overriden by the local key with the same name
357 if ( IsImmutable(path
.Name()) ) {
358 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
359 if ( m_keyLocal
.HasValue(path
.Name()) ) {
360 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
361 path
.Name().c_str());
363 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
367 // don't waste time - it's not there anyhow
368 bQueryGlobal
= FALSE
;
372 // first try local key
373 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
374 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
377 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
384 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
385 const wxString
& szDefault
) 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(), *pStr
) ) {
395 if ( m_keyLocal
.HasValue(path
.Name()) ) {
396 wxLogWarning(wxT("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(), *pStr
) ||
410 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
411 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
415 if ( IsRecordingDefaults() ) {
416 ((wxRegConfig
*)this)->Write(key
, szDefault
);
423 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
428 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
430 wxConfigPathChanger
path(this, key
);
432 bool bQueryGlobal
= TRUE
;
434 // if immutable key exists in global key we must check that it's not
435 // overriden by the local key with the same name
436 if ( IsImmutable(path
.Name()) ) {
437 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
438 if ( m_keyLocal
.HasValue(path
.Name()) ) {
439 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
440 path
.Name().c_str());
446 // don't waste time - it's not there anyhow
447 bQueryGlobal
= FALSE
;
451 // first try local key
452 if ( TryGetValue(m_keyLocal
, path
.Name(), plResult
) ||
453 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
459 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
461 wxConfigPathChanger
path(this, key
);
463 if ( IsImmutable(path
.Name()) ) {
464 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
468 return m_keyLocal
.SetValue(path
.Name(), szValue
);
471 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
473 wxConfigPathChanger
path(this, key
);
475 if ( IsImmutable(path
.Name()) ) {
476 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
480 return m_keyLocal
.SetValue(path
.Name(), lValue
);
483 // ----------------------------------------------------------------------------
485 // ----------------------------------------------------------------------------
487 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
489 // check that the old entry exists...
490 if ( !HasEntry(oldName
) )
493 // and that the new one doesn't
494 if ( HasEntry(newName
) )
497 // delete the old entry and create the new one - but do in the reverse
498 // order to not lose the data if Create() fails
501 if ( m_keyLocal
.IsNumericValue(oldName
) )
504 ok
= m_keyLocal
.QueryValue(oldName
, &val
) &&
505 m_keyLocal
.SetValue(newName
, val
);
510 ok
= m_keyLocal
.QueryValue(oldName
, val
) &&
511 m_keyLocal
.SetValue(newName
, val
);
517 if ( !m_keyLocal
.DeleteValue(oldName
) )
519 m_keyLocal
.DeleteValue(newName
);
527 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
529 // check that the old group exists...
530 if ( !HasGroup(oldName
) )
533 // and that the new one doesn't
534 if ( HasGroup(newName
) )
537 // TODO there is no way to rename a registry key - we must do a deep copy
539 wxFAIL_MSG(wxT("Registry key renaming not implemented"));
544 // ----------------------------------------------------------------------------
546 // ----------------------------------------------------------------------------
547 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
549 wxConfigPathChanger
path(this, value
);
551 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
554 if ( m_keyLocal
.IsEmpty() ) {
555 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
556 SetPath(".."); // changes m_keyLocal
557 return m_keyLocal
.DeleteKey(strKey
);
563 bool wxRegConfig::DeleteGroup(const wxString
& key
)
565 wxConfigPathChanger
path(this, key
);
567 return m_keyLocal
.DeleteKey(path
.Name());
570 bool wxRegConfig::DeleteAll()
575 bool bOk
= m_keyLocalRoot
.DeleteSelf();
577 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
578 // otherwise we will delete HKEY_CLASSES_ROOT recursively
579 if ( bOk
&& m_keyGlobalRoot
.IsOpened() )
580 bOk
= m_keyGlobalRoot
.DeleteSelf();