]> git.saurik.com Git - wxWidgets.git/blob - src/msw/regconf.cpp
mingw32 compilation fixes
[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 #include "wx/config.h"
31
32 #ifndef __WIN16__
33
34 #include "wx/msw/registry.h"
35 #include "wx/msw/regconf.h"
36
37 // ----------------------------------------------------------------------------
38 // constants
39 // ----------------------------------------------------------------------------
40
41 // we put our data in HKLM\SOFTWARE_KEY\appname
42 #define SOFTWARE_KEY wxString("Software\\")
43
44 // ----------------------------------------------------------------------------
45 // global functions
46 // ----------------------------------------------------------------------------
47
48 // get the value if the key is opened and it exists
49 bool TryGetValue(const wxRegKey& key, const wxString& str, wxString& strVal)
50 {
51 return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, strVal);
52 }
53
54 bool TryGetValue(const wxRegKey& key, const wxString& str, long *plVal)
55 {
56 return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, plVal);
57 }
58
59 // ============================================================================
60 // implementation
61 // ============================================================================
62
63 // ----------------------------------------------------------------------------
64 // ctor/dtor
65 // ----------------------------------------------------------------------------
66
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,
71 long style)
72 : wxConfigBase(appName, vendorName, strLocal, strGlobal, style)
73 {
74 wxString strRoot;
75
76 bool bDoUseGlobal = (style & wxCONFIG_USE_GLOBAL_FILE) != 0;
77
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
80 // and/or strGlobal)
81 if ( strLocal.IsEmpty() || (strGlobal.IsEmpty() && bDoUseGlobal) )
82 {
83 if ( vendorName.IsEmpty() )
84 {
85 if ( wxTheApp )
86 strRoot = wxTheApp->GetVendorName();
87 }
88 else
89 {
90 strRoot = vendorName;
91 }
92
93 // no '\\' needed if no vendor name
94 if ( !strRoot.IsEmpty() )
95 {
96 strRoot += '\\';
97 }
98
99 if ( appName.IsEmpty() )
100 {
101 wxCHECK_RET( wxTheApp, wxT("No application name in wxRegConfig ctor!") );
102 strRoot << wxTheApp->GetAppName();
103 }
104 else
105 {
106 strRoot << appName;
107 }
108 }
109 //else: we don't need to do all the complicated stuff above
110
111 wxString str = strLocal.IsEmpty() ? strRoot : strLocal;
112 m_keyLocalRoot.SetName(wxRegKey::HKCU, SOFTWARE_KEY + str);
113 m_keyLocal.SetName(m_keyLocalRoot, "");
114
115 if ( bDoUseGlobal )
116 {
117 str = strGlobal.IsEmpty() ? strRoot : strGlobal;
118 m_keyGlobalRoot.SetName(wxRegKey::HKLM, SOFTWARE_KEY + str);
119 m_keyGlobal.SetName(m_keyGlobalRoot, "");
120 }
121
122 // Create() will Open() if key already exists
123 m_keyLocalRoot.Create();
124
125 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
126 m_keyLocal.Open();
127
128 // OTOH, this key may perfectly not exist, so suppress error messages the call
129 // to Open() might generate
130 if ( bDoUseGlobal )
131 {
132 wxLogNull nolog;
133 m_keyGlobalRoot.Open();
134 }
135 }
136
137 wxRegConfig::~wxRegConfig()
138 {
139 // nothing to do - key will be closed in their dtors
140 }
141
142 // ----------------------------------------------------------------------------
143 // path management
144 // ----------------------------------------------------------------------------
145 void wxRegConfig::SetPath(const wxString& strPath)
146 {
147 wxArrayString aParts;
148
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) ) {
152 // absolute path
153 wxSplitPath(aParts, strPath);
154 }
155 else {
156 // relative path, combine with current one
157 wxString strFullPath = GetPath();
158 strFullPath << wxCONFIG_PATH_SEPARATOR << strPath;
159 wxSplitPath(aParts, strFullPath);
160 }
161
162 // recombine path parts in one variable
163 wxString strOldPath = m_strPath, strRegPath;
164 m_strPath.Empty();
165 for ( size_t n = 0; n < aParts.Count(); n++ ) {
166 strRegPath << '\\' << aParts[n];
167 m_strPath << wxCONFIG_PATH_SEPARATOR << aParts[n];
168 }
169
170 if ( m_strPath == strOldPath )
171 return;
172
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() )
178 {
179 m_keyLocal.DeleteSelf();
180 }
181
182 // change current key(s)
183 m_keyLocal.SetName(m_keyLocalRoot, strRegPath);
184 m_keyGlobal.SetName(m_keyGlobalRoot, strRegPath);
185 m_keyLocal.Create();
186
187 wxLogNull nolog;
188 m_keyGlobal.Open();
189 }
190
191 // ----------------------------------------------------------------------------
192 // enumeration (works only with current group)
193 // ----------------------------------------------------------------------------
194
195 /*
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
198 globally twice.
199
200 We use the 15th bit of lIndex for distinction between global and local.
201 */
202
203 #define LOCAL_MASK 0x8000
204 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
205
206 bool wxRegConfig::GetFirstGroup(wxString& str, long& lIndex) const
207 {
208 lIndex = 0;
209 return GetNextGroup(str, lIndex);
210 }
211
212 bool wxRegConfig::GetNextGroup(wxString& str, long& lIndex) const
213 {
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
217 do {
218 if ( !m_keyGlobal.GetNextKey(str, lIndex) ) {
219 // no more global entries
220 lIndex |= LOCAL_MASK;
221 break;
222 }
223 } while( m_keyLocal.HasSubKey(str) );
224 }
225
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;
231
232 return bOk;
233 }
234
235 bool wxRegConfig::GetFirstEntry(wxString& str, long& lIndex) const
236 {
237 lIndex = 0;
238 return GetNextEntry(str, lIndex);
239 }
240
241 bool wxRegConfig::GetNextEntry(wxString& str, long& lIndex) const
242 {
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
246 do {
247 if ( !m_keyGlobal.GetNextValue(str, lIndex) ) {
248 // no more global entries
249 lIndex |= LOCAL_MASK;
250 break;
251 }
252 } while( m_keyLocal.HasValue(str) );
253 }
254
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;
260
261 return bOk;
262 }
263
264 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive) const
265 {
266 size_t nEntries = 0;
267
268 // dummy vars
269 wxString str;
270 long l;
271 bool bCont = ((wxRegConfig*)this)->GetFirstEntry(str, l);
272 while ( bCont ) {
273 nEntries++;
274
275 bCont = ((wxRegConfig*)this)->GetNextEntry(str, l);
276 }
277
278 return nEntries;
279 }
280
281 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive) const
282 {
283 size_t nGroups = 0;
284
285 // dummy vars
286 wxString str;
287 long l;
288 bool bCont = ((wxRegConfig*)this)->GetFirstGroup(str, l);
289 while ( bCont ) {
290 nGroups++;
291
292 bCont = ((wxRegConfig*)this)->GetNextGroup(str, l);
293 }
294
295 return nGroups;
296 }
297
298 // ----------------------------------------------------------------------------
299 // tests for existence
300 // ----------------------------------------------------------------------------
301
302 bool wxRegConfig::HasGroup(const wxString& key) const
303 {
304 wxConfigPathChanger path(this, key);
305
306 wxString strName(path.Name());
307
308 return m_keyLocal.HasSubKey(strName) || m_keyGlobal.HasSubKey(strName);
309 }
310
311 bool wxRegConfig::HasEntry(const wxString& key) const
312 {
313 wxConfigPathChanger path(this, key);
314
315 wxString strName(path.Name());
316
317 return m_keyLocal.HasValue(strName) || m_keyGlobal.HasValue(strName);
318 }
319
320 wxConfigBase::EntryType wxRegConfig::GetEntryType(const wxString& key) const
321 {
322 wxConfigPathChanger path(this, key);
323
324 wxString strName(path.Name());
325
326 bool isNumeric;
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);
331 else
332 return wxConfigBase::Type_Unknown;
333
334 return isNumeric ? wxConfigBase::Type_Integer : wxConfigBase::Type_String;
335 }
336
337 // ----------------------------------------------------------------------------
338 // reading/writing
339 // ----------------------------------------------------------------------------
340
341 bool wxRegConfig::Read(const wxString& key, wxString *pStr) const
342 {
343 wxConfigPathChanger path(this, key);
344
345 bool bQueryGlobal = TRUE;
346
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());
354 }
355 *pStr = wxConfigBase::ExpandEnvVars(*pStr);
356 return TRUE;
357 }
358 else {
359 // don't waste time - it's not there anyhow
360 bQueryGlobal = FALSE;
361 }
362 }
363
364 // first try local key
365 if ( TryGetValue(m_keyLocal, path.Name(), *pStr) ||
366 (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) {
367 // nothing to do
368
369 *pStr = wxConfigBase::ExpandEnvVars(*pStr);
370 return TRUE;
371 }
372
373 return FALSE;
374 }
375
376 bool wxRegConfig::Read(const wxString& key, wxString *pStr,
377 const wxString& szDefault) const
378 {
379 wxConfigPathChanger path(this, key);
380
381 bool bQueryGlobal = TRUE;
382
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());
390 }
391
392 return TRUE;
393 }
394 else {
395 // don't waste time - it's not there anyhow
396 bQueryGlobal = FALSE;
397 }
398 }
399
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);
404 return TRUE;
405 }
406 else {
407 if ( IsRecordingDefaults() ) {
408 ((wxRegConfig*)this)->Write(key, szDefault);
409 }
410
411 // default value
412 *pStr = szDefault;
413 }
414
415 *pStr = wxConfigBase::ExpandEnvVars(*pStr);
416
417 return FALSE;
418 }
419
420 bool wxRegConfig::Read(const wxString& key, long *plResult) const
421 {
422 wxConfigPathChanger path(this, key);
423
424 bool bQueryGlobal = TRUE;
425
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());
433 }
434
435 return TRUE;
436 }
437 else {
438 // don't waste time - it's not there anyhow
439 bQueryGlobal = FALSE;
440 }
441 }
442
443 // first try local key
444 if ( TryGetValue(m_keyLocal, path.Name(), plResult) ||
445 (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), plResult)) ) {
446 return TRUE;
447 }
448 return FALSE;
449 }
450
451 bool wxRegConfig::Write(const wxString& key, const wxString& szValue)
452 {
453 wxConfigPathChanger path(this, key);
454
455 if ( IsImmutable(path.Name()) ) {
456 wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str());
457 return FALSE;
458 }
459
460 return m_keyLocal.SetValue(path.Name(), szValue);
461 }
462
463 bool wxRegConfig::Write(const wxString& key, long lValue)
464 {
465 wxConfigPathChanger path(this, key);
466
467 if ( IsImmutable(path.Name()) ) {
468 wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str());
469 return FALSE;
470 }
471
472 return m_keyLocal.SetValue(path.Name(), lValue);
473 }
474
475 // ----------------------------------------------------------------------------
476 // renaming
477 // ----------------------------------------------------------------------------
478
479 bool wxRegConfig::RenameEntry(const wxString& oldName, const wxString& newName)
480 {
481 // check that the old entry exists...
482 if ( !HasEntry(oldName) )
483 return FALSE;
484
485 // and that the new one doesn't
486 if ( HasEntry(newName) )
487 return FALSE;
488
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
491
492 bool ok;
493 if ( m_keyLocal.IsNumericValue(oldName) )
494 {
495 long val;
496 ok = m_keyLocal.QueryValue(oldName, &val) &&
497 m_keyLocal.SetValue(newName, val);
498 }
499 else
500 {
501 wxString val;
502 ok = m_keyLocal.QueryValue(oldName, val) &&
503 m_keyLocal.SetValue(newName, val);
504 }
505
506 if ( !ok )
507 return FALSE;
508
509 if ( !m_keyLocal.DeleteValue(oldName) )
510 {
511 m_keyLocal.DeleteValue(newName);
512
513 return FALSE;
514 }
515
516 return TRUE;
517 }
518
519 bool wxRegConfig::RenameGroup(const wxString& oldName, const wxString& newName)
520 {
521 // check that the old group exists...
522 if ( !HasGroup(oldName) )
523 return FALSE;
524
525 // and that the new one doesn't
526 if ( HasGroup(newName) )
527 return FALSE;
528
529 // TODO there is no way to rename a registry key - we must do a deep copy
530 // ourselves
531 wxFAIL_MSG(wxT("Registry key renaming not implemented"));
532
533 return FALSE;
534 }
535
536 // ----------------------------------------------------------------------------
537 // deleting
538 // ----------------------------------------------------------------------------
539 bool wxRegConfig::DeleteEntry(const wxString& value, bool bGroupIfEmptyAlso)
540 {
541 wxConfigPathChanger path(this, value);
542
543 if ( !m_keyLocal.DeleteValue(path.Name()) )
544 return FALSE;
545
546 if ( m_keyLocal.IsEmpty() ) {
547 wxString strKey = GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR);
548 SetPath(".."); // changes m_keyLocal
549 return m_keyLocal.DeleteKey(strKey);
550 }
551
552 return TRUE;
553 }
554
555 bool wxRegConfig::DeleteGroup(const wxString& key)
556 {
557 wxConfigPathChanger path(this, key);
558
559 return m_keyLocal.DeleteKey(path.Name());
560 }
561
562 bool wxRegConfig::DeleteAll()
563 {
564 m_keyLocal.Close();
565 m_keyGlobal.Close();
566
567 bool bOk = m_keyLocalRoot.DeleteSelf();
568
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();
573
574 return bOk;
575 }
576
577 #endif
578 // __WIN16__
579