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

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