]>
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 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
25 #pragma implementation "regconf.h"
28 #include "wx/wxprec.h"
35 #include <wx/string.h>
40 #include <wx/config.h>
41 #include <wx/msw/registry.h>
42 #include <wx/msw/regconf.h>
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // we put our data in HKLM\SOFTWARE_KEY\appname
49 #define SOFTWARE_KEY wxString("Software\\")
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // get the value if the key is opened and it exists
56 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
58 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
61 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
63 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
66 // ============================================================================
68 // ============================================================================
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
75 wxRegConfig::wxRegConfig(const wxString
& strRoot
)
76 : m_keyLocalRoot(wxRegKey::HKCU
, SOFTWARE_KEY
+ strRoot
),
77 m_keyLocal(m_keyLocalRoot
, ""),
78 m_keyGlobalRoot(wxRegKey::HKLM
, SOFTWARE_KEY
+ strRoot
),
79 m_keyGlobal(m_keyGlobalRoot
, "")
81 // Create() will Open() if key already exists
82 m_keyLocalRoot
.Create();
84 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
88 m_keyGlobalRoot
.Open();
92 // TODO: vendor name is ignored, because we can't yet do the test for optional vendor
93 // name in the constructor body. We need a wxRegKey::Set that takes the same
94 // args as the constructor. Then we'll set m_keyLocalRoot etc. in the constructor body.
96 wxRegConfig::wxRegConfig(const wxString
& appName
, const wxString
& vendorName
,
97 const wxString
& strLocal
, const wxString
& strGlobal
, long style
)
98 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
),
100 m_keyLocalRoot(wxRegKey::HKCU
, SOFTWARE_KEY
+ appName
),
101 m_keyLocal(m_keyLocalRoot
, ""),
102 m_keyGlobalRoot(wxRegKey::HKLM
, SOFTWARE_KEY
+ appName
),
103 m_keyGlobal(m_keyGlobalRoot
, "")
105 // TODO: really, we should check and supply an app name if one isn't supplied.
106 // Unfortunately I don't know how to initialise the member wxRegKey
107 // variables from within the constructor body. -- JACS
108 // Vadim - we just need an implementation of wxRegKey::Set,
109 // and then we can uncomment this and remove the constructor lines above.
111 wxString strRoot(appName);
112 if (appName.IsEmpty() && wxTheApp)
114 strRoot = wxTheApp->GetAppName();
116 wxASSERT( !strRoot.IsEmpty() );
118 if (!vendorName.IsEmpty())
121 strRoot += vendorName;
124 m_keyLocalRoot.Set(wxRegKey::HKCU, SOFTWARE_KEY + strRoot),
125 m_keyLocal.Set(m_keyLocalRoot, ""),
127 m_keyGlobalRoot.Set(wxRegKey::HKLM, SOFTWARE_KEY + strRoot),
128 m_keyGlobal.Set(m_keyGlobalRoot, "")
131 // Create() will Open() if key already exists
132 m_keyLocalRoot
.Create();
134 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
138 m_keyGlobalRoot
.Open();
142 wxRegConfig::~wxRegConfig()
144 // nothing to do - key will be closed in their dtors
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
150 void wxRegConfig::SetPath(const wxString
& strPath
)
152 wxArrayString aParts
;
154 // because GetPath() returns "" when we're at root, we must understand
155 // empty string as "/"
156 if ( strPath
.IsEmpty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
158 wxSplitPath(aParts
, strPath
);
161 // relative path, combine with current one
162 wxString strFullPath
= GetPath();
163 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
164 wxSplitPath(aParts
, strFullPath
);
167 // recombine path parts in one variable
170 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
171 strRegPath
<< '\\' << aParts
[n
];
172 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
175 // change current key(s)
176 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
177 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
184 // ----------------------------------------------------------------------------
185 // enumeration (works only with current group)
186 // ----------------------------------------------------------------------------
189 We want to enumerate all local keys/values after the global ones, but, of
190 course, we don't want to repeat a key which appears locally as well as
193 We use the 15th bit of lIndex for distinction between global and local.
196 #define LOCAL_MASK 0x8000
197 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
199 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
202 return GetNextGroup(str
, lIndex
);
205 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
207 // are we already enumerating local entries?
208 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
209 // try to find a global entry which doesn't appear locally
211 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
212 // no more global entries
213 lIndex
|= LOCAL_MASK
;
216 } while( m_keyLocal
.HasSubKey(str
) );
219 // much easier with local entries: get the next one we find
220 // (don't forget to clear our flag bit and set it again later)
221 lIndex
&= ~LOCAL_MASK
;
222 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
223 lIndex
|= LOCAL_MASK
;
228 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
231 return GetNextEntry(str
, lIndex
);
234 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
236 // are we already enumerating local entries?
237 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
238 // try to find a global entry which doesn't appear locally
240 if ( !m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
241 // no more global entries
242 lIndex
|= LOCAL_MASK
;
245 } while( m_keyLocal
.HasValue(str
) );
248 // much easier with local entries: get the next one we find
249 // (don't forget to clear our flag bit and set it again later)
250 lIndex
&= ~LOCAL_MASK
;
251 bool bOk
= m_keyLocal
.GetNextValue(str
, lIndex
);
252 lIndex
|= LOCAL_MASK
;
257 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive
) const
264 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
268 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
274 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
281 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
285 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
291 // ----------------------------------------------------------------------------
292 // tests for existence
293 // ----------------------------------------------------------------------------
295 bool wxRegConfig::HasGroup(const wxString
& strName
) const
297 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
300 bool wxRegConfig::HasEntry(const wxString
& strName
) const
302 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
311 wxConfigPathChanger
path(this, key
);
313 bool bQueryGlobal
= TRUE
;
315 // if immutable key exists in global key we must check that it's not
316 // overriden by the local key with the same name
317 if ( IsImmutable(path
.Name()) ) {
318 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
319 if ( m_keyLocal
.HasValue(path
.Name()) ) {
320 wxLogWarning("User value for immutable key '%s' ignored.",
321 path
.Name().c_str());
323 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
327 // don't waste time - it's not there anyhow
328 bQueryGlobal
= FALSE
;
332 // first try local key
333 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
334 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
337 // TODO: do we return TRUE? Not in original implementation,
338 // but I don't see why not. -- JACS
339 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
346 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
347 const wxString
& szDefault
) const
349 wxConfigPathChanger
path(this, key
);
351 bool bQueryGlobal
= TRUE
;
353 // if immutable key exists in global key we must check that it's not
354 // overriden by the local key with the same name
355 if ( IsImmutable(path
.Name()) ) {
356 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
357 if ( m_keyLocal
.HasValue(path
.Name()) ) {
358 wxLogWarning("User value for immutable key '%s' ignored.",
359 path
.Name().c_str());
365 // don't waste time - it's not there anyhow
366 bQueryGlobal
= FALSE
;
370 // first try local key
371 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
372 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
373 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
377 if ( IsRecordingDefaults() ) {
378 ((wxRegConfig
*)this)->Write(key
, szDefault
);
385 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
390 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
392 wxConfigPathChanger
path(this, key
);
394 bool bQueryGlobal
= TRUE
;
396 // if immutable key exists in global key we must check that it's not
397 // overriden by the local key with the same name
398 if ( IsImmutable(path
.Name()) ) {
399 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
400 if ( m_keyLocal
.HasValue(path
.Name()) ) {
401 wxLogWarning("User value for immutable key '%s' ignored.",
402 path
.Name().c_str());
408 // don't waste time - it's not there anyhow
409 bQueryGlobal
= FALSE
;
413 // first try local key
414 if ( TryGetValue(m_keyLocal
, path
.Name(), plResult
) ||
415 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
421 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
423 wxConfigPathChanger
path(this, key
);
425 if ( IsImmutable(path
.Name()) ) {
426 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
430 return m_keyLocal
.SetValue(path
.Name(), szValue
);
433 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
435 wxConfigPathChanger
path(this, key
);
437 if ( IsImmutable(path
.Name()) ) {
438 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
442 return m_keyLocal
.SetValue(path
.Name(), lValue
);
445 // ----------------------------------------------------------------------------
447 // ----------------------------------------------------------------------------
448 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
450 wxConfigPathChanger
path(this, value
);
452 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
455 if ( !m_keyLocal
.HasSubkeys() ) {
456 wxString strKey
= GetPath().Right(wxCONFIG_PATH_SEPARATOR
);
457 SetPath(".."); // changes m_keyLocal
458 return m_keyLocal
.DeleteKey(strKey
);
464 bool wxRegConfig::DeleteGroup(const wxString
& key
)
466 wxConfigPathChanger
path(this, key
);
468 return m_keyLocal
.DeleteKey(path
.Name());
471 bool wxRegConfig::DeleteAll()
476 bool bOk
= m_keyLocalRoot
.DeleteSelf();
478 bOk
= m_keyGlobalRoot
.DeleteSelf();