[d6386d]: pwsafe / pwsafe / corelib / PWSprefs.cpp  Maximize  Restore  History

Download this file

1116 lines (999 with data), 36.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
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
/*
* Copyright (c) 2003-2007 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"
#include "PWSdirs.h"
#ifdef POCKET_PC
// use wcelibcex's impl of time functions
#include <wce_time.h>
#define time wceex_time
#endif
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
HANDLE s_cfglockFileHandle = INVALID_HANDLE_VALUE;
int s_cfgLockCount = 0;
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("ShowPasswordInTree"), 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"), true, false}, // application
{_T("PreExpiryWarn"), false, false}, // application
{_T("ExplorerTypeTree"), false, false}, // application
{_T("ListViewGridLines"), false, false}, // application
{_T("MinimizeOnAutotype"), true, false}, // application
{_T("ShowUsernameInTree"), true, true}, // database
};
// 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, 15}, // 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
// Specified by supported masks
{_T("BackupSuffix"), 0, false, minBKSFX, maxBKSFX}, // application
{_T("BackupMaxIncremented"), 1, false, 1, 999}, // application
{_T("PreExpiryWarnDays"), 1, false, 1, 30}, // application
};
const PWSprefs::stringPref PWSprefs::m_string_prefs[NumStringPrefs] = {
{_T("currentbackup"), _T(""), false}, // application
{_T("currentfile"), _T(""), false}, // application
{_T("lastview"), _T("tree"), false}, // application
{_T("defusername"), _T(""), true}, // database
{_T("treefont"), _T(""), false}, // application
{_T("BackupPrefixValue"), _T(""), false}, // application
{_T("BackupDir"), _T(""), false}, // application
{_T("AltBrowser"), _T(""), false}, // application
{_T("ListColumns"), _T(""), false}, // application
{_T("ColumnWidths"), _T(""), false}, // application
{_T("DefaultAutotypeString"), _T(""), true}, // database
{_T("AltBrowserCmdLineParms"), _T(""), false}, // application
};
PWSprefs *PWSprefs::GetInstance()
{
if (self == NULL) {
self = new PWSprefs();
}
return self;
}
void PWSprefs::DeleteInstance()
{
delete self;
self = NULL;
SysInfo::DeleteInstance();
}
PWSprefs::PWSprefs() : m_app(::AfxGetApp()), m_XML_Config(NULL)
{
ASSERT(m_app != NULL);
int i;
m_prefs_changed[DB_PREF] = false;
m_prefs_changed[APP_PREF] = false;
for (i = 0; i < NumBoolPrefs; i++)
m_boolChanged[i] = false;
for (i = 0; i < NumIntPrefs; i++)
m_intChanged[i] = false;
for (i = 0; i < NumStringPrefs; i++)
m_stringChanged[i] = false;
m_rect.top = m_rect.bottom = m_rect.left = m_rect.right = -1;
m_rect.changed = false;
m_MRUitems = new CString[m_int_prefs[MaxMRUItems].maxVal];
InitializePreferences();
}
PWSprefs::~PWSprefs()
{
// delete m_XML_Config;
delete[] m_MRUitems;
}
bool
PWSprefs::CheckRegistryExists() const
{
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
{
top = m_rect.top;
bottom = m_rect.bottom;
left = m_rect.left;
right = m_rect.right;
}
int PWSprefs::GetMRUList(CString *MRUFiles)
{
ASSERT(MRUFiles != NULL);
if (m_ConfigOptions == CF_NONE || m_ConfigOptions == CF_REGISTRY)
return 0;
const int n = GetPref(PWSprefs::MaxMRUItems);
for (int i = 0; i < n; i++)
MRUFiles[i] = m_MRUitems[i];
return n;
}
int PWSprefs::SetMRUList(const CString *MRUFiles, int n, int max_MRU)
{
ASSERT(MRUFiles != NULL);
if (m_ConfigOptions == CF_NONE || m_ConfigOptions == CF_REGISTRY ||
m_ConfigOptions == CF_FILE_RO)
return 0;
int i, cnt;
bool changed = false;
// remember the ones in use
for (i = 0, cnt = 1; i < n; i++) {
if (MRUFiles[i].IsEmpty() ||
// Don't remember backup files
MRUFiles[i].Right(4) == _T(".bak") ||
MRUFiles[i].Right(5) == _T(".bak~") ||
MRUFiles[i].Right(5) == _T(".ibak") ||
MRUFiles[i].Right(6) == _T(".ibak~"))
continue;
if (m_MRUitems[cnt-1] != MRUFiles[i]) {
m_MRUitems[cnt-1] = MRUFiles[i];
changed = true;
}
cnt++;
}
// Remove any not in use
for (i = cnt - 1; i < max_MRU; i++) {
if (!m_MRUitems[i].IsEmpty()) {
m_MRUitems[i] = _T("");
changed = true;
}
}
if (changed)
m_prefs_changed[APP_PREF] = true;
return n;
}
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:
/// avoid XML!
bRetVal = true; //(m_XML_Config->Set(m_csHKCU_PREF, name, val ? 1 : 0) == 0);
break;
case CF_FILE_RO:
case CF_NONE:
default:
break;
}
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:
/// avoid XML!
bRetVal = true; //(m_XML_Config->Set(m_csHKCU_PREF, name, val) == 0);
break;
case CF_FILE_RO:
case CF_NONE:
default:
break;
}
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:
/// avoid XML!
bRetVal = true; //(m_XML_Config->Set(m_csHKCU_PREF, name, val) == 0);
break;
case CF_FILE_RO:
case CF_NONE:
default:
break;
}
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:
/// avoid XML!
bRetVal = true; //(m_XML_Config->DeleteSetting(m_csHKCU_PREF, name) == TRUE);
break;
case CF_FILE_RO:
case CF_NONE:
default:
break;
}
return bRetVal;
}
void PWSprefs::SetPrefRect(long top, long bottom,
long left, long right)
{
if (m_rect.top != top) {
m_rect.top = top; m_rect.changed = true;
}
if (m_rect.bottom != bottom) {
m_rect.bottom = bottom; m_rect.changed = true;
}
if (m_rect.left != left) {
m_rect.left = left; m_rect.changed = true;
}
if (m_rect.right != right) {
m_rect.right = right; m_rect.changed = true;
}
if (m_rect.changed)
m_prefs_changed[APP_PREF] = true;
}
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
ostringstream os;
#endif
int i;
for (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 (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 (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().c_str();
return CMyString(retval);
}
void PWSprefs::Load(const CMyString &prefString)
{
// Set default values for preferences stored in Database
int i;
for (i = 0; i < NumBoolPrefs; i++)
if (m_bool_prefs[i].isStoredinDB)
m_boolValues[i] = m_bool_prefs[i].defVal != 0;
for (i = 0; i < NumIntPrefs; i++)
if (m_int_prefs[i].isStoredinDB)
m_intValues[i] = m_int_prefs[i].defVal;
for (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
wstring sps(prefString);
wistringstream is(sps);
#else
string sps(prefString);
istringstream is(sps);
#endif
TCHAR type;
int index, ival;
unsigned int iuval;
CMyString msval;
const int N = prefString.GetLength(); // safe upper limit on string size
TCHAR *buf = new TCHAR[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);
/// avoid XML!
//m_XML_Config->Set(m_csHKCU, _T("LastUpdated"), now);
}
}
static void xmlify( TCHAR t, CString &name)
{
if (!_istalpha(name[0]))
name = t + name;
int N = name.GetLength();
for (int i = 0; i < N; i++)
if (!_istalnum(name[i]) &&
name[i] != TCHAR('_') &&
name[i] != TCHAR('-') &&
name[i] != TCHAR(':') &&
name[i] != TCHAR('.'))
name.SetAt(i, TCHAR('_'));
}
void PWSprefs::InitializePreferences()
{
/*
* 1. If the config file exists, use it, ignore registry (common case)
* 2. If no config file and old (*) registry tree, import registry prefs,
* create config file. (1st run on upgrade)
* 3. If no config file and no registry, create config file. (virgin install)
*
* (*) Old == "Counterpane Systems" reg key
* - User can delete old registry key explicitly via options.
* - "No config file" also means config file exists but no entry for
* host/user
* - If config file can't be created, fallback to "Password Safe" registry
*/
// Set path & name of config file
m_configfilename = PWSdirs::GetConfigDir() + _T("pwsafe.cfg");
// Start with fallback position: hardcoded defaults
LoadProfileFromDefaults();
m_ConfigOptions = CF_NONE;
// Actually, "config file exists" means:
// 1. File exists &&
// 2. host/user key found.
// 1. Does config file exist (and if, so, can we write to it?)?
bool isRO = false;
bool configFileExists = PWSfile::FileExists(m_configfilename, isRO);
if (configFileExists)
m_ConfigOptions = (isRO) ? CF_FILE_RO : CF_FILE_RW;
else
m_ConfigOptions = CF_FILE_RW_NEW;
const SysInfo *si = SysInfo::GetInstance();
// Set up XML "keys": host/user
// ensure that they start with letter,
// and otherwise conforms with
// http://www.w3.org/TR/2000/REC-xml-20001006#NT-Name
CString hn = si->GetCurrentHost();
xmlify('H', hn);
CString un = si->GetCurrentUser();
xmlify('u', un);
m_csHKCU = hn + _T("\\") + un;
// set up other keys
m_csHKCU_MRU = m_csHKCU + _T("\\MRU");
m_csHKCU_POS = m_csHKCU + _T("\\Position");
m_csHKCU_PREF = m_csHKCU + _T("\\Preferences");
// Does the registry entry exist for this user?
m_bRegistryKeyExists = CheckRegistryExists();
// 2. host/user key found?
if (configFileExists) {
if (!LoadProfileFromFile()) {
// Config file exists, but host/user not in it
if (!isRO) { // we can create one
ImportOldPrefs(); // get pre-3.05, if any
// If we didn't have r/w but now do:
LoadProfileFromRegistry();
} else { // isRO
// awkward situation, config file exists, we're not in it,
// can't write to it either.
// Would a warning to user be appropriate?
if (CheckRegistryExists())
LoadProfileFromRegistry();
else
ImportOldPrefs();
} // isRO
} // host/user not found
} else { // File doesn't exist
ImportOldPrefs();
LoadProfileFromRegistry();
// can we create one? If not, fallback to registry
// We assume that if we can create a lock file, we can create
// a config file in the same directory
CMyString locker;
if (LockCFGFile(m_configfilename, locker)) {
UnlockCFGFile(m_configfilename);
} else {
m_ConfigOptions = CF_REGISTRY; // CF_FILE_RW_NEW -> CF_REGISTRY
}
}
CString cs_msg;
switch (m_ConfigOptions) {
case CF_REGISTRY:
cs_msg.LoadString(IDSC_CANTCREATEXMLCFG);
break;
case CF_FILE_RW:
case CF_FILE_RW_NEW:
break;
case CF_FILE_RO:
cs_msg.LoadString(IDSC_CANTUPDATEXMLCFG);
break;
case CF_NONE:
default:
cs_msg.LoadString(IDSC_CANTDETERMINECFG);
break;
}
if (!cs_msg.IsEmpty())
TRACE(cs_msg);
}
void PWSprefs::SetDatabasePrefsToDefaults()
{
// set prefs to hardcoded values
int i;
// Default values only
for (i = 0; i < NumBoolPrefs; i++)
if (m_bool_prefs[i].isStoredinDB)
m_boolValues[i] = m_bool_prefs[i].defVal != 0;
for (i = 0; i < NumIntPrefs; i++)
if (m_int_prefs[i].isStoredinDB)
m_intValues[i] = m_int_prefs[i].defVal;
for (i = 0; i < NumStringPrefs; i++)
if (m_string_prefs[i].isStoredinDB)
m_stringValues[i] = CMyString(m_string_prefs[i].defVal);
}
void PWSprefs::LoadProfileFromDefaults()
{
// set prefs to hardcoded values
int i;
// Default values only
for (i = 0; i < NumBoolPrefs; i++)
m_boolValues[i] = m_bool_prefs[i].defVal != 0;
for (i = 0; i < NumIntPrefs; i++)
m_intValues[i] = m_int_prefs[i].defVal;
for (i = 0; i < NumStringPrefs; i++)
m_stringValues[i] = CMyString(m_string_prefs[i].defVal);
}
void PWSprefs::LoadProfileFromRegistry()
{
// Read in values from registry
if (!CheckRegistryExists())
return; // Avoid creating keys if none already, as
// GetProfile* creates keys if not found!
// Note that default values are now current values,
// as they've been set in LoadProfileFromDefaults, and
// may have been overridden by ImportOldPrefs()
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_boolValues[i]) != 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_intValues[i]);
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 (i = 0; i < NumStringPrefs; i++)
m_stringValues[i] = CMyString(m_app->GetProfileString(PWS_REG_OPTIONS,
m_string_prefs[i].name,
m_stringValues[i]));
/*
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
// Load last main window size & pos:
m_rect.top = m_app->GetProfileInt(PWS_REG_POSITION,
_T("top"), -1);
m_rect.bottom = m_app->GetProfileInt(PWS_REG_POSITION,
_T("bottom"), -1);
m_rect.left = m_app->GetProfileInt(PWS_REG_POSITION,
_T("left"), -1);
m_rect.right = m_app->GetProfileInt(PWS_REG_POSITION,
_T("right"), -1);
}
bool PWSprefs::LoadProfileFromFile()
{
/*
* Called from InitializePreferences() at startup,
* attempts to read in application preferences
* from pref file.
* Returns false if couldn't read (unlikely, since exists),
* or if no host/user section
* found.
*/
return false;
/// avoid XML!
/* bool retval;
CString ts, csSubkey;
m_XML_Config = new CXMLprefs(m_configfilename);
if (!m_XML_Config->Load()) {
retval = false;
goto exit;
}
// Are we (host/user) already in the config file?
ts = m_XML_Config->Get(m_csHKCU, _T("LastUpdated"), _T(""));
time_t tt;
if (!PWSUtil::VerifyXMLDateTimeString(ts, tt)) {
// No, nothing to load, return false
retval = false;
goto exit;
}
int i;
// Defensive programming, if not "0", then "TRUE", all other values = FALSE
for (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 (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 (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));
// Load last main window size & pos:
m_rect.top = m_XML_Config->Get(m_csHKCU_POS, _T("top"), -1);
m_rect.bottom = m_XML_Config->Get(m_csHKCU_POS, _T("bottom"), -1);
m_rect.left = m_XML_Config->Get(m_csHKCU_POS, _T("left"), -1);
m_rect.right = m_XML_Config->Get(m_csHKCU_POS, _T("right"), -1);
// Load most recently used file list
const int nMRUItems = m_intValues[MaxMRUItems];
for (i = nMRUItems; i > 0; i--) {
csSubkey.Format(_T("Safe%02d"), i);
m_MRUitems[i-1] = m_XML_Config->Get(m_csHKCU_MRU, csSubkey, _T(""));
}
retval = true;
exit:
delete m_XML_Config;
m_XML_Config = NULL;
return retval;
*/
}
void PWSprefs::SaveApplicationPreferences()
{
int i;
if (!m_prefs_changed[APP_PREF])
return;
if (m_ConfigOptions == CF_FILE_RW ||
m_ConfigOptions == CF_FILE_RW_NEW) {
// Load prefs file in case it was changed elsewhere
// Here we need to explicitly lock from before
// load to after store
/// avoid XML!
// m_XML_Config = new CXMLprefs(m_configfilename);
// if (!m_XML_Config->Lock()) {
// punt to registry!
m_ConfigOptions = CF_REGISTRY;
// delete m_XML_Config;
// m_XML_Config = NULL;
// } else { // acquired lock
// // if file exists, load to get other values
// if (PWSfile::FileExists(m_configfilename))
// m_XML_Config->Load();
// }
}
UpdateTimeStamp();
// Write values to XML file or registry
for (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 (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 (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_rect.changed) {
switch (m_ConfigOptions) {
case CF_REGISTRY:
m_app->WriteProfileInt(PWS_REG_POSITION,
_T("top"), m_rect.top);
m_app->WriteProfileInt(PWS_REG_POSITION,
_T("bottom"), m_rect.bottom);
m_app->WriteProfileInt(PWS_REG_POSITION,
_T("left"), m_rect.left);
m_app->WriteProfileInt(PWS_REG_POSITION,
_T("right"), m_rect.right);
break;
case CF_FILE_RW:
case CF_FILE_RW_NEW: {
CString obuff;
obuff.Format(_T("%d"), m_rect.top);
/// avoid XML!
//VERIFY(m_XML_Config->Set(m_csHKCU_POS, _T("top"), obuff) == 0);
obuff.Format(_T("%d"), m_rect.bottom);
//VERIFY(m_XML_Config->Set(m_csHKCU_POS, _T("bottom"), obuff) == 0);
obuff.Format(_T("%d"), m_rect.left);
//VERIFY(m_XML_Config->Set(m_csHKCU_POS, _T("left"), obuff) == 0);
obuff.Format(_T("%d"), m_rect.right);
//VERIFY(m_XML_Config->Set(m_csHKCU_POS, _T("right"), obuff) == 0);
}
break;
case CF_FILE_RO:
case CF_NONE:
default:
break;
}
m_rect.changed = false;
} // m_rect.changed
if (m_ConfigOptions == CF_FILE_RW ||
m_ConfigOptions == CF_FILE_RW_NEW) {
int i;
const int n = GetPref(PWSprefs::MaxMRUItems);
// Delete ALL entries
/// avoid XML!
//m_XML_Config->DeleteSetting(m_csHKCU_MRU, _T(""));
// Now put back the ones we want
CString csSubkey;
for (i = 0; i < n; i++)
if (!m_MRUitems[i].IsEmpty()) {
csSubkey.Format(_T("Safe%02d"), i+1);
/// avoid XML!
//m_XML_Config->Set(m_csHKCU_MRU, csSubkey, m_MRUitems[i]);
}
}
if (m_ConfigOptions == CF_FILE_RW ||
m_ConfigOptions == CF_FILE_RW_NEW) {
/// avoid XML!
//m_XML_Config->Store();
//m_XML_Config->Unlock();
//delete m_XML_Config;
//m_XML_Config = NULL;
}
m_prefs_changed[APP_PREF] = false;
}
bool PWSprefs::OfferDeleteRegistry() const
{
return (m_ConfigOptions == CF_FILE_RW &&
(m_bRegistryKeyExists || OldPrefsExist()));
}
void PWSprefs::DeleteRegistryEntries()
{
DeleteOldPrefs();
HKEY hSubkey;
const CString csSubkey = _T("Software\\") + CString(m_app->m_pszRegistryKey);
LONG dw = RegOpenKeyEx(HKEY_CURRENT_USER,
csSubkey,
NULL,
KEY_ALL_ACCESS,
&hSubkey);
if (dw != ERROR_SUCCESS) {
return; // may have been called due to OldPrefs
}
dw = m_app->DelRegTree(hSubkey, m_app->m_pszAppName);
ASSERT(dw == ERROR_SUCCESS);
dw = RegCloseKey(hSubkey);
ASSERT(dw == ERROR_SUCCESS);
m_bRegistryKeyExists = false;
}
int PWSprefs::GetConfigIndicator() const
{
switch (m_ConfigOptions) {
case CF_NONE: return IDSC_CONFIG_NONE;
case CF_REGISTRY: return IDSC_CONFIG_REGISTRY;
case CF_FILE_RW:
case CF_FILE_RW_NEW: return IDSC_CONFIG_FILE_RW;
case CF_FILE_RO: return IDSC_CONFIG_FILE_RO;
default: ASSERT(0); return 0;
}
}
// Old registry handling code:
const CString OldSubKey(_T("Counterpane Systems"));
const CString Software(_T("Software"));
bool PWSprefs::OldPrefsExist() const
{
bool bExists;
HKEY hSubkey;
bExists = (::RegOpenKeyEx(HKEY_CURRENT_USER,
Software + _T("\\") + OldSubKey,
0L,
KEY_READ,
&hSubkey) == ERROR_SUCCESS);
if (bExists)
::RegCloseKey(hSubkey);
return bExists;
}
void PWSprefs::ImportOldPrefs()
{
HKEY hSubkey;
CString OldAppKey = Software + _T("\\") + OldSubKey + _T("\\Password Safe");
LONG dw = ::RegOpenKeyEx(HKEY_CURRENT_USER,
OldAppKey,
NULL,
KEY_ALL_ACCESS,
&hSubkey);
if (dw != ERROR_SUCCESS) {
return;
}
// Iterate over app preferences (those not stored
// in database, read values and store if exist
int i;
LONG rv;
DWORD dwType;
for (i = 0; i < NumBoolPrefs; i++)
if (!m_bool_prefs[i].isStoredinDB) {
DWORD vData, DataLen(sizeof(vData));
rv = ::RegQueryValueEx(hSubkey,
m_bool_prefs[i].name,
NULL,
&dwType,
LPBYTE(&vData),
&DataLen
);
if (rv == ERROR_SUCCESS && dwType == REG_DWORD)
SetPref(BoolPrefs(i), (vData != 0));
}
for (i = 0; i < NumIntPrefs; i++)
if (!m_int_prefs[i].isStoredinDB) {
DWORD vData, DataLen(sizeof(vData));
rv = ::RegQueryValueEx(hSubkey,
m_int_prefs[i].name,
NULL,
&dwType,
LPBYTE(&vData),
&DataLen
);
if (rv == ERROR_SUCCESS && dwType == REG_DWORD)
SetPref(IntPrefs(i), vData);
}
for (i = 0; i < NumStringPrefs; i++)
if (!m_string_prefs[i].isStoredinDB) {
DWORD DataLen = 0;
rv = ::RegQueryValueEx(hSubkey,
m_string_prefs[i].name,
NULL,
&dwType,
NULL,
&DataLen
);
if (rv == ERROR_SUCCESS && dwType == REG_SZ) {
DataLen++;
BYTE *pData = new BYTE[DataLen];
::memset(pData, 0, DataLen);
rv = ::RegQueryValueEx(hSubkey,
m_string_prefs[i].name,
NULL,
&dwType,
pData,
&DataLen
);
if (rv == ERROR_SUCCESS)
SetPref(StringPrefs(i), CMyString(pData));
delete[] pData;
} // Get the value
} // pref in registry
// Last but not least, rectangle
long rectVals[4] = {-1, -1, -1, -1};
TCHAR *rectNames[4] = {_T("top"), _T("bottom"), _T("left"), _T("right")};
for (i = 0; i < 4; i++) {
DWORD vData, DataLen(sizeof(vData));
rv = ::RegQueryValueEx(hSubkey,
rectNames[i],
NULL,
&dwType,
LPBYTE(&vData),
&DataLen
);
if (rv == ERROR_SUCCESS && dwType == REG_DWORD)
rectVals[i] = vData;
}
SetPrefRect(rectVals[0], rectVals[1], rectVals[2], rectVals[3]);
dw = ::RegCloseKey(hSubkey);
ASSERT(dw == ERROR_SUCCESS);
}
void PWSprefs::DeleteOldPrefs()
{
HKEY hSubkey;
LONG dw = ::RegOpenKeyEx(HKEY_CURRENT_USER,
Software,
NULL,
KEY_ALL_ACCESS,
&hSubkey);
if (dw != ERROR_SUCCESS) {
return;
}
dw = m_app->DelRegTree(hSubkey, OldSubKey);
ASSERT(dw == ERROR_SUCCESS);
dw = ::RegCloseKey(hSubkey);
ASSERT(dw == ERROR_SUCCESS);
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks