Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[02c82b]: src / ui / Windows / ThisMfcApp.cpp Maximize Restore History

Download this file

ThisMfcApp.cpp    1518 lines (1293 with data), 48.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
/*
* Copyright (c) 2003-2013 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 ThisMfcApp.cpp
/// \brief App object of MFC version of Password Safe
//-----------------------------------------------------------------------------
/*
* The code for checking on multiple instances comes from the article
* "Avoiding Multiple Instances of an Application" by
* Joseph M. Newcomer [MVP]; http://www.flounder.com/nomultiples.htm
*/
#include "PasswordSafe.h"
#include "resource.h"
#include "resource2.h" // Menu, Toolbar & Accelerator resources
#include "resource3.h" // String resources
#include "ThisMfcApp.h"
#include "DboxMain.h"
#include "SingleInstance.h"
#include "CryptKeyEntry.h"
#include "PWSRecentFileList.h"
#include "MFCMessages.h"
#include "GeneralMsgBox.h"
#include "PWSFaultHandler.h"
#include "Fonts.h"
#include "PWSversion.h"
#include "core/Util.h"
#include "core/BlowFish.h"
#include "core/PWSprefs.h"
#include "core/PWSrand.h"
#include "core/PWSdirs.h"
#include "core/SysInfo.h"
#include "core/PWSLog.h"
#include "os/windows/pws_autotype/pws_at.h"
#include "os/dir.h"
#include "os/file.h"
#include "os/env.h"
#include "os/lib.h"
#include "Shlwapi.h"
#include <vector>
#include <errno.h>
#include <io.h>
// Only produce minidumps in release code
#ifndef _DEBUG
#include "Dbghelp.h"
#endif
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define UNIQUE_PWS_GUID L"PasswordSafe-{3FE0D665-1AE6-49b2-8359-326407D56470}"
const UINT ThisMfcApp::m_uiRegMsg = RegisterWindowMessage(UNIQUE_PWS_GUID);
const UINT ThisMfcApp::m_uiWH_SHELL = RegisterWindowMessage(UNIQUE_PWS_SHELL);
BEGIN_MESSAGE_MAP(ThisMfcApp, CWinApp)
END_MESSAGE_MAP()
static MFCReporter aReporter;
static MFCAsker anAsker;
#ifndef _DEBUG
extern wchar_t *wcRevision;
extern wchar_t *wcMsg1;
extern wchar_t *wcMsg2;
extern wchar_t *wcMsg3;
extern wchar_t *wcCaption;
#endif
ThisMfcApp::ThisMfcApp() :
m_bUseAccelerator(true),
m_pMRU(NULL), m_TrayLockedState(LOCKED), m_pTrayIcon(NULL),
m_HotKeyPressed(false), m_hMutexOneInstance(NULL),
m_ghAccelTable(NULL), m_pMainMenu(NULL),
m_bACCEL_Table_Created(false), m_noSysEnvWarnings(false),
m_bPermitTestdump(false), m_hInstResDLL(NULL), m_ResLangID(0),
m_pMRUMenu(NULL)
{
// Get my Thread ID
m_nBaseThreadID = AfxGetThread()->m_nThreadID;
// Get application version information
GetApplicationVersionData();
// Only produce minidumps in release code
#ifndef _DEBUG
// ONLY add minidump generation to Release versions!
HMODULE module = GetModuleHandle(NULL);
DWORD timestamp = GetTimestampForLoadedLibrary(module);
DWORD dwMajorMinor = GetFileVersionMajorMinor();
DWORD dwBuildRevision = GetFileVersionBuildRevision();
int iMajor(0), iMinor(0), iBuild(0);
if (dwMajorMinor > 0) {
iMajor = HIWORD(dwMajorMinor);
iMinor = LOWORD(dwMajorMinor);
iBuild = HIWORD(dwBuildRevision);
}
CString csFileVersionString, csRevision(L"");
csFileVersionString = GetFileVersionString();
int revIndex = csFileVersionString.ReverseFind(L',');
if (revIndex >= 0) {
int len = csFileVersionString.GetLength();
csRevision = csFileVersionString.Right(len - revIndex - 1);
csRevision.Trim();
}
LPTSTR revision = csRevision.GetBuffer(csRevision.GetLength() + 1);
InstallFaultHandler(iMajor, iMinor, iBuild, revision, timestamp);
csRevision.ReleaseBuffer();
#endif
// Set this process to be one of the first to be shut down:
SetProcessShutdownParameters(0x3ff, 0);
PWSprefs::SetReporter(&aReporter);
PWScore::SetReporter(&aReporter);
PWScore::SetAsker(&anAsker);
EnableHtmlHelp();
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); // Initializes the COM library
// (for XML and Yubikeyprocessing)
AfxEnableControlContainer();
AfxOleInit();
}
ThisMfcApp::~ThisMfcApp()
{
delete m_pMRU;
// Remove MRU menu
if (m_pMRUMenu) {
m_pMRUMenu->DestroyMenu();
delete m_pMRUMenu;
m_pMRUMenu = NULL;
}
// Remove main menu
if (m_pMainMenu) {
m_pMainMenu->DestroyMenu();
delete m_pMainMenu;
}
// Rules state - if create then must destroy
if (m_bACCEL_Table_Created && app.m_ghAccelTable != NULL)
DestroyAcceleratorTable(app.m_ghAccelTable);
// Alhough the system will do this automatically - I like to be clean!
CloseHandle(m_hMutexOneInstance);
PWSprefs::DeleteInstance();
PWSrand::DeleteInstance();
PWSversion::DeleteInstance();
Fonts::GetInstance()->DeleteInstance();
PWSLog::DeleteLog();
CoUninitialize(); // Uninitialize COM library
// WinApp::HtmlHelp asserts that main windows is valid, which (1) isn't true
// here, and (2) is irrelevant for HH_CLOSE_ALL, so we call ::HtmlHelp
::HtmlHelp(NULL, NULL, HH_CLOSE_ALL, 0);
#ifndef _DEBUG
RemoveFaultHandler();
#endif
}
static void Usage()
{
CGeneralMsgBox gmb;
gmb.AfxMessageBox(IDS_USAGE);
}
// tests if file exists, returns true if so, displays error message if not
static bool CheckFile(const CString &fn)
{
DWORD status = ::GetFileAttributes(fn);
CString cs_msg(L"");
if (status == INVALID_FILE_ATTRIBUTES) {
cs_msg.Format(IDS_FILEERROR1, fn);
} else if (status & FILE_ATTRIBUTE_DIRECTORY) {
cs_msg.Format(IDS_FILEERROR2, fn);
}
if (cs_msg.IsEmpty()) {
return true;
} else {
CGeneralMsgBox gmb;
gmb.AfxMessageBox(cs_msg);
return false;
}
}
void ThisMfcApp::SetMinidumpUserStreams(const bool bOpen, const bool bRW, UserStream iStream)
{
#ifndef _DEBUG
PWSprefs *prefs = PWSprefs::GetInstance();
if (prefs != NULL) {
PopulateMinidumpUserStreams(prefs, bOpen, bRW, iStream);
}
#else
UNREFERENCED_PARAMETER(bOpen);
UNREFERENCED_PARAMETER(bRW);
UNREFERENCED_PARAMETER(iStream);
#endif
}
int ThisMfcApp::ExitInstance()
{
if (m_hInstResDLL != m_hInstance)
pws_os::FreeLibrary(m_hInstResDLL);
CWinApp::ExitInstance();
return 0;
}
// Listener window whose sole purpose in life is to receive
// the "AppStop" message from the U3 framework and then
// cause a semi-graceful exit.
class CLWnd : public CWnd
{
public:
CLWnd(DboxMain &dbox) : m_dbox(dbox) {}
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message != (WM_APP+0x765))
return CWnd::WindowProc(message, wParam, lParam);
else
m_dbox.U3ExitNow();
return 0L;
}
private:
DboxMain &m_dbox;
};
static void GetVersionInfoFromFile(const CString &csFileName,
DWORD &MajorMinor, DWORD &BuildRevision)
{
MajorMinor = BuildRevision = DWORD(-1);
DWORD dwVerHnd, dwVerInfoSize;
// Get version information from the given file
dwVerInfoSize = ::GetFileVersionInfoSize((LPWSTR)(LPCWSTR)csFileName, &dwVerHnd);
if (dwVerInfoSize > 0) {
char* pVersionInfo = new char[dwVerInfoSize];
if (pVersionInfo != NULL) {
BOOL bRet = ::GetFileVersionInfo((LPWSTR)(LPCWSTR)csFileName,
(DWORD)dwVerHnd, (DWORD)dwVerInfoSize, (LPVOID)pVersionInfo);
if (bRet) {
// get binary file version information
VS_FIXEDFILEINFO *szVer = NULL;
UINT uVerLength;
bRet = ::VerQueryValue(pVersionInfo, TEXT("\\"), (LPVOID*)&szVer, &uVerLength);
if (bRet) {
MajorMinor = szVer->dwProductVersionMS;
BuildRevision = szVer->dwProductVersionLS;
}
}
}
delete[] pVersionInfo;
}
}
void ThisMfcApp::LoadLocalizedStuff()
{
/*
Looks for localized version of resources and help files, loads them if found
(Also tries to setup on-screen keyboard)
Format of resource-only DLL names (in dir returned by GetExeDir)
pwsafeLL_CC.dll
or
pwsafeLL.dll
where LL = ISO 639-1 two-character Language code e.g. EN, FR, DE, HE...
see http://www.loc.gov/standards/iso639-2/
and CC = ISO 3166-1 two-character Country code e.g. US, GB, FR, CA...
see http://www.iso.org/iso/en/prods-services/iso3166ma/index.html
Although ISO 639 has been superceded, MS only supports the new RFC 3066bis in
.NET V2 and later applications (CultureInfo Class) or under Vista (via LOCALE_SNAME).
Older native and .NET V1 applications only support the ISO 639-1 two character
language codes.
Search order will be pwsafeLL_CC.dll, followed by pwsafeLL.dll. If neither exist or
can't be found, the resources embedded in the executable pwsafe.exe will be used
(US English i.e. equivalent to pwsafeEN_US.dll)).
Likewise, we will look for localized versions of pwsafe.chm in GetHelpDir,
defaulting to pwsafe.chm if not found.
*/
CString cs_ResPath, cs_HelpPath, cs_LANG(L""), cs_CTRY(L"");
const CString cs_HelpDir(PWSdirs::GetHelpDir().c_str());
const CString cs_ExePath(PWSdirs::GetExeDir().c_str());
// See if user overridden default via Environmental Variable
bool bPLRC(FALSE);
// See if user has set preference instead.
StringX sxLL = PWSprefs::GetInstance()->GetPref(PWSprefs::LanguageFile);
if (!sxLL.empty()) {
// Preferences trumps Environmental Variable!!!!!
size_t len = sxLL.length();
ASSERT(len == 2 || len == 5);
if (len == 2) {
cs_LANG = sxLL.c_str();
} else {
cs_LANG = sxLL.substr(0,2).c_str();
cs_CTRY = sxLL.substr(3,2).c_str();
}
bPLRC = true;
}
if (cs_LANG == L"EN" && cs_CTRY.IsEmpty()) {
// This means use the embedded resources!
pws_os::FreeLibrary(m_hInstResDLL);
m_hInstResDLL = NULL;
pws_os::Trace(L"Using embedded resources\n");
// and supplied Help file
free((void *)m_pszHelpFilePath);
cs_HelpPath.Format(L"%spwsafe.chm", cs_HelpDir);
if (PathFileExists(cs_HelpPath)) {
m_pszHelpFilePath = _wcsdup(cs_HelpPath);
m_csHelpFile = cs_HelpPath;
pws_os::Trace(L"Using help file: %s\n", cs_HelpPath);
} else {
m_pszHelpFilePath = NULL;
m_csHelpFile = L"";
pws_os::Trace(L"Not using a help file!\n");
}
return;
}
if (!bPLRC) {
// no override, use Locale info
int inum;
wchar_t szLang[4], szCtry[4];
inum = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, szLang, 4);
ASSERT(inum == 3);
_wcsupr_s(szLang, 4);
inum = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, szCtry, 4);
ASSERT(inum == 3);
cs_LANG = szLang; cs_CTRY = szCtry;
}
// Find reseource-only DLL if requested
const CString format_string = (cs_CTRY.IsEmpty()) ?
_T("pwsafe%s%s.dll") : _T("pwsafe%s_%s.dll");
cs_ResPath.Format(format_string, cs_LANG, cs_CTRY);
m_hInstResDLL = HMODULE(pws_os::LoadLibrary(LPCTSTR(cs_ResPath),
pws_os::LOAD_LIBRARY_APP));
if (m_hInstResDLL == NULL && !cs_CTRY.IsEmpty()) {
// Now try base
cs_ResPath.Format(_T("pwsafe%s.dll"), cs_LANG);
m_hInstResDLL = HMODULE(pws_os::LoadLibrary(LPCTSTR(cs_ResPath),
pws_os::LOAD_LIBRARY_APP));
}
if (m_hInstResDLL == NULL) {
pws_os::Trace(L"Could not load language DLLs - using embedded resources.\n");
} else { // successfully loaded a resource dll, check version
DWORD dw_fileMajorMinor, dw_fileBuildRevision;
GetVersionInfoFromFile(cs_ResPath, dw_fileMajorMinor, dw_fileBuildRevision);
if (dw_fileMajorMinor != m_dwMajorMinor) { // ignore build for now
CGeneralMsgBox gmb;
CString oops;
oops.Format(L"Executable/language DLL (%s) version mismatch %d.%d.%d/%d.%d.%d.\n",
cs_ResPath,
HIWORD(dw_fileMajorMinor), LOWORD(dw_fileMajorMinor), HIWORD(dw_fileBuildRevision),
HIWORD(m_dwMajorMinor), LOWORD(m_dwMajorMinor), HIWORD(m_dwBuildRevision));
gmb.AfxMessageBox(oops);
pws_os::FreeLibrary(m_hInstResDLL);
m_hInstResDLL = NULL;
} else { // Passed version check
pws_os::Trace(L"Using language DLL '%s'.\n", cs_ResPath);
GetDLLVersionData(cs_ResPath, m_ResLangID);
}
} // end of resource dll hunt
if (m_hInstResDLL != NULL)
AfxSetResourceHandle(m_hInstResDLL);
// Find requested Help file
bool helpFileFound = false;
if (!cs_LANG.IsEmpty() && !cs_CTRY.IsEmpty()) {
cs_HelpPath.Format(L"%spwsafe%s_%s.chm", cs_HelpDir, cs_LANG, cs_CTRY);
if (PathFileExists(cs_HelpPath)) {
helpFileFound = true;
}
}
if (!helpFileFound && !cs_LANG.IsEmpty()) {
cs_HelpPath.Format(L"%spwsafe%s.chm", cs_HelpDir, cs_LANG);
if (PathFileExists(cs_HelpPath)) {
helpFileFound = true;
}
}
if (!helpFileFound) {
cs_HelpPath.Format(L"%spwsafe.chm", cs_HelpDir);
if (PathFileExists(cs_HelpPath)) {
helpFileFound = true;
}
}
if (!helpFileFound && m_pszHelpFilePath != NULL && _tcslen(m_pszHelpFilePath) > 0) { // last resort
wchar_t fname[_MAX_FNAME];
wchar_t ext[_MAX_EXT];
#if (_MSC_VER >= 1400)
_wsplitpath_s(m_pszHelpFilePath, NULL, 0, NULL, 0, fname,
_MAX_FNAME, ext, _MAX_EXT);
_wcslwr_s(fname, _MAX_FNAME);
_wcslwr_s(ext, _MAX_EXT);
#else
_wsplitpath(m_pszHelpFilePath, NULL, NULL, fname, ext);
_wcslwr(ext);
_wcslwr(fname);
#endif
cs_HelpPath.Format(L"%s%s", fname, ext);
}
if (helpFileFound) {
free((void*)m_pszHelpFilePath);
m_pszHelpFilePath = _wcsdup(cs_HelpPath);
m_csHelpFile = cs_HelpPath;
pws_os::Trace(L"Using help file: %s\n", cs_HelpPath);
}
}
void ThisMfcApp::SetupMenu()
{
// Remove MRU menu
if (m_pMRUMenu) {
m_pMRUMenu->DestroyMenu();
delete m_pMRUMenu;
m_pMRUMenu = NULL;
}
// Remove Main menu
if (m_pMainMenu) {
m_pMainMenu->DestroyMenu();
delete m_pMainMenu;
m_pMainMenu = NULL;
}
//Reload accelerators for current locale
if (app.m_ghAccelTable)
DestroyAcceleratorTable(app.m_ghAccelTable);
app.m_ghAccelTable=LoadAccelerators(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_ACCS));
int nMRUItems = PWSprefs::GetInstance()->GetPref(PWSprefs::MaxMRUItems);
m_mruonfilemenu = PWSprefs::GetInstance()->GetPref(PWSprefs::MRUOnFileMenu);
m_pMainMenu = new CMenu;
m_pMainMenu->LoadMenu(IDR_MAINMENU);
m_pMRUMenu = new CMenu;
MENUINFO minfo = {0};
minfo.cbSize = sizeof(MENUINFO);
minfo.fMask = MIM_MENUDATA;
// Menu position indices (File & Close).
int pos1, pos2;
CMenu *pMenu1, *pMenu2;
// Look for "File" menu.
pos1 = app.FindMenuItem(m_pMainMenu, ID_FILEMENU);
ASSERT(pos1 != -1);
pMenu1 = m_pMainMenu->GetSubMenu(pos1);
minfo.dwMenuData = ID_FILEMENU;
if (pMenu1 != NULL) { // Look for "Close Database"
pMenu1->SetMenuInfo(&minfo);
pos2 = FindMenuItem(pMenu1, ID_MENUITEM_CLOSE);
}
else
pos2 = -1;
if (m_pMRU != NULL) {
delete m_pMRU;
m_pMRU = NULL;
}
m_pMRU = new CPWSRecentFileList(0, L"MRU", L"Safe%d",
((nMRUItems != 0) ? nMRUItems : 1));
if (nMRUItems > 0) {
if (pos2 > -1) {
int irc;
// Create New Popup Menu
m_pMRUMenu->CreatePopupMenu();
CString cs_recent(MAKEINTRESOURCE(IDS_RECENT));
CString cs_recentsafes(MAKEINTRESOURCE(IDS_RECENTSAFES));
if (!m_mruonfilemenu) { // MRU entries in popup menu
// Insert Item onto new popup
irc = m_pMRUMenu->InsertMenu(0, MF_BYPOSITION,
ID_FILE_MRU_ENTRY1, cs_recent);
ASSERT(irc != 0);
// Insert Popup onto main menu
ASSERT(pMenu1 != NULL);
irc = pMenu1->InsertMenu(pos2 + 2,
MF_BYPOSITION | MF_POPUP,
UINT_PTR(m_pMRUMenu->m_hMenu),
cs_recentsafes);
ASSERT(irc != 0);
} else { // MRU entries inline
ASSERT(pMenu1 != NULL);
irc = pMenu1->InsertMenu(pos2 + 2, MF_BYPOSITION,
ID_FILE_MRU_ENTRY1, cs_recent);
ASSERT(irc != 0);
} // m_mruonfilemenu
m_pMRU->ReadList();
} // pos > -1
} else { // nMRUItems <= 0
if (pos2 > -1) {
int irc;
// Remove extra separator
ASSERT(pMenu1 != NULL);
irc = pMenu1->RemoveMenu(pos2 + 1, MF_BYPOSITION);
ASSERT(irc != 0);
// Remove Clear MRU menu item.
irc = pMenu1->RemoveMenu(ID_MENUITEM_CLEAR_MRU, MF_BYCOMMAND);
ASSERT(irc != 0);
}
}
// Do File Menu Export submenu
pos2 = app.FindMenuItem(pMenu1, ID_EXPORTMENU);
ASSERT(pos2 != -1);
pMenu2 = pMenu1->GetSubMenu(pos2);
minfo.dwMenuData = ID_EXPORTMENU;
pMenu2->SetMenuInfo(&minfo);
// Do File Menu Import submenu
pos2 = app.FindMenuItem(pMenu1, ID_IMPORTMENU);
ASSERT(pos2 != -1);
pMenu2 = pMenu1->GetSubMenu(pos2);
minfo.dwMenuData = ID_IMPORTMENU;
pMenu2->SetMenuInfo(&minfo);
// Do Edit Menu
pos1 = app.FindMenuItem(m_pMainMenu, ID_EDITMENU);
ASSERT(pos1 != -1);
pMenu1 = m_pMainMenu->GetSubMenu(pos1);
minfo.dwMenuData = ID_EDITMENU;
pMenu1->SetMenuInfo(&minfo);
// Do Edit Menu Export Entry submenu
pos2 = app.FindMenuItem(pMenu1, ID_EXPORTENTMENU);
ASSERT(pos2 != -1);
pMenu2 = pMenu1->GetSubMenu(pos2);
minfo.dwMenuData = ID_EXPORTENTMENU;
pMenu2->SetMenuInfo(&minfo);
// Do View Menu
pos1 = app.FindMenuItem(m_pMainMenu, ID_VIEWMENU);
ASSERT(pos1 != -1);
pMenu1 = m_pMainMenu->GetSubMenu(pos1);
minfo.dwMenuData = ID_VIEWMENU;
pMenu1->SetMenuInfo(&minfo);
// Do View Menu Filter submenu
pos2 = app.FindMenuItem(pMenu1, ID_FILTERMENU);
ASSERT(pos2 != -1);
pMenu2 = pMenu1->GetSubMenu(pos2);
minfo.dwMenuData = ID_FILTERMENU;
pMenu2->SetMenuInfo(&minfo);
// Do View Menu ChangeFont submenu
pos2 = app.FindMenuItem(pMenu1, ID_CHANGEFONTMENU);
ASSERT(pos2 != -1);
pMenu2 = pMenu1->GetSubMenu(pos2);
minfo.dwMenuData = ID_CHANGEFONTMENU;
pMenu2->SetMenuInfo(&minfo);
// Do View Menu Reports submenu
pos2 = app.FindMenuItem(pMenu1, ID_REPORTSMENU);
ASSERT(pos2 != -1);
pMenu2 = pMenu1->GetSubMenu(pos2);
minfo.dwMenuData = ID_REPORTSMENU;
pMenu2->SetMenuInfo(&minfo);
// Do Manage Menu
pos1 = app.FindMenuItem(m_pMainMenu, ID_MANAGEMENU);
ASSERT(pos1 != -1);
pMenu1 = m_pMainMenu->GetSubMenu(pos1);
minfo.dwMenuData = ID_MANAGEMENU;
pMenu1->SetMenuInfo(&minfo);
// No need to do the Manage Menu Languages submenu as rebuilt each time
// Do Help Menu
pos1 = app.FindMenuItem(m_pMainMenu, ID_HELPMENU);
ASSERT(pos1 != -1);
pMenu1 = m_pMainMenu->GetSubMenu(pos1);
minfo.dwMenuData = ID_HELPMENU;
pMenu1->SetMenuInfo(&minfo);
#ifdef DEMO
// add specific menu item for demo version
// relies on last pMenu -> Help Menu
if (pMenu1 != NULL) {
pMenu1->InsertMenu(2, MF_BYPOSITION, ID_MENUITEM_U3SHOP_WEBSITE,
CString(MAKEINTRESOURCE(IDS_U3PURCHASE)));
}
#endif /* DEMO */
}
void ThisMfcApp::SetLanguage()
{
// Free old resource only DLL
if (m_hInstResDLL != m_hInstance)
FreeLibrary(m_hInstResDLL);
m_hInstResDLL = NULL;
// Close the current Help file
::HtmlHelp(NULL, NULL, HH_CLOSE_ALL, 0);
// Go set it up again
LoadLocalizedStuff();
if (m_hInstResDLL == NULL) {
// OK - not to use resource-only DLL - need to set to embedded resources
AfxSetResourceHandle(m_hInstance);
m_ResLangID = m_AppLangID;
}
// Setup main menu again
SetupMenu();
}
bool ThisMfcApp::ParseCommandLine(DboxMain &dbox, bool &allDone)
{
/*
* Command line processing:
* Historically, it appears that if a filename was passed as a commandline argument,
* the application would prompt the user for the password, and the encrypt or decrypt
* the named file, based on the file's suffix. Ugh.
*
* What I'll do is as follows:
* If a file is given in the command line, it is used as the database, overriding the
* registry value. This will allow the user to have several databases, say, one for work
* and one for personal use, and to set up a different shortcut for each.
*
* I think I'll keep the old functionality, but activate it with a "-e" or "-d" flag. (ronys)
*/
allDone = false;
if (m_lpCmdLine[0] != L'\0') {
CString args = m_lpCmdLine;
// Start command line parsing by pushing each argument into a vector
// Quotes are stripped here
vector<CString> argvec;
int pos = 0;
CString tok;
do {
if (args[pos] == L'\"')
tok = args.Tokenize(L"\"", pos);
else
tok = args.Tokenize(L" ",pos);
if (!tok.IsEmpty())
argvec.push_back(tok);
} while (pos != -1 && pos < args.GetLength());
vector<CString>::iterator arg = argvec.begin();
bool isEncrypt = false; // for -e/-d handling
bool startSilent = false;
bool fileGiven = false;
while (arg != argvec.end()) {
// Everything starting with '-' is a normal flag,
// everything else is the name of a file, unless an
// "extended flags" which starts with '--' and so must
// be dealt with first.
if ((*arg).Left(2) == L"--") {
// Extended flags must be in full (no abbreviations)
// If extended flag is not recognised - just ignore
// If a normal flag is not recognised - show Usage
if ((*arg) == L"--testdump") {
m_bPermitTestdump = true;
} else if ((*arg) == L"--fix-utf8") { // for reading non-utf8 non-English databases
// Databases created with ~3.05.02 and used non-English text were incorrectly
// encoded. This flag allows them to be read correctly. Saving them will
// then encode them as utf8
pws_os::setenv("PWS_CP_ACP", "1");
} else if ((*arg) == L"--setup") {
/**
* '--setup' is meant to be used when invoking PasswordSafe at the end of the installation process.
* It will cause the application to create a new database with the default name at the default location,
* prompting the user for the safe combination.
* State of m_bSetup is accessible via public IsSetup() member function
*/
dbox.SetSetup();
} else if ((*arg) == L"--novalidate") {
/**
* '--novalidate' prevents SOME of validation during open
*/
dbox.NoValidation();
} else if ((*arg) == L"--cetreeview") {
/**
* '--cetreeview' will allow the user to select 2 entries and compare them
* TEMPORARY solution until multi-select is coded in the main Tree view
* Supported natively in List View
*/
dbox.AllowCompareEntries();
} else {
// unrecognized extended flag. Silently ignore.
}
arg++;
continue;
}
if ((*arg)[0] == L'-') {
switch ((*arg)[1]) {
case L'E': case L'e':
isEncrypt = true;
// deliberate fallthru
case L'D': case L'd':
allDone = true; // no need for further processing for -e/-d
{
// Make sure there's another argument
// and it's not a flag, and it is an existing file
if ((arg + 1) == argvec.end() || (arg + 1)[0] == L'-' ||
!CheckFile(*(arg + 1))) {
Usage();
return false;
}
// get password from user
StringX passkey;
CCryptKeyEntry dlg(isEncrypt);
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK) {
passkey = LPCWSTR(dlg.m_cryptkey1);
} else {
return false;
}
BOOL status;
if (isEncrypt) {
std::wstring errstr;
status = PWSfile::Encrypt(std::wstring(*(arg + 1)), passkey, errstr);
if (!status) {
CGeneralMsgBox gmb;
gmb.AfxMessageBox(errstr.c_str(), NULL, MB_OK | MB_ICONEXCLAMATION);
}
return true;
} else {
std::wstring errstr;
status = PWSfile::Decrypt(std::wstring(*(arg+1)), passkey, errstr);
if (!status) {
CGeneralMsgBox gmb;
gmb.AfxMessageBox(errstr.c_str(), NULL, MB_OK | MB_ICONEXCLAMATION);
}
return true;
}
} // -e or -d flag
case L'C': case L'c':
m_core.SetCurFile(L"");
dbox.SetStartClosed(true);
break;
case L'M': case L'm':// closed & minimized
m_core.SetCurFile(L"");
dbox.SetStartClosed(true);
dbox.SetStartSilent(true);
break;
case L'R': case L'r':
m_core.SetReadOnly(true);
break;
case L'S': case L's':
startSilent = true;
dbox.SetStartSilent(true);
break;
case L'V': case L'v':
// Obsolete - databases are always validated during opening unless --novalidate specified
break;
case L'U': case L'u': // set effective user
// ensure there's another non-flag argument
if ((arg + 1) == argvec.end() || (arg + 1)[0] == L'-') {
Usage();
return FALSE;
} else {
arg++;
SysInfo::GetInstance()->SetEffectiveUser(LPCWSTR(*arg));
}
break;
case L'H': case L'h': // set effective host
// ensure there's another non-flag argument
if ((arg + 1) == argvec.end() || (arg + 1)[0] == L'-') {
Usage();
return FALSE;
} else {
arg++;
SysInfo::GetInstance()->SetEffectiveHost(LPCWSTR(*arg));
}
break;
case L'G': case L'g': // override default config file
// ensure there's another non-flag argument
if ((arg + 1) == argvec.end() || (arg + 1)[0] == L'-') {
Usage();
return FALSE;
} else {
arg++;
if (!PWSprefs::SetConfigFile(std::wstring(*arg))) {
// SetConfigFile returns false if specified file not found
CGeneralMsgBox gmb;
CString missing_cfg;
// Classic chicken-and-egg here: Since the local
// language could be specified in the config file, and since
// the desired config file can't be found, it seems
// safest to keep this particular error message in English
// and out of the language-specific dlls.
missing_cfg.Format(L"Configuration file %s not found - creating it.",
*arg);
gmb.AfxMessageBox(missing_cfg, L"Error", MB_OK|MB_ICONINFORMATION);
}
}
break;
case L'Q': case L'q': // be Quiet re missing fonts, dlls, etc.
m_noSysEnvWarnings = true;
break;
default:
Usage();
return false;
} // switch on flag
} else { // arg isn't a flag, treat it as a filename
if (CheckFile(*arg)) {
fileGiven = true;
// We send core the full path 'cause otherwise incr. backups & such get confused.
stringT abspath = pws_os::fullpath(arg->GetString());
if (abspath.empty()) // Should never happen, but try to slog on if so.
abspath = arg->GetString();
m_core.SetCurFile(abspath.c_str());
} else {
return false;
}
} // if arg is a flag or not
arg++;
} // while argvec
// If start silent && no filename specified, start closed as well
if (startSilent && !fileGiven)
dbox.SetStartClosed(true);
} // Command line not empty
return true;
}
BOOL ThisMfcApp::InitInstance()
{
/*
* It's always best to start at the beginning. [Glinda, Witch of the North]
*/
/*
"SetRegistryKey" MUST be called before there is any reference to 'PWSprefs'
in this application.
This instructs the app to use the registry instead of .ini files. The
path ends up being
HKEY_CURRENT_USER\Software\(companyname)\(appname)\(sectionname)\(valuename)
where companyname is what's set here, and appname is taken from
AFX_IDS_APP_TITLE (actually, CWinApp::m_pszAppName).
Notes:
1. I would love to move this to core/PWSprefs.cpp, but it's a protected
member function!!!
2. Prior to 3.05, the value was "Counterpane Systems". See PWSprefs.cpp
for discussion on how this is handled.
*/
SetRegistryKey(L"Password Safe");
DboxMain dbox(NULL);
std::bitset<UIInterFace::NUM_SUPPORTED> bsSupportedFunctions;
bsSupportedFunctions.set(UIInterFace::DATABASEMODIFIED);
bsSupportedFunctions.set(UIInterFace::UPDATEGUI);
bsSupportedFunctions.set(UIInterFace::GUISETUPDISPLAYINFO);
bsSupportedFunctions.set(UIInterFace::GUIREFRESHENTRY);
bsSupportedFunctions.set(UIInterFace::UPDATEWIZARD);
m_core.SetUIInterFace(&dbox, UIInterFace::NUM_SUPPORTED, bsSupportedFunctions);
m_core.SetReadOnly(false);
// Command line parsing MUST be done before the first PWSprefs lookup!
// (since user/host/config file may be overriden!)
bool allDone = false;
// Note: Even though PWSprefs has not yet been created, parsing the command line
// will (via DboxMain) fill in values within it!
bool parseVal = ParseCommandLine(dbox, allDone);
// allDone will be true iff -e or -d options given, in which case
// we're just a batch encryptor/decryptor
if (allDone)
return parseVal ? TRUE : FALSE;
else if (!parseVal) // bad command line args
return FALSE;
// MUST (indirectly) create PWSprefs first
// Ensures all things like saving locations etc. are set up.
PWSprefs *prefs = PWSprefs::GetInstance();
// And the others - even if not referenced here
Fonts *pFonts = Fonts::GetInstance();
PWSversion *pPWSver = PWSversion::GetInstance();
// Stop compiler warnings
UNREFERENCED_PARAMETER(pFonts);
UNREFERENCED_PARAMETER(pPWSver);
LoadLocalizedStuff();
#ifndef _DEBUG
if (m_hInstResDLL != NULL)
LocalizeFaultHandler(m_hInstResDLL);
#endif
// Update Quiet value if via environmental variable rather than
// command line flag
CString cs_PWS_QUIET;
if (cs_PWS_QUIET.GetEnvironmentVariable(L"PWS_QUIET") != FALSE)
m_noSysEnvWarnings = true;
// Check if the user allows muliple instances.
// For this to apply, consistently, must use the same copy of PasswordSafe
// configuration file.
if (!prefs->GetPref(PWSprefs::MultipleInstances)) {
bool bAlreadyRunning;
wchar_t szName[MAX_PATH];
m_hMutexOneInstance = CreateMutex(NULL, FALSE,
CreateUniqueName(UNIQUE_PWS_GUID, szName,
MAX_PATH, SI_DESKTOP_UNIQUE));
bAlreadyRunning = (::GetLastError() == ERROR_ALREADY_EXISTS ||
::GetLastError() == ERROR_ACCESS_DENIED);
// The call fails with ERROR_ACCESS_DENIED if the Mutex was
// created in a different users session because of passing
// NULL for the SECURITY_ATTRIBUTES on Mutex creation);
if (bAlreadyRunning) { /* kill this */
HWND hOther = NULL;
EnumWindows(searcher, (LPARAM)&hOther);
if (hOther != NULL) { /* pop up */
::SetForegroundWindow(hOther);
if (IsIconic(hOther)) { /* restore */
::ShowWindow(hOther, SW_RESTORE);
} /* restore */
} /* pop up */
return FALSE; // terminates the creation
} /* kill this */
}
// Allow anyone else to take us to the foreground
AllowSetForegroundWindow(ASFW_ANY);
// Needed for RichEditCtrls in Dialogs (i.e. not explicitly "created").
AfxInitRichEdit2();
// PWScore needs it to get into database header if/when saved
m_core.SetApplicationNameAndVersion(AfxGetAppName(), m_dwMajorMinor);
if (m_core.GetCurFile().empty()) {
stringT path = prefs->GetPref(PWSprefs::CurrentFile).c_str();
pws_os::AddDrive(path);
m_core.SetCurFile(path.c_str());
}
SetupMenu();
/*
* normal startup
*/
/*
Here's where PWS currently does DboxMain, which in turn will do
the initial PasskeyEntry (the one that looks like a splash screen).
This makes things very hard to control.
The app object (here) should instead do the initial PasskeyEntry,
and, if successful, move on to DboxMain. I think. {jpr}
*/
m_pDbx = &dbox;
m_pMainWnd = m_pDbx;
//HICON stIcon = app.LoadIcon(IDI_TRAY);
//ASSERT(stIcon != NULL);
m_LockedIcon = app.LoadIcon(IDI_LOCKEDICON);
m_UnLockedIcon = app.LoadIcon(IDI_UNLOCKEDICON);
int iData = prefs->GetPref(PWSprefs::ClosedTrayIconColour);
SetClosedTrayIcon(iData);
m_pTrayIcon = new CSystemTray(m_pDbx, PWS_MSG_ICON_NOTIFY, L"PasswordSafe",
m_LockedIcon, dbox.m_RUEList,
PWS_MSG_ICON_NOTIFY, IDR_POPTRAY);
m_pTrayIcon->SetTarget(&dbox);
CLWnd ListenerWnd(dbox);
if (SysInfo::IsUnderU3()) {
// See comment under CLWnd to understand this.
ListenerWnd.m_hWnd = NULL;
if (!ListenerWnd.CreateEx(0, AfxRegisterWndClass(0),
L"Pwsafe Listener",
WS_OVERLAPPED, 0, 0, 0, 0, NULL, NULL)) {
ASSERT(0);
return FALSE;
}
}
// Run dialog - note that we don't particularly care what the response was
(void) dbox.DoModal();
if (m_pTrayIcon != NULL)
m_pTrayIcon->DestroyWindow();
delete m_pTrayIcon;
::DestroyIcon(m_LockedIcon);
::DestroyIcon(m_UnLockedIcon);
::DestroyIcon(m_ClosedIcon);
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
void ThisMfcApp::AddToMRU(const CString &pszFilename)
{
if (m_pMRU == NULL)
return;
CString csMRUFilename(pszFilename);
csMRUFilename.Trim();
if (!csMRUFilename.IsEmpty()) {
m_pMRU->Add(csMRUFilename);
m_pMRU->WriteList();
}
}
void ThisMfcApp::ClearMRU()
{
if (m_pMRU == NULL)
return;
int numMRU = m_pMRU->GetSize();
for (int i = numMRU; i > 0; i--)
m_pMRU->Remove(i - 1);
m_pMRU->WriteList();
// Can't get the MRU list on the menu to tidy up automatically
// Do it manually!
CWnd* pMain = AfxGetMainWnd();
CMenu* xmainmenu = pMain->GetMenu();
// Look for "File" menu.
int pos = FindMenuItem(xmainmenu, ID_FILEMENU);
CMenu* xfile_submenu = xmainmenu->GetSubMenu(pos);
if (xfile_submenu != NULL) // Look for MRU first entry
pos = FindMenuItem(xfile_submenu, ID_FILE_MRU_ENTRY1);
else
return;
if (pos > -1) {
// Recent databases are on the main File menu - remove them
for (int nID = numMRU; nID > 1; nID--)
xfile_submenu->RemoveMenu(ID_FILE_MRU_ENTRY1 + nID - 1, MF_BYCOMMAND);
return;
}
// Recent databases are on the popup menu off the main File menu
CMenu* xpopupmenu = xfile_submenu->GetSubMenu(3);
if (xpopupmenu != NULL) // Look for MRU first entry
pos = FindMenuItem(xpopupmenu, ID_FILE_MRU_ENTRY1);
else
return;
if (pos > -1) {
// Recent databases are on a popup menu - remove them
for (int nID = numMRU; nID > 1; nID--)
xfile_submenu->RemoveMenu(ID_FILE_MRU_ENTRY1 + nID - 1, MF_BYCOMMAND);
return;
}
}
int ThisMfcApp::SetClosedTrayIcon(int &iData, bool bSet)
{
int icon;
switch (iData) {
case PWSprefs::stiBlack:
icon = IDI_TRAY; // This is black.
break;
case PWSprefs::stiBlue:
icon = IDI_TRAY_BLUE;
break;
case PWSprefs::stiWhite:
icon = IDI_TRAY_WHITE;
break;
case PWSprefs::stiYellow:
icon = IDI_TRAY_YELLOW;
break;
default:
iData = PWSprefs::stiBlack;
icon = IDI_TRAY;
break;
}
if (bSet) {
::DestroyIcon(m_ClosedIcon);
m_ClosedIcon = app.LoadIcon(icon);
}
return icon;
}
void ThisMfcApp::SetSystemTrayState(STATE s)
{
// need to protect against null m_pTrayIcon due to
// tricky initialization order
if (m_pTrayIcon != NULL && s != m_TrayLockedState) {
m_TrayLockedState = s;
HICON hIcon(m_LockedIcon);
switch (s) {
case LOCKED:
hIcon = m_LockedIcon;
break;
case UNLOCKED:
hIcon = m_UnLockedIcon;
break;
case CLOSED:
hIcon = m_ClosedIcon;
break;
default:
break;
}
m_pTrayIcon->SetIcon(hIcon);
}
}
//Copied from Knowledge Base article Q100770
BOOL ThisMfcApp::ProcessMessageFilter(int code, LPMSG lpMsg)
{
if (code < 0)
CWinApp::ProcessMessageFilter(code, lpMsg);
if (m_bUseAccelerator && m_pDbx != NULL && m_ghAccelTable != NULL &&
code != MSGF_MENU) {
if (::TranslateAccelerator(m_pDbx->m_hWnd, m_ghAccelTable, lpMsg))
return TRUE;
}
return CWinApp::ProcessMessageFilter(code, lpMsg);
}
// FindMenuItem() will find a menu item string from the specified
// popup menu and returns its position (0-based) in the specified
// popup menu. It returns -1 if no such menu item string is found.
int ThisMfcApp::FindMenuItem(CMenu* Menu, LPCWSTR MenuString)
{
ASSERT(Menu);
ASSERT(::IsMenu(Menu->GetSafeHmenu()));
const int count = Menu->GetMenuItemCount();
for (int i = 0; i < count; i++) {
CString str;
if (Menu->GetMenuString(i, str, MF_BYPOSITION) &&
(wcscmp(str, MenuString) == 0))
return i;
}
return -1;
}
// FindMenuItem() will find a menu item ID from the specified
// popup menu and returns its position (0-based) in the specified
// popup menu. It returns -1 if no such menu item string is found.
int ThisMfcApp::FindMenuItem(CMenu* Menu, UINT MenuID)
{
ASSERT(Menu);
ASSERT(::IsMenu(Menu->GetSafeHmenu()));
const int count = Menu->GetMenuItemCount();
// Can't use GetMenuItemID as it does not understand that with the MENUEX
// format, Popup menus can have IDs
MENUITEMINFO miteminfo = {0};
miteminfo.cbSize = sizeof(MENUITEMINFO);
miteminfo.fMask = MIIM_ID; // only want the wID of the menu item
for (int i = 0; i < count; i++) {
Menu->GetMenuItemInfo(i, &miteminfo, TRUE);
if (miteminfo.wID >= 1 && miteminfo.wID == MenuID)
return i;
}
return -1;
}
void ThisMfcApp::GetApplicationVersionData()
{
wchar_t szFullPath[MAX_PATH];
DWORD dwVerHnd, dwVerInfoSize;
// Get version information from the application
::GetModuleFileName(NULL, szFullPath, sizeof(szFullPath));
dwVerInfoSize = ::GetFileVersionInfoSize(szFullPath, &dwVerHnd);
if (dwVerInfoSize > 0) {
char* pVersionInfo = new char[dwVerInfoSize];
if (pVersionInfo != NULL) {
BOOL bRet = ::GetFileVersionInfo((LPWSTR)szFullPath,
(DWORD)dwVerHnd,
(DWORD)dwVerInfoSize,
(LPVOID)pVersionInfo);
VS_FIXEDFILEINFO *szVer = NULL;
UINT uVerLength;
if (bRet) {
// get binary file version information
bRet = ::VerQueryValue(pVersionInfo, TEXT("\\"),
(LPVOID*)&szVer, &uVerLength);
if (bRet) {
m_dwMajorMinor = szVer->dwProductVersionMS;
m_dwBuildRevision = szVer->dwProductVersionLS;
} else {
m_dwMajorMinor = m_dwBuildRevision = (DWORD)-1;
}
struct TRANSARRAY {
WORD wLangID;
WORD wCodePage;
};
CString cs_text;
wchar_t *buffer, *lpsztext;
UINT buflen;
TRANSARRAY* lpTransArray;
VerQueryValue(pVersionInfo, L"\\VarFileInfo\\Translation",
(LPVOID*)&buffer, &buflen);
lpTransArray = (TRANSARRAY *)buffer;
// Save for later
m_AppLangID = lpTransArray[0].wLangID;
// Until we know better
m_ResLangID = m_AppLangID;
// Get string File Version information
cs_text.Format(L"\\StringFileInfo\\%04x%04x\\FileVersion",
lpTransArray[0].wLangID, lpTransArray[0].wCodePage);
lpsztext = cs_text.GetBuffer(cs_text.GetLength() + sizeof(wchar_t));
bRet = ::VerQueryValue(pVersionInfo, lpsztext, (LPVOID*)&buffer, &buflen);
m_csFileVersionString = bRet ? buffer : L"";
cs_text.ReleaseBuffer();
// Get string Legal Copyright information
cs_text.Format(L"\\StringFileInfo\\%04x%04x\\LegalCopyright",
lpTransArray[0].wLangID, lpTransArray[0].wCodePage);
lpsztext = cs_text.GetBuffer(cs_text.GetLength() + sizeof(wchar_t));
bRet = ::VerQueryValue(pVersionInfo, lpsztext, (LPVOID*)&buffer, &buflen);
m_csCopyrightString = bRet ? buffer : L"All rights reserved.";
cs_text.ReleaseBuffer();
}
}
delete[] pVersionInfo;
}
}
BOOL CALLBACK ThisMfcApp::searcher(HWND hWnd, LPARAM lParam)
{
DWORD_PTR dwresult;
LRESULT ok = ::SendMessageTimeout(hWnd,
m_uiRegMsg,
0, 0,
SMTO_BLOCK | SMTO_ABORTIFHUNG,
200,
&dwresult);
if (ok == 0)
return TRUE; // ignore this and continue
if (dwresult == m_uiRegMsg) { /* found it */
HWND *target = (HWND *)lParam;
*target = hWnd;
return FALSE; // stop search
} /* found it */
return TRUE; // continue search
}
void ThisMfcApp::GetDLLVersionData(const CString &cs_dll, int &wLangID)
{
wLangID = 0;
DWORD dwVerHnd, dwVerInfoSize;
// Get version information from the DLL
dwVerInfoSize = ::GetFileVersionInfoSize(cs_dll, &dwVerHnd);
if (dwVerInfoSize > 0) {
char* pVersionInfo = new char[dwVerInfoSize];
if (pVersionInfo != NULL) {
BOOL bRet = ::GetFileVersionInfo((LPCWSTR)cs_dll,
(DWORD)dwVerHnd,
(DWORD)dwVerInfoSize,
(LPVOID)pVersionInfo);
if (bRet) {
struct TRANSARRAY {
WORD wLangID;
WORD wCodePage;
};
wchar_t *buffer;
UINT buflen;
VerQueryValue(pVersionInfo, L"\\VarFileInfo\\Translation",
(LPVOID*)&buffer, &buflen);
TRANSARRAY *lpTransArray = (TRANSARRAY *)buffer;
wLangID = lpTransArray[0].wLangID;
}
}
delete[] pVersionInfo;
}
}
void ThisMfcApp::GetLanguageFiles()
{
// NOTE: Assumption that a Help file can only exist if a resource file also exists
const CString cs_ExePath(PWSdirs::GetExeDir().c_str());
CString cs_ResPath = cs_ExePath + L"pwsafe*.dll";
// L"pwsafeLL.dll" : L"pwsafeLL_CC.dll";
// Find all language DLLs
m_vlanguagefiles.clear();
LANGHELPFILE st_lng;
const StringX sxLL = PWSprefs::GetInstance()->GetPref(PWSprefs::LanguageFile);
// Add default embedded language - English
const LCID AppLCID = MAKELCID(m_AppLangID, SORT_DEFAULT); // 0x0409 - English US
wchar_t *szLanguage_Native(NULL);
int inum = ::GetLocaleInfo(AppLCID, LOCALE_SNATIVELANGNAME, szLanguage_Native, 0);
if (inum > 0) {
szLanguage_Native = new wchar_t[inum + 1];
::GetLocaleInfo(AppLCID, LOCALE_SNATIVELANGNAME, szLanguage_Native, inum);
st_lng.lcid = AppLCID;
st_lng.xFlags = (wcscmp(sxLL.c_str(), L"EN") == 0) ? 0xC0 : 0x40;
st_lng.wsLL = L"EN";
st_lng.wsCC = L"";
st_lng.wsLanguage = szLanguage_Native;
m_vlanguagefiles.push_back(st_lng);
delete[] szLanguage_Native;
}
CFileFind finder;
BOOL bWorking = finder.FindFile(cs_ResPath);
while (bWorking) {
bWorking = finder.FindNextFile();
CString cs_temp, cs_helpfile;
CString cs_dll = finder.GetFileName();
// Parse file name
CString cs_LL(L""), cs_CC(L""), cs_language;
// 012345678901 012345678901234
// length should be either 10+2 (pwsafeLL.dll) or 10+5 (pwsafeLL_CC.dll)
int len = cs_dll.GetLength();
ASSERT(len == 12 || len == 15);
if (len != 12 && len != 15)
continue;
cs_LL = cs_dll.Mid(6, 2);
if (len == 15)
cs_CC = cs_dll.Mid(9, 2);
cs_temp.Format(L"pwsafe%s.chm", cs_LL);
cs_helpfile = cs_ExePath + cs_temp;
std::wstring wsHelpFile = (LPCWSTR)cs_helpfile;
bool bHelpFileExists = pws_os::FileExists(wsHelpFile);
int FoundResLangID;
GetDLLVersionData(cs_ExePath + cs_dll, FoundResLangID);
// If the Resource-only DLL has same LCID as application, it is back level
// and we can't use it.
if (FoundResLangID == m_AppLangID)
continue;
// Create LCID
LCID lcid = MAKELCID(FoundResLangID, SORT_DEFAULT);
wchar_t *szLanguage_Native(NULL), *szLanguage_English(NULL);
wchar_t *szCountry_Native(NULL), *szCountry_English(NULL);
int inum = ::GetLocaleInfo(lcid, LOCALE_SNATIVELANGNAME, NULL, 0);
if (inum > 0) {
// Get language name in that language
szLanguage_Native = new wchar_t[inum + 1];
::GetLocaleInfo(lcid, LOCALE_SNATIVELANGNAME, szLanguage_Native, inum);
int jnum = 0, knum = 0;
if (!cs_CC.IsEmpty()) {
// Get Country name in that language
jnum = ::GetLocaleInfo(lcid, LOCALE_SNATIVECTRYNAME, NULL, 0);
if (jnum > 0) {
szCountry_Native = new wchar_t[jnum + 1];
::GetLocaleInfo(lcid, LOCALE_SNATIVECTRYNAME, szCountry_Native, jnum);
}
// Get Country name in English
knum = ::GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY, NULL, 0);
if (knum > 0) {
szCountry_English = new wchar_t[knum + 1];
::GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY, szCountry_English, knum);
}
}
// Now try to make first character of language name in that language upper case
// Should use LCMapStringEx but that is for Vista + and we support XP & later
wchar_t *szLanguage_NativeUpper(NULL);
int iu = LCMapString(lcid, LCMAP_LINGUISTIC_CASING | LCMAP_UPPERCASE,
szLanguage_Native, inum,
szLanguage_NativeUpper, 0);
if (iu > 0) {
szLanguage_NativeUpper = new wchar_t[iu + 1];
iu = LCMapString(lcid, LCMAP_LINGUISTIC_CASING | LCMAP_UPPERCASE,
szLanguage_Native, inum,
szLanguage_NativeUpper, iu);
if (szLanguage_NativeUpper[0] != L'?') {
// Assume all OK and language supports Upper case and is read Left->Right
// Seems to translate non-Latin characters from Hindi, Punjabi, Hebrew, Korean & Chinese
// to the same character - so not issue (and one of them reads Right->Left)
// Must be because they do not have a concept of upper & lower case.
// Works for Russian Cyrillic alphabet though. Can't promise for future languages!
szLanguage_Native[0] = szLanguage_NativeUpper[0];
}
delete[] szLanguage_NativeUpper;
}
// Get language name in Englsh
int lnum = ::GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE, NULL, 0);
if (lnum > 0) {
szLanguage_English = new wchar_t[lnum + 1];
::GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE, szLanguage_English, lnum);
if (wcscmp(szLanguage_Native, szLanguage_English) != 0) {
if (jnum > 0)
cs_language.Format(L"%s - %s (%s - %s)", szLanguage_Native, szCountry_Native, szLanguage_English, szCountry_English);
else
cs_language.Format(L"%s (%s)", szLanguage_Native, szLanguage_English);
} else {
if (jnum > 0)
cs_language.Format(L"%s - %s", szLanguage_Native, szCountry_Native);
else
cs_language.Format(L"%s", szLanguage_Native);
}
} else
cs_language.Format(L"%s", szLanguage_Native);
st_lng.lcid = lcid;
st_lng.xFlags = (m_ResLangID == FoundResLangID) ? 0x80 : 0x00;
st_lng.xFlags |= bHelpFileExists ? 0x40 : 0x00;
st_lng.wsLL = (LPCWSTR)cs_LL;
st_lng.wsCC = (LPCWSTR)cs_CC;
st_lng.wsLanguage = (LPCWSTR)cs_language;
m_vlanguagefiles.push_back(st_lng);
delete[] szLanguage_Native;
delete[] szLanguage_English;
delete[] szCountry_Native;
delete[] szCountry_English;
}
}
finder.Close();
}