1 |
unit _TemplateFileList; |
2 |
|
3 |
interface |
4 |
|
5 |
uses |
6 |
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, |
7 |
Dialogs, _TemplateFile, StdCtrls, ExtCtrls, Menus, Buttons, |
8 |
TypeDefs; |
9 |
|
10 |
type |
11 |
TForm_TemplateFileList = class(TForm_TemplateFile) |
12 |
panel_files: TPanel; |
13 |
filelist: TListBox; |
14 |
panel_extension: TPanel; |
15 |
label_sort2: TLabel; |
16 |
label_sort1: TLabel; |
17 |
label_extension: TLabel; |
18 |
btn_sort_id_asc: TSpeedButton; |
19 |
btn_sort_id_desc: TSpeedButton; |
20 |
btn_sort_name_asc: TSpeedButton; |
21 |
btn_sort_name_desc: TSpeedButton; |
22 |
btn_sort_ext_asc: TSpeedButton; |
23 |
btn_sort_ext_desc: TSpeedButton; |
24 |
combo_extension: TComboBox; |
25 |
check_zerobyte: TCheckBox; |
26 |
edit_filtername: TEdit; |
27 |
check_filtername: TCheckBox; |
28 |
bevel_filelist: TBevel; |
29 |
splitter_content: TSplitter; |
30 |
panel_content: TPanel; |
31 |
filepopup: TPopupMenu; |
32 |
popup_separator2: TMenuItem; |
33 |
popup_linkshere: TMenuItem; |
34 |
popup_separator: TMenuItem; |
35 |
popup_import: TMenuItem; |
36 |
popup_export: TMenuItem; |
37 |
importd: TOpenDialog; |
38 |
exportd: TSaveDialog; |
39 |
private |
40 |
FSortBy: TSortType; |
41 |
FAllowedExts: String; |
42 |
FAllowMultiSelect: Boolean; |
43 |
procedure SetAllowedExts(exts: String); |
44 |
procedure SetMultiSelect(allow: Boolean); |
45 |
|
46 |
procedure check_filternameClick(Sender: TObject); |
47 |
procedure check_zerobyteClick(Sender: TObject); |
48 |
procedure combo_extensionClick(Sender: TObject); |
49 |
procedure btn_sortClick(Sender: TObject); |
50 |
procedure listClick(Sender: TObject); |
51 |
procedure listMouseDown(Sender: TObject; Button: TMouseButton; |
52 |
Shift: TShiftState; X, Y: Integer); |
53 |
|
54 |
procedure popup_importClick(Sender: TObject); |
55 |
procedure popup_exportClick(Sender: TObject); |
56 |
procedure popup_opentool(Sender: TObject); |
57 |
procedure popup_linkshereClick(Sender: TObject); |
58 |
procedure filepopupPopup(Sender: TObject); |
59 |
public |
60 |
constructor Create(AOwner: TComponent); override; |
61 |
procedure RecreateExtList; |
62 |
procedure LoadFileNames; |
63 |
procedure SetFileFilters(pattern, extension: String; zerobytes: Boolean); |
64 |
property AllowedExts: String read FAllowedExts write SetAllowedExts; |
65 |
property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect; |
66 |
end; |
67 |
|
68 |
var |
69 |
Form_TemplateFileList: TForm_TemplateFileList; |
70 |
|
71 |
implementation |
72 |
{$R *.dfm} |
73 |
uses ConnectionManager, Exporters, Functions, StrUtils, WhatLinksHere, Main; |
74 |
|
75 |
|
76 |
procedure TForm_TemplateFileList.RecreateExtList; |
77 |
var |
78 |
i: Integer; |
79 |
exts: TStrings; |
80 |
begin |
81 |
combo_extension.Items.Clear; |
82 |
if FConnectionID > -1 then |
83 |
begin |
84 |
combo_extension.Items.Add('_All files_ (' + |
85 |
IntToStr(ConManager.Connection[FConnectionID].GetFileCount) + ')'); |
86 |
exts := nil; |
87 |
exts := ConManager.Connection[FConnectionID].GetExtensionsList(EF_ExtCount); |
88 |
for i := 0 to exts.Count - 1 do |
89 |
if Length(FAllowedExts) > 0 then |
90 |
begin |
91 |
if Pos(MidStr(exts.Strings[i],1,4), FAllowedExts) > 0 then |
92 |
combo_extension.Items.Add(exts.Strings[i]); |
93 |
end |
94 |
else |
95 |
combo_extension.Items.Add(exts.Strings[i]); |
96 |
combo_extension.ItemIndex := 0; |
97 |
combo_extensionClick(Self); |
98 |
exts.Free; |
99 |
end; |
100 |
end; |
101 |
|
102 |
procedure TForm_TemplateFileList.LoadFileNames; |
103 |
var |
104 |
Extension: String; |
105 |
no_zero_bytes: Boolean; |
106 |
pattern: String; |
107 |
files: TStrings; |
108 |
begin |
109 |
if FConnectionID > -1 then |
110 |
begin |
111 |
Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4); |
112 |
no_zero_bytes := not check_zerobyte.Checked; |
113 |
pattern := ''; |
114 |
if check_filtername.Checked then |
115 |
pattern := edit_filtername.Text; |
116 |
if Extension = '_All' then |
117 |
if Length(FAllowedExts) > 0 then |
118 |
Extension := FAllowedExts |
119 |
else |
120 |
Extension := ''; |
121 |
|
122 |
files := nil; |
123 |
files := ConManager.Connection[FConnectionID].GetFilesList(extension, pattern, no_zero_bytes, FSortBy); |
124 |
|
125 |
filelist.Visible := False; |
126 |
filelist.Items.Clear; |
127 |
if files.Count > 0 then |
128 |
filelist.Items.AddStrings(files); |
129 |
filelist.Visible := True; |
130 |
end; |
131 |
end; |
132 |
|
133 |
|
134 |
procedure TForm_TemplateFileList.popup_exportClick(Sender: TObject); |
135 |
var |
136 |
id: Integer; |
137 |
ext: String; |
138 |
begin |
139 |
id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]); |
140 |
ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4); |
141 |
exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*'; |
142 |
exportd.DefaultExt := ext; |
143 |
if exportd.Execute then |
144 |
ExportDatFile(FConnectionID, id, exportd.FileName); |
145 |
end; |
146 |
|
147 |
procedure TForm_TemplateFileList.popup_importClick(Sender: TObject); |
148 |
var |
149 |
id: Integer; |
150 |
finfo: TFileInfo; |
151 |
fs: TFileStream; |
152 |
begin |
153 |
if CR_EditDat in ConManager.Connection[FConnectionID].ChangeRights then |
154 |
begin |
155 |
id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]); |
156 |
finfo := ConManager.Connection[FConnectionID].GetFileInfo(id); |
157 |
|
158 |
importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' + |
159 |
finfo.Extension + '|All files|*.*'; |
160 |
if importd.Execute then |
161 |
begin |
162 |
fs := TFileStream.Create(importd.FileName, fmOpenRead); |
163 |
if fs.Size <> finfo.Size then |
164 |
begin |
165 |
if not (CR_ResizeDat in ConManager.Connection[FConnectionID].ChangeRights) then |
166 |
begin |
167 |
ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) + |
168 |
', file has to have same size as file in .dat with this backend.' + CrLf + |
169 |
'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf + |
170 |
'Size of chosen file: ' + FormatFileSize(fs.Size)); |
171 |
Exit; |
172 |
end else begin |
173 |
if MessageBox(Self.Handle, |
174 |
PChar('File has different size from the file in the .dat.' + CrLf + |
175 |
'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf + |
176 |
'Size of chosen file: ' + FormatFileSize(fs.Size) + CrLf + |
177 |
'Replace anyway?'), PChar('Different size'), MB_YESNO + MB_ICONWARNING) = ID_NO then |
178 |
begin |
179 |
Exit; |
180 |
end; |
181 |
end; |
182 |
end; |
183 |
ConManager.Connection[FConnectionID].UpdateDatFile(id, fs); |
184 |
Self.listClick(Self); |
185 |
fs.Free; |
186 |
end; |
187 |
end else begin |
188 |
ShowMessage('Editing .dat-contents not allowed with this backend.'); |
189 |
end; |
190 |
end; |
191 |
|
192 |
|
193 |
procedure TForm_TemplateFileList.popup_linkshereClick(Sender: TObject); |
194 |
begin |
195 |
Form_WhatLinksHere.ConID := FConnectionID; |
196 |
Form_WhatLinksHere.FileID := FSelectedFile.ID; |
197 |
Form_WhatLinksHere.SenderForm := Self; |
198 |
Form_WhatLinksHere.Show; |
199 |
end; |
200 |
|
201 |
procedure TForm_TemplateFileList.popup_opentool(Sender: TObject); |
202 |
var |
203 |
sender_name, context: String; |
204 |
id: Integer; |
205 |
begin |
206 |
sender_name := TComponent(Sender).Name; |
207 |
id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]); |
208 |
context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name)); |
209 |
Form_Main.open_child(context, FConnectionID, id); |
210 |
end; |
211 |
|
212 |
procedure TForm_TemplateFileList.filepopupPopup(Sender: TObject); |
213 |
var |
214 |
ext: String; |
215 |
i: Integer; |
216 |
begin |
217 |
ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4); |
218 |
for i := 0 to High(ToolList) do |
219 |
begin |
220 |
filepopup.Items.Items[i].Enabled := True; |
221 |
if Length(ToolList[i].exts) > 0 then |
222 |
if Pos(ext, ToolList[i].exts) = 0 then |
223 |
filepopup.Items.Items[i].Enabled := False; |
224 |
end; |
225 |
end; |
226 |
|
227 |
|
228 |
procedure TForm_TemplateFileList.check_zerobyteClick(Sender: TObject); |
229 |
begin |
230 |
LoadFileNames; |
231 |
end; |
232 |
|
233 |
procedure TForm_TemplateFileList.btn_sortClick(Sender: TObject); |
234 |
begin |
235 |
if btn_sort_id_asc.Down then |
236 |
FSortBy := ST_IDAsc |
237 |
else if btn_sort_id_desc.Down then |
238 |
FSortBy := ST_IDDesc |
239 |
else if btn_sort_name_asc.Down then |
240 |
FSortBy := ST_NameAsc |
241 |
else if btn_sort_name_desc.Down then |
242 |
FSortBy := ST_NameDesc |
243 |
else if btn_sort_ext_asc.Down then |
244 |
FSortBy := ST_ExtAsc |
245 |
else if btn_sort_ext_desc.Down then |
246 |
FSortBy := ST_ExtDesc; |
247 |
LoadFileNames; |
248 |
end; |
249 |
|
250 |
procedure TForm_TemplateFileList.check_filternameClick(Sender: TObject); |
251 |
begin |
252 |
edit_filtername.Enabled := not check_filtername.Checked; |
253 |
LoadFileNames; |
254 |
end; |
255 |
|
256 |
procedure TForm_TemplateFileList.combo_extensionClick(Sender: TObject); |
257 |
begin |
258 |
LoadFileNames; |
259 |
end; |
260 |
|
261 |
procedure TForm_TemplateFileList.listClick(Sender: TObject); |
262 |
var |
263 |
fileid: Integer; |
264 |
begin |
265 |
if filelist.ItemIndex > -1 then |
266 |
begin |
267 |
fileid := ConManager.Connection[FConnectionID].ExtractFileIDOfName( |
268 |
filelist.Items.Strings[filelist.ItemIndex]); |
269 |
FSelectedFile := ConManager.Connection[FConnectionID].GetFileInfo(fileid); |
270 |
if Assigned(FOnNewFileSelected) then |
271 |
FOnNewFileSelected(FSelectedFile); |
272 |
end; |
273 |
end; |
274 |
|
275 |
procedure TForm_TemplateFileList.listMouseDown(Sender: TObject; Button: TMouseButton; |
276 |
Shift: TShiftState; X, Y: Integer); |
277 |
var |
278 |
pt: TPoint; |
279 |
begin |
280 |
pt.X := x; |
281 |
pt.Y := y; |
282 |
if Shift = [ssRight] then |
283 |
begin |
284 |
filelist.ItemIndex := filelist.ItemAtPos(pt, true); |
285 |
Self.listClick(Self); |
286 |
end; |
287 |
end; |
288 |
|
289 |
|
290 |
|
291 |
procedure TForm_TemplateFileList.SetAllowedExts(exts: String); |
292 |
begin |
293 |
FAllowedExts := exts; |
294 |
RecreateExtList; |
295 |
end; |
296 |
|
297 |
procedure TForm_TemplateFileList.SetFileFilters(pattern, extension: String; |
298 |
zerobytes: Boolean); |
299 |
var |
300 |
i: Integer; |
301 |
begin |
302 |
if Length(pattern) > 0 then |
303 |
Self.edit_filtername.Text := pattern; |
304 |
Self.check_filtername.Checked := Length(pattern) > 0; |
305 |
if Length(extension) > 0 then |
306 |
begin |
307 |
for i := 0 to Self.combo_extension.Items.Count - 1 do |
308 |
if Pos(extension, Self.combo_extension.Items.Strings[i]) > 0 then |
309 |
Break; |
310 |
if i < Self.combo_extension.Items.Count then |
311 |
Self.combo_extension.ItemIndex := i |
312 |
else |
313 |
Self.combo_extension.ItemIndex := -1; |
314 |
end; |
315 |
Self.check_zerobyte.Checked := zerobytes; |
316 |
Self.LoadFileNames; |
317 |
end; |
318 |
|
319 |
procedure TForm_TemplateFileList.SetMultiSelect(allow: Boolean); |
320 |
begin |
321 |
FAllowMultiSelect := allow; |
322 |
filelist.MultiSelect := allow; |
323 |
end; |
324 |
|
325 |
|
326 |
|
327 |
constructor TForm_TemplateFileList.Create(AOwner: TComponent); |
328 |
var |
329 |
i: Integer; |
330 |
item: TMenuItem; |
331 |
begin |
332 |
inherited; |
333 |
FAllowedExts := ''; |
334 |
FAllowMultiSelect := False; |
335 |
if Length(ToolList) > 0 then |
336 |
begin |
337 |
for i := 0 to High(ToolList) do |
338 |
begin |
339 |
item := TMenuItem.Create(filepopup); |
340 |
item.Name := 'popup_' + ToolList[i].context; |
341 |
item.Caption := 'Open with ' + ToolList[i].name; |
342 |
item.OnClick := Self.popup_opentool; |
343 |
filepopup.Items.Insert(i, item); |
344 |
end; |
345 |
end; |
346 |
end; |
347 |
|
348 |
|
349 |
end. |