source: oup/current/Tools/Template.pas@ 846

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