]> git.saurik.com Git - wxWidgets.git/blob - src/msw/regconf.cpp
2nd part of wxRegConfig fix
[wxWidgets.git] / src / msw / regconf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/regconf.cpp
3 // Purpose:
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 27.04.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "regconf.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/string.h"
25 #endif //WX_PRECOMP
26
27 #include "wx/event.h"
28 #include "wx/app.h"
29 #include "wx/log.h"
30
31 #if wxUSE_CONFIG
32
33 #include "wx/config.h"
34
35 #ifndef __WIN16__
36
37 #include "wx/msw/registry.h"
38 #include "wx/msw/regconf.h"
39
40 // ----------------------------------------------------------------------------
41 // constants
42 // ----------------------------------------------------------------------------
43
44 // we put our data in HKLM\SOFTWARE_KEY\appname
45 #define SOFTWARE_KEY wxString("Software\\")
46
47 // ----------------------------------------------------------------------------
48 // global functions
49 // ----------------------------------------------------------------------------
50
51 // get the value if the key is opened and it exists
52 bool TryGetValue(const wxRegKey& key, const wxString& str, wxString& strVal)
53 {
54 return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, strVal);
55 }
56
57 bool TryGetValue(const wxRegKey& key, const wxString& str, long *plVal)
58 {
59 return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, plVal);
60 }
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66 // ----------------------------------------------------------------------------
67 // ctor/dtor
68 // ----------------------------------------------------------------------------
69
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,
74 long style)
75 : wxConfigBase(appName, vendorName, strLocal, strGlobal, style)
76 {
77 wxString strRoot;
78
79 bool bDoUseGlobal = (style & wxCONFIG_USE_GLOBAL_FILE) != 0;
80
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
83 // and/or strGlobal)
84 if ( strLocal.IsEmpty() || (strGlobal.IsEmpty() && bDoUseGlobal) )
85 {
86 if ( vendorName.IsEmpty() )
87 {
88 if ( wxTheApp )
89 strRoot = wxTheApp->GetVendorName();
90 }
91 else
92 {
93 strRoot = vendorName;
94 }
95
96 // no '\\' needed if no vendor name
97 if ( !strRoot.IsEmpty() )
98 {
99 strRoot += '\\';
100 }
101
102 if ( appName.IsEmpty() )
103 {
104 wxCHECK_RET( wxTheApp, wxT("No application name in wxRegConfig ctor!") );
105 strRoot << wxTheApp->GetAppName();
106 }
107 else
108 {
109 strRoot << appName;
110 }
111 }
112 //else: we don't need to do all the complicated stuff above
113
114 wxString str = strLocal.IsEmpty() ? strRoot : strLocal;
115 m_keyLocalRoot.SetName(wxRegKey::HKCU, SOFTWARE_KEY + str);
116 m_keyLocal.SetName(m_keyLocalRoot, "");
117
118 if ( bDoUseGlobal )
119 {
120 str = strGlobal.IsEmpty() ? strRoot : strGlobal;
121 m_keyGlobalRoot.SetName(wxRegKey::HKLM, SOFTWARE_KEY + str);
122 m_keyGlobal.SetName(m_keyGlobalRoot, "");
123 }
124
125 // Create() will Open() if key already exists
126 m_keyLocalRoot.Create();
127
128 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
129 m_keyLocal.Open();
130
131 // OTOH, this key may perfectly not exist, so suppress error messages the call
132 // to Open() might generate
133 if ( bDoUseGlobal )
134 {
135 wxLogNull nolog;
136 m_keyGlobalRoot.Open();
137 }
138 }
139
140 wxRegConfig::~wxRegConfig()
141 {
142 // nothing to do - key will be closed in their dtors
143 }
144
145 // ----------------------------------------------------------------------------
146 // path management
147 // ----------------------------------------------------------------------------
148 void wxRegConfig::SetPath(const wxString& strPath)
149 {
150 wxArrayString aParts;
151
152 // because GetPath() returns "" when we're at root, we must understand
153 // empty string as "/"
154 if ( strPath.IsEmpty() || (strPath[0] == wxCONFIG_PATH_SEPARATOR) ) {
155 // absolute path
156 wxSplitPath(aParts, strPath);
157 }
158 else {
159 // relative path, combine with current one
160 wxString strFullPath = GetPath();
161 strFullPath << wxCONFIG_PATH_SEPARATOR << strPath;
162 wxSplitPath(aParts, strFullPath);
163 }
164
165 // recombine path parts in one variable
166 wxString strOldPath = m_strPath, strRegPath;
167 m_strPath.Empty();
168 for ( size_t n = 0; n < aParts.Count(); n++ ) {
169 strRegPath << '\\' << aParts[n];
170 m_strPath << wxCONFIG_PATH_SEPARATOR << aParts[n];
171 }
172
173 if ( m_strPath == strOldPath )
174 return;
175
176 // as we create the registry key when SetPath(key) is done, we can be left
177 // with plenty of empty keys if this was only done to try to read some value
178 // which, in fact, doesn't exist - to prevent this from happening we
179 // automatically delete the old key if it was empty
180 if ( m_keyLocal.IsEmpty() )
181 {
182 m_keyLocal.DeleteSelf();
183 }
184
185 // change current key(s)
186 m_keyLocal.SetName(m_keyLocalRoot, strRegPath);
187 m_keyGlobal.SetName(m_keyGlobalRoot, strRegPath);
188 m_keyLocal.Create();
189
190 wxLogNull nolog;
191 m_keyGlobal.Open();
192 }
193
194 // ----------------------------------------------------------------------------
195 // enumeration (works only with current group)
196 // ----------------------------------------------------------------------------
197
198 /*
199 We want to enumerate all local keys/values after the global ones, but, of
200 course, we don't want to repeat a key which appears locally as well as
201 globally twice.
202
203 We use the 15th bit of lIndex for distinction between global and local.
204 */
205
206 #define LOCAL_MASK 0x8000
207 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
208
209 bool wxRegConfig::GetFirstGroup(wxString& str, long& lIndex) const
210 {
211 lIndex = 0;
212 return GetNextGroup(str, lIndex);
213 }
214
215 bool wxRegConfig::GetNextGroup(wxString& str, long& lIndex) const
216 {
217 // are we already enumerating local entries?
218 if ( m_keyGlobal.IsOpened() && !IS_LOCAL_INDEX(lIndex) ) {
219 // try to find a global entry which doesn't appear locally
220 while ( m_keyGlobal.GetNextKey(str, lIndex) ) {
221 if ( !m_keyLocal.HasSubKey(str) ) {
222 // ok, found one - return it
223 return TRUE;
224 }
225 }
226
227 // no more global entries
228 lIndex |= LOCAL_MASK;
229 }
230
231 // much easier with local entries: get the next one we find
232 // (don't forget to clear our flag bit and set it again later)
233 lIndex &= ~LOCAL_MASK;
234 bool bOk = m_keyLocal.GetNextKey(str, lIndex);
235 lIndex |= LOCAL_MASK;
236
237 return bOk;
238 }
239
240 bool wxRegConfig::GetFirstEntry(wxString& str, long& lIndex) const
241 {
242 lIndex = 0;
243 return GetNextEntry(str, lIndex);
244 }
245
246 bool wxRegConfig::GetNextEntry(wxString& str, long& lIndex) const
247 {
248 // are we already enumerating local entries?
249 if ( m_keyGlobal.IsOpened() && !IS_LOCAL_INDEX(lIndex) ) {
250 // try to find a global entry which doesn't appear locally
251 while ( m_keyGlobal.GetNextValue(str, lIndex) ) {
252 if ( !m_keyLocal.HasValue(str) ) {
253 // ok, found one - return it
254 return TRUE;
255 }
256 }
257
258 // no more global entries
259 lIndex |= LOCAL_MASK;
260 }
261
262 // much easier with local entries: get the next one we find
263 // (don't forget to clear our flag bit and set it again later)
264 lIndex &= ~LOCAL_MASK;
265 bool bOk = m_keyLocal.GetNextValue(str, lIndex);
266 lIndex |= LOCAL_MASK;
267
268 return bOk;
269 }
270
271 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive) const
272 {
273 size_t nEntries = 0;
274
275 // dummy vars
276 wxString str;
277 long l;
278 bool bCont = ((wxRegConfig*)this)->GetFirstEntry(str, l);
279 while ( bCont ) {
280 nEntries++;
281
282 bCont = ((wxRegConfig*)this)->GetNextEntry(str, l);
283 }
284
285 return nEntries;
286 }
287
288 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive) const
289 {
290 size_t nGroups = 0;
291
292 // dummy vars
293 wxString str;
294 long l;
295 bool bCont = ((wxRegConfig*)this)->GetFirstGroup(str, l);
296 while ( bCont ) {
297 nGroups++;
298
299 bCont = ((wxRegConfig*)this)->GetNextGroup(str, l);
300 }
301
302 return nGroups;
303 }
304
305 // ----------------------------------------------------------------------------
306 // tests for existence
307 // ----------------------------------------------------------------------------
308
309 bool wxRegConfig::HasGroup(const wxString& key) const
310 {
311 wxConfigPathChanger path(this, key);
312
313 wxString strName(path.Name());
314
315 return m_keyLocal.HasSubKey(strName) || m_keyGlobal.HasSubKey(strName);
316 }
317
318 bool wxRegConfig::HasEntry(const wxString& key) const
319 {
320 wxConfigPathChanger path(this, key);
321
322 wxString strName(path.Name());
323
324 return m_keyLocal.HasValue(strName) || m_keyGlobal.HasValue(strName);
325 }
326
327 wxConfigBase::EntryType wxRegConfig::GetEntryType(const wxString& key) const
328 {
329 wxConfigPathChanger path(this, key);
330
331 wxString strName(path.Name());
332
333 bool isNumeric;
334 if ( m_keyLocal.HasValue(strName) )
335 isNumeric = m_keyLocal.IsNumericValue(strName);
336 else if ( m_keyGlobal.HasValue(strName) )
337 isNumeric = m_keyGlobal.IsNumericValue(strName);
338 else
339 return wxConfigBase::Type_Unknown;
340
341 return isNumeric ? wxConfigBase::Type_Integer : wxConfigBase::Type_String;
342 }
343
344 // ----------------------------------------------------------------------------
345 // reading/writing
346 // ----------------------------------------------------------------------------
347
348 bool wxRegConfig::Read(const wxString& key, wxString *pStr) const
349 {
350 wxConfigPathChanger path(this, key);
351
352 bool bQueryGlobal = TRUE;
353
354 // if immutable key exists in global key we must check that it's not
355 // overriden by the local key with the same name
356 if ( IsImmutable(path.Name()) ) {
357 if ( TryGetValue(m_keyGlobal, path.Name(), *pStr) ) {
358 if ( m_keyLocal.HasValue(path.Name()) ) {
359 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
360 path.Name().c_str());
361 }
362 *pStr = wxConfigBase::ExpandEnvVars(*pStr);
363 return TRUE;
364 }
365 else {
366 // don't waste time - it's not there anyhow
367 bQueryGlobal = FALSE;
368 }
369 }
370
371 // first try local key
372 if ( TryGetValue(m_keyLocal, path.Name(), *pStr) ||
373 (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) {
374 // nothing to do
375
376 *pStr = wxConfigBase::ExpandEnvVars(*pStr);
377 return TRUE;
378 }
379
380 return FALSE;
381 }
382
383 bool wxRegConfig::Read(const wxString& key, wxString *pStr,
384 const wxString& szDefault) const
385 {
386 wxConfigPathChanger path(this, key);
387
388 bool bQueryGlobal = TRUE;
389
390 // if immutable key exists in global key we must check that it's not
391 // overriden by the local key with the same name
392 if ( IsImmutable(path.Name()) ) {
393 if ( TryGetValue(m_keyGlobal, path.Name(), *pStr) ) {
394 if ( m_keyLocal.HasValue(path.Name()) ) {
395 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
396 path.Name().c_str());
397 }
398
399 return TRUE;
400 }
401 else {
402 // don't waste time - it's not there anyhow
403 bQueryGlobal = FALSE;
404 }
405 }
406
407 // first try local key
408 if ( TryGetValue(m_keyLocal, path.Name(), *pStr) ||
409 (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) {
410 *pStr = wxConfigBase::ExpandEnvVars(*pStr);
411 return TRUE;
412 }
413 else {
414 if ( IsRecordingDefaults() ) {
415 ((wxRegConfig*)this)->Write(key, szDefault);
416 }
417
418 // default value
419 *pStr = szDefault;
420 }
421
422 *pStr = wxConfigBase::ExpandEnvVars(*pStr);
423
424 return FALSE;
425 }
426
427 bool wxRegConfig::Read(const wxString& key, long *plResult) const
428 {
429 wxConfigPathChanger path(this, key);
430
431 bool bQueryGlobal = TRUE;
432
433 // if immutable key exists in global key we must check that it's not
434 // overriden by the local key with the same name
435 if ( IsImmutable(path.Name()) ) {
436 if ( TryGetValue(m_keyGlobal, path.Name(), plResult) ) {
437 if ( m_keyLocal.HasValue(path.Name()) ) {
438 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
439 path.Name().c_str());
440 }
441
442 return TRUE;
443 }
444 else {
445 // don't waste time - it's not there anyhow
446 bQueryGlobal = FALSE;
447 }
448 }
449
450 // first try local key
451 if ( TryGetValue(m_keyLocal, path.Name(), plResult) ||
452 (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), plResult)) ) {
453 return TRUE;
454 }
455 return FALSE;
456 }
457
458 bool wxRegConfig::Write(const wxString& key, const wxString& szValue)
459 {
460 wxConfigPathChanger path(this, key);
461
462 if ( IsImmutable(path.Name()) ) {
463 wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str());
464 return FALSE;
465 }
466
467 return m_keyLocal.SetValue(path.Name(), szValue);
468 }
469
470 bool wxRegConfig::Write(const wxString& key, long lValue)
471 {
472 wxConfigPathChanger path(this, key);
473
474 if ( IsImmutable(path.Name()) ) {
475 wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str());
476 return FALSE;
477 }
478
479 return m_keyLocal.SetValue(path.Name(), lValue);
480 }
481
482 // ----------------------------------------------------------------------------
483 // renaming
484 // ----------------------------------------------------------------------------
485
486 bool wxRegConfig::RenameEntry(const wxString& oldName, const wxString& newName)
487 {
488 // check that the old entry exists...
489 if ( !HasEntry(oldName) )
490 return FALSE;
491
492 // and that the new one doesn't
493 if ( HasEntry(newName) )
494 return FALSE;
495
496 // delete the old entry and create the new one - but do in the reverse
497 // order to not lose the data if Create() fails
498
499 bool ok;
500 if ( m_keyLocal.IsNumericValue(oldName) )
501 {
502 long val;
503 ok = m_keyLocal.QueryValue(oldName, &val) &&
504 m_keyLocal.SetValue(newName, val);
505 }
506 else
507 {
508 wxString val;
509 ok = m_keyLocal.QueryValue(oldName, val) &&
510 m_keyLocal.SetValue(newName, val);
511 }
512
513 if ( !ok )
514 return FALSE;
515
516 if ( !m_keyLocal.DeleteValue(oldName) )
517 {
518 m_keyLocal.DeleteValue(newName);
519
520 return FALSE;
521 }
522
523 return TRUE;
524 }
525
526 bool wxRegConfig::RenameGroup(const wxString& oldName, const wxString& newName)
527 {
528 // check that the old group exists...
529 if ( !HasGroup(oldName) )
530 return FALSE;
531
532 // and that the new one doesn't
533 if ( HasGroup(newName) )
534 return FALSE;
535
536 // TODO there is no way to rename a registry key - we must do a deep copy
537 // ourselves
538 wxFAIL_MSG(wxT("Registry key renaming not implemented"));
539
540 return FALSE;
541 }
542
543 // ----------------------------------------------------------------------------
544 // deleting
545 // ----------------------------------------------------------------------------
546 bool wxRegConfig::DeleteEntry(const wxString& value, bool bGroupIfEmptyAlso)
547 {
548 wxConfigPathChanger path(this, value);
549
550 if ( !m_keyLocal.DeleteValue(path.Name()) )
551 return FALSE;
552
553 if ( m_keyLocal.IsEmpty() ) {
554 wxString strKey = GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR);
555 SetPath(".."); // changes m_keyLocal
556 return m_keyLocal.DeleteKey(strKey);
557 }
558
559 return TRUE;
560 }
561
562 bool wxRegConfig::DeleteGroup(const wxString& key)
563 {
564 wxConfigPathChanger path(this, key);
565
566 return m_keyLocal.DeleteKey(path.Name());
567 }
568
569 bool wxRegConfig::DeleteAll()
570 {
571 m_keyLocal.Close();
572 m_keyGlobal.Close();
573
574 bool bOk = m_keyLocalRoot.DeleteSelf();
575
576 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
577 // otherwise we will delete HKEY_CLASSES_ROOT recursively
578 if ( bOk && m_keyGlobalRoot.IsOpened() )
579 bOk = m_keyGlobalRoot.DeleteSelf();
580
581 return bOk;
582 }
583
584 #endif
585 // __WIN16__
586
587 #endif
588 // wxUSE_CONFIG