]>
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
, wxT("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
163 wxString strOldPath
= m_strPath
, strRegPath
;
165 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
166 strRegPath
<< '\\' << aParts
[n
];
167 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
170 if ( m_strPath
== strOldPath
)
173 // as we create the registry key when SetPath(key) is done, we can be left
174 // with plenty of empty keys if this was only done to try to read some value
175 // which, in fact, doesn't exist - to prevent this from happening we
176 // automatically delete the old key if it was empty
177 if ( m_keyLocal
.IsEmpty() )
179 m_keyLocal
.DeleteSelf();
182 // change current key(s)
183 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
184 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
191 // ----------------------------------------------------------------------------
192 // enumeration (works only with current group)
193 // ----------------------------------------------------------------------------
196 We want to enumerate all local keys/values after the global ones, but, of
197 course, we don't want to repeat a key which appears locally as well as
200 We use the 15th bit of lIndex for distinction between global and local.
203 #define LOCAL_MASK 0x8000
204 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
206 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
209 return GetNextGroup(str
, lIndex
);
212 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
214 // are we already enumerating local entries?
215 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
216 // try to find a global entry which doesn't appear locally
218 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
219 // no more global entries
220 lIndex
|= LOCAL_MASK
;
223 } while( m_keyLocal
.HasSubKey(str
) );
226 // much easier with local entries: get the next one we find
227 // (don't forget to clear our flag bit and set it again later)
228 lIndex
&= ~LOCAL_MASK
;
229 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
230 lIndex
|= LOCAL_MASK
;
235 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
238 return GetNextEntry(str
, lIndex
);
241 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
243 // are we already enumerating local entries?
244 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
245 // try to find a global entry which doesn't appear locally
247 if ( !m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
248 // no more global entries
249 lIndex
|= LOCAL_MASK
;
252 } while( m_keyLocal
.HasValue(str
) );
255 // much easier with local entries: get the next one we find
256 // (don't forget to clear our flag bit and set it again later)
257 lIndex
&= ~LOCAL_MASK
;
258 bool bOk
= m_keyLocal
.GetNextValue(str
, lIndex
);
259 lIndex
|= LOCAL_MASK
;
264 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive
) const
271 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
275 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
281 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
288 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
292 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
298 // ----------------------------------------------------------------------------
299 // tests for existence
300 // ----------------------------------------------------------------------------
302 bool wxRegConfig::HasGroup(const wxString
& key
) const
304 wxConfigPathChanger
path(this, key
);
306 wxString
strName(path
.Name());
308 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
311 bool wxRegConfig::HasEntry(const wxString
& key
) const
313 wxConfigPathChanger
path(this, key
);
315 wxString
strName(path
.Name());
317 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
320 wxConfigBase::EntryType
wxRegConfig::GetEntryType(const wxString
& key
) const
322 wxConfigPathChanger
path(this, key
);
324 wxString
strName(path
.Name());
327 if ( m_keyLocal
.HasValue(strName
) )
328 isNumeric
= m_keyLocal
.IsNumericValue(strName
);
329 else if ( m_keyGlobal
.HasValue(strName
) )
330 isNumeric
= m_keyGlobal
.IsNumericValue(strName
);
332 return wxConfigBase::Type_Unknown
;
334 return isNumeric
? wxConfigBase::Type_Integer
: wxConfigBase::Type_String
;
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
341 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
343 wxConfigPathChanger
path(this, key
);
345 bool bQueryGlobal
= TRUE
;
347 // if immutable key exists in global key we must check that it's not
348 // overriden by the local key with the same name
349 if ( IsImmutable(path
.Name()) ) {
350 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
351 if ( m_keyLocal
.HasValue(path
.Name()) ) {
352 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
353 path
.Name().c_str());
355 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
359 // don't waste time - it's not there anyhow
360 bQueryGlobal
= FALSE
;
364 // first try local key
365 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
366 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
369 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
376 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
377 const wxString
& szDefault
) const
379 wxConfigPathChanger
path(this, key
);
381 bool bQueryGlobal
= TRUE
;
383 // if immutable key exists in global key we must check that it's not
384 // overriden by the local key with the same name
385 if ( IsImmutable(path
.Name()) ) {
386 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
387 if ( m_keyLocal
.HasValue(path
.Name()) ) {
388 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
389 path
.Name().c_str());
395 // don't waste time - it's not there anyhow
396 bQueryGlobal
= FALSE
;
400 // first try local key
401 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
402 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
403 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
407 if ( IsRecordingDefaults() ) {
408 ((wxRegConfig
*)this)->Write(key
, szDefault
);
415 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
420 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
422 wxConfigPathChanger
path(this, key
);
424 bool bQueryGlobal
= TRUE
;
426 // if immutable key exists in global key we must check that it's not
427 // overriden by the local key with the same name
428 if ( IsImmutable(path
.Name()) ) {
429 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
430 if ( m_keyLocal
.HasValue(path
.Name()) ) {
431 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
432 path
.Name().c_str());
438 // don't waste time - it's not there anyhow
439 bQueryGlobal
= FALSE
;
443 // first try local key
444 if ( TryGetValue(m_keyLocal
, path
.Name(), plResult
) ||
445 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
451 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
453 wxConfigPathChanger
path(this, key
);
455 if ( IsImmutable(path
.Name()) ) {
456 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
460 return m_keyLocal
.SetValue(path
.Name(), szValue
);
463 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
465 wxConfigPathChanger
path(this, key
);
467 if ( IsImmutable(path
.Name()) ) {
468 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
472 return m_keyLocal
.SetValue(path
.Name(), lValue
);
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
481 // check that the old entry exists...
482 if ( !HasEntry(oldName
) )
485 // and that the new one doesn't
486 if ( HasEntry(newName
) )
489 // delete the old entry and create the new one - but do in the reverse
490 // order to not lose the data if Create() fails
493 if ( m_keyLocal
.IsNumericValue(oldName
) )
496 ok
= m_keyLocal
.QueryValue(oldName
, &val
) &&
497 m_keyLocal
.SetValue(newName
, val
);
502 ok
= m_keyLocal
.QueryValue(oldName
, val
) &&
503 m_keyLocal
.SetValue(newName
, val
);
509 if ( !m_keyLocal
.DeleteValue(oldName
) )
511 m_keyLocal
.DeleteValue(newName
);
519 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
521 // check that the old group exists...
522 if ( !HasGroup(oldName
) )
525 // and that the new one doesn't
526 if ( HasGroup(newName
) )
529 // TODO there is no way to rename a registry key - we must do a deep copy
531 wxFAIL_MSG(wxT("Registry key renaming not implemented"));
536 // ----------------------------------------------------------------------------
538 // ----------------------------------------------------------------------------
539 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
541 wxConfigPathChanger
path(this, value
);
543 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
546 if ( m_keyLocal
.IsEmpty() ) {
547 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
548 SetPath(".."); // changes m_keyLocal
549 return m_keyLocal
.DeleteKey(strKey
);
555 bool wxRegConfig::DeleteGroup(const wxString
& key
)
557 wxConfigPathChanger
path(this, key
);
559 return m_keyLocal
.DeleteKey(path
.Name());
562 bool wxRegConfig::DeleteAll()
567 bool bOk
= m_keyLocalRoot
.DeleteSelf();
569 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
570 // otherwise we will delete HKEY_CLASSES_ROOT recursively
571 if ( bOk
&& m_keyGlobalRoot
.IsOpened() )
572 bOk
= m_keyGlobalRoot
.DeleteSelf();