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

Last change on this file since 115 was 113, checked in by alloc, 18 years ago
File size: 16.7 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;
268 Form_WhatLinksHere.SenderForm := Self;
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;
[106]347 filepopup.Items.Find('What links here?').Enabled :=
348 ConManager.Connection[FConnectionID].Backend = DB_ADB;
[93]349end;
350
351procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject);
352begin
353 LoadFileNames;
354end;
355
356procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject);
357begin
358 if btn_sort_id_asc.Down then
[97]359 FSortBy := ST_IDAsc
[93]360 else if btn_sort_id_desc.Down then
[97]361 FSortBy := ST_IDDesc
[93]362 else if btn_sort_name_asc.Down then
[97]363 FSortBy := ST_NameAsc
[93]364 else if btn_sort_name_desc.Down then
[97]365 FSortBy := ST_NameDesc
[93]366 else if btn_sort_ext_asc.Down then
[97]367 FSortBy := ST_ExtAsc
[93]368 else if btn_sort_ext_desc.Down then
[97]369 FSortBy := ST_ExtDesc;
[93]370 LoadFileNames;
371end;
372
373procedure TForm_ToolTemplate.check_filternameClick(Sender: TObject);
374begin
375 edit_filtername.Enabled := not check_filtername.Checked;
376 LoadFileNames;
377end;
378
379procedure TForm_ToolTemplate.listClick(Sender: TObject);
380var
381 fileid: Integer;
382begin
383 if filelist.ItemIndex > -1 then
384 begin
[97]385 fileid := ConManager.Connection[FConnectionID].ExtractFileIDOfName(
[93]386 filelist.Items.Strings[filelist.ItemIndex]);
[97]387 FSelectedFile := ConManager.Connection[FConnectionID].GetFileInfo(fileid);
[93]388 if Assigned(FOnNewFileSelected) then
389 FOnNewFileSelected(FSelectedFile);
390 end;
391end;
392
393procedure TForm_ToolTemplate.listMouseDown(Sender: TObject; Button: TMouseButton;
394 Shift: TShiftState; X, Y: Integer);
395var
396 pt: TPoint;
397begin
398 pt.X := x;
399 pt.Y := y;
[106]400 if Shift = [ssRight] then
401 begin
402 filelist.ItemIndex := filelist.ItemAtPos(pt, true);
403 Self.listClick(Self);
404 end;
[93]405end;
406
407
408
[97]409procedure TForm_ToolTemplate.SelectConnection(ConnectionID: Integer);
410begin
411 if FConnectionID <> ConnectionID then
412 begin
413 combo_connection.ItemIndex := ConManager.ConnectionIndexByID[ConnectionID];
414 combo_connectionChange(Self);
415 end;
416end;
417
418procedure TForm_ToolTemplate.SelectFileID(ConnectionID, FileID: Integer);
[93]419var
420 i: Integer;
421begin
[97]422 if FConnectionID <> ConnectionID then
423 SelectConnection(ConnectionID);
[93]424
425 filelist.ItemIndex := -1;
426 if filelist.Items.Count > 0 then
427 for i := 0 to filelist.Items.Count - 1 do
[97]428 if ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[i]) = FileID then
[93]429 begin
430 filelist.ItemIndex := i;
431 Break;
432 end;
433 Self.listClick(Self);
434end;
435
[97]436procedure TForm_ToolTemplate.SelectFileName(ConnectionID: Integer; filename: String);
[93]437var
438 i: Integer;
439begin
[97]440 if FConnectionID <> ConnectionID then
441 SelectConnection(ConnectionID);
[93]442
443 filelist.ItemIndex := -1;
444 if filelist.Items.Count > 0 then
445 for i := 0 to filelist.Items.Count - 1 do
446 if filelist.Items.Strings[i] = filename then
447 filelist.ItemIndex := i;
448 Self.listClick(Self);
449end;
450
451procedure TForm_ToolTemplate.SetAllowedExts(exts: String);
452begin
453 FAllowedExts := exts;
454 RecreateExtList;
455end;
456
457procedure TForm_ToolTemplate.SetFileFilters(pattern, extension: String;
458 zerobytes: Boolean);
459var
460 i: Integer;
461begin
462 if Length(pattern) > 0 then
463 Self.edit_filtername.Text := pattern;
464 Self.check_filtername.Checked := Length(pattern) > 0;
465 if Length(extension) > 0 then
466 begin
467 for i := 0 to Self.combo_extension.Items.Count - 1 do
468 if Pos(extension, Self.combo_extension.Items.Strings[i]) > 0 then
469 Break;
470 if i < Self.combo_extension.Items.Count then
471 Self.combo_extension.ItemIndex := i
472 else
473 Self.combo_extension.ItemIndex := -1;
474 end;
475 Self.check_zerobyte.Checked := zerobytes;
476 Self.LoadFileNames;
477end;
478
479procedure TForm_ToolTemplate.SetMultiSelect(allow: Boolean);
480begin
481 FAllowMultiSelect := allow;
482 filelist.MultiSelect := allow;
483end;
484
485
[97]486function TForm_ToolTemplate.GetToolCloseable: Boolean;
[93]487begin
[97]488 if Assigned(FOnCheckCloseable) then
489 Result := FOnCheckCloseable
490 else
491 Result := True;
[93]492end;
493
494procedure TForm_ToolTemplate.FormActivate(Sender: TObject);
495begin
496 if edit_filtername.CanFocus then
497 edit_filtername.SetFocus
498 else
499 if content.CanFocus then
500 content.SetFocus;
501end;
502
503procedure TForm_ToolTemplate.FormClose(Sender: TObject; var Action: TCloseAction);
504begin
505 Action := caFree;
506end;
507
508
509procedure AddToolListEntryExt(context, ext: String);
510var
511 i: Integer;
512begin
513 for i := 0 to High(ToolList) do
514 if ToolList[i].context = context then
515 begin
516 if Pos(ext, ToolList[i].exts) = 0 then
517 begin
518 if Length(ToolList[i].exts) = 0 then
519 ToolList[i].exts := ext
520 else
521 ToolList[i].exts := ToolList[i].exts + ',' + ext;
522 end;
523 Exit;
524 end;
525end;
526
527procedure AddToolListEntry(context, name, exts: String);
528var
529 i: Integer;
530begin
531 if Length(ToolList) > 0 then
532 begin
533 for i := 0 to High(ToolList) do
534 if ToolList[i].context = context then
535 begin
536 if (Length(ToolList[i].name) = 0) and (Length(name) > 0) then
537 ToolList[i].name := name;
538 if Length(exts) > 0 then
539 AddToolListEntryExt(context, exts);
540 Exit;
541 end;
542 end;
543 SetLength(ToolList, Length(ToolList) + 1);
544 for i := High(ToolList) downto 1 do
545 if ToolList[i - 1].name > name then
546 ToolList[i] := ToolList[i - 1]
547 else
548 Break;
549 ToolList[i].context := context;
550 ToolList[i].name := name;
551 ToolList[i].exts := exts;
552end;
553
554end.
Note: See TracBrowser for help on using the repository browser.