forked from doublecmd/doublecmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfextractdlg.pas
348 lines (303 loc) · 11 KB
/
fextractdlg.pas
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
{
Double Commander
-------------------------------------------------------------------------
File unpacking window
Copyright (C) 2007-2022 Alexander Koblov ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}
unit fExtractDlg;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, StdCtrls, EditBtn, ExtCtrls, Buttons,
Menus, DividerBevel, uFile, uFileSource, uArchiveFileSource, fButtonForm,
uOperationsManager;
type
{ TfrmExtractDlg }
TfrmExtractDlg = class(TfrmButtonForm)
cbExtractPath: TCheckBox;
cbInSeparateFolder: TCheckBox;
cbOverwrite: TCheckBox;
DividerBevel: TDividerBevel;
edtPassword: TEdit;
edtExtractTo: TDirectoryEdit;
lblExtractTo: TLabel;
lblPassword: TLabel;
cbFileMask: TComboBox;
lblFileMask: TLabel;
pnlCheckBoxes: TPanel;
procedure cbExtractPathChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
FArcType: String;
procedure SwitchOptions;
procedure ExtractArchive(ArchiveFileSource: IArchiveFileSource; TargetFileSource: IFileSource;
const TargetPath, TargetMask: String; QueueId: TOperationsManagerQueueIdentifier);
protected
procedure DoAutoSize; override;
public
{ public declarations }
end;
// Frees 'SourceFiles'.
procedure ShowExtractDlg(TheOwner: TComponent; SourceFileSource: IFileSource; var SourceFiles: TFiles;
TargetFileSource: IFileSource; sDestPath: String);
implementation
{$R *.lfm}
uses
Dialogs,
uGlobs, uDCUtils, uShowMsg, uLng, DCStrUtils,
uFileSourceOperation,
uFileSystemFileSource,
uArchiveFileSourceUtil,
uFileSourceOperationTypes,
uMultiArchiveFileSource,
uMultiArchiveCopyOutOperation,
uWcxArchiveFileSource,
uWcxArchiveCopyOutOperation,
uFileSourceOperationOptions,
uArchiveCopyOperation,
uMasks;
function GetTargetPath(FileSource: IArchiveFileSource; const TargetPath: String): String;
begin
// if destination path is not absolute then extract to path there archive is located
if GetPathType(TargetPath) <> ptAbsolute then
Result := GetAbsoluteFileName(ExtractFilePath(FileSource.ArchiveFileName), TargetPath)
else
Result := IncludeTrailingPathDelimiter(TargetPath);
end;
procedure ShowExtractDlg(TheOwner: TComponent; SourceFileSource: IFileSource; var SourceFiles: TFiles;
TargetFileSource: IFileSource; sDestPath: String);
var
Result: Boolean;
I, Count: Integer;
extractDialog: TfrmExtractDlg;
Operation: TFileSourceOperation;
ArchiveFileSource: IArchiveFileSource;
QueueId: TOperationsManagerQueueIdentifier;
begin
if not TargetFileSource.IsClass(TFileSystemFileSource) then
begin
msgWarning(rsMsgErrNotSupported);
Exit;
end;
extractDialog := TfrmExtractDlg.Create(TheOwner);
if Assigned(extractDialog) then
try
with extractDialog do
begin
Count := SourceFiles.Count;
edtExtractTo.Text := sDestPath;
if SourceFileSource.IsClass(TArchiveFileSource) then
cbInSeparateFolder.Visible := False;
cbFileMask.Items.Assign(glsMaskHistory);
EnableControl(edtPassword, False);
// If one archive is selected
if (Count = 1) then
begin
FArcType:= SourceFiles[0].Extension;
SwitchOptions;
end;
// Show form
Result := (ShowModal = mrOk);
if Result then
begin
if glsMaskHistory.IndexOf(cbFileMask.Text) < 0 then
glsMaskHistory.Add(cbFileMask.Text);
sDestPath := edtExtractTo.Text;
// if in archive
if SourceFileSource.IsClass(TArchiveFileSource) then
begin
if fsoCopyOut in SourceFileSource.GetOperationsTypes then
begin
sDestPath := GetTargetPath(SourceFileSource as IArchiveFileSource, sDestPath);
Operation := SourceFileSource.CreateCopyOutOperation(TargetFileSource, SourceFiles, sDestPath);
if Assigned(Operation) then
begin
TArchiveCopyOutOperation(Operation).ExtractMask := cbFileMask.Text;
// Start operation.
OperationsManager.AddOperation(Operation, QueueIdentifier, False);
end
else
msgWarning(rsMsgNotImplemented);
end
else
msgWarning(rsMsgErrNotSupported);
end
else
// if filesystem
if SourceFileSource.IsClass(TFileSystemFileSource) then
begin
// if archives count > 1 then put to queue
if (Count > 1) and (QueueIdentifier = FreeOperationsQueueId) then
QueueId := OperationsManager.GetNewQueueIdentifier
else begin
QueueId := QueueIdentifier;
end;
// extract all selected archives
for I := 0 to Count - 1 do
begin
try
// Check if there is a ArchiveFileSource for possible archive.
ArchiveFileSource := GetArchiveFileSource(SourceFileSource, SourceFiles[i], EmptyStr, False, True);
// Try to determine archive type by content
if (ArchiveFileSource = nil) then begin
ArchiveFileSource := GetArchiveFileSource(SourceFileSource, SourceFiles[i], EmptyStr, True, True);
end;
// Extract current item
ExtractArchive(ArchiveFileSource, TargetFileSource, sDestPath, cbFileMask.Text, QueueId);
except
on E: Exception do
begin
MessageDlg(E.Message, mtError, [mbOK], 0);
end;
end;
end; // for
end
else
msgWarning(rsMsgErrNotSupported);
gExtractOverwrite := cbOverwrite.Checked;
end; // if Result
end;
finally
if Assigned(extractDialog) then
FreeAndNil(extractDialog);
if Assigned(SourceFiles) then
FreeAndNil(SourceFiles);
end;
end;
{ TfrmExtractDlg }
procedure TfrmExtractDlg.FormCreate(Sender: TObject);
begin
InitPropStorage(Self);
cbOverwrite.Checked := gExtractOverwrite;
end;
procedure TfrmExtractDlg.cbExtractPathChange(Sender: TObject);
begin
SwitchOptions;
end;
procedure TfrmExtractDlg.SwitchOptions;
var
I: LongInt;
begin
// Check for this archive will be processed by MultiArc
for I := 0 to gMultiArcList.Count - 1 do
with gMultiArcList.Items[I] do
begin
if FEnabled and MatchesMaskList(FArcType, FExtension, ',') then
begin
// If addon supports unpacking without path
if (Length(FExtractWithoutPath) <> 0) then
cbExtractPath.Enabled:= True
else
begin
cbExtractPath.Enabled:= False;
cbExtractPath.Checked:= True;
end;
// If addon supports unpacking with password
if cbExtractPath.Checked then
EnableControl(edtPassword, (Pos('%W', FExtract) <> 0))
else
EnableControl(edtPassword, (Pos('%W', FExtractWithoutPath) <> 0));
Break;
end;
end;
end;
procedure TfrmExtractDlg.ExtractArchive(ArchiveFileSource: IArchiveFileSource;
TargetFileSource: IFileSource; const TargetPath, TargetMask: String;
QueueId: TOperationsManagerQueueIdentifier);
var
FilesToExtract: TFiles;
Operation: TFileSourceOperation;
sTmpPath: string;
begin
if Assigned(ArchiveFileSource) then
begin
// Check if List and CopyOut are supported.
if [fsoList, fsoCopyOut] * ArchiveFileSource.GetOperationsTypes = [fsoList, fsoCopyOut] then
begin
// Get files to extract.
FilesToExtract := ArchiveFileSource.GetFiles(ArchiveFileSource.GetRootDir);
if Assigned(FilesToExtract) then
try
sTmpPath := GetTargetPath(ArchiveFileSource, TargetPath);
// if each archive in separate folder
if cbInSeparateFolder.Checked then
begin
sTmpPath := sTmpPath + ExtractOnlyFileName(ArchiveFileSource.ArchiveFileName) + PathDelim;
end;
// extract all files
Operation := ArchiveFileSource.CreateCopyOutOperation(TargetFileSource, FilesToExtract, sTmpPath);
// Set operation specific options
if Assigned(Operation) then
begin
if cbInSeparateFolder.State = cbGrayed then
begin
with Operation as TArchiveCopyOutOperation do
ExtractFlags:= ExtractFlags + [efSmartExtract];
end;
if ArchiveFileSource.IsInterface(IMultiArchiveFileSource) then
begin
with Operation as TMultiArchiveCopyOutOperation do
begin
ExtractMask := TargetMask;
Password := edtPassword.Text;
ExtractWithoutPath:= not cbExtractPath.Checked;
end;
end
else if ArchiveFileSource.IsInterface(IWcxArchiveFileSource) then
begin
with Operation as TWcxArchiveCopyOutOperation do
begin
ExtractMask := TargetMask;
if cbOverwrite.Checked then
FileExistsOption := fsoofeOverwrite;
ExtractWithoutPath:= not cbExtractPath.Checked;
end;
end;
// Start operation.
OperationsManager.AddOperation(Operation, QueueId, False);
end
else
msgWarning(rsMsgNotImplemented);
finally
if Assigned(FilesToExtract) then
FreeAndNil(FilesToExtract);
end;
end
else
msgWarning(rsMsgErrNotSupported);
end;
end;
procedure TfrmExtractDlg.DoAutoSize;
var
Index: Integer;
AControl: TControl;
AMaxControl: TControl;
AMaxWidth: Integer = 0;
begin
inherited DoAutoSize;
for Index:= 0 to pnlContent.ControlCount - 1 do
begin
AControl:= pnlContent.Controls[Index];
if AControl is TCustomLabel then
begin
if AControl.Width > AMaxWidth then
begin
AMaxControl:= AControl;
AMaxWidth:= AControl.Width;
end;
end;
end;
cbFileMask.AnchorSide[akLeft].Control:= AMaxControl;
end;
end.