source: oup/rewrite/Tools/Template.pas @ 106

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