[6a7162]: MainEdit.cpp Maximize Restore History

Download this file

MainEdit.cpp    1627 lines (1425 with data), 53.3 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
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
/*
* Copyright (c) 2003-2008 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
/// file MainEdit.cpp
//
// Edit-related methods of DboxMain
//-----------------------------------------------------------------------------
#include "PasswordSafe.h"
#include "ThisMfcApp.h"
#include "corelib/pwsprefs.h"
#include "DDSupport.h"
// dialog boxen
#include "DboxMain.h"
#include "AddDlg.h"
#include "ConfirmDeleteDlg.h"
#include "QuerySetDef.h"
#include "EditDlg.h"
#include "EditShortcutDlg.h"
#include "KeySend.h"
#include "ClearQuestionDlg.h"
#include "CreateShortcutDlg.h"
#include <vector>
#include <algorithm>
#include <Winable.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//Add an item
void DboxMain::OnAdd()
{
CAddDlg dlg_add(this);
if (m_core.GetUseDefUser()) {
dlg_add.m_username = m_core.GetDefUsername();
}
// m_TreeViewGroup may be set by OnContextMenu, if not, try to grok it
if (m_TreeViewGroup.IsEmpty()) {
CItemData *itemData = NULL;
if (m_ctlItemTree.IsWindowVisible()) { // tree view
HTREEITEM ti = m_ctlItemTree.GetSelectedItem();
if (ti != NULL) { // if anything selected
itemData = (CItemData *)m_ctlItemTree.GetItemData(ti);
if (itemData != NULL) { // leaf selected
m_TreeViewGroup = itemData->GetGroup();
} else { // node selected
m_TreeViewGroup = CMyString(m_ctlItemTree.GetGroup(ti));
}
}
} else { // list view
// XXX TBD - get group name of currently selected list entry
}
}
dlg_add.m_group = m_TreeViewGroup;
m_TreeViewGroup = _T(""); // for next time
app.DisableAccelerator();
INT_PTR rc = dlg_add.DoModal();
app.EnableAccelerator();
if (rc == IDOK) {
bool bWasEmpty = m_core.GetNumEntries() == 0;
PWSprefs *prefs = PWSprefs::GetInstance();
//Check if they wish to set a default username
if (!m_core.GetUseDefUser() &&
(prefs->GetPref(PWSprefs::QuerySetDef)) &&
(!dlg_add.m_username.IsEmpty())) {
CQuerySetDef defDlg(this);
defDlg.m_message.Format(IDS_SETUSERNAME, (const CString&)dlg_add.m_username);
INT_PTR rc2 = defDlg.DoModal();
if (rc2 == IDOK) {
prefs->SetPref(PWSprefs::UseDefaultUser, true);
prefs->SetPref(PWSprefs::DefaultUsername, dlg_add.m_username);
m_core.SetUseDefUser(true);
m_core.SetDefUsername(dlg_add.m_username);
RefreshViews();
}
}
//Finish Check (Does that make any geographical sense?)
CItemData temp;
CMyString user;
time_t t;
if (dlg_add.m_username.IsEmpty() && m_core.GetUseDefUser())
user = m_core.GetDefUsername();
else
user = dlg_add.m_username;
temp.CreateUUID();
temp.SetGroup(dlg_add.m_group);
temp.SetTitle(dlg_add.m_title);
temp.SetUser(user);
if (dlg_add.m_ibasedata > 0) {
// Password in alias format AND base entry exists
// No need to check if base is an alias as already done in
// call to PWScore::GetBaseEntry
uuid_array_t alias_uuid;
temp.GetUUID(alias_uuid);
m_core.AddDependentEntry(dlg_add.m_base_uuid, alias_uuid, CItemData::Alias);
temp.SetPassword(CMyString(_T("[Alias]")));
temp.SetAlias();
ItemListIter iter = m_core.Find(dlg_add.m_base_uuid);
if (iter != End()) {
const CItemData &cibase = iter->second;
DisplayInfo *di = (DisplayInfo *)cibase.GetDisplayInfo();
int nImage = GetEntryImage(cibase);
SetEntryImage(di->list_index, nImage, true);
SetEntryImage(di->tree_item, nImage, true);
}
} else {
temp.SetPassword(dlg_add.m_password);
temp.SetNormal();
}
temp.SetNotes(dlg_add.m_notes);
temp.SetURL(dlg_add.m_URL);
temp.SetAutoType(dlg_add.m_autotype);
time(&t);
temp.SetCTime(t);
// Set per-item password policy if user selected
// "Override Policy"
if (dlg_add.m_OverridePolicy == TRUE)
temp.SetPWPolicy(dlg_add.m_pwp);
if (temp.IsAlias()) {
temp.SetLTime((time_t)0);
temp.SetPWPolicy(_T(""));
} else {
temp.SetLTime(dlg_add.m_tttLTime);
temp.SetPWPolicy(dlg_add.m_pwp);
}
if (dlg_add.m_SavePWHistory == TRUE) {
TCHAR buffer[6];
#if _MSC_VER >= 1400
_stprintf_s(buffer, 6, _T("1%02x00"), dlg_add.m_MaxPWHistory);
#else
_stprintf(buffer, _T("1%02x00"), dlg_add.m_MaxPWHistory);
#endif
temp.SetPWHistory(buffer);
}
AddEntry(temp);
if (m_core.GetNumEntries() == 1) {
// For some reason, when adding the first entry, it is not visible!
m_ctlItemTree.SetRedraw(TRUE);
}
m_ctlItemList.SetFocus();
if (prefs->GetPref(PWSprefs::SaveImmediately))
Save();
ChangeOkUpdate();
uuid_array_t uuid;
temp.GetUUID(uuid);
m_RUEList.AddRUEntry(uuid);
// May need to update menu/toolbar if database was previously empty
if (bWasEmpty)
UpdateMenuAndToolBar(m_bOpen);
}
}
//Add a shortcut
void DboxMain::OnCreateShortcut()
{
// disable in read-only mode or nothing selected
if (m_core.IsReadOnly() || SelItemOk() != TRUE)
return;
CItemData *ci = getSelectedItem();
ASSERT(ci != NULL);
CCreateShortcutDlg dlg_createshortcut(this, ci->GetGroup(),
ci->GetTitle(), ci->GetUser());
if (m_core.GetUseDefUser()) {
dlg_createshortcut.m_username = m_core.GetDefUsername();
}
app.DisableAccelerator();
INT_PTR rc = dlg_createshortcut.DoModal();
app.EnableAccelerator();
if (rc == IDOK) {
PWSprefs *prefs = PWSprefs::GetInstance();
//Check if they wish to set a default username
if (!m_core.GetUseDefUser() &&
(prefs->GetPref(PWSprefs::QuerySetDef)) &&
(!dlg_createshortcut.m_username.IsEmpty())) {
CQuerySetDef defDlg(this);
defDlg.m_message.Format(IDS_SETUSERNAME, (const CString&)dlg_createshortcut.m_username);
INT_PTR rc2 = defDlg.DoModal();
if (rc2 == IDOK) {
prefs->SetPref(PWSprefs::UseDefaultUser, true);
prefs->SetPref(PWSprefs::DefaultUsername, dlg_createshortcut.m_username);
m_core.SetUseDefUser(true);
m_core.SetDefUsername(dlg_createshortcut.m_username);
RefreshViews();
}
}
if (dlg_createshortcut.m_username.IsEmpty() && m_core.GetUseDefUser())
dlg_createshortcut.m_username = m_core.GetDefUsername();
CreateShortcutEntry(ci, dlg_createshortcut.m_group,
dlg_createshortcut.m_title,
dlg_createshortcut.m_username);
}
}
void DboxMain::CreateShortcutEntry(CItemData *ci, const CMyString cs_group,
const CMyString cs_title, const CMyString cs_user)
{
uuid_array_t base_uuid, shortcut_uuid;
ASSERT(ci != NULL);
ci->GetUUID(base_uuid);
//Finish Check (Does that make any geographical sense?)
CItemData temp;
time_t t;
temp.CreateUUID();
temp.GetUUID(shortcut_uuid);
temp.SetGroup(cs_group);
temp.SetTitle(cs_title);
temp.SetUser(cs_user);
m_core.AddDependentEntry(base_uuid, shortcut_uuid, CItemData::Shortcut);
temp.SetPassword(CMyString(_T("[Shortcut]")));
temp.SetShortcut();
ItemListIter iter = m_core.Find(base_uuid);
if (iter != End()) {
const CItemData &cibase = iter->second;
DisplayInfo *di = (DisplayInfo *)cibase.GetDisplayInfo();
int nImage = GetEntryImage(cibase);
SetEntryImage(di->list_index, nImage, true);
SetEntryImage(di->tree_item, nImage, true);
}
time(&t);
temp.SetCTime(t);
temp.SetLTime((time_t)0);
AddEntry(temp);
if (m_core.GetNumEntries() == 1) {
// For some reason, when adding the first entry, it is not visible!
m_ctlItemTree.SetRedraw(TRUE);
}
m_ctlItemList.SetFocus();
if (PWSprefs::GetInstance()->GetPref(PWSprefs::SaveImmediately))
Save();
ChangeOkUpdate();
m_RUEList.AddRUEntry(shortcut_uuid);
}
int DboxMain::AddEntry(const CItemData &cinew)
{
// This routine is used by Add and also Drag & Drop
m_core.AddEntry(cinew);
// AddEntry copies the entry, and we want to work with the inserted copy
// Which we'll find by uuid
uuid_array_t uuid;
cinew.GetUUID(uuid);
int newpos = insertItem(m_core.GetEntry(m_core.Find(uuid)));
SelectEntry(newpos);
FixListIndexes();
return newpos;
}
//Add a group (tree view only)
void DboxMain::OnAddGroup()
{
if (m_core.IsReadOnly()) // disable in read-only mode
return;
if (m_ctlItemTree.IsWindowVisible()) {
// This can be reached by right clicking over an existing group node
// or by clicking over "whitespace".
// If the former, add a child node to the current one
// If the latter, add to root.
CMyString cmys_text(MAKEINTRESOURCE(IDS_NEWGROUP));
if (m_TreeViewGroup.IsEmpty())
m_TreeViewGroup = cmys_text;
else
m_TreeViewGroup += _T(".") + cmys_text;
HTREEITEM newGroup = m_ctlItemTree.AddGroup(m_TreeViewGroup);
m_ctlItemTree.SelectItem(newGroup);
m_TreeViewGroup = _T(""); // for next time
m_ctlItemTree.EditLabel(newGroup);
}
}
// Delete key was pressed (in list view or tree view) to delete an entry.
void DboxMain::OnDelete()
{
if (m_core.GetNumEntries() == 0) // easiest way to avoid asking stupid questions...
return;
bool dontaskquestion = PWSprefs::GetInstance()->
GetPref(PWSprefs::DeleteQuestion);
bool dodelete = true;
int num_children = 0;
if (m_ctlItemTree.IsWindowVisible()) {
HTREEITEM hStartItem = m_ctlItemTree.GetSelectedItem();
if (hStartItem != NULL) {
if (m_ctlItemTree.GetItemData(hStartItem) == NULL) { // group node
dontaskquestion = false; // ALWAYS ask if deleting a group
// Find number of child items
num_children = 0;
if (m_ctlItemTree.ItemHasChildren(hStartItem)) {
num_children = CountChildren(hStartItem);
} // if has children
}
}
}
//Confirm whether to delete the item
if (!dontaskquestion) {
CConfirmDeleteDlg deleteDlg(this, num_children);
INT_PTR rc = deleteDlg.DoModal();
if (rc == IDCANCEL) {
dodelete = false;
}
}
if (dodelete) {
Delete();
}
}
void DboxMain::Delete(bool inRecursion)
{
CItemData *ci = getSelectedItem();
if (ci != NULL) {
uuid_array_t entry_uuid;
ci->GetUUID(entry_uuid);
UUIDList dependentslist;
int num_dependents(0);
CItemData::EntryType entrytype = ci->GetEntryType();
if (entrytype == CItemData::AliasBase)
m_core.GetAllDependentEntries(entry_uuid, dependentslist, CItemData::Alias);
else
if (entrytype == CItemData::ShortcutBase)
m_core.GetAllDependentEntries(entry_uuid, dependentslist, CItemData::Shortcut);
num_dependents = dependentslist.size();
if (num_dependents > 0) {
CMyString csDependents;
SortDependents(dependentslist, csDependents);
CString cs_msg, cs_type;
const CString cs_title(MAKEINTRESOURCE(IDS_DELETEBASET));
if (entrytype == CItemData::AliasBase) {
cs_type.LoadString(num_dependents == 1 ? IDS_ALIAS : IDS_ALIASES);
cs_msg.Format(IDS_DELETEABASE, dependentslist.size(), cs_type, csDependents);
} else {
cs_type.LoadString(num_dependents == 1 ? IDS_SHORTCUT : IDS_SHORTCUTS);
cs_msg.Format(IDS_DELETESBASE, dependentslist.size(), cs_type, csDependents);
}
if (MessageBox(cs_msg, cs_title, MB_ICONQUESTION | MB_YESNO) == IDNO) {
dependentslist.clear();
return;
}
}
DisplayInfo *di = (DisplayInfo *)ci->GetDisplayInfo();
ASSERT(di != NULL);
int curSel = di->list_index;
// Find next in treeview, not always curSel after deletion
HTREEITEM curTree_item = di->tree_item;
HTREEITEM nextTree_item = m_ctlItemTree.GetNextItem(curTree_item, TVGN_NEXT);
// Must Find before delete from m_ctlItemList:
ItemListIter listindex = m_core.Find(entry_uuid);
ASSERT(listindex != m_core.GetEntryEndIter());
UnFindItem();
m_ctlItemList.DeleteItem(curSel);
m_ctlItemTree.DeleteWithParents(curTree_item);
delete di;
FixListIndexes();
if (ci->NumberUnknownFields() > 0)
m_core.DecrementNumRecordsWithUnknownFields();
uuid_array_t base_uuid;
if (entrytype == CItemData::Alias) {
// I'm an alias entry
// Get corresponding base uuid
m_core.GetAliasBaseUUID(entry_uuid, base_uuid);
// Delete from both map and multimap
m_core.RemoveDependentEntry(base_uuid, entry_uuid, CItemData::Alias);
// Does my base now become a normal entry?
if (m_core.NumAliases(base_uuid) == 0) {
ItemListIter iter = m_core.Find(base_uuid);
CItemData &cibase = iter->second;
cibase.SetNormal();
DisplayInfo *di = (DisplayInfo *)cibase.GetDisplayInfo();
int nImage = GetEntryImage(cibase);
SetEntryImage(di->list_index, nImage, true);
SetEntryImage(di->tree_item, nImage, true);
}
}
if (entrytype == CItemData::Shortcut) {
// I'm a shortcut entry
// Get corresponding base uuid
m_core.GetShortcutBaseUUID(entry_uuid, base_uuid);
// Delete from both map and multimap
m_core.RemoveDependentEntry(base_uuid, entry_uuid, CItemData::Shortcut);
// Does my base now become a normal entry?
if (m_core.NumShortcuts(base_uuid) == 0) {
ItemListIter iter = m_core.Find(base_uuid);
CItemData &cibase = iter->second;
cibase.SetNormal();
DisplayInfo *di = (DisplayInfo *)cibase.GetDisplayInfo();
int nImage = GetEntryImage(cibase);
SetEntryImage(di->list_index, nImage, true);
SetEntryImage(di->tree_item, nImage, true);
}
}
if (num_dependents > 0) {
// I'm a base entry
if (entrytype == CItemData::AliasBase) {
m_core.ResetAllAliasPasswords(entry_uuid);
m_core.RemoveAllDependentEntries(entry_uuid, CItemData::Alias);
// Now make all my aliases Normal
ItemListIter iter;
UUIDListIter UUIDiter;
for (UUIDiter = dependentslist.begin(); UUIDiter != dependentslist.end(); UUIDiter++) {
uuid_array_t auuid;
UUIDiter->GetUUID(auuid);
iter = m_core.Find(auuid);
CItemData &cialias = iter->second;
DisplayInfo *di = (DisplayInfo *)cialias.GetDisplayInfo();
int nImage = GetEntryImage(cialias);
SetEntryImage(di->list_index, nImage, true);
SetEntryImage(di->tree_item, nImage, true);
}
} else {
m_core.RemoveAllDependentEntries(entry_uuid, CItemData::Shortcut);
// Now delete all my shortcuts
ItemListIter iter;
UUIDListIter UUIDiter;
for (UUIDiter = dependentslist.begin(); UUIDiter != dependentslist.end(); UUIDiter++) {
uuid_array_t suuid;
UUIDiter->GetUUID(suuid);
iter = m_core.Find(suuid);
CItemData &cshortcut = iter->second;
DisplayInfo *di = (DisplayInfo *)cshortcut.GetDisplayInfo();
m_ctlItemList.DeleteItem(di->list_index);
m_ctlItemTree.DeleteItem(di->tree_item);
delete di;
FixListIndexes();
m_core.RemoveEntryAt(iter);
}
}
dependentslist.clear();
}
m_core.RemoveEntryAt(listindex);
if (m_ctlItemList.IsWindowVisible()) {
if (m_core.GetNumEntries() > 0) {
SelectEntry(curSel < (int)m_core.GetNumEntries() ? curSel : (int)(m_core.GetNumEntries() - 1));
}
m_ctlItemList.SetFocus();
} else {// tree view visible
if (!inRecursion && nextTree_item != NULL) {
m_ctlItemTree.SelectItem(nextTree_item);
}
m_ctlItemTree.SetFocus();
}
ChangeOkUpdate();
m_RUEList.DeleteRUEntry(entry_uuid);
} else { // !SelItemOk()
if (m_ctlItemTree.IsWindowVisible()) {
HTREEITEM ti = m_ctlItemTree.GetSelectedItem();
if (ti != NULL) {
if (!m_ctlItemTree.IsLeaf(ti)) {
HTREEITEM cti = m_ctlItemTree.GetChildItem(ti);
m_ctlItemTree.SetRedraw( FALSE );
while (cti != NULL) {
m_ctlItemTree.SelectItem(cti);
Delete(true); // recursion - I'm so lazy!
cti = m_ctlItemTree.GetChildItem(ti);
}
m_ctlItemTree.SetRedraw( TRUE );
m_ctlItemTree.Invalidate();
// delete an empty group.
HTREEITEM parent = m_ctlItemTree.GetParentItem(ti);
m_ctlItemTree.DeleteItem(ti);
m_ctlItemTree.SelectItem(parent);
}
}
}
}
m_TreeViewGroup = _T("");
}
void DboxMain::OnRename()
{
if (m_core.IsReadOnly()) // disable in read-only mode
return;
// Renaming is only allowed while in Tree mode.
if (m_ctlItemTree.IsWindowVisible()) {
HTREEITEM hItem = m_ctlItemTree.GetSelectedItem();
if (hItem != NULL)
m_ctlItemTree.EditLabel(hItem);
}
}
void DboxMain::OnEdit()
{
// Note that Edit is also used for just viewing - don't want to disable
// viewing in read-only mode
if (SelItemOk() == TRUE) {
CItemData *ci = getSelectedItem();
ASSERT(ci != NULL);
if (ci->IsShortcut())
EditShortcut(ci);
else
EditItem(ci);
} else {
// entry item not selected - perhaps here on Enter on tree item?
// perhaps not the most elegant solution to improving non-mouse use,
// but it works. If anyone knows how Enter/Return gets mapped to OnEdit,
// let me know...
CItemData *itemData = NULL;
if (m_ctlItemTree.IsWindowVisible()) { // tree view
HTREEITEM ti = m_ctlItemTree.GetSelectedItem();
if (ti != NULL) { // if anything selected
itemData = (CItemData *)m_ctlItemTree.GetItemData(ti);
if (itemData == NULL) { // node selected
m_ctlItemTree.Expand(ti, TVE_TOGGLE);
}
}
}
}
}
bool DboxMain::EditItem(CItemData *ci, PWScore *pcore)
{
if (pcore == NULL)
pcore = &m_core;
// List might be cleared if db locked.
// Need to take care that we handle a rebuilt list.
CItemData editedItem(*ci);
CEditDlg dlg_edit(&editedItem, this);
if (pcore->GetUseDefUser())
dlg_edit.m_defusername = pcore->GetDefUsername();
dlg_edit.m_Edit_IsReadOnly = pcore->IsReadOnly();
uuid_array_t original_uuid, original_base_uuid, new_base_uuid;
CItemData::EntryType entrytype = ci->GetEntryType();
ci->GetUUID(original_uuid); // Edit doesn't change this!
if (entrytype == CItemData::AliasBase || entrytype == CItemData::ShortcutBase) {
// Base entry
UUIDList dependentslist;
CMyString csDependents(_T(""));
m_core.GetAllDependentEntries(original_uuid, dependentslist,
entrytype == CItemData::AliasBase ? CItemData::Alias : CItemData::Shortcut);
int num_dependents = dependentslist.size();
if (num_dependents > 0) {
SortDependents(dependentslist, csDependents);
}
dlg_edit.m_num_dependents = num_dependents;
dlg_edit.m_dependents = csDependents;
dlg_edit.m_original_entrytype = entrytype;
dependentslist.clear();
}
if (entrytype == CItemData::Alias) {
// Alias entry
// Get corresponding base uuid
m_core.GetAliasBaseUUID(original_uuid, original_base_uuid);
ItemListIter iter = m_core.Find(original_base_uuid);
if (iter != End()) {
const CItemData &cibase = iter->second;
dlg_edit.m_base = _T("[") +
cibase.GetGroup() + _T(":") +
cibase.GetTitle() + _T(":") +
cibase.GetUser() + _T("]");
dlg_edit.m_original_entrytype = CItemData::Alias;
}
} else {
editedItem.GetPWPolicy(dlg_edit.m_pwp);
}
app.DisableAccelerator();
INT_PTR rc = dlg_edit.DoModal();
app.EnableAccelerator();
if (rc == IDOK) {
// Out with the old, in with the new
ItemListIter listpos = Find(original_uuid);
ASSERT(listpos != m_core.GetEntryEndIter());
CItemData oldElem = GetEntryAt(listpos);
DisplayInfo *di = (DisplayInfo *)oldElem.GetDisplayInfo();
ASSERT(di != NULL);
// editedItem's displayinfo will have been deleted if
// application "locked" (Cleared list)
DisplayInfo *ndi = new DisplayInfo;
ndi->list_index = -1; // so that insertItem will set new values
ndi->tree_item = 0;
editedItem.SetDisplayInfo(ndi);
CMyString newPassword = editedItem.GetPassword();
memcpy(new_base_uuid, dlg_edit.m_base_uuid, sizeof(uuid_array_t));
ItemListIter iter;
if (dlg_edit.m_original_entrytype == CItemData::Normal &&
editedItem.GetPassword() != newPassword) {
// Original was a 'normal' entry and the password has changed
if (dlg_edit.m_ibasedata > 0) {
// Now an alias
pcore->AddDependentEntry(new_base_uuid, original_uuid, CItemData::Alias);
editedItem.SetPassword(CMyString(_T("[Alias]")));
editedItem.SetAlias();
} else {
// Still 'normal'
editedItem.SetPassword(newPassword);
editedItem.SetNormal();
}
}
if (dlg_edit.m_original_entrytype == CItemData::Alias) {
// Original was an alias - delete it from multimap
// RemoveDependentEntry also resets base to normal if the last alias is delete
pcore->RemoveDependentEntry(original_base_uuid, original_uuid, CItemData::Alias);
if (newPassword == dlg_edit.m_base) {
// Password (i.e. base) unchanged - put it back
pcore->AddDependentEntry(original_base_uuid, original_uuid, CItemData::Alias);
} else {
// Password changed so might be an alias of another entry!
// Could also be the same entry i.e. [:t:] == [t] !
if (dlg_edit.m_ibasedata > 0) {
// Still an alias
pcore->AddDependentEntry(new_base_uuid, original_uuid, CItemData::Alias);
editedItem.SetPassword(CMyString(_T("[Alias]")));
editedItem.SetAlias();
} else {
// No longer an alias
editedItem.SetPassword(newPassword);
editedItem.SetNormal();
}
}
}
if (dlg_edit.m_original_entrytype == CItemData::AliasBase &&
editedItem.GetPassword() != newPassword) {
// Original was a base but might now be an alias of another entry!
if (dlg_edit.m_ibasedata > 0) {
// Now an alias
// Make this one an alias
pcore->AddDependentEntry(new_base_uuid, original_uuid, CItemData::Alias);
editedItem.SetPassword(CMyString(_T("[Alias]")));
editedItem.SetAlias();
// Move old aliases across
pcore->MoveDependentEntries(original_uuid, new_base_uuid, CItemData::Alias);
} else {
// Still a base entry but with a new password
editedItem.SetPassword(newPassword);
editedItem.SetAliasBase();
}
}
// Reset all images!
// This entry's image will be set by DboxMain::insertItem
// Next the original base entry
iter = m_core.Find(original_base_uuid);
if (iter != End()) {
const CItemData &cibase = iter->second;
DisplayInfo *di = (DisplayInfo *)cibase.GetDisplayInfo();
int nImage = GetEntryImage(cibase);
SetEntryImage(di->list_index, nImage, true);
SetEntryImage(di->tree_item, nImage, true);
}
// Last the new base entry (only if different to the one we have done!
if (::memcmp(new_base_uuid, original_base_uuid, sizeof(uuid_array_t)) != 0) {
iter = m_core.Find(new_base_uuid);
if (iter != End()) {
const CItemData &cibase = iter->second;
DisplayInfo *di = (DisplayInfo *)cibase.GetDisplayInfo();
int nImage = GetEntryImage(cibase);
SetEntryImage(di->list_index, nImage, true);
SetEntryImage(di->tree_item, nImage, true);
}
}
if (editedItem.IsAlias() || editedItem.IsShortcut()) {
editedItem.SetLTime((time_t)0);
editedItem.SetPWPolicy(_T(""));
} else {
editedItem.SetPWPolicy(dlg_edit.m_pwp);
}
pcore->RemoveEntryAt(listpos);
pcore->AddEntry(editedItem);
m_ctlItemList.DeleteItem(di->list_index);
m_ctlItemTree.DeleteWithParents(di->tree_item);
// AddEntry copies the entry, and we want to work with the inserted copy
// Which we'll find by uuid
insertItem(pcore->GetEntry(m_core.Find(original_uuid)));
FixListIndexes();
// Now delete old entry's DisplayInfo
delete di;
if (PWSprefs::GetInstance()->
GetPref(PWSprefs::SaveImmediately)) {
Save();
}
rc = SelectEntry(ndi->list_index);
if (rc == 0) {
SelectEntry(m_ctlItemList.GetItemCount() - 1);
}
ChangeOkUpdate();
// Order may have changed as a result of edit
m_ctlItemTree.SortTree(TVI_ROOT);
SortListView();
return true;
} // rc == IDOK
return false;
}
bool DboxMain::EditShortcut(CItemData *ci, PWScore *pcore)
{
if (pcore == NULL)
pcore = &m_core;
// List might be cleared if db locked.
// Need to take care that we handle a rebuilt list.
CItemData editedItem(*ci);
uuid_array_t entry_uuid, base_uuid;
ci->GetUUID(entry_uuid); // Edit doesn't change this!
// Shortcut entry
// Get corresponding base uuid
m_core.GetShortcutBaseUUID(entry_uuid, base_uuid);
ItemListIter iter = m_core.Find(base_uuid);
if (iter == End())
return false;
CEditShortcutDlg dlg_editshortcut(&editedItem, this, iter->second.GetGroup(),
iter->second.GetTitle(), iter->second.GetUser());
if (pcore->GetUseDefUser())
dlg_editshortcut.m_defusername = pcore->GetDefUsername();
dlg_editshortcut.m_Edit_IsReadOnly = pcore->IsReadOnly();
app.DisableAccelerator();
INT_PTR rc = dlg_editshortcut.DoModal();
app.EnableAccelerator();
if (rc == IDOK) {
// Out with the old, in with the new
// User cannot change a shortcut entry to anything else!
ItemListIter listpos = Find(entry_uuid);
ASSERT(listpos != m_core.GetEntryEndIter());
CItemData oldElem = GetEntryAt(listpos);
DisplayInfo *di = (DisplayInfo *)oldElem.GetDisplayInfo();
ASSERT(di != NULL);
// editedItem's displayinfo will have been deleted if
// application "locked" (Cleared list)
DisplayInfo *ndi = new DisplayInfo;
ndi->list_index = -1; // so that insertItem will set new values
ndi->tree_item = 0;
editedItem.SetDisplayInfo(ndi);
editedItem.SetLTime((time_t)0);
pcore->RemoveEntryAt(listpos);
pcore->AddEntry(editedItem);
m_ctlItemList.DeleteItem(di->list_index);
m_ctlItemTree.DeleteWithParents(di->tree_item);
// AddEntry copies the entry, and we want to work with the inserted copy
// Which we'll find by uuid
insertItem(pcore->GetEntry(m_core.Find(entry_uuid)));
FixListIndexes();
// Now delete old entry's DisplayInfo
delete di;
if (PWSprefs::GetInstance()->
GetPref(PWSprefs::SaveImmediately)) {
Save();
}
rc = SelectEntry(ndi->list_index);
if (rc == 0) {
SelectEntry(m_ctlItemList.GetItemCount() - 1);
}
ChangeOkUpdate();
return true;
} // rc == IDOK
return false;
}
// Duplicate selected entry but make title unique
void DboxMain::OnDuplicateEntry()
{
if (m_core.IsReadOnly()) // disable in read-only mode
return;
if (SelItemOk() == TRUE) {
CItemData *ci = getSelectedItem();
ASSERT(ci != NULL);
DisplayInfo *di = (DisplayInfo *)ci->GetDisplayInfo();
ASSERT(di != NULL);
// Get information from current selected entry
CMyString ci2_group = ci->GetGroup();
CMyString ci2_user = ci->GetUser();
CMyString ci2_title0 = ci->GetTitle();
CMyString ci2_title;
// Find a unique "Title"
ItemListConstIter listpos;
int i = 0;
CString s_copy;
do {
i++;
s_copy.Format(IDS_COPYNUMBER, i);
ci2_title = ci2_title0 + CMyString(s_copy);
listpos = m_core.Find(ci2_group, ci2_title, ci2_user);
} while (listpos != m_core.GetEntryEndIter());
// Set up new entry
CItemData ci2;
ci2.CreateUUID();
ci2.SetGroup(ci2_group);
ci2.SetTitle(ci2_title);
ci2.SetUser(ci2_user);
ci2.SetPassword(ci->GetPassword());
ci2.SetURL(ci->GetURL());
ci2.SetAutoType(ci->GetAutoType());
ci2.SetNotes(ci->GetNotes());
time_t t;
ci->GetCTime(t);
if ((long) t != 0)
ci2.SetCTime(t);
ci->GetATime(t);
if ((long) t != 0)
ci2.SetATime(t);
ci->GetLTime(t);
if ((long) t != 0)
ci2.SetLTime(t);
ci->GetPMTime(t);
if ((long) t != 0)
ci2.SetPMTime(t);
ci->GetRMTime(t);
if ((long) t != 0)
ci2.SetRMTime(t);
CMyString tmp = ci->GetPWHistory();
if (tmp.GetLength() >= 5)
ci2.SetPWHistory(tmp);
uuid_array_t base_uuid, original_entry_uuid, new_entry_uuid;
CItemData::EntryType entrytype = ci->GetEntryType();
if (entrytype == CItemData::Alias || entrytype == CItemData::Shortcut) {
ci->GetUUID(original_entry_uuid);
ci2.GetUUID(new_entry_uuid);
if (entrytype == CItemData::Alias) {
m_core.GetAliasBaseUUID(original_entry_uuid, base_uuid);
ci2.SetAlias();
m_core.AddDependentEntry(base_uuid, new_entry_uuid, CItemData::Alias);
} else {
m_core.GetShortcutBaseUUID(original_entry_uuid, base_uuid);
ci2.SetShortcut();
m_core.AddDependentEntry(base_uuid, new_entry_uuid, CItemData::Shortcut);
}
ItemListIter iter;
iter = m_core.Find(base_uuid);
if (iter != m_core.GetEntryEndIter()) {
CMyString cs_tmp;
cs_tmp = _T("[") +
iter->second.GetGroup() + _T(":") +
iter->second.GetTitle() + _T(":") +
iter->second.GetUser() + _T("]");
ci2.SetPassword(cs_tmp);
}
}
// Add it to the end of the list
m_core.AddEntry(ci2);
di->list_index = -1; // so that insertItem will set new values
uuid_array_t uuid;
ci2.GetUUID(uuid);
insertItem(m_core.GetEntry(m_core.Find(uuid)));
FixListIndexes();
if (PWSprefs::GetInstance()->
GetPref(PWSprefs::SaveImmediately)) {
Save();
}
int rc = SelectEntry(di->list_index);
if (rc == 0) {
SelectEntry(m_ctlItemList.GetItemCount() - 1);
}
ChangeOkUpdate();
m_RUEList.AddRUEntry(uuid);
}
}
void DboxMain::OnCopyPassword()
{
if (!SelItemOk())
return;
//Remind the user about clipboard security
CClearQuestionDlg clearDlg(this);
if (clearDlg.m_dontaskquestion == FALSE &&
clearDlg.DoModal() == IDCANCEL)
return;
CItemData *ci = getSelectedItem();
ASSERT(ci != NULL);
CItemData *ci_original(ci);
uuid_array_t base_uuid, entry_uuid;
const CItemData::EntryType entrytype = ci->GetEntryType();
if (entrytype == CItemData::Alias || entrytype == CItemData::Shortcut) {
// This is an alias/shortcut
ci->GetUUID(entry_uuid);
if (entrytype == CItemData::Alias)
m_core.GetAliasBaseUUID(entry_uuid, base_uuid);
else
m_core.GetShortcutBaseUUID(entry_uuid, base_uuid);
ItemListIter iter = m_core.Find(base_uuid);
if (iter != End()) {
ci = &iter->second;
}
}
SetClipboardData(ci->GetPassword());
UpdateAccessTime(ci_original);
}
void DboxMain::OnCopyUsername()
{
if (SelItemOk() != TRUE)
return;
CItemData *ci = getSelectedItem();
ASSERT(ci != NULL);
CItemData *ci_original(ci);
if (ci->IsShortcut()) {
// This is an shortcut
uuid_array_t entry_uuid, base_uuid;
ci->GetUUID(entry_uuid);
m_core.GetShortcutBaseUUID(entry_uuid, base_uuid);
ItemListIter iter = m_core.Find(base_uuid);
if (iter != End()) {
ci = &iter->second;
}
}
const CMyString username = ci->GetUser();
if (!username.IsEmpty())
SetClipboardData(username);
else
ClearClipboardData();
UpdateAccessTime(ci_original);
}
void DboxMain::OnCopyNotes()
{
if (SelItemOk() != TRUE)
return;
CItemData *ci = getSelectedItem();
ASSERT(ci != NULL);
CItemData *ci_original(ci);
if (ci->IsShortcut()) {
// This is an shortcut
uuid_array_t entry_uuid, base_uuid;
ci->GetUUID(entry_uuid);
m_core.GetShortcutBaseUUID(entry_uuid, base_uuid);
ItemListIter iter = m_core.Find(base_uuid);
if (iter != End()) {
ci = &iter->second;
}
}
const CMyString notes = ci->GetNotes();
const CMyString url = ci->GetURL();
const CMyString autotype = ci->GetAutoType();
CMyString clipboard_data;
CString cs_text;
clipboard_data = notes;
if (!url.IsEmpty()) {
if (ci->IsURLEmail())
cs_text.LoadString(IDS_COPYURL);
else
cs_text.LoadString(IDS_COPYEMAIL);
clipboard_data += CMyString(cs_text);
clipboard_data += url;
}
if (!autotype.IsEmpty()) {
cs_text.LoadString(IDS_COPYAUTOTYPE);
clipboard_data += CMyString(cs_text);
clipboard_data += autotype;
}
if (!clipboard_data.IsEmpty())
SetClipboardData(clipboard_data);
else
ClearClipboardData();
UpdateAccessTime(ci_original);
}
void DboxMain::OnCopyURL()
{
if (SelItemOk() != TRUE)
return;
CItemData *ci = getSelectedItem();
ASSERT(ci != NULL);
CItemData *ci_original(ci);
if (ci->IsShortcut()) {
// This is an shortcut
uuid_array_t entry_uuid, base_uuid;
ci->GetUUID(entry_uuid);
m_core.GetShortcutBaseUUID(entry_uuid, base_uuid);
ItemListIter iter = m_core.Find(base_uuid);
if (iter != End()) {
ci = &iter->second;
}
}
const CMyString cs_URL = ci->GetURL();
if (!cs_URL.IsEmpty())
SetClipboardData(cs_URL);
else
ClearClipboardData();
UpdateAccessTime(ci_original);
}
void DboxMain::OnFind()
{
// Note that this "toggles" the Find Tool Bar so that the user can use Ctrl+F
// to show it and then hide it.
SetFindToolBar(!m_FindToolBar.IsVisible());
}
void DboxMain::OnClearClipboard()
{
ClearClipboardData();
}
void DboxMain::OnAutoType()
{
if (SelItemOk() == TRUE) {
CItemData *ci = getSelectedItem();
ASSERT(ci != NULL);
UpdateAccessTime(ci);
// All code using ci must be before this AutoType since the
// latter may trash *ci if lock-on-minimize
AutoType(*ci);
}
}
const CString DboxMain::DEFAULT_AUTOTYPE = _T("\\u\\t\\p\\n");
void DboxMain::AutoType(const CItemData &ci)
{
CMyString AutoCmd = ci.GetAutoType();
CMyString user(ci.GetUser());
CMyString pwd;
uuid_array_t base_uuid, entry_uuid;
CItemData::EntryType entrytype = ci.GetEntryType();
if (entrytype == CItemData::Alias || entrytype == CItemData::Shortcut) {
// This is an alias
ci.GetUUID(entry_uuid);
if (entrytype == CItemData::Alias)
m_core.GetAliasBaseUUID(entry_uuid, base_uuid);
else
m_core.GetShortcutBaseUUID(entry_uuid, base_uuid);
ItemListIter iter = m_core.Find(base_uuid);
if (iter != End()) {
pwd = iter->second.GetPassword();
if (entrytype == CItemData::Shortcut) {
user = iter->second.GetUser();
AutoCmd = iter->second.GetAutoType();
}
}
} else {
pwd = ci.GetPassword();
}
// If empty, try the database default
if (AutoCmd.IsEmpty()) {
AutoCmd = PWSprefs::GetInstance()->
GetPref(PWSprefs::DefaultAutotypeString);
// If still empty, take this default
if (AutoCmd.IsEmpty()) {
// checking for user and password for default settings
if (!pwd.IsEmpty()){
if (!user.IsEmpty())
AutoCmd = CMyString(DEFAULT_AUTOTYPE);
else
AutoCmd = _T("\\p\\n");
}
}
}
CKeySend ks;
// Turn off CAPSLOCK
bool bCapsLock = false;
if (GetKeyState(VK_CAPITAL)) {
bCapsLock = true;
ks.SetCapsLock(false);
}
CMyString tmp;
TCHAR curChar;
const int N = AutoCmd.GetLength();
ks.ResetKeyboardState();
::BlockInput(true);
// Note that minimizing the window before calling ci.Get*()
// will cause garbage to be read if "lock on minimize" selected,
// since that will clear the data [Bugs item #1026630]
// (this is why we read user & pwd before actual use)
// Rules are ("Minimize on Autotype" takes precedence):
// 1. If "MinimizeOnAutotype" - minimize PWS during Autotype but do
// not restore it (previous default action - but a pain if locked
// in the system tray!)
// 2. If "Always on Top" - hide PWS during Autotype and then make it
// "AlwaysOnTop" again, unless minimized!
// 3. If not "Always on Top" - hide PWS during Autotype and show
// it again once finished - but behind other windows.
bool bMinOnAuto = PWSprefs::GetInstance()->
GetPref(PWSprefs::MinimizeOnAutotype) == TRUE;
if (bMinOnAuto)
ShowWindow(SW_MINIMIZE);
else
ShowWindow(SW_HIDE);
Sleep(1000); // Karl Student's suggestion, to ensure focus set correctly on minimize.
for(int n = 0; n < N; n++){
curChar = AutoCmd[n];
if(curChar == TCHAR('\\')) {
n++;
if(n < N)
curChar=AutoCmd[n];
switch(curChar){
case TCHAR('\\'):
tmp += TCHAR('\\');
break;
case TCHAR('n'):
case TCHAR('r'):
tmp += TCHAR('\r');
break;
case TCHAR('t'):
tmp += TCHAR('\t');
break;
case TCHAR('u'):
tmp += user;
break;
case TCHAR('p'):
tmp += pwd;
break;
case TCHAR('d'):
{
// Delay is going to change - send what we have with old delay
ks.SendString(tmp);
// start collecting new delay
tmp = _T("");
int newdelay = 0;
int gNumIts = 0;
for (n++; n < N && (gNumIts < 3); ++gNumIts, n++) {
if(isdigit(AutoCmd[n])){
newdelay *= 10;
newdelay += (AutoCmd[n] - TCHAR('0'));
} else
break; // for loop
}
n--;
ks.SetAndDelay(newdelay);
break; // case
}
default:
tmp += _T("\\") + curChar;
break;
}
} else
tmp += curChar;
}
ks.SendString(tmp);
// If we turned off CAPSLOCK, put it back
if (bCapsLock)
ks.SetCapsLock(true);
Sleep(100);
::BlockInput(false);
// If we hid it, now show it
if (bMinOnAuto)
return;
if (PWSprefs::GetInstance()->GetPref(PWSprefs::AlwaysOnTop)) {
SetWindowPos(&wndTopMost, 0, 0, 0, 0,
SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
} else {
SetWindowPos(&wndBottom, 0, 0, 0, 0,
SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
}
}
void DboxMain::AddEntries(CDDObList &in_oblist, const CMyString &DropGroup)
{
// Add Drop entries
CItemData tempitem;
UUIDList possible_aliases, possible_shortcuts;
CMyString Group, Title, User;
POSITION pos;
TCHAR *dot;
uuid_array_t entry_uuid;
bool bAddToViews;
for (pos = in_oblist.GetHeadPosition(); pos != NULL; in_oblist.GetNext(pos)) {
CDDObject *pDDObject = (CDDObject *)in_oblist.GetAt(pos);
#ifdef DEMO
if (m_core.GetNumEntries() >= MAXDEMO)
break;
#endif /* DEMO */
tempitem.Clear();
// Only set to false if adding a shortcut where the base isn't there (yet)
bAddToViews = true;
pDDObject->ToItem(tempitem);
if (in_oblist.m_bDragNode) {
dot = (!DropGroup.IsEmpty() && !tempitem.GetGroup().IsEmpty()) ? _T(".") : _T("");
Group = DropGroup + dot + tempitem.GetGroup();
} else {
Group = DropGroup;
}
User = tempitem.GetUser();
Title = GetUniqueTitle(Group, tempitem.GetTitle(), User, IDS_DRAGNUMBER);
tempitem.GetUUID(entry_uuid);
if (m_core.Find(entry_uuid) != End()) {
// Already in use - get a new one!
tempitem.CreateUUID();
tempitem.GetUUID(entry_uuid);
}
tempitem.SetGroup(Group);
tempitem.SetTitle(Title);
CMyString cs_tmp = tempitem.GetPassword();
GetBaseEntryPL pl;
pl.InputType = CItemData::Normal;
// Potentially remove outer single square brackets as GetBaseEntry expects only
// one set of square brackets (processing import and user edit of entries)
if (cs_tmp.Left(2) == _T("[[") && cs_tmp.Right(2) == _T("]]")) {
cs_tmp = cs_tmp.Mid(1, cs_tmp.GetLength() - 2);
pl.InputType = CItemData::Alias;
}
// Potentially remove tilde as GetBaseEntry expects only
// one set of square brackets (processing import and user edit of entries)
if (cs_tmp.Left(2) == _T("[~") && cs_tmp.Right(2) == _T("~]")) {
cs_tmp = _T("[") + cs_tmp.Mid(2, cs_tmp.GetLength() - 4) + _T("]");
pl.InputType = CItemData::Shortcut;
}
m_core.GetBaseEntry(cs_tmp, pl);
if (pl.ibasedata > 0) {
// Password in alias/shortcut format AND base entry exists
if (pl.InputType == CItemData::Alias) {
ItemListIter iter = m_core.Find(pl.base_uuid);
ASSERT(iter != End());
if (pl.TargetType == CItemData::Alias) {
// This base is in fact an alias. GetBaseEntry already found 'proper base'
// So dropped entry will point to the 'proper base' and tell the user.
CString cs_msg;
cs_msg.Format(IDS_DDBASEISALIAS, Group, Title, User);
AfxMessageBox(cs_msg, MB_OK);
} else
if (pl.TargetType != CItemData::Normal && pl.TargetType != CItemData::AliasBase) {
// Only normal or alias base allowed as target
CString cs_msg;
cs_msg.Format(IDS_ABASEINVALID, Group, Title, User);
AfxMessageBox(cs_msg, MB_OK);
continue;
}
m_core.AddDependentEntry(pl.base_uuid, entry_uuid, CItemData::Alias);
tempitem.SetPassword(CMyString(_T("[Alias]")));
tempitem.SetAlias();
} else
if (pl.InputType == CItemData::Shortcut) {
ItemListIter iter = m_core.Find(pl.base_uuid);
ASSERT(iter != End());
if (pl.TargetType != CItemData::Normal && pl.TargetType != CItemData::ShortcutBase) {
// Only normal or shortcut base allowed as target
CString cs_msg;
cs_msg.Format(IDS_SBASEINVALID, Group, Title, User);
AfxMessageBox(cs_msg, MB_OK);
continue;
}
m_core.AddDependentEntry(pl.base_uuid, entry_uuid, CItemData::Shortcut);
tempitem.SetPassword(CMyString(_T("[Shortcut]")));
tempitem.SetShortcut();
}
} else
if (pl.ibasedata == 0) {
// Password NOT in alias/shortcut format
tempitem.SetNormal();
} else
if (pl.ibasedata < 0) {
// Password in alias/shortcut format AND base entry does not exist or multiple possible
// base entries exit.
// Note: As more entries are added, what was "not exist" may become "OK",
// "no unique exists" or "multiple exist".
// Let the code that processes the possible aliases after all have been added sort this out.
if (pl.InputType == CItemData::Alias) {
possible_aliases.push_back(entry_uuid);
} else
if (pl.InputType == CItemData::Shortcut) {
possible_shortcuts.push_back(entry_uuid);
bAddToViews = false;
}
}
if (bAddToViews) {
// Add to pwlist + Tree + List views
AddEntry(tempitem);
} else {
// ONLY Add to pwlist and NOT to Tree or List views
// After the call to AddDependentEntries for shortcuts, check if still
// in password list and, if so, then add to Tree + List views
m_core.AddEntry(tempitem);
}
} // iteration over in_oblist
// Now try to add aliases/shortcuts we couldn't add in previous processing
m_core.AddDependentEntries(possible_aliases, NULL, CItemData::Alias,
CItemData::PASSWORD);
m_core.AddDependentEntries(possible_shortcuts, NULL, CItemData::Shortcut,
CItemData::PASSWORD);
possible_aliases.clear();
// Some shortcuts may have been deleted from the database as base does not exist
// Tidy up Tree/List
UUIDListIter paiter;
ItemListIter iter;
for (paiter = possible_shortcuts.begin();
paiter != possible_shortcuts.end(); paiter++) {
paiter->GetUUID(entry_uuid);
iter = m_core.Find(entry_uuid);
if (iter != End()) {
// Still in pwlist - NOW add to Tree and List views
insertItem(m_core.GetEntry(iter));
}
}
possible_shortcuts.clear();
if (PWSprefs::GetInstance()->GetPref(PWSprefs::SaveImmediately)) {
Save();
ChangeOkUpdate();
}
FixListIndexes();
RefreshViews();
}
// Return whether first [g:t:u] is greater than the second [g:t:u]
// used in std::sort in SortDependents below.
bool GTUCompare(CMyString elem1, CMyString elem2)
{
CMyString g1, t1, u1, g2, t2, u2, tmp1, tmp2;
g1 = elem1.SpanExcluding(_T(":"));
g2 = elem2.SpanExcluding(_T(":"));
if (g1 != g2)
return g1.Compare(g2) < 0;
tmp1 = elem1.Mid(g1.GetLength() + 1);
tmp2 = elem2.Mid(g2.GetLength() + 1);
t1 = tmp1.SpanExcluding(_T(":"));
t2 = tmp2.SpanExcluding(_T(":"));
if (t1 != t2)
return t1.Compare(t2) < 0;
tmp1 = tmp1.Mid(t1.GetLength() + 1);
tmp2 = tmp2.Mid(t2.GetLength() + 1);
u1 = tmp1.SpanExcluding(_T(":"));
u2 = tmp2.SpanExcluding(_T(":"));
return u1.Compare(u2) < 0;
}
void DboxMain::SortDependents(UUIDList &dlist, CMyString &csDependents)
{
std::vector<CMyString> sorted_dependents;
std::vector<CMyString>::iterator sd_iter;
ItemListIter iter;
UUIDListIter diter;
CMyString cs_dependent;
for (diter = dlist.begin(); diter != dlist.end(); diter++) {
uuid_array_t dependent_uuid;
diter->GetUUID(dependent_uuid);
iter = m_core.Find(dependent_uuid);
if (iter != m_core.GetEntryEndIter()) {
cs_dependent = iter->second.GetGroup() + _T(":") +
iter->second.GetTitle() + _T(":") +
iter->second.GetUser();
sorted_dependents.push_back(cs_dependent);
}
}
std::sort(sorted_dependents.begin(), sorted_dependents.end(), GTUCompare);
csDependents.Empty();
for (sd_iter = sorted_dependents.begin(); sd_iter != sorted_dependents.end(); sd_iter++) {
csDependents += _T("\t[") + *sd_iter + _T("]\r\n");
}
}
LRESULT DboxMain::OnToolBarFindMessage(WPARAM /* wParam */, LPARAM /* lParam */)
{
// Called when user types into the Find search edit control on the Find Toolbar
// and presses enter.
OnToolBarFind();
return 0L;
}
void DboxMain::OnToolBarFind()
{
// Called when the user presses the Find button on the Find Toolbar or
// when they press enter when the search string has focus or
// when they press F3 with the Find Toolbar visible
if (m_FindToolBar.IsVisible())
m_FindToolBar.Find();
}
bool DboxMain::CheckNewPassword(const CMyString &group, const CMyString &title,
const CMyString &user, const CMyString &password,
const bool bIsEdit, const CItemData::EntryType &InputType,
uuid_array_t &base_uuid, int &ibasedata, bool &b_msg_issued)
{
// bmsgissued - whether this routine issued a message
b_msg_issued = false;
// Called from Add and Edit entry
// Returns false if not a special alias or shortcut password
GetBaseEntryPL pl;
pl.InputType = InputType;
bool brc = m_core.GetBaseEntry(password, pl);
// Copy data back before possibly returning
ibasedata = pl.ibasedata;
memcpy(base_uuid, pl.base_uuid, sizeof(uuid_array_t));
if (!brc)
return false;
// if we ever return 'false', this routine will have issued a message to the user
b_msg_issued = true;
if (bIsEdit &&
(pl.csPwdGroup == group && pl.csPwdTitle == title && pl.csPwdUser == user)) {
// In Edit, check user isn't changing entry to point to itself (circular/self reference)
// Can't happen during Add as already checked entry does not exist so if accepted the
// password would be treated as an unusal "normal" password
AfxMessageBox(IDS_ALIASCANTREFERTOITSELF, MB_OK);
return false;
}
// ibasedata:
// +n: password contains (n-1) colons and base entry found (n = 1, 2 or 3)
// 0: password not in alias format
// -n: password contains (n-1) colons but base entry NOT found (n = 1, 2 or 3)
// "bMultipleEntriesFound" is set if no "unique" base entry could be found and is only valid if n = -1 or -2.
if (pl.ibasedata < 0) {
if (InputType == CItemData::Shortcut) {
// Target must exist (unlike for aliases where it could be an unusual password)
if (pl.bMultipleEntriesFound)
AfxMessageBox(IDS_MULTIPLETARGETSFOUND, MB_OK);
else
AfxMessageBox(IDS_TARGETNOTFOUND, MB_OK);
return false;
}
CString cs_msg;
const CString cs_msgA(MAKEINTRESOURCE(IDS_ALIASNOTFOUNDA));
const CString cs_msgZ(MAKEINTRESOURCE(IDS_ALIASNOTFOUNDZ));
int rc(IDNO);
switch (pl.ibasedata) {
case -1: // [t] - must be title as this is the only mandatory field
if (pl.bMultipleEntriesFound)
cs_msg.Format(IDS_ALIASNOTFOUND0A, pl.csPwdTitle); // multiple entries exist with title=x
else
cs_msg.Format(IDS_ALIASNOTFOUND0B, pl.csPwdTitle); // no entry exists with title=x
rc = AfxMessageBox(cs_msgA + cs_msg + cs_msgZ, MB_YESNO | MB_DEFBUTTON2);
break;
case -2: // [g,t], [t:u]
// In this case the 2 fields from the password are in Group & Title
if (pl.bMultipleEntriesFound)
cs_msg.Format(IDS_ALIASNOTFOUND1A, pl.csPwdGroup, pl.csPwdTitle, pl.csPwdGroup, pl.csPwdTitle);
else
cs_msg.Format(IDS_ALIASNOTFOUND1B, pl.csPwdGroup, pl.csPwdTitle, pl.csPwdGroup, pl.csPwdTitle);
rc = AfxMessageBox(cs_msgA + cs_msg + cs_msgZ, MB_YESNO | MB_DEFBUTTON2);
break;
case -3: // [g:t:u], [g:t:], [:t:u], [:t:] (title cannot be empty)
{
const bool bGE = pl.csPwdGroup.IsEmpty() == TRUE;
const bool bTE = pl.csPwdTitle.IsEmpty() == TRUE;
const bool bUE = pl.csPwdUser.IsEmpty() == TRUE;
if (bTE) {
// Title is mandatory for all entries!
AfxMessageBox(IDS_BASEHASNOTITLE, MB_OK);
rc = IDNO;
break;
} else if (!bGE && !bUE) // [x:y:z]
cs_msg.Format(IDS_ALIASNOTFOUND2A, pl.csPwdGroup, pl.csPwdTitle, pl.csPwdUser);
else if (!bGE && bUE) // [x:y:]
cs_msg.Format(IDS_ALIASNOTFOUND2B, pl.csPwdGroup, pl.csPwdTitle);
else if (bGE && !bUE) // [:y:z]
cs_msg.Format(IDS_ALIASNOTFOUND2C, pl.csPwdTitle, pl.csPwdUser);
else if (bGE && bUE) // [:y:]
cs_msg.Format(IDS_ALIASNOTFOUND0B, pl.csPwdTitle);
rc = AfxMessageBox(cs_msgA + cs_msg + cs_msgZ, MB_YESNO | MB_DEFBUTTON2);
break;
}
default:
// Never happens
ASSERT(0);
}
if (rc == IDNO)
return false;
}
if (pl.ibasedata > 0) {
if (pl.TargetType == CItemData::Alias) {
// If user tried to point to an alias -> change to point to the 'real' base
CString cs_msg;
cs_msg.Format(IDS_BASEISALIAS, pl.csPwdGroup, pl.csPwdTitle, pl.csPwdUser);
if (AfxMessageBox(cs_msg, MB_YESNO | MB_DEFBUTTON2) == IDNO) {
return false;
}
} else {
if (pl.TargetType != CItemData::Normal && pl.TargetType != CItemData::AliasBase) {
// An alias can only point to a normal entry or an alias base entry
CString cs_msg;
cs_msg.Format(IDS_ABASEINVALID, pl.csPwdGroup, pl.csPwdTitle, pl.csPwdUser);
AfxMessageBox(cs_msg, MB_OK);
return false;
} else {
return true;
}
}
}
// All OK
return true;
}