[ec62bb]: / corelib / PWSprefs.cpp  Maximize  Restore  History

Download this file

887 lines (779 with data), 26.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/*
* Copyright (c) 2003-2006 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license.php
*/
#include "PWSprefs.h"
#include "corelib.h"
#include <AfxWin.h> // for AfxGetApp()
#include <sstream>
#include <strstream>
#include <LMCons.h> // for UNLEN
#include "PWSfile.h"
#include "SysInfo.h"
#include "XMLprefs.h"
#include "util.h"
using namespace std;
#if defined(POCKET_PC)
const LPCTSTR PWS_REG_POSITION = _T("Position");
const LPCTSTR PWS_REG_OPTIONS = _T("Options");
#else
const LPCTSTR PWS_REG_POSITION = _T("");
const LPCTSTR PWS_REG_OPTIONS = _T("");
#endif
PWSprefs *PWSprefs::self = NULL;
// 1st parameter = name of preference
// 2nd parameter = default value
// 3rd parameter if 'true' means value stored in db, if 'false' means application related
const PWSprefs::boolPref PWSprefs::m_bool_prefs[NumBoolPrefs] = {
{_T("alwaysontop"), false, false}, // application
{_T("showpwdefault"), false, true}, // database
{_T("showpwinlist"), false, true}, // database
{_T("sortascending"), true, true}, // database
{_T("usedefuser"), false, true}, // database
{_T("saveimmediately"), true, true}, // database
{_T("pwuselowercase"), true, true}, // database
{_T("pwuseuppercase"), true, true}, // database
{_T("pwusedigits"), true, true}, // database
{_T("pwusesymbols"), false, true}, // database
{_T("pwusehexdigits"), false, true}, // database
{_T("pweasyvision"), false, true}, // database
{_T("dontaskquestion"), false, false}, // application
{_T("deletequestion"), false, false}, // application
{_T("DCShowsPassword"), false, false}, // application
{_T("DontAskMinimizeClearYesNo"), true, false}, // application
{_T("DatabaseClear"), false, false}, // application
{_T("DontAskSaveMinimize"), false, false}, // application
{_T("QuerySetDef"), true, false}, // application
{_T("UseNewToolbar"), true, false}, // application
{_T("UseSystemTray"), true, false}, // application
{_T("LockOnWindowLock"), true, false}, // application
{_T("LockOnIdleTimeout"), true, false}, // application
{_T("EscExits"), true, false}, // application
{_T("IsUTF8"), false, true}, // database
{_T("HotKeyEnabled"), false, false}, // application
{_T("MRUOnFileMenu"), true, false}, // application
{_T("DisplayExpandedAddEditDlg"), true, true}, // database
{_T("MaintainDateTimeStamps"), false, true}, // database
{_T("SavePasswordHistory"), false, true}, // database
{_T("FindWraps"), false, false}, // application
{_T("ShowNotesDefault"), false, true}, // database
{_T("BackupBeforeEverySave"), false, false}, // application
};
// Default value = -1 means set at runtime
// Extra two values for Integer - min and max acceptable values (ignored if = -1)
const PWSprefs::intPref PWSprefs::m_int_prefs[NumIntPrefs] = {
{_T("column1width"), (unsigned int)-1, false, -1, -1}, // application
{_T("column2width"), (unsigned int)-1, false, -1, -1}, // application
{_T("column3width"), (unsigned int)-1, false, -1, -1}, // application
{_T("column4width"), (unsigned int)-1, false, -1, -1}, // application
{_T("sortedcolumn"), 0, false, 0, 7}, // application
{_T("pwlendefault"), 8, true, 4, 1024}, // database
// maxmruitems maximum = (ID_FILE_MRU_ENTRYMAX - ID_FILE_MRU_ENTRY1 + 1)
{_T("maxmruitems"), 4, false, 0, 20}, // application
{_T("IdleTimeout"), 5, true, 1, 120}, // database
{_T("DoubleClickAction"), DoubleClickCopyPassword, false, minDCA, maxDCA}, // application
{_T("HotKey"), 0, false, -1, -1}, // 0=disabled, >0=keycode. // application
// MaxREItems maximum = (ID_TRAYRECENT_ENTRYMAX - ID_TRAYRECENT_ENTRY1 + 1)
{_T("MaxREItems"), 25, false, 0, 25}, // application
{_T("TreeDisplayStatusAtOpen"), AllCollapsed, true, minTDS, maxTDS}, // database
{_T("NumPWHistoryDefault"), 3, true, 0, 255}, // database
// Default and user specified
{_T("BackupPrefix"), 0, false, 0, 1}, // application
// Specified by supported masks
{_T("BackupSuffix"), 0, false, minBKSFX, maxBKSFX}, // application
// default, user specified sub-directory and user specified other location
{_T("BackupLocation"), 0, false, 0, 2}, // application
{_T("BackupMaxIncremented"), 3, false, 1, 999}, // application
{_T("UseDefaultBrowser"), 0, false, 0, 1}, // application
};
const PWSprefs::stringPref PWSprefs::m_string_prefs[NumStringPrefs] = {
{_T("currentbackup"), _T(""), false}, // application
{_T("currentfile"), _T(""), false}, // application
{_T("lastview"), _T("tree"), true}, // database
{_T("defusername"), _T(""), true}, // database
{_T("treefont"), _T(""), false}, // application
{_T("BackupPrefixValue"), _T(""), false}, // application
{_T("BackupSubDirectoryValue"), _T(""), false}, // application
{_T("BackupOtherLocationValue"), _T(""), false}, // application
{_T("OtherBrowser"), _T(""), false}, // application
};
PWSprefs *PWSprefs::GetInstance()
{
if (self == NULL) {
self = new PWSprefs();
}
return self;
}
void PWSprefs::DeleteInstance()
{
delete self->m_XML_Config; // should have ~PWSprefs
delete self;
self = NULL;
SysInfo::DeleteInstance();
}
PWSprefs::PWSprefs() : m_app(::AfxGetApp())
{
ASSERT(m_app != NULL);
m_prefs_changed[DB_PREF] = false;
m_prefs_changed[APP_PREF] = false;
for (int i = 0; i < NumBoolPrefs; i++)
m_boolChanged[i] = false;
for (int i = 0; i < NumIntPrefs; i++)
m_intChanged[i] = false;
for (int i = 0; i < NumStringPrefs; i++)
m_stringChanged[i] = false;
InitializePreferences();
}
bool
PWSprefs::CheckRegistryExists()
{
bool bExists;
HKEY hSubkey;
const CString csSubkey = _T("Software\\") + CString(m_app->m_pszRegistryKey);
bExists = (::RegOpenKeyEx(HKEY_CURRENT_USER,
csSubkey,
0L,
KEY_READ,
&hSubkey) == ERROR_SUCCESS);
if (bExists)
::RegCloseKey(hSubkey);
return bExists;
}
bool PWSprefs::GetPref(BoolPrefs pref_enum) const
{
return m_boolValues[pref_enum];
}
unsigned int PWSprefs::GetPref(IntPrefs pref_enum) const
{
return m_intValues[pref_enum];
}
CMyString PWSprefs::GetPref(StringPrefs pref_enum) const
{
return m_stringValues[pref_enum];
}
// Following for case where default value is determined at runtime
unsigned int PWSprefs::GetPref(IntPrefs pref_enum, unsigned int defVal) const
{
return m_intValues[pref_enum] == (unsigned int)-1 ? defVal : m_intValues[pref_enum];
}
void PWSprefs::GetPrefRect(long &top, long &bottom,
long &left, long &right) const
{
// this is never stored in db
switch (m_ConfigOptions) {
case CF_REGISTRY:
top = m_app->GetProfileInt(PWS_REG_POSITION, _T("top"), -1);
bottom = m_app->GetProfileInt(PWS_REG_POSITION, _T("bottom"), -1);
left = m_app->GetProfileInt(PWS_REG_POSITION, _T("left"), -1);
right = m_app->GetProfileInt(PWS_REG_POSITION, _T("right"), -1);
break;
case CF_FILE_RO:
case CF_FILE_RW:
case CF_FILE_RW_NEW:
m_XML_Config->SetKeepXMLLock(true);
top = m_XML_Config->Get(m_csHKCU_POS, _T("top"), -1);
bottom = m_XML_Config->Get(m_csHKCU_POS, _T("bottom"), -1);
left = m_XML_Config->Get(m_csHKCU_POS, _T("left"), -1);
right = m_XML_Config->Get(m_csHKCU_POS, _T("right"), -1);
m_XML_Config->SetKeepXMLLock(false);
break;
case CF_NONE:
default:
top = bottom = left = right = -1;
break;
}
}
void PWSprefs::SetPref(BoolPrefs pref_enum, bool value)
{
// ONLY save in memory - written out at database save (to database and config destination)
m_prefs_changed[m_bool_prefs[pref_enum].isStoredinDB ? DB_PREF : APP_PREF] |=
(m_boolValues[pref_enum] != value);
if (m_boolValues[pref_enum] != value) { // Only if changed
m_boolValues[pref_enum] = value;
m_boolChanged[pref_enum] = true;
}
}
void PWSprefs::SetPref(IntPrefs pref_enum, unsigned int value)
{
// ONLY save in memory - written out at database save (to database and config destination)
m_prefs_changed[m_int_prefs[pref_enum].isStoredinDB ? DB_PREF : APP_PREF] |=
(m_intValues[pref_enum] != value);
if (m_intValues[pref_enum] != value) { // Only if changed
m_intValues[pref_enum] = value;
m_intChanged[pref_enum] = true;
}
}
void PWSprefs::SetPref(StringPrefs pref_enum, const CMyString &value)
{
// ONLY save in memory - written out at database save (to database and config destination)
m_prefs_changed[m_string_prefs[pref_enum].isStoredinDB ? DB_PREF : APP_PREF] |=
(m_stringValues[pref_enum] != value);
if (m_stringValues[pref_enum] != value) { // Only if changed
m_stringValues[pref_enum] = value;
m_stringChanged[pref_enum] = true;
}
}
bool PWSprefs::WritePref(const CMyString &name, bool val)
{
// Used to save to config destination at database save and application termination
bool bRetVal(false);
switch (m_ConfigOptions) {
case CF_REGISTRY:
bRetVal = (m_app->WriteProfileInt(PWS_REG_OPTIONS, name, val ? 1 : 0) == TRUE);
break;
case CF_FILE_RW:
case CF_FILE_RW_NEW:
bRetVal = (m_XML_Config->Set(m_csHKCU_PREF, name, val ? 1 : 0) == 0);
break;
case CF_FILE_RO:
case CF_NONE:
default:
break;
}
UpdateTimeStamp();
return bRetVal;
}
bool PWSprefs::WritePref(const CMyString &name, unsigned int val)
{
// Used to save to config destination at database save and application termination
bool bRetVal(false);
switch (m_ConfigOptions) {
case CF_REGISTRY:
bRetVal = (m_app->WriteProfileInt(PWS_REG_OPTIONS, name, val) == TRUE);
break;
case CF_FILE_RW:
case CF_FILE_RW_NEW:
bRetVal = (m_XML_Config->Set(m_csHKCU_PREF, name, val) == 0);
break;
case CF_FILE_RO:
case CF_NONE:
default:
break;
}
UpdateTimeStamp();
return bRetVal;
}
bool PWSprefs::WritePref(const CMyString &name, const CMyString &val)
{
// Used to save to config destination at database save and application termination
bool bRetVal(false);
switch (m_ConfigOptions) {
case CF_REGISTRY:
bRetVal = (m_app->WriteProfileString(PWS_REG_OPTIONS, name, val) == TRUE);
break;
case CF_FILE_RW:
case CF_FILE_RW_NEW:
bRetVal = (m_XML_Config->Set(m_csHKCU_PREF, name, val) == 0);
break;
case CF_FILE_RO:
case CF_NONE:
default:
break;
}
UpdateTimeStamp();
return bRetVal;
}
bool PWSprefs::DeletePref(const CMyString &name)
{
bool bRetVal = true;
switch (m_ConfigOptions) {
case CF_REGISTRY:
m_app->WriteProfileInt(PWS_REG_OPTIONS, name, NULL);
break;
case CF_FILE_RW:
case CF_FILE_RW_NEW:
bRetVal = (m_XML_Config->DeleteSetting(m_csHKCU_PREF, name) == TRUE);
break;
case CF_FILE_RO:
case CF_NONE:
default:
break;
}
UpdateTimeStamp();
return bRetVal;
}
void PWSprefs::SetKeepXMLLock(bool state)
{
m_XML_Config->SetKeepXMLLock(state);
}
void PWSprefs::SetPrefRect(long top, long bottom,
long left, long right)
{
UpdateTimeStamp();
switch (m_ConfigOptions) {
case CF_REGISTRY:
m_app->WriteProfileInt(PWS_REG_POSITION, _T("top"), top);
m_app->WriteProfileInt(PWS_REG_POSITION, _T("bottom"), bottom);
m_app->WriteProfileInt(PWS_REG_POSITION, _T("left"), left);
m_app->WriteProfileInt(PWS_REG_POSITION, _T("right"), right);
break;
case CF_FILE_RW:
case CF_FILE_RW_NEW:
{
CString obuff;
m_XML_Config->SetKeepXMLLock(true);
obuff.Format(_T("%d"), top);
VERIFY(m_XML_Config->Set(m_csHKCU_POS, _T("top"), obuff) == 0);
obuff.Format(_T("%d"), bottom);
VERIFY(m_XML_Config->Set(m_csHKCU_POS, _T("bottom"), obuff) == 0);
obuff.Format(_T("%d"), left);
VERIFY(m_XML_Config->Set(m_csHKCU_POS, _T("left"), obuff) == 0);
obuff.Format(_T("%d"), right);
VERIFY(m_XML_Config->Set(m_csHKCU_POS, _T("right"), obuff) == 0);
m_XML_Config->SetKeepXMLLock(false);
}
break;
case CF_FILE_RO:
case CF_NONE:
default:
break;
}
}
CMyString PWSprefs::Store()
{
/*
* Create a string of values that are (1) different from the defaults, &&
* (2) are isStoredinDB
* String is of the form "X nn vv X nn vv..." Where X=[BIS] for binary,
* integer and string, resp.,
* nn is the numeric value of the enum, and vv is the value,
* {1,0} for bool, unsigned integer for int, and quoted string for String.
*/
CString retval(_T(""));
#ifdef _UNICODE
wostringstream os;
#else
ostrstream os;
#endif
for (int i = 0; i < NumBoolPrefs; i++)
if (m_boolValues[i] != m_bool_prefs[i].defVal &&
m_bool_prefs[i].isStoredinDB)
os << _T("B ") << i << TCHAR(' ') << (m_boolValues[i] ? 1 : 0) << TCHAR(' ');
for (int i = 0; i < NumIntPrefs; i++)
if (m_intValues[i] != m_int_prefs[i].defVal &&
m_int_prefs[i].isStoredinDB)
os << _T("I ") << i << TCHAR(' ') << m_intValues[i] << TCHAR(' ');
for (int i = 0; i < NumStringPrefs; i++)
if (m_stringValues[i] != m_string_prefs[i].defVal &&
m_string_prefs[i].isStoredinDB)
os << _T("S ") << i << _T(" \"") << LPCTSTR(m_stringValues[i]) << _T("\" ");
os << ends;
retval = os.str();
delete[] os.str(); // reports memory leaks in spite of this!
return CMyString(retval);
}
void PWSprefs::Load(const CMyString &prefString)
{
// Set default values for preferences stored in Database
for (int i = 0; i < NumBoolPrefs; i++)
if (m_bool_prefs[i].isStoredinDB)
m_boolValues[i] = m_bool_prefs[i].defVal != 0;
for (int i = 0; i < NumIntPrefs; i++)
if (m_int_prefs[i].isStoredinDB)
m_intValues[i] = m_int_prefs[i].defVal;
for (int i = 0; i < NumStringPrefs; i++)
if (m_string_prefs[i].isStoredinDB)
m_stringValues[i] = CMyString(m_string_prefs[i].defVal);
if (prefString.GetLength() == 0)
return;
// parse prefString, updating current values
#ifdef _UNICODE
wistringstream is(prefString);
#else
istrstream is(prefString);
#endif
char type;
int index, ival;
unsigned int iuval;
CMyString msval;
const int N = prefString.GetLength(); // safe upper limit on string size
char *buf = new char[N];
while (is) {
is >> type >> index;
switch (type) {
case TCHAR('B'):
// Need to get value - even of not understood or wanted
is >> ival;
// forward compatibility and check whether still in DB
if (index < NumBoolPrefs && m_bool_prefs[index].isStoredinDB) {
ASSERT(ival == 0 || ival == 1);
m_boolValues[index] = (ival != 0);
}
break;
case TCHAR('I'):
// Need to get value - even of not understood or wanted
is >> iuval;
// forward compatibility and check whether still in DB
if (index < NumIntPrefs && m_int_prefs[index].isStoredinDB)
m_intValues[index] = iuval;
break;
case TCHAR('S'):
// Need to get value - even of not understood or wanted
is.ignore(2, TCHAR('\"')); // skip over space and leading "
is.get(buf, N, TCHAR('\"')); // get string value
// forward compatibility and check whether still in DB
if (index < NumStringPrefs && m_string_prefs[index].isStoredinDB) {
msval= buf;
m_stringValues[index] = msval;
}
break;
default:
continue; // forward compatibility (also last space)
} // switch
} // while
delete[] buf;
}
void PWSprefs::UpdateTimeStamp()
{
if (m_ConfigOptions == CF_FILE_RW || m_ConfigOptions == CF_FILE_RW_NEW) {
time_t time_now;
time(&time_now);
const CMyString now = PWSUtil::ConvertToDateTimeString(time_now, TMC_XML);
m_XML_Config->Set(m_csHKCU, _T("LastUpdated"), now);
}
}
void PWSprefs::InitializePreferences()
{
// Does the registry entry exist for this user?
m_bRegistryKeyExists = CheckRegistryExists();
// Find out name of config file (should it exist).
TCHAR path_buffer[_MAX_PATH];
TCHAR drive[_MAX_DRIVE];
TCHAR dir[_MAX_DIR];
GetModuleFileName(NULL, path_buffer, _MAX_PATH);
#if _MSC_VER >= 1400
_tsplitpath_s(path_buffer, drive, _MAX_DRIVE, dir, _MAX_DIR, NULL, 0, NULL, 0);
_tmakepath_s(path_buffer, _MAX_PATH, drive, dir, _T("pwsafe"), _T("cfg"));
#else
_tsplitpath(path_buffer, drive, dir, NULL, NULL);
_tmakepath(path_buffer, drive, dir, _T("pwsafe"), _T("cfg"));
#endif
m_configfilename = (CString)path_buffer;
// Set up all the XML "keys"
const SysInfo *si = SysInfo::GetInstance();
// Get the name of the computer
m_csHKCU = si->GetCurrentHost() + _T("\\");
// Get the user name
m_csHKCU += si->GetCurrentUser();
m_csHKCU_MRU = m_csHKCU + _T("\\MRU");
m_csHKCU_POS = m_csHKCU + _T("\\Position");
m_csHKCU_PREF = m_csHKCU + _T("\\Preferences");
m_XML_Config = new CXMLprefs ();
m_XML_Config->SetConfigFile(m_configfilename);
CFile iniFile;
CFileException fileException;
CFileStatus fileStatus;
// Does it exist?
m_bConfigFileExists = CFile::GetStatus(m_configfilename, fileStatus) == TRUE;
CMyString locker(_T(""));
// We only loop on getting the lock in this one place
// as we need to know what to use
bool bgotconfiglock;
for (int iLoop = 0; iLoop < 10; iLoop++) {
bgotconfiglock = PWSfile::LockFile(m_configfilename, locker, false);
if (bgotconfiglock)
break;
Sleep(200); // try max of 10 * 0.2 seconds = 2 secs
}
if (!bgotconfiglock) {
AfxMessageBox(IDSC_NOCONFIGLOCK, MB_OK);
m_ConfigOptions = CF_REGISTRY;
if (m_bRegistryKeyExists)
LoadProfileFromRegistry();
else
LoadProfileFromDefaults();
return;
}
m_ConfigOptions = CF_NONE;
// checking for: CFileException::accessDenied
if (m_bConfigFileExists) {
// File exists: check we can write to it
if (iniFile.Open(m_configfilename, CFile::modeWrite,
&fileException)) {
// it exists and we can use it!
iniFile.Close();
m_ConfigOptions = CF_FILE_RW;
// Are we already in it?
CString tmp = m_XML_Config->Get(m_csHKCU, _T("LastUpdated"), _T(""));
time_t tt;
if (!PWSUtil::VerifyXMLDateTimeString(tmp, tt)) {
// No - try and load from registry
if (m_bRegistryKeyExists)
LoadProfileFromRegistry();
else
LoadProfileFromDefaults();
// Now save to file
SaveProfileToXML();
} else {
// Yes - load previous values
LoadProfileFromFile();
}
} else {
// it exists but we can't write to it!
FileError(fileException.m_cause);
// Are we already in it?
CString tmp = m_XML_Config->Get(m_csHKCU, _T("LastUpdated"), _T(""));
time_t tt;
if (!PWSUtil::VerifyXMLDateTimeString(tmp, tt)) {
// No - use registry
m_ConfigOptions = CF_REGISTRY;
if (m_bRegistryKeyExists)
LoadProfileFromRegistry();
else
LoadProfileFromDefaults();
} else {
// Yes - load previous values
m_ConfigOptions = CF_FILE_RO;
LoadProfileFromFile();
}
}
} else {
// File doesn't exist: check we can create it and write to it
if (iniFile.Open(m_configfilename, CFile::modeCreate | CFile::modeWrite,
&fileException)) {
iniFile.Close();
m_ConfigOptions = CF_FILE_RW_NEW;
DeleteFile(m_configfilename); // Since we deleted it!
LoadProfileFromRegistry();
SaveProfileToXML();
} else {
m_ConfigOptions = CF_REGISTRY;
if (m_bRegistryKeyExists)
LoadProfileFromRegistry();
else
LoadProfileFromDefaults();
FileError(fileException.m_cause);
}
}
PWSfile::UnlockFile(m_configfilename, false);
CString cs_msg;
switch (m_ConfigOptions) {
case CF_REGISTRY:
cs_msg.LoadString(IDSC_CANTCREATEXMLCFG);
break;
case CF_FILE_RW:
case CF_FILE_RW_NEW:
m_XML_Config->SetReadWriteStatus(true);
break;
case CF_FILE_RO:
m_XML_Config->SetReadWriteStatus(false);
cs_msg.LoadString(IDSC_CANTUPDATEXMLCFG);
break;
case CF_NONE:
default:
cs_msg.LoadString(IDSC_CANTDETERMINECFG);
break;
}
if (!cs_msg.IsEmpty())
AfxMessageBox(cs_msg, MB_OK);
}
void PWSprefs::LoadProfileFromDefaults()
{
// Default values only
for (int i = 0; i < NumBoolPrefs; i++)
m_boolValues[i] = m_bool_prefs[i].defVal != 0;
for (int i = 0; i < NumIntPrefs; i++)
m_intValues[i] = m_int_prefs[i].defVal;
for (int i = 0; i < NumStringPrefs; i++)
m_stringValues[i] = CMyString(m_string_prefs[i].defVal);
}
void PWSprefs::LoadProfileFromRegistry()
{
// Read in values from registry
int i;
// Defensive programming, if not "0", then "TRUE", all other values = FALSE
for (i = 0; i < NumBoolPrefs; i++)
m_boolValues[i] = m_app->GetProfileInt(PWS_REG_OPTIONS,
m_bool_prefs[i].name,
m_bool_prefs[i].defVal) != 0;
// Defensive programming, if outside the permitted range, then set to default
for (i = 0; i < NumIntPrefs; i++) {
const int iVal = m_app->GetProfileInt(PWS_REG_OPTIONS,
m_int_prefs[i].name,
m_int_prefs[i].defVal);
if (m_int_prefs[i].minVal != -1 && iVal < m_int_prefs[i].minVal)
m_intValues[i] = m_int_prefs[i].defVal;
else if (m_int_prefs[i].maxVal != -1 && iVal > m_int_prefs[i].maxVal)
m_intValues[i] = m_int_prefs[i].defVal;
else m_intValues[i] = iVal;
}
// Defensive programming not applicable.
for (int i = 0; i < NumStringPrefs; i++)
m_stringValues[i] = CMyString(m_app->GetProfileString(PWS_REG_OPTIONS,
m_string_prefs[i].name,
m_string_prefs[i].defVal));
/*
The following is "defensive" code because there was "a code ordering
issue" in V3.02 and earlier. PWSprefs.cpp and PWSprefs.h differed in
the order of the HotKey and DoubleClickAction preferences.
This is to protect the application should a HotKey value be assigned
to DoubleClickAction.
Note: HotKey also made an "Application preference" from a "Database
preference".
*/
if (m_intValues[HotKey] > 0 && m_intValues[HotKey] <= 3) {
m_boolValues[HotKeyEnabled] = false;
m_intValues[DoubleClickAction] = m_intValues[HotKey];
m_intValues[HotKey] = 0;
m_prefs_changed[APP_PREF] = true;
}
if (m_intValues[DoubleClickAction] > 3) {
m_intValues[DoubleClickAction] = 1;
m_prefs_changed[APP_PREF] = true;
}
// End of "defensive" code
}
void PWSprefs::LoadProfileFromFile()
{
// Read in values from file
// Defensive programming, if not "0", then "TRUE", all other values = FALSE
for (int i = 0; i < NumBoolPrefs; i++)
m_boolValues[i] = m_XML_Config->Get(m_csHKCU_PREF,
m_bool_prefs[i].name,
m_bool_prefs[i].defVal) != 0;
// Defensive programming, if outside the permitted range, then set to default
for (int i = 0; i < NumIntPrefs; i++) {
const int iVal = m_XML_Config->Get(m_csHKCU_PREF,
m_int_prefs[i].name,
m_int_prefs[i].defVal);
if (m_int_prefs[i].minVal != -1 && iVal < m_int_prefs[i].minVal)
m_intValues[i] = m_int_prefs[i].defVal;
else if (m_int_prefs[i].maxVal != -1 && iVal > m_int_prefs[i].maxVal)
m_intValues[i] = m_int_prefs[i].defVal;
else m_intValues[i] = iVal;
}
// Defensive programming not applicable.
for (int i = 0; i < NumStringPrefs; i++)
m_stringValues[i] = CMyString(m_XML_Config->Get(m_csHKCU_PREF,
m_string_prefs[i].name,
m_string_prefs[i].defVal));
}
void PWSprefs::SaveApplicationPreferences()
{
if (m_prefs_changed[APP_PREF] == false)
return;
if (m_ConfigOptions == CF_FILE_RW ||
m_ConfigOptions == CF_FILE_RW_NEW)
m_XML_Config->SetKeepXMLLock(true);
UpdateTimeStamp();
// Write in values to XML file
for (int i = 0; i < NumBoolPrefs; i++) {
if (!m_bool_prefs[i].isStoredinDB && m_boolChanged[i]) {
if (m_boolValues[i] != m_bool_prefs[i].defVal) {
WritePref(m_bool_prefs[i].name, m_boolValues[i]);
} else {
DeletePref(m_bool_prefs[i].name);
}
m_boolChanged[i] = false;
}
}
for (int i = 0; i < NumIntPrefs; i++) {
if (!m_int_prefs[i].isStoredinDB && m_intChanged[i]) {
if (m_intValues[i] != m_int_prefs[i].defVal) {
WritePref(m_int_prefs[i].name, m_intValues[i]);
} else {
DeletePref(m_int_prefs[i].name);
}
m_intChanged[i] = false;
}
}
for (int i = 0; i < NumStringPrefs; i++) {
if (!m_string_prefs[i].isStoredinDB && m_stringChanged[i]) {
if (m_stringValues[i] != m_string_prefs[i].defVal) {
WritePref(m_string_prefs[i].name, m_stringValues[i]);
} else {
DeletePref(m_string_prefs[i].name);
}
m_stringChanged[i] = false;
}
}
if (m_ConfigOptions == CF_FILE_RW ||
m_ConfigOptions == CF_FILE_RW_NEW)
m_XML_Config->SetKeepXMLLock(false);
m_prefs_changed[APP_PREF] = false;
}
void PWSprefs::SaveProfileToXML()
{
m_XML_Config->SetKeepXMLLock(true);
UpdateTimeStamp();
// Write in values to XML file
for (int i = 0; i < NumBoolPrefs; i++) {
if (!m_bool_prefs[i].isStoredinDB) {
if (m_boolValues[i] != m_bool_prefs[i].defVal) {
VERIFY(m_XML_Config->Set(m_csHKCU_PREF,
m_bool_prefs[i].name,
m_boolValues[i] ? 1 : 0) == 0);
} else {
DeletePref(m_bool_prefs[i].name);
}
}
}
for (int i = 0; i < NumIntPrefs; i++) {
if (!m_int_prefs[i].isStoredinDB) {
if (m_intValues[i] != m_int_prefs[i].defVal) {
VERIFY(m_XML_Config->Set(m_csHKCU_PREF,
m_int_prefs[i].name,
m_intValues[i]) == 0);
} else {
DeletePref(m_int_prefs[i].name);
}
}
}
for (int i = 0; i < NumStringPrefs; i++) {
if (!m_string_prefs[i].isStoredinDB) {
if (m_stringValues[i] != m_string_prefs[i].defVal) {
VERIFY(m_XML_Config->Set(m_csHKCU_PREF,
m_string_prefs[i].name,
m_stringValues[i]) == 0);
} else {
DeletePref(m_string_prefs[i].name);
}
}
}
m_XML_Config->SetKeepXMLLock(false);
}
void PWSprefs::DeleteMRUFromXML(const CString &csSubkey)
{
m_XML_Config->DeleteSetting(m_csHKCU_MRU, csSubkey);
}
CString PWSprefs::ReadMRUFromXML(const CString &csSubkey)
{
return m_XML_Config->Get(m_csHKCU_MRU, csSubkey, _T(""));
}
void PWSprefs::WriteMRUToXML(const CString &csSubkey, const CString &csMRUFilename)
{
if (csMRUFilename.IsEmpty()) return;
// Don't remember backup files
if (csMRUFilename.Right(4) == _T(".bak") ||
csMRUFilename.Right(5) == _T(".bak~") ||
csMRUFilename.Right(5) == _T(".ibak") ||
csMRUFilename.Right(6) == _T(".ibak~"))
return;
m_XML_Config->Set(m_csHKCU_MRU, csSubkey, csMRUFilename);
}
void PWSprefs::DeleteRegistryEntries()
{
HKEY hSubkey;
const CString csSubkey = _T("Software\\") + CString(m_app->m_pszRegistryKey);
LONG dw = RegOpenKeyEx(HKEY_CURRENT_USER,
csSubkey,
NULL,
KEY_ALL_ACCESS,
&hSubkey);
ASSERT(dw == ERROR_SUCCESS);
dw = m_app->DelRegTree(hSubkey, m_app->m_pszAppName);
ASSERT(dw == ERROR_SUCCESS);
dw = RegCloseKey(hSubkey);
ASSERT(dw == ERROR_SUCCESS);
}
void PWSprefs::FileError(const int &icause)
{
CString cs_error, cs_msg;
ASSERT(icause >= 0 && icause <= 14);
cs_error.LoadString(IDSC_FILEEXCEPTION00 + icause);
cs_msg.Format(IDSC_CANTUSECONFIGFILE, cs_error);
AfxMessageBox(cs_msg, MB_OK);
}