source: oup/current/Tools/_TemplateFileList.pas@ 229

Last change on this file since 229 was 229, checked in by alloc, 17 years ago
File size: 14.8 KB
Line 
1unit _TemplateFileList;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, _TemplateFile, StdCtrls, ExtCtrls, Menus, Buttons,
8 ComCtrls, TypeDefs, VirtualTrees;
9
10type
11 TForm_TemplateFileList = class(TForm_TemplateFile)
12 panel_files: TPanel;
13 splitter_content: TSplitter;
14 panel_content: TPanel;
15 filepopup: TPopupMenu;
16 popup_separator2: TMenuItem;
17 popup_linkshere: TMenuItem;
18 popup_separator: TMenuItem;
19 popup_import: TMenuItem;
20 popup_export: TMenuItem;
21 importd: TOpenDialog;
22 exportd: TSaveDialog;
23 FilePages: TPageControl;
24 tab_files: TTabSheet;
25 panel_extension: TPanel;
26 label_sort2: TLabel;
27 label_sort1: TLabel;
28 label_extension: TLabel;
29 btn_sort_id_asc: TSpeedButton;
30 btn_sort_id_desc: TSpeedButton;
31 btn_sort_name_asc: TSpeedButton;
32 btn_sort_name_desc: TSpeedButton;
33 btn_sort_ext_asc: TSpeedButton;
34 btn_sort_ext_desc: TSpeedButton;
35 combo_extension: TComboBox;
36 check_zerobyte: TCheckBox;
37 edit_filtername: TEdit;
38 check_filtername: TCheckBox;
39 filelist: TListBox;
40 tab_meta: TTabSheet;
41 filelist_meta: TVirtualStringTree;
42 procedure NewCon(ID: Integer);
43
44 procedure check_filternameClick(Sender: TObject);
45 procedure check_zerobyteClick(Sender: TObject);
46 procedure combo_extensionClick(Sender: TObject);
47 procedure btn_sortClick(Sender: TObject);
48 procedure listClick(Sender: TObject);
49 procedure listMouseDown(Sender: TObject; Button: TMouseButton;
50 Shift: TShiftState; X, Y: Integer);
51
52 procedure popup_importClick(Sender: TObject);
53 procedure popup_exportClick(Sender: TObject);
54 procedure popup_opentool(Sender: TObject);
55 procedure popup_linkshereClick(Sender: TObject);
56 procedure filepopupPopup(Sender: TObject);
57 procedure filelist_metaGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
58 Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
59 procedure filelist_metaInitChildren(Sender: TBaseVirtualTree;
60 Node: PVirtualNode; var ChildCount: Cardinal);
61 procedure filelist_metaPaintText(Sender: TBaseVirtualTree;
62 const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
63 TextType: TVSTTextType);
64 private
65 FSortBy: TSortType;
66 FAllowedExts: String;
67 FAllowMultiSelect: Boolean;
68 procedure SetAllowedExts(exts: String);
69 procedure SetMultiSelect(allow: Boolean);
70 public
71 constructor Create(AOwner: TComponent); override;
72 procedure RecreateExtList;
73 procedure LoadFileNames;
74 procedure SetFileFilters(pattern, extension: String; zerobytes: Boolean);
75 property AllowedExts: String read FAllowedExts write SetAllowedExts;
76 property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect;
77 end;
78
79implementation
80{$R *.dfm}
81uses ConnectionManager, Exporters, Functions, StrUtils, WhatLinksHere, Main,
82 _BaseTemplate, _MetaTypes, Data, _MetaManager, _FileTypes;
83
84type
85 PNodeData = ^TNodeData;
86
87 TNodeData = record
88 Field: TObject;
89 end;
90
91function AddVSTEntry(AVST: TCustomVirtualStringTree; ANode: PVirtualNode;
92 ARecord: TNodeData): PVirtualNode;
93var
94 Data: PNodeData;
95begin
96 Result := AVST.AddChild(ANode);
97 Data := AVST.GetNodeData(Result);
98 AVST.ValidateNode(Result, False);
99 Data^ := ARecord;
100end;
101
102
103
104procedure TForm_TemplateFileList.RecreateExtList;
105var
106 i: Integer;
107 exts: TStrings;
108begin
109 combo_extension.Items.Clear;
110 if FConnectionID > -1 then
111 begin
112 combo_extension.Items.Add('_All files_ (' +
113 IntToStr(ConManager.Connection[FConnectionID].GetFileCount) + ')');
114 exts := nil;
115 exts := ConManager.Connection[FConnectionID].GetExtensionsList(EF_ExtCount);
116 for i := 0 to exts.Count - 1 do
117 if Length(FAllowedExts) > 0 then
118 begin
119 if Pos(MidStr(exts.Strings[i],1,4), FAllowedExts) > 0 then
120 combo_extension.Items.Add(exts.Strings[i]);
121 end
122 else
123 combo_extension.Items.Add(exts.Strings[i]);
124 combo_extension.ItemIndex := 0;
125 combo_extensionClick(Self);
126 exts.Free;
127 end;
128end;
129
130procedure TForm_TemplateFileList.LoadFileNames;
131var
132 Extension: String;
133 no_zero_bytes: Boolean;
134 pattern: String;
135 files: TStrings;
136 root: TExtensions;
137 i: Integer;
138 data: TNodeData;
139 node: PVirtualNode;
140begin
141 if FConnectionID > -1 then
142 begin
143 Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4);
144 no_zero_bytes := not check_zerobyte.Checked;
145 pattern := '';
146 if check_filtername.Checked then
147 pattern := edit_filtername.Text;
148 if Extension = '_All' then
149 if Length(FAllowedExts) > 0 then
150 Extension := FAllowedExts
151 else
152 Extension := '';
153
154 files := nil;
155 files := ConManager.Connection[FConnectionID].GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
156
157 filelist.Visible := False;
158 filelist.Items.Clear;
159 if files.Count > 0 then
160 filelist.Items.AddStrings(files);
161 filelist.Visible := True;
162
163 //VST
164 filelist_meta.Clear;
165 filelist_meta.BeginUpdate;
166 root := ConManager.Connection[FConnectionID].MetaData.Root;
167 for i := 0 to High(root) do
168 begin
169 data.Field := root[i];
170 node := AddVSTEntry(filelist_meta, nil, data);
171 filelist_meta.HasChildren[node] := True;
172 end;
173 filelist_meta.EndUpdate;
174 end;
175end;
176
177
178procedure TForm_TemplateFileList.NewCon(ID: Integer);
179begin
180 RecreateExtList;
181end;
182
183procedure TForm_TemplateFileList.popup_exportClick(Sender: TObject);
184var
185 id: Integer;
186 ext: String;
187begin
188 id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
189 ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
190 exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*';
191 exportd.DefaultExt := ext;
192 if exportd.Execute then
193 ExportDatFile(FConnectionID, id, exportd.FileName);
194end;
195
196procedure TForm_TemplateFileList.popup_importClick(Sender: TObject);
197var
198 id: Integer;
199 finfo: TFileInfo;
200 fs: TFileStream;
201begin
202 if CR_EditDat in ConManager.Connection[FConnectionID].ChangeRights then
203 begin
204 id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
205 finfo := ConManager.Connection[FConnectionID].GetFileInfo(id);
206
207 importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' +
208 finfo.Extension + '|All files|*.*';
209 if importd.Execute then
210 begin
211 fs := TFileStream.Create(importd.FileName, fmOpenRead);
212 if fs.Size <> finfo.Size then
213 begin
214 if not (CR_ResizeDat in ConManager.Connection[FConnectionID].ChangeRights) then
215 begin
216 ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) +
217 ', file has to have same size as file in .dat with this backend.' + CrLf +
218 'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
219 'Size of chosen file: ' + FormatFileSize(fs.Size));
220 Exit;
221 end else begin
222 if MessageBox(Self.Handle,
223 PChar('File has different size from the file in the .dat.' + CrLf +
224 'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
225 'Size of chosen file: ' + FormatFileSize(fs.Size) + CrLf +
226 'Replace anyway?'), PChar('Different size'), MB_YESNO + MB_ICONWARNING) = ID_NO then
227 begin
228 Exit;
229 end;
230 end;
231 end;
232 ConManager.Connection[FConnectionID].UpdateDatFile(id, fs);
233 Self.listClick(Self);
234 fs.Free;
235 end;
236 end else begin
237 ShowMessage('Editing .dat-contents not allowed with this backend.');
238 end;
239end;
240
241
242procedure TForm_TemplateFileList.popup_linkshereClick(Sender: TObject);
243begin
244 Form_WhatLinksHere.ConID := FConnectionID;
245 Form_WhatLinksHere.FileID := FSelectedFile.ID;
246 Form_WhatLinksHere.SenderForm := Self;
247 Form_WhatLinksHere.Show;
248end;
249
250procedure TForm_TemplateFileList.popup_opentool(Sender: TObject);
251var
252 sender_name, context: String;
253 id: Integer;
254begin
255 sender_name := TComponent(Sender).Name;
256 id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
257 context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name));
258 Form_Main.open_child(context, FConnectionID, id);
259end;
260
261
262procedure TForm_TemplateFileList.filepopupPopup(Sender: TObject);
263var
264 ext: String;
265 i: Integer;
266begin
267 ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
268 for i := 0 to High(ToolList) do
269 begin
270 filepopup.Items.Items[i].Enabled := True;
271 if Length(ToolList[i].exts) > 0 then
272 if Pos(ext, ToolList[i].exts) = 0 then
273 filepopup.Items.Items[i].Enabled := False;
274 end;
275end;
276
277
278procedure TForm_TemplateFileList.check_zerobyteClick(Sender: TObject);
279begin
280 LoadFileNames;
281end;
282
283procedure TForm_TemplateFileList.btn_sortClick(Sender: TObject);
284begin
285 if btn_sort_id_asc.Down then
286 FSortBy := ST_IDAsc
287 else if btn_sort_id_desc.Down then
288 FSortBy := ST_IDDesc
289 else if btn_sort_name_asc.Down then
290 FSortBy := ST_NameAsc
291 else if btn_sort_name_desc.Down then
292 FSortBy := ST_NameDesc
293 else if btn_sort_ext_asc.Down then
294 FSortBy := ST_ExtAsc
295 else if btn_sort_ext_desc.Down then
296 FSortBy := ST_ExtDesc;
297 LoadFileNames;
298end;
299
300procedure TForm_TemplateFileList.check_filternameClick(Sender: TObject);
301begin
302 edit_filtername.Enabled := not check_filtername.Checked;
303 LoadFileNames;
304end;
305
306procedure TForm_TemplateFileList.combo_extensionClick(Sender: TObject);
307begin
308 LoadFileNames;
309end;
310
311procedure TForm_TemplateFileList.listClick(Sender: TObject);
312var
313 fileid: Integer;
314begin
315 if filelist.ItemIndex > -1 then
316 begin
317 fileid := ConManager.Connection[FConnectionID].ExtractFileIDOfName(
318 filelist.Items.Strings[filelist.ItemIndex]);
319 FSelectedFile := ConManager.Connection[FConnectionID].GetFileInfo(fileid);
320 if Assigned(FOnNewFileSelected) then
321 FOnNewFileSelected(FSelectedFile);
322 end;
323end;
324
325procedure TForm_TemplateFileList.listMouseDown(Sender: TObject; Button: TMouseButton;
326 Shift: TShiftState; X, Y: Integer);
327var
328 pt: TPoint;
329begin
330 pt.X := x;
331 pt.Y := y;
332 if Shift = [ssRight] then
333 begin
334 filelist.ItemIndex := filelist.ItemAtPos(pt, true);
335 Self.listClick(Self);
336 end;
337end;
338
339
340
341procedure TForm_TemplateFileList.SetAllowedExts(exts: String);
342begin
343 FAllowedExts := exts;
344 RecreateExtList;
345end;
346
347procedure TForm_TemplateFileList.SetFileFilters(pattern, extension: String;
348 zerobytes: Boolean);
349var
350 i: Integer;
351begin
352 if Length(pattern) > 0 then
353 Self.edit_filtername.Text := pattern;
354 Self.check_filtername.Checked := Length(pattern) > 0;
355 if Length(extension) > 0 then
356 begin
357 for i := 0 to Self.combo_extension.Items.Count - 1 do
358 if Pos(extension, Self.combo_extension.Items.Strings[i]) > 0 then
359 Break;
360 if i < Self.combo_extension.Items.Count then
361 Self.combo_extension.ItemIndex := i
362 else
363 Self.combo_extension.ItemIndex := -1;
364 end;
365 Self.check_zerobyte.Checked := zerobytes;
366 Self.LoadFileNames;
367end;
368
369procedure TForm_TemplateFileList.SetMultiSelect(allow: Boolean);
370begin
371 FAllowMultiSelect := allow;
372 filelist.MultiSelect := allow;
373end;
374
375
376
377constructor TForm_TemplateFileList.Create(AOwner: TComponent);
378var
379 i: Integer;
380 item: TMenuItem;
381begin
382 inherited;
383 filelist_meta.NodeDataSize := SizeOf(TNodeData);
384 filelist_meta.Font.Charset := AppSettings.CharSet;
385 filelist_meta.Clear;
386
387 FAllowedExts := '';
388 FAllowMultiSelect := False;
389 FOnNewConnection := NewCon;
390 UpdateConList;
391 if Length(ToolList) > 0 then
392 begin
393 for i := 0 to High(ToolList) do
394 begin
395 item := TMenuItem.Create(filepopup);
396 item.Name := 'popup_' + ToolList[i].context;
397 item.Caption := 'Open with ' + ToolList[i].name;
398 item.OnClick := Self.popup_opentool;
399 filepopup.Items.Insert(i, item);
400 end;
401 end;
402end;
403
404
405
406procedure TForm_TemplateFileList.filelist_metaGetText(Sender: TBaseVirtualTree;
407 Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
408 var CellText: WideString);
409var
410 data: PNodeData;
411begin
412 inherited;
413 data := Sender.GetNodeData(Node);
414 if data.Field is TExtension then
415 begin
416 CellText := TExtension(data.Field).Ext;
417 end;
418 if data.Field is TFile then
419 begin
420 if TFile(data.Field).FileName = '' then
421 CellText := TFile(data.Field).FileExt + ' (unnamed)'
422 else
423 CellText := TFile(data.Field).FileExt + ': ' + TFile(data.Field).FileName;
424 end;
425end;
426
427
428procedure TForm_TemplateFileList.filelist_metaInitChildren(
429 Sender: TBaseVirtualTree; Node: PVirtualNode; var ChildCount: Cardinal);
430var
431 data: PNodeData;
432 i: Integer;
433 newdata: TNodeData;
434 newnode: PVirtualNode;
435 id: Integer;
436begin
437 inherited;
438 data := Sender.GetNodeData(Node);
439 if data.Field is TExtension then
440 begin
441 if TExtension(data.Field).FileCount = 0 then
442 TExtension(data.Field).InitList;
443 for i := 0 to TExtension(data.Field).FileCount - 1 do
444 begin
445 id := TExtension(data.Field).Files[i];
446 ConManager.Connection[FConnectionID].MetaData.InitFile(id);
447 newdata.Field := ConManager.Connection[FConnectionID].MetaData.FileById[id];
448 newnode := AddVSTEntry(filelist_meta, Node, newdata);
449 if ConManager.Connection[FConnectionID].MetaData.FileById[id].ChildCount > 0 then
450 filelist_meta.HasChildren[newnode] := True;
451 ChildCount := filelist_meta.ChildCount[node];
452 end;
453 end;
454 if data.Field is TFile then
455 begin
456 for i := 0 to TFile(data.Field).ChildCount - 1 do
457 begin
458 id := TFile(data.Field).LinkByIndex[i].DestID;
459 ConManager.Connection[FConnectionID].MetaData.InitFile(id);
460 newdata.Field := ConManager.Connection[FConnectionID].MetaData.FileById[id];
461 newnode := AddVSTEntry(filelist_meta, Node, newdata);
462 if ConManager.Connection[FConnectionID].MetaData.FileById[id].ChildCount > 0 then
463 filelist_meta.HasChildren[newnode] := True;
464 end;
465 ChildCount := filelist_meta.ChildCount[node];
466 end;
467end;
468
469procedure TForm_TemplateFileList.filelist_metaPaintText(
470 Sender: TBaseVirtualTree; const TargetCanvas: TCanvas; Node: PVirtualNode;
471 Column: TColumnIndex; TextType: TVSTTextType);
472var
473 Data: PNodeData;
474begin
475 Data := Sender.GetNodeData(Node);
476 if Data.Field is TFile then
477 begin
478 if Length(TFile(Data.Field).FileName) = 0 then
479 TargetCanvas.Font.Color := $C06060;
480 if TFile(Data.Field).FileSize = 0 then
481 TargetCanvas.Font.Color := $2020A0;
482 end;
483end;
484
485end.
Note: See TracBrowser for help on using the repository browser.