[f1e2cf]: DboxMain.cpp Maximize Restore History

Download this file

DboxMain.cpp    1436 lines (1269 with data), 43.9 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
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
// file DboxMain.cpp
//
// The implementation of DboxMain's member functions is spread over source
// files that start with "Main" and reflect the main menu partitioning:
// MainFile.cpp implements the methods pertaining to the functions under
// the File menu, MainView.cpp does the same for the View menu, etc.
//
//This file contains what's left.
//-----------------------------------------------------------------------------
#include "PasswordSafe.h"
#include "ThisMfcApp.h"
#include "PwFont.h"
#include "corelib/PWSprefs.h"
#include "corelib/PWSrand.h"
#if defined(POCKET_PC)
#include "pocketpc/resource.h"
#else
#include "resource.h"
#endif
// dialog boxen
#include "DboxMain.h"
#include "TryAgainDlg.h"
#include "PasskeyEntry.h"
#include "ExpPWListDlg.h"
#include "FindDlg.h"
// widget override?
#include "SysColStatic.h"
#ifdef POCKET_PC
#include "pocketpc/PocketPC.h"
#include "ShowPasswordDlg.h"
#endif
#include <afxpriv.h>
#include <stdlib.h> // for qsort
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*
* This is the string to be displayed instead of the actual password, unless
* the user chooses to see the password:
*/
const TCHAR *HIDDEN_PASSWORD = _T("**************");
//-----------------------------------------------------------------------------
class DboxAbout
#if defined(POCKET_PC)
: public CPwsPopupDialog
#else
: public CDialog
#endif
{
public:
#if defined(POCKET_PC)
typedef CPwsPopupDialog super;
#else
typedef CDialog super;
#endif
DboxAbout()
: super(DboxAbout::IDD)
{}
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX) // DDX/DDV support
{
super::DoDataExchange(pDX);
}
protected:
DECLARE_MESSAGE_MAP()
};
// I don't think we need this, but...
BEGIN_MESSAGE_MAP(DboxAbout, super)
END_MESSAGE_MAP()
//-----------------------------------------------------------------------------
DboxMain::DboxMain(CWnd* pParent)
: CDialog(DboxMain::IDD, pParent),
m_bSizing( false ), m_needsreading(true), m_windowok(false),
m_toolbarsSetup(FALSE),
m_bShowPasswordInEdit(false), m_bShowPasswordInList(false),
m_bSortAscending(true), m_iSortedColumn(0),
m_lastFindCS(FALSE), m_lastFindStr(_T("")),
m_core(app.m_core), m_IsStartSilent(false),
m_hFontTree(NULL), m_IsReadOnly(false),
m_selectedAtMinimize(NULL), m_bTSUpdated(false),
m_iSessionEndingStatus(IDIGNORE),
m_bFindActive(false)
{
//{{AFX_DATA_INIT(DboxMain)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = app.LoadIcon(IDI_CORNERICON);
m_hIconSm = (HICON) ::LoadImage(app.m_hInstance, MAKEINTRESOURCE(IDI_CORNERICON), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
ClearData();
/*
* current file and current backup file specs are stored in registry
* Note that if m_currfile is non-empty, we will not read the registry value.
* This will happen if a filename was given in the command line.
*/
if (m_core.GetCurFile().IsEmpty()) {
// If there's no stored preference, this is probably a fresh install.
// CheckPassword will catch this and handle it correctly
m_core.SetCurFile(PWSprefs::GetInstance()->
GetPref(PWSprefs::CurrentFile));
}
#if !defined(POCKET_PC)
m_title = _T("");
m_toolbarsSetup = FALSE;
#endif
m_ctlItemTree.SetDboxPointer((void *)this);
}
DboxMain::~DboxMain()
{
::DeleteObject(m_hFontTree);
ReleasePasswordFont();
CFindDlg::EndIt();
}
BEGIN_MESSAGE_MAP(DboxMain, CDialog)
//{{AFX_MSG_MAP(DboxMain)
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_QUERYENDSESSION()
ON_WM_ENDSESSION()
ON_COMMAND(ID_MENUITEM_ABOUT, OnAbout)
ON_COMMAND(ID_PWSAFE_WEBSITE, OnPasswordSafeWebsite)
ON_COMMAND(ID_MENUITEM_COPYUSERNAME, OnCopyUsername)
#if defined(POCKET_PC)
ON_WM_CREATE()
#else
ON_WM_CONTEXTMENU()
#endif
ON_NOTIFY(LVN_KEYDOWN, IDC_ITEMLIST, OnKeydownItemlist)
ON_NOTIFY(NM_DBLCLK, IDC_ITEMLIST, OnItemDoubleClick)
ON_NOTIFY(NM_DBLCLK, IDC_ITEMTREE, OnItemDoubleClick)
ON_COMMAND(ID_MENUITEM_BROWSE, OnBrowse)
ON_COMMAND(ID_MENUITEM_COPYPASSWORD, OnCopyPassword)
ON_COMMAND(ID_MENUITEM_COPYNOTESFLD, OnCopyNotes)
ON_COMMAND(ID_MENUITEM_NEW, OnNew)
ON_COMMAND(ID_MENUITEM_OPEN, OnOpen)
ON_COMMAND(ID_MENUITEM_CLEAR_MRU, OnClearMRU)
ON_COMMAND(ID_MENUITEM_MERGE, OnMerge)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_MERGE, OnUpdateROCommand)
ON_COMMAND(ID_MENUITEM_COMPARE, OnCompare)
ON_COMMAND(ID_MENUITEM_RESTORE, OnRestore)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_RESTORE, OnUpdateROCommand)
ON_COMMAND(ID_MENUTIME_SAVEAS, OnSaveAs)
ON_COMMAND(ID_MENUITEM_BACKUPSAFE, OnBackupSafe)
ON_COMMAND(ID_MENUITEM_CHANGECOMBO, OnPasswordChange)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_CHANGECOMBO, OnUpdateROCommand)
ON_COMMAND(ID_MENUITEM_CLEARCLIPBOARD, OnClearClipboard)
ON_COMMAND(ID_MENUITEM_DELETE, OnDelete)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_DELETE, OnUpdateROCommand)
ON_COMMAND(ID_MENUITEM_EDIT, OnEdit)
ON_COMMAND(ID_MENUITEM_RENAME, OnRename)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_RENAME, OnUpdateROCommand)
ON_COMMAND(ID_MENUITEM_FIND, OnFind)
ON_COMMAND(ID_MENUITEM_DUPLICATEENTRY, OnDuplicateEntry)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_DUPLICATEENTRY, OnUpdateROCommand)
ON_COMMAND(ID_MENUITEM_OPTIONS, OnOptions)
ON_COMMAND(ID_MENUITEM_SAVE, OnSave)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_SAVE, OnUpdateROCommand)
ON_COMMAND(ID_MENUITEM_LIST_VIEW, OnListView)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_LIST_VIEW, OnUpdateViewCommand)
ON_COMMAND(ID_MENUITEM_TREE_VIEW, OnTreeView)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_TREE_VIEW, OnUpdateViewCommand)
ON_COMMAND(ID_MENUITEM_OLD_TOOLBAR, OnOldToolbar)
ON_COMMAND(ID_MENUITEM_NEW_TOOLBAR, OnNewToolbar)
ON_COMMAND(ID_MENUITEM_EXPANDALL, OnExpandAll)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_EXPANDALL, OnUpdateTVCommand)
ON_COMMAND(ID_MENUITEM_COLLAPSEALL, OnCollapseAll)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_COLLAPSEALL, OnUpdateTVCommand)
ON_COMMAND(ID_MENUITEM_CHANGEFONT, OnChangeFont)
ON_COMMAND_RANGE(ID_FILE_EXPORTTO_OLD1XFORMAT, ID_FILE_EXPORTTO_V2FORMAT, OnExportVx)
ON_COMMAND(ID_FILE_EXPORTTO_PLAINTEXT, OnExportText)
ON_COMMAND(ID_FILE_EXPORTTO_XML, OnExportXML)
ON_COMMAND(ID_FILE_IMPORT_PLAINTEXT, OnImportText)
ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_PLAINTEXT, OnUpdateROCommand)
ON_COMMAND(ID_FILE_IMPORT_KEEPASS, OnImportKeePass)
ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_KEEPASS, OnUpdateROCommand)
ON_COMMAND(ID_FILE_IMPORT_XML, OnImportXML)
ON_UPDATE_COMMAND_UI(ID_FILE_IMPORT_XML, OnUpdateROCommand)
ON_COMMAND(ID_MENUITEM_ADD, OnAdd)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_ADD, OnUpdateROCommand)
ON_COMMAND(ID_MENUITEM_ADDGROUP, OnAddGroup)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_ADDGROUP, OnUpdateROCommand)
ON_WM_TIMER()
ON_COMMAND(ID_MENUITEM_AUTOTYPE, OnAutoType)
#if defined(POCKET_PC)
ON_COMMAND(ID_MENUITEM_SHOWPASSWORD, OnShowPassword)
#else
ON_NOTIFY(NM_SETFOCUS, IDC_ITEMLIST, OnSetfocusItemlist)
ON_NOTIFY(NM_KILLFOCUS, IDC_ITEMLIST, OnKillfocusItemlist)
ON_NOTIFY(NM_SETFOCUS, IDC_ITEMTREE, OnSetfocusItemlist)
ON_NOTIFY(NM_KILLFOCUS, IDC_ITEMTREE, OnKillfocusItemlist)
ON_WM_DROPFILES()
#endif
ON_NOTIFY(LVN_COLUMNCLICK, IDC_ITEMLIST, OnColumnClick)
ON_UPDATE_COMMAND_UI(ID_FILE_MRU_ENTRY1, OnUpdateMRU)
ON_WM_INITMENUPOPUP()
ON_COMMAND(ID_MENUITEM_EXIT, OnOK)
ON_COMMAND(ID_MENUITEM_MINIMIZE, OnMinimize)
ON_COMMAND(ID_MENUITEM_UNMINIMIZE, OnUnMinimize)
#ifndef POCKET_PC
ON_COMMAND(ID_MENUITEM_TRAYLOCKUNLOCK, OnTrayLockUnLock)
ON_UPDATE_COMMAND_UI(ID_MENUITEM_TRAYLOCKUNLOCK, OnUpdateTrayLockUnLockCommand)
ON_COMMAND(ID_TRAYRECENT_ENTRY_CLEAR, OnTrayClearRecentEntries)
ON_UPDATE_COMMAND_UI(ID_TRAYRECENT_ENTRY_CLEAR, OnUpdateTrayClearRecentEntries)
ON_WM_INITMENU()
ON_COMMAND(ID_TOOLBUTTON_NEW, OnNew)
ON_COMMAND(ID_TOOLBUTTON_OPEN, OnOpen)
ON_COMMAND(ID_TOOLBUTTON_SAVE, OnSave)
ON_COMMAND(ID_TOOLBUTTON_COPYPASSWORD, OnCopyPassword)
ON_COMMAND(ID_TOOLBUTTON_COPYUSERNAME, OnCopyUsername)
ON_COMMAND(ID_TOOLBUTTON_COPYNOTESFLD, OnCopyNotes)
ON_COMMAND(ID_TOOLBUTTON_CLEARCLIPBOARD, OnClearClipboard)
ON_COMMAND(ID_TOOLBUTTON_AUTOTYPE, OnAutoType)
ON_COMMAND(ID_TOOLBUTTON_BROWSEURL, OnBrowse)
ON_COMMAND(ID_TOOLBUTTON_ADD, OnAdd)
ON_COMMAND(ID_TOOLBUTTON_EDIT, OnEdit)
ON_COMMAND(ID_TOOLBUTTON_DELETE, OnDelete)
#endif
ON_WM_SYSCOMMAND()
#if !defined(POCKET_PC)
ON_BN_CLICKED(IDOK, OnEdit)
ON_WM_SIZING()
#endif
ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification)
//}}AFX_MSG_MAP
ON_COMMAND_EX_RANGE(ID_FILE_MRU_ENTRY1, ID_FILE_MRU_ENTRYMAX, OnOpenMRU)
#ifndef POCKET_PC
ON_COMMAND_RANGE(ID_MENUITEM_TRAYCOPYUSERNAME1, ID_MENUITEM_TRAYCOPYUSERNAMEMAX, OnTrayCopyUsername)
ON_UPDATE_COMMAND_UI_RANGE(ID_MENUITEM_TRAYCOPYUSERNAME1, ID_MENUITEM_TRAYCOPYUSERNAMEMAX, OnUpdateTrayCopyUsername)
ON_COMMAND_RANGE(ID_MENUITEM_TRAYCOPYPASSWORD1, ID_MENUITEM_TRAYCOPYPASSWORDMAX, OnTrayCopyPassword)
ON_UPDATE_COMMAND_UI_RANGE(ID_MENUITEM_TRAYCOPYPASSWORD1, ID_MENUITEM_TRAYCOPYPASSWORDMAX, OnUpdateTrayCopyPassword)
ON_COMMAND_RANGE(ID_MENUITEM_TRAYCOPYNOTESFLD1, ID_MENUITEM_TRAYCOPYNOTESFLDMAX, OnTrayCopyNotes)
ON_UPDATE_COMMAND_UI_RANGE(ID_MENUITEM_TRAYCOPYNOTESFLD1, ID_MENUITEM_TRAYCOPYNOTESFLDMAX, OnUpdateTrayCopyNotes)
ON_COMMAND_RANGE(ID_MENUITEM_TRAYBROWSE1, ID_MENUITEM_TRAYBROWSEMAX, OnTrayBrowse)
ON_UPDATE_COMMAND_UI_RANGE(ID_MENUITEM_TRAYBROWSE1, ID_MENUITEM_TRAYBROWSEMAX, OnUpdateTrayBrowse)
ON_COMMAND_RANGE(ID_MENUITEM_TRAYDELETE1, ID_MENUITEM_TRAYDELETEMAX, OnTrayDeleteEntry)
ON_UPDATE_COMMAND_UI_RANGE(ID_MENUITEM_TRAYDELETE1, ID_MENUITEM_TRAYDELETEMAX, OnUpdateTrayDeleteEntry)
ON_COMMAND_RANGE(ID_MENUITEM_TRAYAUTOTYPE1, ID_MENUITEM_TRAYAUTOTYPEMAX, OnTrayAutoType)
ON_UPDATE_COMMAND_UI_RANGE(ID_MENUITEM_TRAYAUTOTYPE1, ID_MENUITEM_TRAYAUTOTYPEMAX, OnUpdateTrayAutoType)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
#endif
END_MESSAGE_MAP()
void
DboxMain::InitPasswordSafe()
{
// Real initialization done here
// Requires OnInitDialog to have passed OK
// AlwaysOnTop preference read from database, if possible, hence set after OpenOnInit
m_bAlwaysOnTop = PWSprefs::GetInstance()->GetPref(PWSprefs::AlwaysOnTop);
UpdateAlwaysOnTop();
// ... same for UseSystemTray
// StartSilent trumps preference
if (!m_IsStartSilent && !PWSprefs::GetInstance()->
GetPref(PWSprefs::UseSystemTray))
app.HideIcon();
m_RUEList.SetMax(PWSprefs::GetInstance()->GetPref(PWSprefs::MaxREItems));
// Set timer for user-defined lockout, if selected
if (PWSprefs::GetInstance()->
GetPref(PWSprefs::LockOnIdleTimeout)) {
const UINT MINUTE = 60*1000;
TRACE(_T("Starting Idle time lock timer"));
SetTimer(TIMER_USERLOCK, MINUTE, NULL);
ResetIdleLockCounter();
}
// JHF : no hotkeys on WinCE
#if !defined(POCKET_PC)
// Set Hotkey, if active
if (PWSprefs::GetInstance()->
GetPref(PWSprefs::HotKeyEnabled)) {
const DWORD value = DWORD(PWSprefs::GetInstance()->
GetPref(PWSprefs::HotKey));
// Following contortions needed 'cause MS couldn't get their
// act together and keep a consistent interface. Argh.
WORD v = WORD((value & 0xff) | ((value & 0xff0000) >> 8));
SendMessage(WM_SETHOTKEY, v);
}
#endif
m_windowok = true;
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIconSm, FALSE); // Set small icon
// Init stuff for tree view
CImageList *pImageList = new CImageList();
BOOL status = pImageList->Create(9, 9, ILC_COLOR, 2, 0);
ASSERT(status != 0);
CBitmap bitmap;
// Order of LoadBitmap() calls matches CMyTreeCtrl public enum
bitmap.LoadBitmap(IDB_NODE);
pImageList->Add(&bitmap, (COLORREF)0x0);
bitmap.DeleteObject();
bitmap.LoadBitmap(IDB_LEAF);
pImageList->Add(&bitmap, (COLORREF)0x0);
bitmap.DeleteObject();
bitmap.LoadBitmap(IDB_LEAF_EXPIRED);
pImageList->Add(&bitmap, (COLORREF)0x0);
bitmap.DeleteObject();
m_ctlItemTree.SetImageList(pImageList, TVSIL_NORMAL);
// Init stuff for list view
m_ctlItemList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
m_nColumns = 8;
m_ctlItemList.InsertColumn(0, _T("Title"));
m_ctlItemList.InsertColumn(1, _T("User Name"));
m_ctlItemList.InsertColumn(2, _T("Notes"));
m_ctlItemList.InsertColumn(3, _T("Created"));
m_ctlItemList.InsertColumn(4, _T("Password Modified"));
m_ctlItemList.InsertColumn(5, _T("Last Accessed"));
m_ctlItemList.InsertColumn(6, _T("Password Expiry Date"));
m_ctlItemList.InsertColumn(7, _T("Last Modified"));
m_bShowPasswordInEdit = PWSprefs::GetInstance()->
GetPref(PWSprefs::ShowPWDefault);
m_bShowPasswordInList = PWSprefs::GetInstance()->
GetPref(PWSprefs::ShowPWInList);
const CString lastView = PWSprefs::GetInstance()->
GetPref(PWSprefs::LastView);
m_IsListView = true;
if (lastView != _T("list")) {
// not list mode, so start in tree view.
m_ctlItemList.ShowWindow(SW_HIDE);
m_ctlItemTree.ShowWindow(SW_SHOW);
m_IsListView = false;
}
CRect rect;
m_ctlItemList.GetClientRect(&rect);
int i1stWidth = PWSprefs::GetInstance()->GetPref(PWSprefs::Column1Width,
(rect.Width() / 3 +
rect.Width() % 3));
int i2ndWidth = PWSprefs::GetInstance()->GetPref(PWSprefs::Column2Width,
rect.Width() / 3);
int i3rdWidth = PWSprefs::GetInstance()->GetPref(PWSprefs::Column3Width,
rect.Width() / 3);
m_ctlItemList.SetColumnWidth(0, i1stWidth);
m_ctlItemList.SetColumnWidth(1, i2ndWidth);
m_ctlItemList.SetColumnWidth(2, i3rdWidth);
m_ctlItemList.SetRedraw(FALSE);
const CString ascdatetime = "XXX Xxx DD HH:MM:SS YYYY";
int nWidth = m_ctlItemList.GetStringWidth(ascdatetime);
for (int i = 3; i < 8; i++) {
m_ctlItemList.SetColumnWidth(i, LVSCW_AUTOSIZE);
m_ctlItemList.SetColumnWidth(i, LVSCW_AUTOSIZE_USEHEADER);
m_ctlItemList.SetColumnWidth(i, nWidth);
}
m_ctlItemList.SetRedraw(TRUE);
m_iSortedColumn = PWSprefs::GetInstance()->GetPref(PWSprefs::SortedColumn);
m_bSortAscending = PWSprefs::GetInstance()->
GetPref(PWSprefs::SortAscending);
// refresh list will add and size password column if necessary...
RefreshList();
switch (PWSprefs::GetInstance()->GetPref(PWSprefs::TreeDisplayStatusAtOpen)) {
case PWSprefs::AllCollapsed:
m_ctlItemTree.OnCollapseAll();
break;
case PWSprefs::AllExpanded:
m_ctlItemTree.OnExpandAll();
break;
case PWSprefs::AsPerLastSave:
RestoreDisplayStatus();
break;
default:
ASSERT(0);
}
ChangeOkUpdate();
setupBars(); // Just to keep things a little bit cleaner
#if !defined(POCKET_PC)
// {kjp} Can't drag and drop files onto an application in PocketPC
DragAcceptFiles(TRUE);
// {kjp} meaningless when target is a PocketPC device.
PWSprefs::GetInstance()->GetPrefRect(rect.top, rect.bottom, rect.left, rect.right);
if (rect.top == -1 || rect.bottom == -1 || rect.left == -1 || rect.right == -1) {
GetWindowRect(&rect);
SendMessage(WM_SIZE, SIZE_RESTORED, MAKEWPARAM(rect.Width(), rect.Height()));
} else {
// Sanity checks on stored rect - displays change...
const int screenWidth = GetSystemMetrics(SM_CXSCREEN);
const int screenHeight = GetSystemMetrics(SM_CYSCREEN);
if (rect.right > screenWidth || rect.bottom > screenHeight ||
rect.left > screenWidth || rect.top > screenHeight) {
// if any corner is out of screen, fallback to sane values
rect.top = rect.left = 10;
rect.bottom = 320; rect.right = 230;
}
MoveWindow(&rect, TRUE);
}
#endif
m_core.SetUseDefUser(PWSprefs::GetInstance()->
GetPref(PWSprefs::UseDefUser));
m_core.SetDefUsername(PWSprefs::GetInstance()->
GetPref(PWSprefs::DefUserName));
CString szTreeFont = PWSprefs::GetInstance()->
GetPref(PWSprefs::TreeFont);
if (szTreeFont != _T("")) {
ExtractFont(szTreeFont);
m_hFontTree = ::CreateFontIndirect(&m_treefont);
// transfer the fonts to the tree windows
m_ctlItemTree.SendMessage(WM_SETFONT, (WPARAM) m_hFontTree, true);
m_ctlItemList.SendMessage(WM_SETFONT, (WPARAM) m_hFontTree, true);
}
SetMenu(app.m_mainmenu); // Now show menu...
}
BOOL
DboxMain::OnInitDialog()
{
ConfigureSystemMenu();
CDialog::OnInitDialog();
if (!m_IsStartSilent && (OpenOnInit() == FALSE))
return TRUE;
InitPasswordSafe();
return TRUE; // return TRUE unless you set the focus to a control
}
void
DboxMain::OnDestroy()
{
const CMyString filename(m_core.GetCurFile());
// The only way we're the locker is if it's locked & we're !readonly
if (!filename.IsEmpty() && !m_IsReadOnly && m_core.IsLockedFile(filename))
m_core.UnlockFile(filename);
CDialog::OnDestroy();
}
void DboxMain::FixListIndexes()
{
int N = m_ctlItemList.GetItemCount();
for (int i = 0; i < N; i++) {
CItemData *ci = (CItemData *)m_ctlItemList.GetItemData(i);
ASSERT(ci != NULL);
DisplayInfo *di = (DisplayInfo *)ci->GetDisplayInfo();
ASSERT(di != NULL);
if (di->list_index != i)
di->list_index = i;
}
}
void
DboxMain::OnItemDoubleClick( NMHDR *, LRESULT *)
{
// TreeView only - use DoubleClick to Expand/Collapse group
if (m_ctlItemTree.IsWindowVisible()) {
HTREEITEM hItem = m_ctlItemTree.GetSelectedItem();
// Only if a group is selected
if ((hItem != NULL && !m_ctlItemTree.IsLeafNode(hItem))) {
// Do standard double-click processing - i.e. toggle expand/collapse!
return;
}
}
// Continue if in ListView or Leaf in TreeView
#if defined(POCKET_PC)
if ( app.GetProfileInt(_T(PWS_REG_OPTIONS), _T("dcshowspassword"), FALSE) == FALSE ) {
OnCopyPassword();
} else {
OnShowPassword();
}
#else
switch (PWSprefs::GetInstance()->
GetPref(PWSprefs::DoubleClickAction)) {
case PWSprefs::DoubleClickCopy:
OnCopyPassword();
break;
case PWSprefs::DoubleClickEdit:
PostMessage(WM_COMMAND, ID_MENUITEM_EDIT);
break;
case PWSprefs::DoubleClickAutoType:
PostMessage(WM_COMMAND, ID_MENUITEM_AUTOTYPE);
break;
case PWSprefs::DoubleClickBrowse:
PostMessage(WM_COMMAND, ID_MENUITEM_BROWSE);
break;
default:
ASSERT(0);
}
#endif
}
// Called to open a web browser to the URL associated with an entry.
void DboxMain::OnBrowse()
{
CItemData *ci = getSelectedItem();
if(ci != NULL) {
if (!ci->GetURL().IsEmpty()) {
LaunchBrowser(ci->GetURL());
UpdateAccessTime(ci);
uuid_array_t RUEuuid;
ci->GetUUID(RUEuuid);
m_RUEList.AddRUEntry(RUEuuid);
}
}
}
void
DboxMain::ToClipboard(const CMyString &data)
{
app.SetClipboardData(data);
}
// this tells OnSize that the user is currently
// changing the size of the dialog, and not restoring it
void DboxMain::OnSizing(UINT fwSide, LPRECT pRect)
{
#if !defined(POCKET_PC)
CDialog::OnSizing(fwSide, pRect);
m_bSizing = true;
#endif
}
void
DboxMain::OnUpdateROCommand(CCmdUI *pCmdUI)
{
// Use this callback for commands that need to
// be disabled in read-only mode
pCmdUI->Enable(m_IsReadOnly ? FALSE : TRUE);
}
void
DboxMain::OnUpdateViewCommand(CCmdUI *pCmdUI)
{
// Use this callback to disable swap between Tree and List modes
// during a Find operation
pCmdUI->Enable(m_bFindActive ? FALSE : TRUE);
}
void
DboxMain::OnUpdateNSCommand(CCmdUI *pCmdUI)
{
// Use this callback for commands that need to
// be disabled if not supported (yet)
pCmdUI->Enable(FALSE);
}
void
DboxMain::OnUpdateTVCommand(CCmdUI *pCmdUI)
{
// Use this callback for commands that need to
// be disabled in ListView mode
if (m_IsListView) {
pCmdUI->Enable(FALSE);
} else {
// Should be TRUE in TreeView but only if there are entries
pCmdUI->Enable(m_ctlItemTree.GetCount() > 0 ? TRUE : FALSE);
}
}
void
DboxMain::SetChanged(ChangeType changed)
{
switch (changed) {
case Data:
if (PWSprefs::GetInstance()->GetPref(PWSprefs::SaveImmediately)) {
Save();
} else {
m_core.SetChanged(true);
}
break;
case Clear:
m_core.SetChanged(false);
m_bTSUpdated = false;
break;
case TimeStamp:
if (PWSprefs::GetInstance()->GetPref(PWSprefs::MaintainDateTimeStamps))
m_bTSUpdated = true;
break;
default:
ASSERT(0);
}
}
void
DboxMain::ChangeOkUpdate()
{
if (! m_windowok)
return;
#if defined(POCKET_PC)
CMenu *menu = m_wndMenu;
#else
CMenu *menu = GetMenu();
#endif
menu->EnableMenuItem(ID_MENUITEM_SAVE,
m_core.IsChanged() ? MF_ENABLED : MF_GRAYED);
/*
This doesn't exactly belong here, but it makes sure that the
title is fresh...
*/
#if !defined(POCKET_PC)
SetWindowText(LPCTSTR(m_title));
#endif
UpdateStatusBar();
}
void
DboxMain::OnAbout()
{
DboxAbout dbox;
dbox.DoModal();
}
void
DboxMain::OnPasswordSafeWebsite()
{
HINSTANCE stat = ::ShellExecute(NULL, NULL, "http://passwordsafe.sourceforge.net/",
NULL, _T("."), SW_SHOWNORMAL);
if (int(stat) < 32) {
#ifdef _DEBUG
AfxMessageBox("oops");
#endif
}
}
int
DboxMain::GetAndCheckPassword(const CMyString &filename,
CMyString& passkey,
int index ,bool bForceReadOnly)
{
// index:
// GCP_FIRST (0) first
// GCP_NORMAL (1) OK, CANCEL & HELP buttons
// GCP_UNMINIMIZE (2) OK, CANCEL & HELP buttons
// GCP_WITHEXIT (3} OK, CANCEL, EXIT & HELP buttons
// Called for an existing database. Prompt user
// for password, verify against file. Lock file to
// prevent multiple r/w access.
int retval;
bool bFileIsReadOnly = false;
if (!filename.IsEmpty())
{
bool exists = m_core.FileExists(filename, bFileIsReadOnly);
if (!exists) {
// Used to display an error message, but this is really the caller's business
return PWScore::CANT_OPEN_FILE;
} // !exists
} // !filename.IsEmpty()
/*
* with my unsightly hacks of PasskeyEntry, it should now accept
* a blank filename, which will disable passkey entry and the OK button
*/
if (bFileIsReadOnly || bForceReadOnly) {
// As file is read-only, we must honour it and not permit user to change it
m_IsReadOnly = true;
bFileIsReadOnly = true;
}
CPasskeyEntry dbox_pkentry(this, filename, index, m_IsReadOnly, bFileIsReadOnly);
app.DisableAccelerator();
int rc = dbox_pkentry.DoModal();
app.EnableAccelerator();
if (rc == IDOK) {
DBGMSG("PasskeyEntry returns IDOK\n");
CMyString locker(_T("")); // null init is important here
passkey = dbox_pkentry.GetPasskey();
// This dialog's setting of read-only overrides file dialog
m_IsReadOnly = dbox_pkentry.IsReadOnly();
SetReadOnly(m_IsReadOnly);
// Set read-only mode if user explicitly requested it OR
// we could not create a lock file.
// Note that we depend on lazy evaluation: if the 1st is true,
// the 2nd won't be called!
if (index == GCP_FIRST) // if first, then m_IsReadOnly is set in Open
SetReadOnly(m_IsReadOnly || !m_core.LockFile(filename, locker));
else if (!m_IsReadOnly) // !first, lock if !m_IsReadOnly
SetReadOnly(!m_core.LockFile(filename, locker));
// locker won't be null IFF tried to lock and failed, in which case
// it shows the current file locker
if (!locker.IsEmpty()) {
CString str = _T("The database ");
str += CString(filename);
str += _T(" is apparently being used by ");
str += CString(locker);
str += _T(".\r\nOpen the database for read-only (Yes), ");
str += _T("read-write (No), or exit (Cancel)?");
str += _T("\r\n\r\nNote: Choose \"No\" only if you are certain ");
str += _T("that the file is in fact not being used by anyone else.");
switch( MessageBox(str, _T("File In Use"),
MB_YESNOCANCEL|MB_ICONQUESTION)) {
case IDYES:
SetReadOnly(true);
retval = PWScore::SUCCESS;
break;
case IDNO:
SetReadOnly(false); // Caveat Emptor!
retval = PWScore::SUCCESS;
break;
case IDCANCEL:
retval = PWScore::USER_CANCEL;
break;
default:
ASSERT(false);
retval = PWScore::USER_CANCEL;
}
} else // locker.IsEmpty() means no lock needed or lock was successful
retval = PWScore::SUCCESS;
} else {/*if (rc==IDCANCEL) */ //Determine reason for cancel
int cancelreturn = dbox_pkentry.GetStatus();
switch (cancelreturn)
{
case TAR_OPEN:
case TAR_NEW:
DBGMSG("PasskeyEntry TAR_OPEN or TAR_NEW\n");
retval = cancelreturn; //Return either open or new flag...
break;
case TAR_CANCEL:
retval = PWScore::USER_CANCEL;
break;
case TAR_EXIT:
retval = PWScore::USER_EXIT;
break;
default:
DBGMSG("Default to WRONG_PASSWORD\n");
retval = PWScore::WRONG_PASSWORD; //Just a normal cancel
break;
}
}
return retval;
}
BOOL
DboxMain::OnToolTipText(UINT,
NMHDR* pNMHDR,
LRESULT* pResult)
// This code is copied from the DLGCBR32 example that comes with MFC
{
#if !defined(POCKET_PC)
ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);
// allow top level routing frame to handle the message
if (GetRoutingFrame() != NULL)
return FALSE;
// need to handle both ANSI and UNICODE versions of the message
TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
TCHAR szFullText[256];
CString strTipText;
UINT nID = pNMHDR->idFrom;
if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND)) {
// idFrom is actually the HWND of the tool
nID = ((UINT)(WORD)::GetDlgCtrlID((HWND)nID));
}
if (nID != 0) // will be zero on a separator
{
AfxLoadString(nID, szFullText);
// this is the command id, not the button index
AfxExtractSubString(strTipText, szFullText, 1, '\n');
}
#ifndef _UNICODE
if (pNMHDR->code == TTN_NEEDTEXTA)
lstrcpyn(pTTTA->szText, strTipText,
(sizeof(pTTTA->szText)/sizeof(pTTTA->szText[0])));
#if 0 // build problem with new cl? - jpr
else
_mbstowcsz(pTTTW->szText, strTipText,
(sizeof(pTTTW->szText)/sizeof(pTTTW->szText[0])));
#endif // 0
#else
if (pNMHDR->code == TTN_NEEDTEXTA)
_wcstombsz(pTTTA->szText, strTipText,
(sizeof(pTTTA->szText)/sizeof(pTTTA->szText[0])));
else
lstrcpyn(pTTTW->szText, strTipText,
(sizeof(pTTTW->szText)/sizeof(pTTTW->szText[0])));
#endif
*pResult = 0;
// bring the tooltip window above other popup windows
::SetWindowPos(pNMHDR->hwndFrom, HWND_TOP, 0, 0, 0, 0,
SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE);
#endif
return TRUE; // message was handled
}
#if !defined(POCKET_PC)
void
DboxMain::OnDropFiles(HDROP hDrop)
{
//SetActiveWindow();
SetForegroundWindow();
#if 0
// here's what we really want - sorta
HDROP m_hDropInfo = hDropInfo;
CString Filename;
if (m_hDropInfo) {
int iFiles = DragQueryFile(m_hDropInfo, (UINT)-1, NULL, 0);
for (int i=0; i<ifiles; i++) {
char* pFilename = Filename.GetBuffer(_MAX_PATH);
// do whatever...
} // for each files...
} // if DropInfo
DragFinish(m_hDropInfo);
m_hDropInfo = 0;
#endif
DragFinish(hDrop);
}
#endif
void
DboxMain::UpdateAlwaysOnTop()
{
#if !defined(POCKET_PC)
CMenu* sysMenu = GetSystemMenu( FALSE );
if ( m_bAlwaysOnTop ) {
SetWindowPos( &wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
sysMenu->CheckMenuItem( ID_SYSMENU_ALWAYSONTOP, MF_BYCOMMAND | MF_CHECKED );
} else {
SetWindowPos( &wndNoTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
sysMenu->CheckMenuItem( ID_SYSMENU_ALWAYSONTOP, MF_BYCOMMAND | MF_UNCHECKED );
}
#endif
}
void
DboxMain::OnSysCommand( UINT nID, LPARAM lParam )
{
#if !defined(POCKET_PC)
if ( ID_SYSMENU_ALWAYSONTOP == nID ) {
m_bAlwaysOnTop = !m_bAlwaysOnTop;
PWSprefs::GetInstance()->SetPref(PWSprefs::AlwaysOnTop,
m_bAlwaysOnTop);
UpdateAlwaysOnTop();
return;
}
if ((nID & 0xFFF0) == SC_RESTORE) {
UnMinimize(true);
if (!m_passphraseOK) // password bad or cancel pressed
return;
}
CDialog::OnSysCommand( nID, lParam );
#endif
}
void
DboxMain::ConfigureSystemMenu()
{
#if defined(POCKET_PC)
m_wndCommandBar = (CCeCommandBar*) m_pWndEmptyCB;
m_wndMenu = m_wndCommandBar->InsertMenuBar( IDR_MAINMENU );
ASSERT( m_wndMenu != NULL );
#else
CMenu* sysMenu = GetSystemMenu( FALSE );
CString str;
str.LoadString( IDS_ALWAYSONTOP );
sysMenu->InsertMenu( 5, MF_BYPOSITION | MF_STRING, ID_SYSMENU_ALWAYSONTOP, (LPCTSTR)str );
#endif
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void
DboxMain::OnUpdateMRU(CCmdUI* pCmdUI)
{
if (app.GetMRU() == NULL)
return;
if (!app.m_mruonfilemenu) {
if (pCmdUI->m_nIndex == 0) { // Add to popup menu
app.GetMRU()->UpdateMenu( pCmdUI );
} else {
return;
}
} else {
app.GetMRU()->UpdateMenu( pCmdUI );
}
}
#if _MFC_VER > 1200
BOOL
#else
void
#endif
DboxMain::OnOpenMRU(UINT nID)
{
UINT uMRUItem = nID - ID_FILE_MRU_ENTRY1;
CString mruItem = (*app.GetMRU())[uMRUItem];
Open( mruItem );
#if _MFC_VER > 1200
return TRUE;
#endif
}
// Called just before any pulldown or popup menu is displayed, so that menu items
// can be enabled/disabled or checked/unchecked dynamically.
void
DboxMain::OnInitMenu(CMenu* pMenu)
{
const BOOL bTreeView = m_ctlItemTree.IsWindowVisible();
const BOOL bItemSelected = SelItemOk();
UINT uiItemCmdFlags = MF_BYCOMMAND | (bItemSelected ? MF_ENABLED : MF_GRAYED);
pMenu->EnableMenuItem(ID_MENUITEM_COPYPASSWORD, uiItemCmdFlags);
pMenu->EnableMenuItem(ID_MENUITEM_COPYUSERNAME, uiItemCmdFlags);
pMenu->EnableMenuItem(ID_MENUITEM_EDIT, uiItemCmdFlags);
#if defined(POCKET_PC)
pMenu->EnableMenuItem(ID_MENUITEM_SHOWPASSWORD, uiItemCmdFlags);
#endif
pMenu->EnableMenuItem(ID_MENUITEM_AUTOTYPE, uiItemCmdFlags);
pMenu->EnableMenuItem(ID_MENUITEM_DUPLICATEENTRY, uiItemCmdFlags);
bool bGroupSelected = false;
bool bEmptyGroupSelected = false;
if (bTreeView) {
HTREEITEM hi = m_ctlItemTree.GetSelectedItem();
bGroupSelected = (hi != NULL && !m_ctlItemTree.IsLeafNode(hi));
bEmptyGroupSelected = (bGroupSelected && !m_ctlItemTree.ItemHasChildren(hi));
}
pMenu->EnableMenuItem(ID_MENUITEM_DELETE, ((bItemSelected || bEmptyGroupSelected) ? MF_ENABLED : MF_GRAYED));
pMenu->EnableMenuItem(ID_MENUITEM_RENAME, ((bTreeView && (bItemSelected || bGroupSelected)) ? MF_ENABLED : MF_GRAYED));
pMenu->EnableMenuItem(ID_MENUITEM_ADDGROUP, (bTreeView ? MF_ENABLED : MF_GRAYED));
pMenu->CheckMenuRadioItem(ID_MENUITEM_LIST_VIEW, ID_MENUITEM_TREE_VIEW,
(bTreeView ? ID_MENUITEM_TREE_VIEW : ID_MENUITEM_LIST_VIEW), MF_BYCOMMAND);
CDC* pDC = this->GetDC();
int NumBits = ( pDC ? pDC->GetDeviceCaps(12 /*BITSPIXEL*/) : 32 );
// JHF m_toolbarMode is not for WinCE (as in .h)
#if !defined(POCKET_PC)
if (NumBits < 16 && m_toolbarMode == ID_MENUITEM_OLD_TOOLBAR) {
// Less that 16 color bits available, no choice, disable menu items
pMenu->EnableMenuItem(ID_MENUITEM_NEW_TOOLBAR, MF_GRAYED | MF_BYCOMMAND);
pMenu->EnableMenuItem(ID_MENUITEM_OLD_TOOLBAR, MF_GRAYED | MF_BYCOMMAND);
} else {
// High-color screen mode so all choices available.
// (or a low-color screen, but leave choices enabled so that the user still can switch.)
pMenu->EnableMenuItem(ID_MENUITEM_NEW_TOOLBAR, MF_ENABLED | MF_BYCOMMAND);
pMenu->EnableMenuItem(ID_MENUITEM_OLD_TOOLBAR, MF_ENABLED | MF_BYCOMMAND);
}
pMenu->CheckMenuRadioItem(ID_MENUITEM_NEW_TOOLBAR, ID_MENUITEM_OLD_TOOLBAR,
m_toolbarMode, MF_BYCOMMAND);
#endif
pMenu->EnableMenuItem(ID_MENUITEM_SAVE,
m_core.IsChanged() ? MF_ENABLED : MF_GRAYED);
// enable/disable w.r.t read-only mode
// is handled via ON_UPDATE_COMMAND_UI/OnUpdateROCommand
}
// helps with MRU by allowing ON_UPDATE_COMMAND_UI
void
DboxMain::OnInitMenuPopup(CMenu* pPopupMenu, UINT, BOOL)
{
// http://www4.ncsu.edu:8030/~jgbishop/codetips/dialog/updatecommandui_menu.html
// This code comes from the MFC Documentation, and is adapted from CFrameWnd::OnInitMenuPopup() in WinFrm.cpp.
ASSERT(pPopupMenu != NULL);
CCmdUI state; // Check the enabled state of various menu items
state.m_pMenu = pPopupMenu;
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pParentMenu == NULL);
// Is the menu in question a popup in the top-level menu? If so, set m_pOther
// to this menu. Note that m_pParentMenu == NULL indicates that the menu is a
// secondary popup.
CMenu *hParentMenu;
if(AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu) {
state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
} else if((hParentMenu = this->GetMenu()) != NULL) {
CWnd* pParent = this;
// Child windows don't have menus--need to go to the top!
if(pParent != NULL && (hParentMenu = pParent->GetMenu()) != NULL) {
int nIndexMax = hParentMenu->GetMenuItemCount();
for (int nIndex = 0; nIndex < nIndexMax; nIndex++) {
CMenu *submenu = hParentMenu->GetSubMenu(nIndex);
if(submenu != NULL && submenu->m_hMenu == pPopupMenu->m_hMenu) {
// When popup is found, m_pParentMenu is containing menu.
state.m_pParentMenu = CMenu::FromHandle(hParentMenu->GetSafeHmenu());
break;
}
}
}
}
state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
for(state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax; state.m_nIndex++) {
state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
if(state.m_nID == 0)
continue; // Menu separator or invalid cmd - ignore it.
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pMenu != NULL);
if(state.m_nID == (UINT)-1) {
// Possibly a popup menu, route to first item of that popup.
state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
if(state.m_pSubMenu == NULL ||
(state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
state.m_nID == (UINT)-1) {
continue; // First item of popup can't be routed to.
}
state.DoUpdate(this, TRUE); // Popups are never auto disabled.
} else {
// Normal menu item.
// Auto enable/disable if frame window has m_bAutoMenuEnable
// set and command is _not_ a system command.
state.m_pSubMenu = NULL;
state.DoUpdate(this, FALSE);
}
// Adjust for menu deletions and additions.
UINT nCount = pPopupMenu->GetMenuItemCount();
if(nCount < state.m_nIndexMax) {
state.m_nIndex -= (state.m_nIndexMax - nCount);
while(state.m_nIndex < nCount &&
pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID) {
state.m_nIndex++;
}
}
state.m_nIndexMax = nCount;
}
}
#if defined(POCKET_PC)
void
DboxMain::OnShowPassword()
{
if (SelItemOk() == TRUE) {
CItemData item;
CMyString password;
CMyString name;
CMyString title;
CMyString username;
CShowPasswordDlg pwDlg( this );
item = m_pwlist.GetAt( Find(getSelectedItem()) );
item.GetPassword(password);
item.GetName( name );
SplitName( name, title, username );
pwDlg.SetTitle( title );
pwDlg.SetPassword( password );
pwDlg.DoModal();
}
}
#endif
LRESULT DboxMain::OnTrayNotification(WPARAM , LPARAM )
{
#if 0
return m_TrayIcon.OnTrayNotification(wParam, lParam);
#else
return 0L;
#endif
}
void
DboxMain::OnMinimize()
{
ShowWindow(SW_MINIMIZE);
}
void
DboxMain::OnUnMinimize()
{
UnMinimize(true);
}
void
DboxMain::UnMinimize(bool update_windows)
{
m_passphraseOK = false;
// Case 1 - data available but is currently locked
if (!m_needsreading
&& (app.GetSystemTrayState() == ThisMfcApp::LOCKED)
&& (PWSprefs::GetInstance()->GetPref(PWSprefs::UseSystemTray))) {
CMyString passkey;
int rc;
rc = GetAndCheckPassword(m_core.GetCurFile(), passkey, GCP_UNMINIMIZE); // OK, CANCEL, HELP
if (rc != PWScore::SUCCESS)
return; // don't even think of restoring window!
app.SetSystemTrayState(ThisMfcApp::UNLOCKED);
m_passphraseOK = true;
if (update_windows)
ShowWindow(SW_RESTORE);
return;
}
// Case 2 - data unavailable
if (m_needsreading && m_windowok) {
CMyString passkey, temp;
int rc, rc2;
if (!PWSprefs::GetInstance()->GetPref(PWSprefs::UseSystemTray)) {
rc = GetAndCheckPassword(m_core.GetCurFile(), passkey, GCP_WITHEXIT); // OK, CANCEL, EXIT, HELP
} else {
rc = GetAndCheckPassword(m_core.GetCurFile(), passkey, GCP_UNMINIMIZE); // OK, CANCEL, HELP
}
switch (rc) {
case PWScore::SUCCESS:
rc2 = m_core.ReadCurFile(passkey);
#if !defined(POCKET_PC)
m_title = _T("Password Safe - ") + m_core.GetCurFile();
#endif
break;
case PWScore::CANT_OPEN_FILE:
temp = m_core.GetCurFile()
+ "\n\nCannot open database. It likely does not exist."
+ "\nA new database will be created.";
MessageBox(temp, _T("File open error."), MB_OK|MB_ICONWARNING);
case TAR_NEW:
rc2 = New();
break;
case TAR_OPEN:
rc2 = Open();
break;
case PWScore::WRONG_PASSWORD:
rc2 = PWScore::NOT_SUCCESS;
break;
case PWScore::USER_CANCEL:
rc2 = PWScore::NOT_SUCCESS;
break;
case PWScore::USER_EXIT:
m_core.UnlockFile(m_core.GetCurFile());
PostQuitMessage(0);
return;
default:
rc2 = PWScore::NOT_SUCCESS;
break;
}
if (rc2 == PWScore::SUCCESS) {
m_needsreading = false;
UpdateSystemTray(UNLOCKED);
startLockCheckTimer();
m_passphraseOK = true;
if (update_windows)
ShowWindow(SW_RESTORE);
} else {
m_needsreading = true;
if (PWSprefs::GetInstance()->GetPref(PWSprefs::UseSystemTray))
ShowWindow( SW_HIDE );
else
ShowWindow( SW_MINIMIZE );
}
return;
}
if (update_windows)
ShowWindow(SW_RESTORE);
}
void
DboxMain::startLockCheckTimer(){
const UINT INTERVAL = 5000; // every 5 seconds should suffice
if (PWSprefs::GetInstance()->
GetPref(PWSprefs::LockOnWindowLock )==TRUE){
TRACE(_T("startLockCheckTimer: Starting timer\n"));
SetTimer(TIMER_CHECKLOCK, INTERVAL, NULL);
} else
TRACE(_T("startLockCheckTimer: Not Starting timer\n"));
}
BOOL
DboxMain::PreTranslateMessage(MSG* pMsg)
{
// Do NOT pass the ESC along if preference EscExits is false.
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE &&
!PWSprefs::GetInstance()->
GetPref(PWSprefs::EscExits)) {
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
void
DboxMain::ResetIdleLockCounter()
{
m_IdleLockCountDown = PWSprefs::GetInstance()->
GetPref(PWSprefs::IdleTimeout);
}
bool
DboxMain::DecrementAndTestIdleLockCounter()
{
if (m_IdleLockCountDown > 0)
return (--m_IdleLockCountDown == 0);
else
return false; // so we return true only once if idle
}
LRESULT
DboxMain::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
static DWORD last_t = 0;
DWORD t = GetTickCount();
if (t != last_t) {
PWSrand::GetInstance()->AddEntropy((unsigned char *)&t, sizeof(t));
last_t = t;
}
// list of all the events that signify actual user activity, as opposed
// to Windows internal events...
if (message == WM_KEYDOWN ||
message == WM_COMMAND ||
message == WM_SYSCOMMAND ||
message == WM_MOUSEMOVE ||
message == WM_MOVE ||
message == WM_LBUTTONDOWN ||
message == WM_LBUTTONDBLCLK ||
message == WM_CONTEXTMENU ||
// JHF undeclared identifier -> removed to get code to compile
#if !defined(POCKET_PC)
message == WM_MENUSELECT ||
#endif
message == WM_VSCROLL
)
ResetIdleLockCounter();
return CDialog::WindowProc(message, wParam, lParam);
}
void
DboxMain::CheckExpiredPasswords()
{
ExpPWEntry exppwentry;
time_t now, LTime;
CList<ExpPWEntry, ExpPWEntry&>* p_expPWList = new CList<ExpPWEntry, ExpPWEntry&>;
POSITION listPos = m_core.GetFirstEntryPosition();
time(&now);
while (listPos != NULL)
{
const CItemData &curitem = m_core.GetEntryAt(listPos);
curitem.GetLTime(LTime);
if (((long)LTime != 0) && (LTime < now)) {
exppwentry.group = curitem.GetGroup();
exppwentry.title = curitem.GetTitle();
exppwentry.user = curitem.GetUser();
exppwentry.expiryascdate = curitem.GetLTime();
exppwentry.expiryexpdate = curitem.GetLTimeExp();
exppwentry.expirytttdate = LTime;
p_expPWList->AddTail(exppwentry);
}
m_core.GetNextEntry(listPos);
}
if (p_expPWList->GetCount() > 0) {
CExpPWListDlg dlg(this, m_core.GetCurFile());
dlg.m_pexpPWList = p_expPWList;
int rc = 0;
rc = dlg.DoModal();
p_expPWList->RemoveAll();
}
delete p_expPWList;
}
void
DboxMain::UpdateAccessTime(CItemData *ci)
{
// Mark access time if so configured
ASSERT(ci != NULL);
bool bMaintainDateTimeStamps = PWSprefs::GetInstance()->
GetPref(PWSprefs::MaintainDateTimeStamps);
if (!m_IsReadOnly && bMaintainDateTimeStamps) {
ci->SetATime();
SetChanged(TimeStamp);
}
}
BOOL
DboxMain::OnQueryEndSession()
{
m_iSessionEndingStatus = IDOK;
if (m_IsReadOnly)
return TRUE;
if (m_bTSUpdated && m_core.GetNumEntries() > 0) {
Save();
return TRUE;
}
if (m_core.IsChanged()) {
CString msg = _T("Do you wish to save the changes made to database:\r\n\r\n");
msg += m_core.GetCurFile();
msg += _T("\r\n\r\nor Cancel the shutdown\\restart\\logoff?");
int rc = AfxMessageBox(msg, MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON3);
m_iSessionEndingStatus = rc;
switch (rc) {
case IDCANCEL:
// Cancel shutdown\restart\logoff
return FALSE;
case IDYES:
// Save the changes and say OK to shutdown\restart\logoff
return TRUE;
case IDNO:
// Don't save the changes but say OK to shutdown\restart\logoff
return TRUE;
}
}
return TRUE;
}
void
DboxMain::OnEndSession(BOOL bEnding)
{
if (bEnding == TRUE) {
switch (m_iSessionEndingStatus) {
case IDOK:
// Either not changed, R/O or already saved timestamps
case IDCANCEL:
// Shouldn't happen as the user said NO to shutdown\restart\logoff
case IDNO:
// User said NO to saving - so don't!
break;
case IDYES:
// User said YES they wanted to save - so do!
OnOK();
break;
default:
ASSERT(0);
}
} else {
// Reset status since the EndSession was cancelled
m_iSessionEndingStatus = IDIGNORE;
}
}
void
DboxMain::UpdateStatusBar()
{
if (m_toolbarsSetup == TRUE) {
CString s;
s = m_IsReadOnly ? _T(" R/O ") : _T(" R/W ");
m_statusBar.SetPaneText(1, s);
s.Format("# entries: %d", m_core.GetNumEntries());
m_statusBar.SetPaneText(2, s);
}
}
void DboxMain::MakeSortedItemList(ItemList &il)
{
// Walk the Tree!
HTREEITEM hItem = NULL;
while ( NULL != (hItem = m_ctlItemTree.GetNextTreeItem(hItem)) ) {
if (!m_ctlItemTree.ItemHasChildren(hItem)) {
CItemData *ci = (CItemData *)m_ctlItemTree.GetItemData(hItem);
ASSERT(ci != NULL);
il.AddTail(*ci);
}
}
}