-
Notifications
You must be signed in to change notification settings - Fork 189
/
ui.cpp
1921 lines (1743 loc) · 68 KB
/
ui.cpp
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
#include "msg.h"
#include "guids.hpp"
#include "utils.hpp"
#include "farutils.hpp"
#include "sysutils.hpp"
#include "common.hpp"
#include "ui.hpp"
#include "archive.hpp"
#include "options.hpp"
wstring get_error_dlg_title() {
return Far::get_msg(MSG_PLUGIN_NAME);
}
ProgressMonitor::ProgressMonitor(const wstring& progress_title, bool progress_known, bool lazy): progress_title(progress_title), progress_known(progress_known), h_scr(nullptr), percent_done(0), paused(false), low_priority(false) {
QueryPerformanceCounter(reinterpret_cast<PLARGE_INTEGER>(&time_cnt));
QueryPerformanceFrequency(reinterpret_cast<PLARGE_INTEGER>(&time_freq));
time_total = 0;
if (lazy)
time_update = time_total + time_freq / c_first_delay_div;
else
time_update = time_total;
CHECK(get_app_option(FSSF_CONFIRMATIONS, c_esc_confirmation_option, confirm_esc));
initial_priority = GetPriorityClass(GetCurrentProcess());
}
ProgressMonitor::~ProgressMonitor() {
clean();
}
void ProgressMonitor::display() {
do_update_ui();
wstring title;
if (progress_known) {
title += L"{" + int_to_str(percent_done) + L"%} ";
}
if (paused) {
title += L"[" + Far::get_msg(MSG_PROGRESS_PAUSED) + L"] ";
}
else if (low_priority) {
title += L"[" + Far::get_msg(MSG_PROGRESS_LOW_PRIORITY) + L"] ";
}
title += progress_title;
if (progress_known) {
Far::set_progress_state(TBPF_NORMAL);
Far::set_progress_value(percent_done, 100);
}
else {
Far::set_progress_state(TBPF_INDETERMINATE);
}
Far::message(c_progress_dialog_guid, title + L'\n' + progress_text, 0, FMSG_LEFTALIGN);
SetConsoleTitleW(title.c_str());
}
void ProgressMonitor::update_ui(bool force) {
update_time();
if ((time_total >= time_update) || force) {
time_update = time_total + time_freq / c_update_delay_div;
if (h_scr == nullptr) {
h_scr = Far::save_screen();
con_title = get_console_title();
}
HANDLE h_con = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD rec;
DWORD read_cnt;
while (true) {
if (!paused) {
PeekConsoleInputW(h_con, &rec, 1, &read_cnt);
if (read_cnt == 0) break;
}
ReadConsoleInputW(h_con, &rec, 1, &read_cnt);
if (rec.EventType == KEY_EVENT) {
const KEY_EVENT_RECORD& key_event = rec.Event.KeyEvent;
const WORD c_vk_b = 0x42;
const WORD c_vk_p = 0x50;
if (is_single_key(key_event)) {
if (key_event.wVirtualKeyCode == VK_ESCAPE) {
handle_esc();
}
else if (key_event.wVirtualKeyCode == c_vk_b) {
low_priority = !low_priority;
SetPriorityClass(GetCurrentProcess(), low_priority ? IDLE_PRIORITY_CLASS : initial_priority);
if (paused)
display();
}
else if (key_event.wVirtualKeyCode == c_vk_p) {
paused = !paused;
if (paused) {
update_time();
display();
}
else
discard_time();
}
}
}
}
display();
}
}
bool ProgressMonitor::is_single_key(const KEY_EVENT_RECORD& key_event) {
return key_event.bKeyDown && (key_event.dwControlKeyState & (LEFT_ALT_PRESSED | LEFT_CTRL_PRESSED | RIGHT_ALT_PRESSED | RIGHT_CTRL_PRESSED | SHIFT_PRESSED)) == 0;
}
void ProgressMonitor::handle_esc() {
if (!confirm_esc)
FAIL(E_ABORT);
ProgressSuspend ps(*this);
if (Far::message(c_interrupt_dialog_guid, Far::get_msg(MSG_PLUGIN_NAME) + L"\n" + Far::get_msg(MSG_PROGRESS_INTERRUPT), 0, FMSG_MB_YESNO) == 0)
FAIL(E_ABORT);
}
void ProgressMonitor::clean() {
if (h_scr) {
Far::restore_screen(h_scr);
SetConsoleTitleW(con_title.data());
Far::set_progress_state(TBPF_NOPROGRESS);
h_scr = nullptr;
}
SetPriorityClass(GetCurrentProcess(), initial_priority);
}
void ProgressMonitor::update_time() {
unsigned __int64 time_curr;
QueryPerformanceCounter(reinterpret_cast<PLARGE_INTEGER>(&time_curr));
time_total += time_curr - time_cnt;
time_cnt = time_curr;
}
void ProgressMonitor::discard_time() {
QueryPerformanceCounter(reinterpret_cast<PLARGE_INTEGER>(&time_cnt));
}
unsigned __int64 ProgressMonitor::time_elapsed() {
update_time();
return time_total;
}
unsigned __int64 ProgressMonitor::ticks_per_sec() {
return time_freq;
}
const wchar_t** get_size_suffixes() {
static const wchar_t* suffixes[5] = {
L"",
Far::msg_ptr(MSG_SUFFIX_SIZE_KB),
Far::msg_ptr(MSG_SUFFIX_SIZE_MB),
Far::msg_ptr(MSG_SUFFIX_SIZE_GB),
Far::msg_ptr(MSG_SUFFIX_SIZE_TB),
};
return suffixes;
}
const wchar_t** get_speed_suffixes() {
static const wchar_t* suffixes[5] = {
Far::msg_ptr(MSG_SUFFIX_SPEED_B),
Far::msg_ptr(MSG_SUFFIX_SPEED_KB),
Far::msg_ptr(MSG_SUFFIX_SPEED_MB),
Far::msg_ptr(MSG_SUFFIX_SPEED_GB),
Far::msg_ptr(MSG_SUFFIX_SPEED_TB),
};
return suffixes;
}
class PasswordDialog: public Far::Dialog {
private:
enum {
c_client_xs = 60
};
wstring arc_path;
wstring& password;
int password_ctrl_id;
int ok_ctrl_id;
int cancel_ctrl_id;
intptr_t dialog_proc(intptr_t msg, intptr_t param1, void* param2) {
if ((msg == DN_CLOSE) && (param1 >= 0) && (param1 != cancel_ctrl_id)) {
password = get_text(password_ctrl_id);
}
return default_dialog_proc(msg, param1, param2);
}
public:
PasswordDialog(wstring& password, const wstring& arc_path): Far::Dialog(Far::get_msg(MSG_PASSWORD_TITLE), &c_password_dialog_guid, c_client_xs), password(password), arc_path(arc_path) {
}
bool show() {
label(fit_str(arc_path, c_client_xs), c_client_xs, DIF_SHOWAMPERSAND);
new_line();
label(Far::get_msg(MSG_PASSWORD_PASSWORD));
password_ctrl_id = pwd_edit_box(password);
new_line();
separator();
new_line();
ok_ctrl_id = def_button(Far::get_msg(MSG_BUTTON_OK), DIF_CENTERGROUP);
cancel_ctrl_id = button(Far::get_msg(MSG_BUTTON_CANCEL), DIF_CENTERGROUP);
new_line();
intptr_t item = Far::Dialog::show();
return (item != -1) && (item != cancel_ctrl_id);
}
};
bool password_dialog(wstring& password, const wstring& arc_path) {
return PasswordDialog(password, arc_path).show();
}
class OverwriteDialog: public Far::Dialog {
private:
enum {
c_client_xs = 60
};
wstring file_path;
OverwriteFileInfo src_file_info;
OverwriteFileInfo dst_file_info;
OverwriteDialogKind kind;
OverwriteOptions& options;
int all_ctrl_id;
int overwrite_ctrl_id;
int skip_ctrl_id;
int rename_ctrl_id;
int append_ctrl_id;
int cancel_ctrl_id;
intptr_t dialog_proc(intptr_t msg, intptr_t param1, void* param2) {
if (msg == DN_CLOSE && param1 >= 0 && param1 != cancel_ctrl_id) {
options.all = get_check(all_ctrl_id);
if (param1 == overwrite_ctrl_id)
options.action = oaOverwrite;
else if (param1 == skip_ctrl_id)
options.action = oaSkip;
else if (kind == odkExtract && param1 == rename_ctrl_id)
options.action = oaRename;
else if (kind == odkExtract && param1 == append_ctrl_id)
options.action = oaAppend;
else
FAIL(E_ABORT);
}
return default_dialog_proc(msg, param1, param2);
}
public:
OverwriteDialog(const wstring& file_path, const OverwriteFileInfo& src_file_info, const OverwriteFileInfo& dst_file_info, OverwriteDialogKind kind, OverwriteOptions& options):
Far::Dialog(Far::get_msg(MSG_OVERWRITE_DLG_TITLE), &c_overwrite_dialog_guid, c_client_xs),
file_path(file_path),
src_file_info(src_file_info),
dst_file_info(dst_file_info),
kind(kind),
options(options) {
}
bool show() {
label(fit_str(file_path, c_client_xs), c_client_xs, DIF_SHOWAMPERSAND);
new_line();
label(Far::get_msg(MSG_OVERWRITE_DLG_QUESTION));
new_line();
separator();
new_line();
wstring src_label = Far::get_msg(MSG_OVERWRITE_DLG_SOURCE);
wstring dst_label = Far::get_msg(MSG_OVERWRITE_DLG_DESTINATION);
uintptr_t label_pad = max(src_label.size(), dst_label.size()) + 1;
wstring src_size = uint_to_str(src_file_info.size);
wstring dst_size = uint_to_str(dst_file_info.size);
uintptr_t size_pad = label_pad + max(src_size.size(), dst_size.size()) + 1;
label(src_label);
pad(label_pad);
if (!src_file_info.is_dir) {
label(src_size);
pad(size_pad);
}
label(format_file_time(src_file_info.mtime));
if (CompareFileTime(&src_file_info.mtime, &dst_file_info.mtime) > 0) {
spacer(1);
label(Far::get_msg(MSG_OVERWRITE_DLG_NEWER));
}
new_line();
label(dst_label);
pad(label_pad);
if (!dst_file_info.is_dir) {
label(dst_size);
pad(size_pad);
}
label(format_file_time(dst_file_info.mtime));
if (CompareFileTime(&src_file_info.mtime, &dst_file_info.mtime) < 0) {
spacer(1);
label(Far::get_msg(MSG_OVERWRITE_DLG_NEWER));
}
new_line();
separator();
new_line();
all_ctrl_id = check_box(Far::get_msg(MSG_OVERWRITE_DLG_ALL), false);
new_line();
separator();
new_line();
overwrite_ctrl_id = def_button(Far::get_msg(MSG_OVERWRITE_DLG_OVERWRITE), DIF_CENTERGROUP);
spacer(1);
skip_ctrl_id = button(Far::get_msg(MSG_OVERWRITE_DLG_SKIP), DIF_CENTERGROUP);
spacer(1);
if (kind == odkExtract) {
rename_ctrl_id = button(Far::get_msg(MSG_OVERWRITE_DLG_RENAME), DIF_CENTERGROUP);
spacer(1);
append_ctrl_id = button(Far::get_msg(MSG_OVERWRITE_DLG_APPEND), DIF_CENTERGROUP);
spacer(1);
}
cancel_ctrl_id = button(Far::get_msg(MSG_OVERWRITE_DLG_CANCEL), DIF_CENTERGROUP);
new_line();
intptr_t item = Far::Dialog::show();
return item >= 0 && item != cancel_ctrl_id;
}
};
bool overwrite_dialog(const wstring& file_path, const OverwriteFileInfo& src_file_info, const OverwriteFileInfo& dst_file_info, OverwriteDialogKind kind, OverwriteOptions& options) {
return OverwriteDialog(file_path, src_file_info, dst_file_info, kind, options).show();
}
class ExtractDialog: public Far::Dialog {
private:
enum {
c_client_xs = 60
};
ExtractOptions& options;
int dst_dir_ctrl_id;
int ignore_errors_ctrl_id;
int oa_ask_ctrl_id;
int oa_overwrite_ctrl_id;
int oa_skip_ctrl_id;
int oa_rename_ctrl_id;
int oa_append_ctrl_id;
int move_files_ctrl_id;
int password_ctrl_id;
int separate_dir_ctrl_id;
int delete_archive_ctrl_id;
int open_dir_ctrl_id;
int ok_ctrl_id;
int cancel_ctrl_id;
int save_params_ctrl_id;
void read_controls(ExtractOptions& options) {
auto dst = search_and_replace(strip(get_text(dst_dir_ctrl_id)), L"\"", wstring());
bool expand_env = add_trailing_slash(options.dst_dir) != add_trailing_slash(dst);
options.dst_dir = expand_env ? expand_env_vars(dst) : dst;
options.ignore_errors = get_check(ignore_errors_ctrl_id);
if (get_check(oa_ask_ctrl_id)) options.overwrite = oaAsk;
else if (get_check(oa_overwrite_ctrl_id)) options.overwrite = oaOverwrite;
else if (get_check(oa_skip_ctrl_id)) options.overwrite = oaSkip;
else if (get_check(oa_rename_ctrl_id)) options.overwrite = oaRename;
else if (get_check(oa_append_ctrl_id)) options.overwrite = oaAppend;
else options.overwrite = oaAsk;
if (options.move_files != triUndef)
options.move_files = get_check3(move_files_ctrl_id);
options.password = get_text(password_ctrl_id);
options.separate_dir = get_check3(separate_dir_ctrl_id);
options.delete_archive = get_check(delete_archive_ctrl_id);
if (options.open_dir != triUndef)
options.open_dir = get_check3(open_dir_ctrl_id);
}
intptr_t dialog_proc(intptr_t msg, intptr_t param1, void* param2) {
if ((msg == DN_CLOSE) && (param1 >= 0) && (param1 != cancel_ctrl_id)) {
read_controls(options);
}
else if (msg == DN_BTNCLICK && param1 == delete_archive_ctrl_id) {
enable(move_files_ctrl_id, options.move_files != triUndef && !get_check(delete_archive_ctrl_id));
}
else if (msg == DN_BTNCLICK && param1 == save_params_ctrl_id) {
ExtractOptions options;
read_controls(options);
g_options.extract_ignore_errors = options.ignore_errors;
g_options.extract_overwrite = options.overwrite;
g_options.extract_separate_dir = options.separate_dir;
if (options.open_dir != triUndef)
g_options.extract_open_dir = options.open_dir == triTrue;
g_options.save();
Far::info_dlg(c_extract_params_saved_dialog_guid, Far::get_msg(MSG_EXTRACT_DLG_TITLE), Far::get_msg(MSG_EXTRACT_DLG_PARAMS_SAVED));
set_focus(ok_ctrl_id);
}
return default_dialog_proc(msg, param1, param2);
}
public:
ExtractDialog(ExtractOptions& options): Far::Dialog(Far::get_msg(MSG_EXTRACT_DLG_TITLE), &c_extract_dialog_guid, c_client_xs, L"Extract"), options(options) {
}
bool show() {
label(Far::get_msg(MSG_EXTRACT_DLG_DST_DIR));
new_line();
dst_dir_ctrl_id = history_edit_box(add_trailing_slash(options.dst_dir), L"arclite.extract_dir", c_client_xs, DIF_EDITPATH);
new_line();
separator();
new_line();
ignore_errors_ctrl_id = check_box(Far::get_msg(MSG_EXTRACT_DLG_IGNORE_ERRORS), options.ignore_errors);
new_line();
label(Far::get_msg(MSG_EXTRACT_DLG_OA));
new_line();
spacer(2);
oa_ask_ctrl_id = radio_button(Far::get_msg(MSG_EXTRACT_DLG_OA_ASK), options.overwrite == oaAsk);
spacer(2);
oa_overwrite_ctrl_id = radio_button(Far::get_msg(MSG_EXTRACT_DLG_OA_OVERWRITE), options.overwrite == oaOverwrite);
spacer(2);
oa_skip_ctrl_id = radio_button(Far::get_msg(MSG_EXTRACT_DLG_OA_SKIP), options.overwrite == oaSkip);
new_line();
spacer(2);
oa_rename_ctrl_id = radio_button(Far::get_msg(MSG_EXTRACT_DLG_OA_RENAME), options.overwrite == oaRename);
spacer(2);
oa_append_ctrl_id = radio_button(Far::get_msg(MSG_EXTRACT_DLG_OA_APPEND), options.overwrite == oaAppend);
new_line();
move_files_ctrl_id = check_box3(Far::get_msg(MSG_EXTRACT_DLG_MOVE_FILES), options.move_files, options.move_files == triUndef ? DIF_DISABLE : 0);
new_line();
delete_archive_ctrl_id = check_box(Far::get_msg(MSG_EXTRACT_DLG_DELETE_ARCHIVE), options.delete_archive);
new_line();
separate_dir_ctrl_id = check_box3(Far::get_msg(MSG_EXTRACT_DLG_SEPARATE_DIR), options.separate_dir);
new_line();
open_dir_ctrl_id = check_box3(Far::get_msg(MSG_EXTRACT_DLG_OPEN_DIR), options.open_dir, options.open_dir == triUndef ? DIF_DISABLE : 0);
new_line();
label(Far::get_msg(MSG_EXTRACT_DLG_PASSWORD));
password_ctrl_id = pwd_edit_box(options.password, 20);
new_line();
separator();
new_line();
ok_ctrl_id = def_button(Far::get_msg(MSG_BUTTON_OK), DIF_CENTERGROUP);
cancel_ctrl_id = button(Far::get_msg(MSG_BUTTON_CANCEL), DIF_CENTERGROUP);
save_params_ctrl_id = button(Far::get_msg(MSG_EXTRACT_DLG_SAVE_PARAMS), DIF_CENTERGROUP | DIF_BTNNOCLOSE);
new_line();
intptr_t item = Far::Dialog::show();
return (item != -1) && (item != cancel_ctrl_id);
}
};
bool extract_dialog(ExtractOptions& options) {
return ExtractDialog(options).show();
}
void retry_or_ignore_error(const Error& error, bool& ignore, bool& ignore_errors, ErrorLog& error_log, ProgressMonitor& progress, bool can_retry, bool can_ignore) {
if (error.code == E_ABORT)
throw error;
ignore = ignore_errors;
if (!ignore) {
wostringstream st;
st << Far::get_msg(MSG_PLUGIN_NAME) << L'\n';
if (error.code != E_MESSAGE) {
wstring sys_msg = get_system_message(error.code, Far::get_lang_id());
if (!sys_msg.empty())
st << word_wrap(sys_msg, Far::get_optimal_msg_width()) << L'\n';
}
for (list<wstring>::const_iterator msg = error.messages.begin(); msg != error.messages.end(); msg++) {
st << word_wrap(*msg, Far::get_optimal_msg_width()) << L'\n';
}
st << extract_file_name(widen(error.file)) << L':' << error.line << L'\n';
unsigned button_cnt = 0;
unsigned retry_id, ignore_id, ignore_all_id, cancel_id;
if (can_retry) {
st << Far::get_msg(MSG_BUTTON_RETRY) << L'\n';
retry_id = button_cnt;
button_cnt++;
}
if (can_ignore) {
st << Far::get_msg(MSG_BUTTON_IGNORE) << L'\n';
ignore_id = button_cnt;
button_cnt++;
st << Far::get_msg(MSG_BUTTON_IGNORE_ALL) << L'\n';
ignore_all_id = button_cnt;
button_cnt++;
}
st << Far::get_msg(MSG_BUTTON_CANCEL) << L'\n';
cancel_id = button_cnt;
button_cnt++;
ProgressSuspend ps(progress);
intptr_t id = Far::message(c_retry_ignore_dialog_guid, st.str(), button_cnt, FMSG_WARNING);
if (can_retry && id == retry_id) {
}
else if (can_ignore && id == ignore_id) {
ignore = true;
}
else if (can_ignore && id == ignore_all_id) {
ignore = true;
ignore_errors = true;
}
else
FAIL(E_ABORT);
}
if (ignore)
error_log.push_back(error);
}
void show_error_log(const ErrorLog& error_log) {
wstring msg;
msg += Far::get_msg(MSG_LOG_TITLE) + L'\n';
msg += Far::get_msg(MSG_LOG_INFO) + L'\n';
msg += Far::get_msg(MSG_LOG_CLOSE) + L'\n';
msg += Far::get_msg(MSG_LOG_SHOW) + L'\n';
if (Far::message(c_error_log_dialog_guid, msg, 2, FMSG_WARNING) != 1) return;
TempFile temp_file;
File file(temp_file.get_path(), GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
const wchar_t sig = 0xFEFF;
file.write(&sig, sizeof(sig));
wstring line;
for (ErrorLog::const_iterator error = error_log.begin(); error != error_log.end(); error++) {
line.clear();
if (error->code != E_MESSAGE) {
wstring sys_msg = get_system_message(error->code, Far::get_lang_id());
if (!sys_msg.empty())
line.append(sys_msg).append(1, L'\n');
}
for (list<wstring>::const_iterator err_msg = error->messages.begin(); err_msg != error->messages.end(); err_msg++) {
line.append(*err_msg).append(1, L'\n');
}
line.append(1, L'\n');
file.write(line.data(), static_cast<unsigned>(line.size()) * sizeof(wchar_t));
}
Far::viewer(temp_file.get_path(), Far::get_msg(MSG_LOG_TITLE), VF_DISABLEHISTORY | VF_ENABLE_F6);
}
bool operator==(const SfxOptions& o1, const SfxOptions& o2) {
if (o1.name != o2.name || o1.replace_icon != o2.replace_icon ||
o1.replace_version != o2.replace_version || o1.append_install_config != o2.append_install_config)
return false;
if (o1.replace_icon) {
if (o1.icon_path != o2.icon_path)
return false;
}
if (o1.replace_version) {
if (o1.ver_info.version != o2.ver_info.version || o1.ver_info.comments != o2.ver_info.comments ||
o1.ver_info.company_name != o2.ver_info.company_name || o1.ver_info.file_description != o2.ver_info.file_description ||
o1.ver_info.legal_copyright != o2.ver_info.legal_copyright || o1.ver_info.product_name != o2.ver_info.product_name)
return false;
}
if (o1.append_install_config) {
if (o1.install_config.title != o2.install_config.title || o1.install_config.begin_prompt != o2.install_config.begin_prompt ||
o1.install_config.progress != o2.install_config.progress || o1.install_config.run_program != o2.install_config.run_program ||
o1.install_config.directory != o2.install_config.directory || o1.install_config.execute_file != o2.install_config.execute_file ||
o1.install_config.execute_parameters != o2.install_config.execute_parameters)
return false;
}
return true;
}
bool operator==(const ProfileOptions& o1, const ProfileOptions& o2) {
if (o1.arc_type != o2.arc_type || o1.level != o2.level)
return false;
bool is_7z = o1.arc_type == c_7z;
bool is_zip = o1.arc_type == c_zip;
bool is_compressed = o1.level != 0;
if (is_7z) {
if (is_compressed) {
if (o1.method != o2.method)
return false;
}
if (o1.solid != o2.solid)
return false;
}
if (o1.advanced != o2.advanced)
return false;
if (is_7z || is_zip) {
if (o1.encrypt != o2.encrypt)
return false;
bool is_encrypted = o1.encrypt;
if (is_encrypted) {
if (o1.password != o2.password)
return false;
if (is_7z) {
if (o1.encrypt_header != o2.encrypt_header)
return false;
}
}
}
bool is_sfx = o1.create_sfx;
if (is_7z) {
if (o1.create_sfx != o2.create_sfx)
return false;
if (is_sfx) {
if (!(o1.sfx_options == o2.sfx_options))
return false;
}
}
if (!is_7z || !is_sfx) {
if (o1.enable_volumes != o2.enable_volumes)
return false;
bool is_multi_volume = o1.enable_volumes;
if (is_multi_volume) {
if (o1.volume_size != o2.volume_size)
return false;
}
}
if (o1.move_files != o2.move_files || o1.ignore_errors != o2.ignore_errors)
return false;
return true;
}
struct ArchiveType {
unsigned name_id;
const ArcType& value;
};
const ArchiveType c_archive_types[] = {
{ MSG_COMPRESSION_ARCHIVE_7Z, c_7z },
{ MSG_COMPRESSION_ARCHIVE_ZIP, c_zip },
};
struct CompressionLevel {
unsigned name_id;
unsigned value;
};
const CompressionLevel c_levels[] = {
{ MSG_COMPRESSION_LEVEL_STORE, 0 },
{ MSG_COMPRESSION_LEVEL_FASTEST, 1 },
{ MSG_COMPRESSION_LEVEL_FAST, 3 },
{ MSG_COMPRESSION_LEVEL_NORMAL, 5 },
{ MSG_COMPRESSION_LEVEL_MAXIMUM, 7 },
{ MSG_COMPRESSION_LEVEL_ULTRA, 9 },
};
struct CompressionMethod {
unsigned name_id;
const wchar_t* value;
};
const CompressionMethod c_methods[] = {
{ MSG_COMPRESSION_METHOD_LZMA, c_method_lzma },
{ MSG_COMPRESSION_METHOD_LZMA2, c_method_lzma2 },
{ MSG_COMPRESSION_METHOD_PPMD, c_method_ppmd },
};
class UpdateDialog: public Far::Dialog {
private:
enum {
c_client_xs = 68
};
bool new_arc;
wstring default_arc_name;
vector<ArcType> main_formats;
vector<ArcType> other_formats;
UpdateOptions& options;
UpdateProfiles& profiles;
int profile_ctrl_id;
int save_profile_ctrl_id;
int delete_profile_ctrl_id;
int arc_path_ctrl_id;
int arc_path_eval_ctrl_id;
int append_ext_ctrl_id;
int main_formats_ctrl_id;
int other_formats_ctrl_id;
int level_ctrl_id;
int method_ctrl_id;
int solid_ctrl_id;
int advanced_ctrl_id;
int encrypt_ctrl_id;
int encrypt_header_ctrl_id;
int show_password_ctrl_id;
int password_ctrl_id;
int password_verify_ctrl_id;
int password_visible_ctrl_id;
int create_sfx_ctrl_id;
int sfx_options_ctrl_id;
int move_files_ctrl_id;
int open_shared_ctrl_id;
int ignore_errors_ctrl_id;
int enable_volumes_ctrl_id;
int volume_size_ctrl_id;
int oa_ask_ctrl_id;
int oa_overwrite_ctrl_id;
int oa_skip_ctrl_id;
int enable_filter_ctrl_id;
int ok_ctrl_id;
int cancel_ctrl_id;
int save_params_ctrl_id;
wstring old_ext;
ArcType arc_type;
unsigned level;
wstring get_default_ext() const {
wstring ext;
bool create_sfx = get_check(create_sfx_ctrl_id);
bool enable_volumes = get_check(enable_volumes_ctrl_id);
if (ArcAPI::formats().count(arc_type))
ext = ArcAPI::formats().at(arc_type).default_extension();
if (create_sfx && arc_type == c_7z)
ext += c_sfx_ext;
else if (enable_volumes)
ext += c_volume_ext;
return ext;
}
bool change_extension() {
assert(new_arc);
wstring new_ext = get_default_ext();
if (old_ext.empty() || new_ext.empty())
return false;
wstring arc_path = get_text(arc_path_ctrl_id);
wstring file_name = extract_file_name(strip(unquote(strip(arc_path))));
size_t pos = file_name.find_last_of(L'.');
if (pos == wstring::npos || pos == 0)
return false;
wstring ext = file_name.substr(pos);
if (_wcsicmp(ext.c_str(), c_sfx_ext) == 0 || _wcsicmp(ext.c_str(), c_volume_ext) == 0) {
pos = file_name.find_last_of(L'.', pos - 1);
if (pos != wstring::npos && pos != 0)
ext = file_name.substr(pos);
}
if (_wcsicmp(old_ext.c_str(), ext.c_str()) != 0)
return false;
pos = arc_path.find_last_of(ext) - (ext.size() - 1);
arc_path.replace(pos, ext.size(), new_ext);
set_text(arc_path_ctrl_id, arc_path);
old_ext = new_ext;
return true;
}
void update_level_list() {
unsigned level_sel = get_list_pos(level_ctrl_id);
unsigned new_level_sel = -1;
for (unsigned i = 0; i < ARRAYSIZE(c_levels); ++i) {
bool skip = c_levels[i].value == 0 && (arc_type == c_bzip2 || arc_type == c_gzip || arc_type == c_xz);
skip = skip || (c_levels[i].value != 0 && (arc_type == c_wim || arc_type == c_tar));
skip = skip || ((c_levels[i].value == 1 || c_levels[i].value == 3) && arc_type == c_zip);
skip = skip || ((c_levels[i].value == 7 || c_levels[i].value == 9) && arc_type == c_bzip2);
skip = skip || ((c_levels[i].value == 1 || c_levels[i].value == 3) && arc_type == c_gzip);
FarListGetItem flgi;
memzero(flgi);
flgi.StructSize = sizeof(FarListGetItem);
flgi.ItemIndex = i;
CHECK(send_message(DM_LISTGETITEM, level_ctrl_id, &flgi));
if ((skip && (flgi.Item.Flags & LIF_DISABLE) == 0) || (!skip && (flgi.Item.Flags & LIF_DISABLE) != 0)) {
FarListUpdate flu;
memzero(flu);
flu.StructSize = sizeof(FarListUpdate);
flu.Index = i;
flu.Item.Flags = skip ? LIF_DISABLE : 0;
flu.Item.Text = Far::msg_ptr(c_levels[i].name_id);
send_message(DM_LISTUPDATE, level_ctrl_id, &flu);
}
if (new_level_sel == -1 && c_levels[i].value == 5) {
new_level_sel = i;
}
if (c_levels[i].value == level && !skip) {
new_level_sel = i;
}
}
if (new_level_sel != level_sel) {
set_list_pos(level_ctrl_id, new_level_sel);
}
}
void set_control_state() {
DisableEvents de(*this);
bool is_7z = arc_type == c_7z;
bool is_zip = arc_type == c_zip;
update_level_list();
bool is_compressed = get_list_pos(level_ctrl_id) != 0;
for (int i = method_ctrl_id - 1; i <= method_ctrl_id; i++) {
enable(i, is_7z & is_compressed);
}
enable(solid_ctrl_id, is_7z && is_compressed);
enable(encrypt_ctrl_id, is_7z || is_zip);
bool encrypt = get_check(encrypt_ctrl_id);
for (int i = encrypt_ctrl_id + 1; i <= password_visible_ctrl_id; i++) {
enable(i, encrypt && (is_7z || is_zip));
}
enable(encrypt_header_ctrl_id, is_7z && encrypt);
bool show_password = get_check(show_password_ctrl_id);
for (int i = password_ctrl_id - 1; i <= password_verify_ctrl_id; i++) {
set_visible(i, !show_password);
}
for (int i = password_visible_ctrl_id - 1; i <= password_visible_ctrl_id; i++) {
set_visible(i, show_password);
}
if (new_arc) {
change_extension();
bool create_sfx = get_check(create_sfx_ctrl_id);
bool enable_volumes = get_check(enable_volumes_ctrl_id);
if (create_sfx && enable_volumes)
enable_volumes = false;
enable(create_sfx_ctrl_id, is_7z && !enable_volumes);
for (int i = create_sfx_ctrl_id + 1; i <= sfx_options_ctrl_id; i++) {
enable(i, is_7z && create_sfx && !enable_volumes);
}
enable(enable_volumes_ctrl_id, !is_7z || !create_sfx);
for (int i = enable_volumes_ctrl_id + 1; i <= volume_size_ctrl_id; i++) {
enable(i, enable_volumes && (!is_7z || !create_sfx));
}
bool other_format = get_check(other_formats_ctrl_id);
enable(other_formats_ctrl_id + 1, other_format);
unsigned profile_idx = get_list_pos(profile_ctrl_id);
enable(delete_profile_ctrl_id, profile_idx != -1 && profile_idx < profiles.size());
}
enable(move_files_ctrl_id, !get_check(enable_filter_ctrl_id));
}
wstring eval_arc_path() {
wstring arc_path = expand_env_vars(search_and_replace(expand_macros(strip(get_text(arc_path_ctrl_id))), L"\"", wstring()));
if (arc_path.empty() || arc_path.back() == L'\\')
arc_path += default_arc_name + get_default_ext();
if (get_check(append_ext_ctrl_id)) {
wstring ext = get_default_ext();
if (ext.size() > arc_path.size() || upcase(arc_path.substr(arc_path.size() - ext.size())) != upcase(ext))
arc_path += ext;
}
return Far::get_absolute_path(arc_path);
}
void read_controls(UpdateOptions& options) {
if (new_arc) {
options.append_ext = get_check(append_ext_ctrl_id);
for (unsigned i = 0; i < main_formats.size(); i++) {
if (get_check(main_formats_ctrl_id + i)) {
options.arc_type = c_archive_types[i].value;
break;
}
}
if (!other_formats.empty() && get_check(other_formats_ctrl_id)) {
options.arc_type = other_formats[get_list_pos(other_formats_ctrl_id + 1)];
}
if (options.arc_type.empty()) {
FAIL_MSG(Far::get_msg(MSG_UPDATE_DLG_WRONG_ARC_TYPE));
}
}
else {
options.arc_type = arc_type;
}
bool is_7z = options.arc_type == c_7z;
bool is_zip = options.arc_type == c_zip;
options.level = -1;
unsigned level_sel = get_list_pos(level_ctrl_id);
if (level_sel < ARRAYSIZE(c_levels))
options.level = c_levels[level_sel].value;
if (options.level == -1) {
FAIL_MSG(Far::get_msg(MSG_UPDATE_DLG_WRONG_LEVEL));
}
bool is_compressed = options.level != 0;
if (is_compressed) {
if (is_7z) {
options.method.clear();
unsigned method_sel = get_list_pos(method_ctrl_id);
if (method_sel < ARRAYSIZE(c_methods))
options.method = c_methods[method_sel].value;
if (options.method.empty()) {
FAIL_MSG(Far::get_msg(MSG_UPDATE_DLG_WRONG_METHOD));
}
}
}
else
options.method = c_method_copy;
if (is_7z && is_compressed)
options.solid = get_check(solid_ctrl_id);
options.advanced = get_text(advanced_ctrl_id);
if (is_7z || is_zip)
options.encrypt = get_check(encrypt_ctrl_id);
if (options.encrypt) {
options.show_password = get_check(show_password_ctrl_id);
if (options.show_password) {
options.password = get_text(password_visible_ctrl_id);
}
else {
options.password = get_text(password_ctrl_id);
wstring password_verify = get_text(password_verify_ctrl_id);
if (options.password != password_verify) {
FAIL_MSG(Far::get_msg(MSG_UPDATE_DLG_PASSWORDS_DONT_MATCH));
}
}
if (options.password.empty()) {
FAIL_MSG(Far::get_msg(MSG_UPDATE_DLG_PASSWORD_IS_EMPTY));
}
if (is_7z)
options.encrypt_header = get_check3(encrypt_header_ctrl_id);
}
if (new_arc) {
options.create_sfx = is_7z && get_check(create_sfx_ctrl_id);
if (options.create_sfx) {
options.sfx_options = this->options.sfx_options;
uintptr_t sfx_id = ArcAPI::sfx().find_by_name(options.sfx_options.name);
if (sfx_id >= ArcAPI::sfx().size())
FAIL_MSG(Far::get_msg(MSG_SFX_OPTIONS_DLG_WRONG_MODULE));
if (options.method == c_method_ppmd && !ArcAPI::sfx()[sfx_id].all_codecs())
FAIL_MSG(Far::get_msg(MSG_UPDATE_DLG_SFX_NO_PPMD));
if (options.encrypt && !ArcAPI::sfx()[sfx_id].all_codecs())
FAIL_MSG(Far::get_msg(MSG_UPDATE_DLG_SFX_NO_ENCRYPT));
}
options.enable_volumes = get_check(enable_volumes_ctrl_id);
if (options.enable_volumes) {
options.volume_size = get_text(volume_size_ctrl_id);
if (parse_size_string(options.volume_size) < c_min_volume_size) {
FAIL_MSG(Far::get_msg(MSG_UPDATE_DLG_WRONG_VOLUME_SIZE));
}
}
}
options.move_files = get_check(enable_filter_ctrl_id) ? false : get_check(move_files_ctrl_id);
options.open_shared = get_check(open_shared_ctrl_id);
options.ignore_errors = get_check(ignore_errors_ctrl_id);
if (!new_arc) {
if (get_check(oa_ask_ctrl_id)) options.overwrite = oaAsk;
else if (get_check(oa_overwrite_ctrl_id)) options.overwrite = oaOverwrite;
else if (get_check(oa_skip_ctrl_id)) options.overwrite = oaSkip;
else options.overwrite = oaAsk;
}
}
void write_controls(const ProfileOptions& options) {
DisableEvents de(*this);
if (new_arc) {
arc_type = options.arc_type;
for (unsigned i = 0; i < main_formats.size(); i++) {
if (options.arc_type == main_formats[i]) {
set_check(main_formats_ctrl_id + i);
break;
}
}
for (unsigned i = 0; i < other_formats.size(); i++) {
if (options.arc_type == other_formats[i]) {
set_check(other_formats_ctrl_id);
set_list_pos(other_formats_ctrl_id + 1, i);
break;
}
}
}
level = options.level;
unsigned level_sel = 0;
for (unsigned i = 0; i < ARRAYSIZE(c_levels); i++) {
if (options.level == c_levels[i].value) {
level_sel = i;
break;
}
}
set_list_pos(level_ctrl_id, level_sel);
wstring method = options.method.empty() && options.arc_type == c_7z ? c_methods[0].value : options.method;
unsigned method_sel = 0;
for (unsigned i = 0; i < ARRAYSIZE(c_methods); i++) {
if (method == c_methods[i].value) {
method_sel = i;
break;
}
}
set_list_pos(method_ctrl_id, method_sel);
set_check(solid_ctrl_id, options.solid);
set_text(advanced_ctrl_id, options.advanced);
set_check(encrypt_ctrl_id, options.encrypt);
set_check3(encrypt_header_ctrl_id, options.encrypt_header);
set_text(password_ctrl_id, options.password);
set_text(password_verify_ctrl_id, options.password);
set_text(password_visible_ctrl_id, options.password);
if (new_arc) {
set_check(create_sfx_ctrl_id, options.create_sfx);
this->options.sfx_options = options.sfx_options;
set_check(enable_volumes_ctrl_id, options.enable_volumes);
set_text(volume_size_ctrl_id, options.volume_size);
}