-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmain.cpp
More file actions
2107 lines (1830 loc) · 63.1 KB
/
main.cpp
File metadata and controls
2107 lines (1830 loc) · 63.1 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
/*=============================================================================
*
* FFFTP
*
===============================================================================
/ Copyright (C) 1997-2007 Sota. All rights reserved.
/
/ Redistribution and use in source and binary forms, with or without
/ modification, are permitted provided that the following conditions
/ are met:
/
/ 1. Redistributions of source code must retain the above copyright
/ notice, this list of conditions and the following disclaimer.
/ 2. Redistributions in binary form must reproduce the above copyright
/ notice, this list of conditions and the following disclaimer in the
/ documentation and/or other materials provided with the distribution.
/
/ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
/ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
/ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
/ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
/ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
/ USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
/ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
/ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
/ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/============================================================================*/
#include "common.h"
#pragma hdrstop
#include <delayimp.h>
#include <HtmlHelp.h>
#pragma comment(lib, "HtmlHelp.lib")
#pragma comment(lib, "Version.Lib")
#define RESIZE_OFF 0 /* ウインドウの区切り位置変更していない */
#define RESIZE_ON 1 /* ウインドウの区切り位置変更中 */
#define RESIZE_PREPARE 2 /* ウインドウの区切り位置変更の準備 */
#define RESIZE_HPOS 0 /* ローカル-ホスト間の区切り位置変更 */
#define RESIZE_VPOS 1 /* リスト-タスク間の区切り位置の変更 */
/*===== コマンドラインオプション =====*/
#define OPT_MIRROR 0x00000001 /* ミラーリングアップロードを行う */
#define OPT_FORCE 0x00000002 /* ミラーリング開始の確認をしない */
#define OPT_QUIT 0x00000004 /* 終了後プログラム終了 */
#define OPT_EUC 0x00000008 /* 漢字コードはEUC */
#define OPT_JIS 0x00000010 /* 漢字コードはJIS */
#define OPT_ASCII 0x00000020 /* アスキー転送モード */
#define OPT_BINARY 0x00000040 /* バイナリ転送モード */
#define OPT_AUTO 0x00000080 /* 自動判別 */
#define OPT_KANA 0x00000100 /* 半角かなをそのまま通す */
#define OPT_EUC_NAME 0x00000200 /* ファイル名はEUC */
#define OPT_JIS_NAME 0x00000400 /* ファイル名はJIS */
#define OPT_MIRRORDOWN 0x00000800 /* ミラーリングダウンロードを行う */
#define OPT_SAVEOFF 0x00001000 /* 設定の保存を中止する */
#define OPT_SAVEON 0x00002000 /* 設定の保存を再開する */
#define OPT_SJIS 0x00004000 /* 漢字コードはShift_JIS */
#define OPT_UTF8N 0x00008000 /* 漢字コードはUTF-8 */
#define OPT_UTF8BOM 0x00010000 /* 漢字コードはUTF-8 BOM */
#define OPT_SJIS_NAME 0x00020000 /* ファイル名はShift_JIS */
#define OPT_UTF8N_NAME 0x00040000 /* ファイル名はUTF-8 */
/*===== プロトタイプ =====*/
static int InitApp(int cmdShow);
static bool MakeAllWindows(int cmdShow);
static void DeleteAllObject() noexcept;
static LRESULT CALLBACK FtpWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static void StartupProc(std::vector<std::wstring_view> const& args);
static std::optional<int> AnalyzeComLine(std::vector<std::wstring_view> const& args, std::wstring& hostname, std::wstring& unc);
static void ExitProc(HWND hWnd);
static void ChangeDir(int Win, std::wstring dir);
static void ResizeWindowProc(void);
static void CalcWinSize(void);
static void CheckResizeFrame(WPARAM Keys, int x, int y);
static void DispDirInfo(void);
static void DeleteAlltempFile();
static void AboutDialog(HWND hWnd) noexcept;
static int EnterMasterPasswordAndSet(bool newpassword, HWND hWnd);
/*===== ローカルなワーク =====*/
static const wchar_t FtpClass[] = L"FFFTPWin";
static const wchar_t WebURL[] = L"https://github.com/ffftp/ffftp";
static HINSTANCE hInstFtp;
static HWND hWndFtp;
static HWND hWndCurFocus = NULL;
static HACCEL Accel;
static int Resizing = RESIZE_OFF;
static int ResizePos;
static std::vector<fs::path> TempFiles;
static int SaveExit = YES;
static int AutoExit = NO;
static fs::path IniPath;
static int ForceIni = NO;
TRANSPACKET MainTransPkt; /* ファイル転送用パケット */
/* これを使って転送を行うと、ツールバーの転送 */
/* 中止ボタンで中止できる */
std::wstring TitleHostName;
std::wstring FilterStr = L"*"s;
HANDLE initialized = CreateEventW(nullptr, true, false, nullptr);
int SuppressRefresh = 0;
static DWORD dwCookie;
// マルチコアCPUの特定環境下でファイル通信中にクラッシュするバグ対策
static DWORD MainThreadId;
HANDLE ChangeNotification = INVALID_HANDLE_VALUE;
static int ToolWinHeight = 28;
static HWND hHelpWin = NULL;
static int NoopEnable = NO;
fs::path const& systemDirectory() {
static fs::path const directory = [] {
std::wstring directory(32768, L'\0');
auto const length = GetSystemDirectoryW(data(directory), size_as<UINT>(directory));
assert(0 < length);
directory.resize(length);
return directory;
}();
return directory;
}
static auto const& moduleFileName() {
static fs::path const filename = [] {
std::wstring filename(32768, L'\0');
auto const length = GetModuleFileNameW(0, data(filename), size_as<DWORD>(filename));
assert(0 < length);
filename.resize(length);
return filename;
}();
return filename;
}
fs::path const& tempDirectory() {
static auto const directory = [] {
auto const path = fs::temp_directory_path() / std::format(L"ffftp{:08x}"sv, GetCurrentProcessId());
fs::create_directory(path);
return path;
}();
return directory;
}
static auto version() {
auto const size = GetFileVersionInfoSizeW(moduleFileName().c_str(), 0);
assert(0 < size);
std::vector<char> buffer(size);
auto result = GetFileVersionInfoW(moduleFileName().c_str(), 0, size, data(buffer));
assert(result);
LPVOID block;
UINT len;
result = VerQueryValueW(data(buffer), L"\\", &block, &len);
assert(result && sizeof(VS_FIXEDFILEINFO) <= len);
auto const ms = static_cast<VS_FIXEDFILEINFO*>(block)->dwProductVersionMS, ls = static_cast<VS_FIXEDFILEINFO*>(block)->dwProductVersionLS;
auto const major = HIWORD(ms), minor = LOWORD(ms), patch = HIWORD(ls), build = LOWORD(ls);
auto const format = build != 0 ? L"{}.{}.{}.{}"sv : patch != 0 ? L"{}.{}.{}"sv : L"{}.{}"sv;
return std::vformat(format, std::make_wformat_args(major, minor, patch, build));
}
static auto isPortable() {
static auto const isPortable = fs::is_regular_file(fs::path{ moduleFileName() }.replace_filename(L"portable"sv));
return isPortable;
}
static auto const& helpPath() {
static auto const path = fs::path{ moduleFileName() }.replace_extension(L".chm"sv);
return path;
}
Sound Sound::Connected{ L"FFFTP_Connected", L"Connected", IDS_SOUNDCONNECTED };
Sound Sound::Transferred{ L"FFFTP_Transferred", L"Transferred", IDS_SOUNDTRANSFERRED };
Sound Sound::Error{ L"FFFTP_Error", L"Error", IDS_SOUNDERROR };
void Sound::Register() {
if (HKEY eventlabels; RegCreateKeyExW(HKEY_CURRENT_USER, LR"(AppEvents\EventLabels)", 0, nullptr, 0, KEY_WRITE, nullptr, &eventlabels, nullptr) == ERROR_SUCCESS) {
if (HKEY apps; RegCreateKeyExW(HKEY_CURRENT_USER, LR"(AppEvents\Schemes\Apps\ffftp)", 0, nullptr, 0, KEY_WRITE, nullptr, &apps, nullptr) == ERROR_SUCCESS) {
RegSetValueExW(apps, nullptr, 0, REG_SZ, reinterpret_cast<const BYTE*>(L"FFFTP"), 12);
for (auto [keyName, name, id] : { Connected, Transferred, Error }) {
if (HKEY key; RegCreateKeyExW(eventlabels, keyName, 0, nullptr, 0, KEY_SET_VALUE, nullptr, &key, nullptr) == ERROR_SUCCESS) {
RegSetValueExW(key, nullptr, 0, REG_SZ, reinterpret_cast<const BYTE*>(name), ((DWORD)wcslen(name) + 1) * sizeof(wchar_t));
auto const value = std::format(L"@{},{}"sv, moduleFileName().native(), -id);
RegSetValueExW(key, L"DispFileName", 0, REG_SZ, reinterpret_cast<const BYTE*>(value.c_str()), (size_as<DWORD>(value) + 1) * sizeof(wchar_t));
}
if (HKEY key; RegCreateKeyExW(apps, keyName, 0, nullptr, 0, KEY_WRITE, nullptr, &key, nullptr) == ERROR_SUCCESS) {
if (HKEY _current; RegCreateKeyExW(key, L".current", 0, nullptr, 0, KEY_WRITE, nullptr, &_current, nullptr) == ERROR_SUCCESS)
RegCloseKey(_current);
RegCloseKey(key);
}
}
RegCloseKey(apps);
}
RegCloseKey(eventlabels);
}
}
// メインルーチン
int WINAPI wWinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPWSTR lpCmdLine, __in int nShowCmd) {
hInstFtp = hInstance;
Sound::Register();
// マルチコアCPUの特定環境下でファイル通信中にクラッシュするバグ対策
#ifdef DISABLE_MULTI_CPUS
SetProcessAffinityMask(GetCurrentProcess(), 1);
#endif
MainThreadId = GetCurrentThreadId();
if (OleInitialize(nullptr) != S_OK) {
Message(IDS_FAIL_TO_INIT_OLE, MB_OK | MB_ICONERROR);
return 0;
}
LoadUPnP();
LoadTaskbarList3();
LoadZoneID();
if (!LoadSSL()) {
Message(IDS_ERR_SSL, MB_OK | MB_ICONERROR);
return 0;
}
int exitCode = FALSE;
if (InitApp(nShowCmd) == FFFTP_SUCCESS) {
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0)) {
if (__pragma(warning(suppress:6387)) HtmlHelpW(NULL, NULL, HH_PRETRANSLATEMESSAGE, (DWORD_PTR)&msg))
continue;
/* ディレクトリ名の表示コンボボックスでBSやRETが効くように */
/* コンボボックス内ではアクセラレータを無効にする */
if (msg.hwnd == GetLocalHistEditHwnd() || msg.hwnd == GetRemoteHistEditHwnd() || hHelpWin && GetAncestor(msg.hwnd, GA_ROOT) == hHelpWin || AskUserOpeDisabled() || TranslateAcceleratorW(GetMainHwnd(), Accel, &msg) == 0) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
exitCode = (int)msg.wParam;
}
// TODO: グローバルに保持されているSocketContextの解放。遅延させると各種エラーが発生するため明示的にここで行う。
MainTransPkt.ctrl_skt.reset();
DisconnectSet();
UnregisterClassW(FtpClass, GetFtpInst());
FreeSSL();
FreeZoneID();
FreeTaskbarList3();
FreeUPnP();
OleUninitialize();
return exitCode;
}
// アプリケーションの初期設定
static int InitApp(int cmdShow)
{
int sts;
int Err;
WSADATA WSAData;
int useDefautPassword = 0; /* 警告文表示用 */
int masterpass;
// ポータブル版判定
int ImportPortable;
sts = FFFTP_FAIL;
__pragma(warning(suppress:6387)) HtmlHelpW(NULL, NULL, HH_INITIALIZE, (DWORD_PTR)&dwCookie);
if((Err = WSAStartup((WORD)0x0202, &WSAData)) != 0)
MessageBoxW(GetMainHwnd(), GetErrorMessage(Err).c_str(), GetString(IDS_APP).c_str(), MB_OK);
else
{
Accel = LoadAcceleratorsW(GetFtpInst(), MAKEINTRESOURCEW(ffftp_accel));
std::copy(std::begin(LocalTabWidthDefault), std::end(LocalTabWidthDefault), std::begin(LocalTabWidth));
std::copy(std::begin(RemoteTabWidthDefault), std::end(RemoteTabWidthDefault), std::begin(RemoteTabWidth));
std::vector<std::wstring_view> args{ __wargv + 1, __wargv + __argc };
if (auto it = std::find_if(begin(args), end(args), [](auto const& arg) { return ieq(arg, L"-n"sv) || ieq(arg, L"--ini"sv); }); it != end(args) && ++it != end(args)) {
ForceIni = YES;
RegType = REGTYPE_INI;
IniPath = *it;
} else
IniPath = fs::path{ moduleFileName() }.replace_extension(L".ini"sv);
ImportPortable = NO;
if (isPortable()) {
ForceIni = YES;
RegType = REGTYPE_INI;
if(IsRegAvailable() == YES && IsIniAvailable() == NO)
{
if (Dialog(GetFtpInst(), ini_from_reg_dlg, GetMainHwnd()))
ImportPortable = YES;
}
} else {
if(ReadSettingsVersion() > VER_NUM)
{
if(IsRegAvailable() == YES && IsIniAvailable() == NO)
{
switch(Message(IDS_FOUND_NEW_VERSION_INI, MB_YESNOCANCEL | MB_DEFBUTTON2))
{
case IDCANCEL:
ReadOnlySettings = YES;
break;
case IDYES:
break;
case IDNO:
ImportPortable = YES;
break;
}
}
}
}
// ポータブル版判定
if(ImportPortable == YES)
{
ForceIni = NO;
RegType = REGTYPE_REG;
}
/* 2010.02.01 genta マスターパスワードを入力させる
-z オプションがあるときは最初だけスキップ
-z オプションがないときは,デフォルトパスワードをまず試す
LoadRegistry()する
パスワードが不一致なら再入力するか尋ねる.
(破損していた場合はさせない)
*/
if(auto it = std::find_if(begin(args), end(args), [](auto const& arg) { return ieq(arg, L"-z"sv) || ieq(arg, L"--mpasswd"sv); }); it != end(args) && ++it != end(args))
{
SetMasterPassword(*it);
useDefautPassword = 0;
}
else {
/* パスワード指定無し */
SetMasterPassword();
/* この場では表示できないのでフラグだけ立てておく*/
useDefautPassword = 2;
}
/* パスワードチェックのみ実施 */
masterpass = 1;
while( ValidateMasterPassword() == YES &&
GetMasterPasswordStatus() == PASSWORD_UNMATCH ){
if( useDefautPassword != 2 ){
/* 再トライするか確認 */
if( Message(IDS_MASTER_PASSWORD_INCORRECT, MB_YESNO | MB_ICONEXCLAMATION) == IDNO ){
useDefautPassword = 0; /* 不一致なので,もはやデフォルトかどうかは分からない */
break;
}
}
/* 再入力させる*/
masterpass = EnterMasterPasswordAndSet(false, NULL);
if( masterpass == 2 ){
useDefautPassword = 1;
}
else if( masterpass == 0 ){
SaveExit = NO;
break;
}
else {
useDefautPassword = 0;
}
}
if(masterpass != 0)
{
// ホスト共通設定機能
ResetDefaultHost();
LoadRegistry();
// ポータブル版判定
if(ImportPortable == YES)
{
ForceIni = YES;
RegType = REGTYPE_INI;
}
//タイマの精度を改善
timeBeginPeriod(1);
if(MakeAllWindows(cmdShow))
{
hWndCurFocus = GetLocalHwnd();
if (std::error_code ec; !empty(DefaultLocalPath))
fs::current_path(DefaultLocalPath, ec);
SetSortTypeImm(Sort);
SetTransferTypeImm(TransMode);
DispTransferType();
SetHostKanaCnvImm(YES);
SetHostKanjiCodeImm(KANJI_NOCNV);
// UTF-8対応
SetLocalKanjiCodeImm(LocalKanjiCode);
DispListType();
DispDotFileMode();
DispSyncMoveMode();
if(MakeTransferThread() == FFFTP_SUCCESS)
{
Debug(L"DEBUG MESSAGE ON ! ##"sv);
DispWindowTitle();
UpdateStatusBar();
Notice(IDS_COPYRIGHT, version(), sizeof(void*) == 4 ? L"32bit"sv : L"64bit"sv);
if(ForceIni)
Notice(IDS_MSGJPN283, IniPath.native());
Debug(L"Help={}", helpPath().native());
DragAcceptFiles(GetRemoteHwnd(), TRUE);
DragAcceptFiles(GetLocalHwnd(), TRUE);
SetAllHistoryToMenu();
GetLocalDirForWnd();
MakeButtonsFocus();
DispTransferFiles();
StartupProc(args);
sts = FFFTP_SUCCESS;
/* セキュリティ警告文の表示 */
if( useDefautPassword ){
Notice(IDS_MSGJPN300);
}
/* パスワード不一致警告文の表示 */
switch( GetMasterPasswordStatus() ){
case PASSWORD_UNMATCH:
Notice(IDS_MSGJPN301);
break;
case BAD_PASSWORD_HASH:
Notice(IDS_MSGJPN302);
break;
default:
break;
}
}
}
}
}
if(sts == FFFTP_FAIL)
DeleteAllObject();
return(sts);
}
// ウインドウを作成する
static bool MakeAllWindows(int cmdShow) {
WNDCLASSEXW classEx{ sizeof(WNDCLASSEXW), 0, FtpWndProc, 0, 0, GetFtpInst(), LoadIconW(GetFtpInst(), MAKEINTRESOURCEW(ffftp)), 0, GetSysColorBrush(COLOR_3DFACE), MAKEINTRESOURCEW(main_menu), FtpClass };
RegisterClassExW(&classEx);
if (SaveWinPos == NO) {
WinPosX = CW_USEDEFAULT;
WinPosY = 0;
}
hWndFtp = CreateWindowExW(0, FtpClass, L"FFFTP", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, WinPosX, WinPosY, WinWidth, WinHeight, HWND_DESKTOP, 0, GetFtpInst(), nullptr);
if (!hWndFtp)
return false;
RECT workArea;
SystemParametersInfoW(SPI_GETWORKAREA, 0, &workArea, 0);
RECT windowRect;
GetWindowRect(GetMainHwnd(), &windowRect);
if (workArea.bottom < windowRect.bottom)
MoveWindow(GetMainHwnd(), windowRect.left, std::max(0L, windowRect.top - windowRect.bottom + workArea.bottom), WinWidth, WinHeight, FALSE);
if (MakeStatusBarWindow() == FFFTP_FAIL)
return false;
CalcWinSize();
if (!MakeToolBarWindow())
return false;
if (MakeListWin() == FFFTP_FAIL)
return false;
if (MakeTaskWindow() == FFFTP_FAIL)
return false;
ShowWindow(GetMainHwnd(), cmdShow != SW_MINIMIZE && cmdShow != SW_SHOWMINIMIZED && cmdShow != SW_SHOWMINNOACTIVE && Sizing == SW_MAXIMIZE ? SW_MAXIMIZE : cmdShow);
SetListViewType();
return true;
}
// ウインドウのタイトルを表示する
void DispWindowTitle() {
auto const text = std::vformat(AskConnecting() == YES ? L"{0} ({1}) - FFFTP"sv : L"FFFTP ({1})"sv, std::make_wformat_args(TitleHostName, FilterStr));
SetWindowTextW(GetMainHwnd(), text.c_str());
}
// 全てのオブジェクトを削除
static void DeleteAllObject() noexcept {
WSACleanup();
if (hWndFtp != NULL)
DestroyWindow(hWndFtp);
}
// メインウインドウのウインドウハンドルを返す
HWND GetMainHwnd() noexcept {
return hWndFtp;
}
// 現在フォーカスがあるウインドウのをセットする
void SetFocusHwnd(HWND hWnd) noexcept {
hWndCurFocus = hWnd;
}
// プログラムのインスタンスを返す
HINSTANCE GetFtpInst() noexcept {
return hInstFtp;
}
static void OtpCalcTool() noexcept {
struct Data {
using result_t = int;
using AlgoButton = RadioButton<OTPCALC_MD4, OTPCALC_MD5, OTPCALC_SHA1>;
INT_PTR OnInit(HWND hDlg) noexcept {
SendDlgItemMessageW(hDlg, OTPCALC_KEY, EM_LIMITTEXT, 40, 0);
SendDlgItemMessageW(hDlg, OTPCALC_PASS, EM_LIMITTEXT, PASSWORD_LEN, 0);
AlgoButton::Set(hDlg, MD4);
return(TRUE);
}
void OnCommand(HWND hDlg, WORD id) {
switch (id) {
case IDOK:
{
static boost::wregex re{ LR"(^ *(\d+)(?=[^ ]* +([^ ]+)))" };
auto const key = GetText(hDlg, OTPCALC_KEY);
if (boost::wsmatch m; boost::regex_search(key, m, re)) {
if (m[2].matched) {
auto seq = std::stoi(m[1]);
auto seed = u8(m[2].str());
auto pass = u8(GetText(hDlg, OTPCALC_PASS));
auto result = Make6WordPass(seq, seed, pass, AlgoButton::Get(hDlg));
SetText(hDlg, OTPCALC_RES, u8(result));
} else
SetText(hDlg, OTPCALC_RES, GetString(IDS_MSGJPN251));
} else
SetText(hDlg, OTPCALC_RES, GetString(IDS_MSGJPN253));
break;
}
case IDCANCEL:
EndDialog(hDlg, NO);
break;
case IDHELP:
ShowHelp(IDH_HELP_TOPIC_0000037);
break;
}
}
};
Dialog(GetFtpInst(), otp_calc_dlg, GetMainHwnd(), Data{});
}
static void TurnStatefulFTPFilter() {
if (auto const ID = Message(IDS_MANAGE_STATEFUL_FTP, MB_YESNOCANCEL); ID == IDYES || ID == IDNO)
if (PtrToInt(ShellExecuteW(NULL, L"runas", L"netsh", ID == IDYES ? L"advfirewall set global statefulftp enable" : L"advfirewall set global statefulftp disable", systemDirectory().c_str(), SW_SHOW)) <= 32)
Message(IDS_FAIL_TO_MANAGE_STATEFUL_FTP, MB_OK | MB_ICONERROR);
}
/*----- メインウインドウのメッセージ処理 --------------------------------------
*
* Parameter
* HWND hWnd : ウインドウハンドル
* UINT message : メッセージ番号
* WPARAM wParam : メッセージの WPARAM 引数
* LPARAM lParam : メッセージの LPARAM 引数
*
* Return Value
* メッセージに対応する戻り値
*----------------------------------------------------------------------------*/
static LRESULT CALLBACK FtpWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
RECT Rect;
int TmpTransType;
switch (message)
{
// ローカル側自動更新
// タスクバー進捗表示
case WM_CREATE :
SetTimer(hWnd, 1, 1000, NULL);
SetTimer(hWnd, 2, 100, NULL);
break;
// ローカル側自動更新
// 自動切断対策
// タスクバー進捗表示
case WM_TIMER :
switch(wParam)
{
case 1:
if(WaitForSingleObject(ChangeNotification, 0) == WAIT_OBJECT_0)
{
if (!AskUserOpeDisabled())
{
FindNextChangeNotification(ChangeNotification);
if (AutoRefreshFileList == YES)
RefreshLocal();
}
}
if(CancelFlg == YES)
AbortRecoveryProc();
if (auto const& curHost = GetCurHost(); NoopEnable == YES && curHost.NoopInterval > 0 && time(NULL) - LastDataConnectionTime >= curHost.NoopInterval) {
NoopProc(NO);
LastDataConnectionTime = time(NULL);
}
break;
case 2:
if(IsTaskbarList3Loaded() == YES)
UpdateTaskbarProgress();
break;
}
break;
case WM_COMMAND :
// 同時接続対応
// 中断後に受信バッファに応答が残っていると次のコマンドの応答が正しく処理できない
if(CancelFlg == YES)
AbortRecoveryProc();
switch(LOWORD(wParam))
{
case MENU_CONNECT :
// 自動切断対策
NoopEnable = NO;
ConnectProc(DLG_TYPE_CON, -1);
// 自動切断対策
NoopEnable = YES;
break;
case MENU_CONNECT_NUM :
// 自動切断対策
NoopEnable = NO;
ConnectProc(DLG_TYPE_CON, (int)lParam);
// 自動切断対策
NoopEnable = YES;
if(AskConnecting() == YES)
{
if(HIWORD(wParam) & OPT_MIRROR)
{
if(HIWORD(wParam) & OPT_FORCE)
MirrorUploadProc(NO);
else
MirrorUploadProc(YES);
}
else if(HIWORD(wParam) & OPT_MIRRORDOWN)
{
if(HIWORD(wParam) & OPT_FORCE)
MirrorDownloadProc(NO);
else
MirrorDownloadProc(YES);
}
}
SetEvent(initialized);
break;
case MENU_SET_CONNECT :
// 自動切断対策
NoopEnable = NO;
ConnectProc(DLG_TYPE_SET, -1);
// 自動切断対策
NoopEnable = YES;
break;
case MENU_QUICK :
// 自動切断対策
NoopEnable = NO;
QuickConnectProc();
// 自動切断対策
NoopEnable = YES;
break;
case MENU_DISCONNECT :
if(AskTryingConnect() == YES)
CancelFlg = YES;
else if(AskConnecting() == YES)
{
SaveBookMark();
SaveCurrentSetToHost();
DisconnectProc();
}
break;
case MENU_HIST_1 :
case MENU_HIST_2 :
case MENU_HIST_3 :
case MENU_HIST_4 :
case MENU_HIST_5 :
case MENU_HIST_6 :
case MENU_HIST_7 :
case MENU_HIST_8 :
case MENU_HIST_9 :
case MENU_HIST_10 :
case MENU_HIST_11 :
case MENU_HIST_12 :
case MENU_HIST_13 :
case MENU_HIST_14 :
case MENU_HIST_15 :
case MENU_HIST_16 :
case MENU_HIST_17 :
case MENU_HIST_18 :
case MENU_HIST_19 :
case MENU_HIST_20 :
// 自動切断対策
NoopEnable = NO;
HistoryConnectProc(LOWORD(wParam));
// 自動切断対策
NoopEnable = YES;
break;
case MENU_UPDIR :
if(hWndCurFocus == GetLocalHwnd())
PostMessageW(hWnd, WM_COMMAND, MAKEWPARAM(MENU_LOCAL_UPDIR, 0), 0);
else
PostMessageW(hWnd, WM_COMMAND, MAKEWPARAM(MENU_REMOTE_UPDIR, 0), 0);
break;
case MENU_DCLICK :
if(hWndCurFocus == GetLocalHwnd())
// ローカルフォルダを開く
// DoubleClickProc(WIN_LOCAL, YES, -1);
DoubleClickProc(WIN_LOCAL, NO, -1);
else
{
SuppressRefresh = 1;
// ローカルフォルダを開く
// DoubleClickProc(WIN_REMOTE, YES, -1);
DoubleClickProc(WIN_REMOTE, NO, -1);
SuppressRefresh = 0;
}
break;
// ローカルフォルダを開く
case MENU_OPEN :
if(hWndCurFocus == GetLocalHwnd())
DoubleClickProc(WIN_LOCAL, YES, -1);
else
{
SuppressRefresh = 1;
DoubleClickProc(WIN_REMOTE, YES, -1);
SuppressRefresh = 0;
}
break;
case MENU_OPEN1 :
if(hWndCurFocus == GetLocalHwnd())
DoubleClickProc(WIN_LOCAL, YES, 0);
else
{
SuppressRefresh = 1;
DoubleClickProc(WIN_REMOTE, YES, 0);
SuppressRefresh = 0;
}
break;
case MENU_OPEN2 :
if(hWndCurFocus == GetLocalHwnd())
DoubleClickProc(WIN_LOCAL, YES, 1);
else
{
SuppressRefresh = 1;
DoubleClickProc(WIN_REMOTE, YES, 1);
SuppressRefresh = 0;
}
break;
case MENU_OPEN3 :
if(hWndCurFocus == GetLocalHwnd())
DoubleClickProc(WIN_LOCAL, YES, 2);
else
{
SuppressRefresh = 1;
DoubleClickProc(WIN_REMOTE, YES, 2);
SuppressRefresh = 0;
}
break;
case MENU_REMOTE_UPDIR :
if (AskUserOpeDisabled())
break;
SuppressRefresh = 1;
SetCurrentDirAsDirHist();
ChangeDir(WIN_REMOTE, L".."s);
SuppressRefresh = 0;
break;
case MENU_LOCAL_UPDIR :
if (AskUserOpeDisabled())
break;
SetCurrentDirAsDirHist();
ChangeDir(WIN_LOCAL, L".."s);
break;
case MENU_REMOTE_CHDIR :
SuppressRefresh = 1;
SetCurrentDirAsDirHist();
ChangeDirDirectProc(WIN_REMOTE);
SuppressRefresh = 0;
break;
case MENU_LOCAL_CHDIR :
SetCurrentDirAsDirHist();
ChangeDirDirectProc(WIN_LOCAL);
break;
case MENU_DOWNLOAD :
SetCurrentDirAsDirHist();
DownloadProc(NO, NO, NO);
break;
case MENU_DOWNLOAD_AS :
SetCurrentDirAsDirHist();
DownloadProc(YES, NO, NO);
break;
case MENU_DOWNLOAD_AS_FILE :
SetCurrentDirAsDirHist();
DownloadProc(NO, YES, NO);
break;
case MENU_DOWNLOAD_ALL :
SetCurrentDirAsDirHist();
DownloadProc(NO, NO, YES);
break;
case MENU_DOWNLOAD_NAME :
SetCurrentDirAsDirHist();
if (std::wstring path; InputDialog(downname_dlg, GetMainHwnd(), 0, path, FMAX_PATH))
DirectDownloadProc(path);
break;
case MENU_UPLOAD :
SetCurrentDirAsDirHist();
UploadListProc(NO, NO);
break;
case MENU_UPLOAD_AS :
SetCurrentDirAsDirHist();
UploadListProc(YES, NO);
break;
case MENU_UPLOAD_ALL :
SetCurrentDirAsDirHist();
UploadListProc(NO, YES);
break;
case MENU_MIRROR_UPLOAD :
SetCurrentDirAsDirHist();
MirrorUploadProc(YES);
break;
case MENU_MIRROR_DOWNLOAD :
SetCurrentDirAsDirHist();
MirrorDownloadProc(YES);
break;
case MENU_FILESIZE :
SetCurrentDirAsDirHist();
CalcFileSizeProc();
break;
case MENU_DELETE :
SuppressRefresh = 1;
SetCurrentDirAsDirHist();
DeleteProc();
SuppressRefresh = 0;
break;
case MENU_RENAME :
SuppressRefresh = 1;
SetCurrentDirAsDirHist();
RenameProc();
SuppressRefresh = 0;
break;
case MENU_MKDIR :
SuppressRefresh = 1;
SetCurrentDirAsDirHist();
MkdirProc();
SuppressRefresh = 0;
break;
case MENU_CHMOD :
SuppressRefresh = 1;
ChmodProc();
SuppressRefresh = 0;
break;
case MENU_SOMECMD :
SuppressRefresh = 1;
SomeCmdProc();
SuppressRefresh = 0;
break;
case MENU_OPTION :
SetOption();
if(ListFont != NULL)
{
SendMessageW(GetLocalHwnd(), WM_SETFONT, (WPARAM)ListFont, MAKELPARAM(TRUE, 0));
SendMessageW(GetRemoteHwnd(), WM_SETFONT, (WPARAM)ListFont, MAKELPARAM(TRUE, 0));
SendMessageW(GetTaskWnd(), WM_SETFONT, (WPARAM)ListFont, MAKELPARAM(TRUE, 0));
}
GetLocalDirForWnd();
DispTransferType();
SetAllHistoryToMenu();
break;
case MENU_FILTER :
// 同時接続対応
CancelFlg = NO;
SetFilter(&CancelFlg);
break;
case MENU_SORT :
if(SortSetting() == YES)
{
// 同時接続対応
CancelFlg = NO;
Sort = AskSortType();
ReSortDispList(WIN_LOCAL, &CancelFlg);
ReSortDispList(WIN_REMOTE, &CancelFlg);
}
break;
case MENU_EXIT :
PostMessageW(hWnd, WM_CLOSE, 0, 0L);
break;
case MENU_AUTO_EXIT :
if(AutoExit == YES)
PostMessageW(hWnd, WM_CLOSE, 0, 0L);
break;
case MENU_ABOUT :
AboutDialog(hWnd);
break;
case MENU_TEXT :
case MENU_BINARY :
case MENU_AUTO :
SetTransferType(LOWORD(wParam));
DispTransferType();
break;
case MENU_XFRMODE :
switch(AskTransferType())
{
case TYPE_A :
TmpTransType = MENU_BINARY;
break;
case TYPE_I :
TmpTransType = MENU_AUTO;
break;
default :
TmpTransType = MENU_TEXT;
break;
}
SetTransferType(TmpTransType);
DispTransferType();
break;
// UTF-8対応
case MENU_KNJ_SJIS :
case MENU_KNJ_EUC :
case MENU_KNJ_JIS :
case MENU_KNJ_UTF8N :