ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/oup/current/Tools/Template.pas
(Generate patch)

Comparing:
oup/rewrite/Tools/Template.pas (file contents), Revision 93 by alloc, Thu Jan 18 17:15:59 2007 UTC vs.
oup/current/Tools/Template.pas (file contents), Revision 113 by alloc, Sun Feb 25 17:20:22 2007 UTC

# Line 5 | Line 5 | interface
5   uses
6    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7    Dialogs, ExtCtrls, StdCtrls, StrUtils,
8 <  ConnectionManager, {Functions,} Data, TypeDefs, Menus, Buttons;
8 >  Data, TypeDefs, Menus, Buttons;
9  
10   type
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;
# Line 39 | Line 40 | type
40      Label3: TLabel;
41      combo_connection: TComboBox;
42      Bevel1: TBevel;
43 <    procedure RecreateList;
44 <    procedure UpdateList;
43 >    popup_linkshere: TMenuItem;
44 >    popup_separator2: TMenuItem;
45      procedure RecreateExtList;
46 +    procedure UpdateConList;
47      procedure LoadFileNames;
48 <    procedure SelectFileName(connection: Integer; FileName: String);
49 <    procedure SelectFileID(connection: Integer; FileID: Integer);
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);
# Line 52 | Line 55 | type
55      procedure listMouseDown(Sender: TObject; Button: TMouseButton;
56        Shift: TShiftState; X, Y: Integer);
57  
55    procedure FormResize(Sender: TObject);
56    procedure FormCreate(Sender: TObject);
58      procedure FormClose(Sender: TObject; var Action: TCloseAction);
59      procedure popup_importClick(Sender: TObject);
60      procedure popup_exportClick(Sender: TObject);
# Line 62 | Line 63 | type
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 <    FConnection: Integer;
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 Connection: Integer read FConnection;
89 >    property ConnectionID: Integer read FConnectionID;
90 >    property Closeable: Boolean read GetToolCloseable;
91    end;
92  
93   var
# Line 90 | Line 96 | procedure AddToolListEntry(context, name
96  
97   implementation
98   {$R *.dfm}
99 < uses Main, Exporters;
99 > uses Main, ConnectionManager, Exporters, Functions, WhatLinksHere;
100  
101  
102 < procedure TForm_ToolTemplate.UpdateList;
102 > procedure TForm_ToolTemplate.UpdateConList;
103   var
104    i: Integer;
105    fn, datatype, boxstring: String;
106    level: Integer;
101  oldcon: String;
107   begin
103  oldcon := combo_connection.Items.Strings[combo_connection.ItemIndex];
108    combo_connection.ItemIndex := -1;
109    combo_connection.Items.Clear;
110 <  if Length(DataConnections) > 0 then
110 >  if ConManager.Count > 0 then
111    begin
112 <    for i := 0 to High(DataConnections) do
112 >    for i := 0 to ConManager.Count - 1 do
113      begin
114 <      level := DataConnections[i].LevelInfo.LevelNumber;
115 <      fn := ExtractFileName(DataConnections[i].FileName);
116 <      if DataConnections[i].Backend = ODB_Dat then
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 DataConnections[i].Backend = ODB_ADB then
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) + ')';
122 >      boxstring := datatype + fn + ' (Level: ' + IntToStr(level) + ') [' + IntToStr(ConManager.ConnectionByIndex[i].ConnectionID) + ']';
123        combo_connection.Items.Add(boxstring);
124 <      if oldcon = boxstring then
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
# Line 128 | Line 132 | begin
132    end
133    else
134    begin
135 <    FConnection := nil;
135 >    FConnectionID := 0;
136      filelist.Items.Clear;
137      combo_extension.Items.Clear;
138      combo_connectionChange(Self);
139 <    FSelectedFile := GetEmptyFileInfo;
139 >    FSelectedFile.ID := -1;
140      if Assigned(FOnNewFileSelected) then
141        FOnNewFileSelected(FSelectedFile);
142    end;
143   end;
144  
141 procedure TForm_ToolTemplate.RecreateList;
142 var
143  i: Integer;
144  fn, datatype: String;
145  level: Integer;
146 begin
147  combo_connection.Items.Clear;
148  if Length(DataConnections) > 0 then
149  begin
150    for i := 0 to High(DataConnections) do
151    begin
152      level := DataConnections[i].LevelInfo.LevelNumber;
153      fn := ExtractFileName(DataConnections[i].FileName);
154      if DataConnections[i].Backend = ODB_Dat then
155        datatype := 'ONI-.dat: '
156      else if DataConnections[i].Backend = ODB_ADB then
157        datatype := 'OUP-DB: '
158      else
159        datatype := 'Unknown: ';
160      combo_connection.Items.Add(datatype + fn + ' (Level: ' + IntToStr(level) + ')');
161    end;
162    FConnection := DataConnections[0];
163    combo_connection.ItemIndex := 0;
164    combo_connectionChange(Self);
165  end
166  else
167  begin
168    FConnection := nil;
169    filelist.Items.Clear;
170    combo_extension.Items.Clear;
171    combo_connectionChange(Self);
172  end;
173 end;
174
145   procedure TForm_ToolTemplate.RecreateExtList;
146   var
147 <  i:    LongWord;
148 <  exts: TStringArray;
147 >  i:    Integer;
148 >  exts: TStrings;
149   begin
150    combo_extension.Items.Clear;
151 <  if FConnection <> nil then
151 >  if FConnectionID > -1 then
152    begin
153      combo_extension.Items.Add('_All files_ (' +
154 <      IntToStr(FConnection.GetFilesCount) + ')');
155 <    exts := FConnection.GetExtensionsList;
156 <    for i := 0 to High(exts) do
154 >      IntToStr(ConManager.Connection[FConnectionID].GetFileCount) + ')');
155 >    exts := nil;
156 >    exts := ConManager.Connection[FConnectionID].GetExtensionsList(EF_ExtCount);
157 >    for i := 0 to exts.Count - 1 do
158        if Length(FAllowedExts) > 0 then
159        begin
160 <        if Pos(MidStr(exts[i],1,4), FAllowedExts) > 0 then
161 <        begin
162 <          combo_extension.Items.Add(exts[i]);
163 <        end;
164 <      end else
194 <        combo_extension.Items.Add(exts[i]);
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]);
165      combo_extension.ItemIndex := 0;
166      combo_extensionClick(Self);
167 +    exts.Free;
168    end;
169   end;
170  
# Line 205 | Line 176 | var
176    Extension: String;
177    no_zero_bytes: Boolean;
178    pattern: String;
179 <  files: TStringArray;
209 <  i: LongWord;
179 >  files: TStrings;
180   begin
181 <  if FConnection <> nil then
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;
# Line 221 | Line 191 | begin
191        else
192          Extension := '';
193  
194 <    files := FConnection.GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
194 >    files := nil;
195 >    files := ConManager.Connection[FConnectionID].GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
196  
197      filelist.Visible := False;
198      filelist.Items.Clear;
199 <    if Length(files) > 0 then
200 <      for i := 0 to High(files) do
230 <        filelist.Items.Add(files[i]);
199 >    if files.Count > 0 then
200 >      filelist.Items.AddStrings(files);
201      filelist.Visible := True;
202    end;
203   end;
# Line 238 | Line 208 | var
208    id: Integer;
209    ext: String;
210   begin
211 <  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
211 >  id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
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
216 <    ExportDatFile(FConnection, id, exportd.FileName);
216 >    ExportDatFile(FConnectionID, id, exportd.FileName);
217   end;
218  
219   procedure TForm_ToolTemplate.popup_importClick(Sender: TObject);
# Line 251 | Line 221 | var
221    id: Integer;
222    finfo: TFileInfo;
223    fs: TFileStream;
254  data: TData;
224   begin
225 <  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
226 <  finfo := FConnection.GetFileInfo(id);
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);
229  
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 <      ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) +
237 <        ', file has to have same size as file in .dat.' + CrLf +
238 <        'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf +
239 <        'Size of chosen file: ' + FormatFileSize(fs.Size))
240 <    else begin
241 <      SetLength(data, fs.Size);
242 <      fs.Read(data[0], fs.Size);
243 <      FConnection.UpdateDatFile(id, data);
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;
255 >      ConManager.Connection[FConnectionID].UpdateDatFile(id, fs);
256        Self.listClick(Self);
257 +      fs.Free;
258      end;
259 <    fs.Free;
259 >  end else begin
260 >    ShowMessage('Editing .dat-contents not allowed with this backend.');
261    end;
262   end;
263  
264 + procedure TForm_ToolTemplate.popup_linkshereClick(Sender: TObject);
265 + begin
266 +  Form_WhatLinksHere.ConID := FConnectionID;
267 +  Form_WhatLinksHere.FileID := FSelectedFile.ID;
268 +  Form_WhatLinksHere.SenderForm := Self;
269 +  Form_WhatLinksHere.Show;
270 + end;
271 +
272   procedure TForm_ToolTemplate.popup_opentool(Sender: TObject);
273   var
274    sender_name, context: String;
275    id: Integer;
276   begin
277    sender_name := TComponent(Sender).Name;
278 <  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
278 >  id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
279    context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name));
280 <  Form_Main.open_child(context, FConnection, id);
280 >  Form_Main.open_child(context, FConnectionID, id);
281   end;
282  
283   procedure TForm_ToolTemplate.combo_connectionChange(Sender: TObject);
284   var
285    name: String;
293  nstart, nend: Integer;
294  i: Integer;
286   begin
287    if combo_connection.ItemIndex >= 0 then
288    begin
289      name := combo_connection.Items.Strings[combo_connection.ItemIndex];
290 <    nstart := Pos(' ', name) + 1;
291 <    nend := Pos('(', name) - 1;
292 <    name := MidStr(name, nstart, nend - nstart);
293 <
294 <    for i := 0 to High(DataConnections) do
295 <    begin
296 <      if ExtractFileName(DataConnections[i].FileName) = name then
306 <      begin
307 <        FConnection := DataConnections[i];
308 <        Break;
309 <      end;
310 <    end;
311 <    if i = Length(DataConnections) then
312 <      FConnection := nil;
313 <
314 <    RecreateExtList;
315 <    if Assigned(FOnNewConnection) then
316 <      FOnNewConnection(FConnection);
317 <  end;
290 >    FConnectionID := StrToInt(MidStr(name, Pos('[', name) + 1, Pos(']', name) - Pos('[', name)  - 1));
291 >  end
292 >  else
293 >    FConnectionID := -1;
294 >  RecreateExtList;
295 >  if Assigned(FOnNewConnection) then
296 >    FOnNewConnection(FConnectionID);
297   end;
298  
299   procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject);
# Line 329 | Line 308 | var
308    item: TMenuItem;
309   begin
310    inherited;
311 <  RecreateList;
312 <  FSelectedFile := GetEmptyFileInfo;
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;
321    if Length(ToolList) > 0 then
322    begin
323      for i := 0 to High(ToolList) do
# Line 357 | Line 344 | begin
344        if Pos(ext, ToolList[i].exts) = 0 then
345          filepopup.Items.Items[i].Enabled := False;
346    end;
347 +  filepopup.Items.Find('What links here?').Enabled :=
348 +      ConManager.Connection[FConnectionID].Backend = DB_ADB;
349   end;
350  
351   procedure TForm_ToolTemplate.check_zerobyteClick(Sender: TObject);
# Line 367 | Line 356 | end;
356   procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject);
357   begin
358    if btn_sort_id_asc.Down then
359 <    FSortBy := stIDAsc
359 >    FSortBy := ST_IDAsc
360    else if btn_sort_id_desc.Down then
361 <    FSortBy := stIDDesc
361 >    FSortBy := ST_IDDesc
362    else if btn_sort_name_asc.Down then
363 <    FSortBy := stNameAsc
363 >    FSortBy := ST_NameAsc
364    else if btn_sort_name_desc.Down then
365 <    FSortBy := stNameDesc
365 >    FSortBy := ST_NameDesc
366    else if btn_sort_ext_asc.Down then
367 <    FSortBy := stExtAsc
367 >    FSortBy := ST_ExtAsc
368    else if btn_sort_ext_desc.Down then
369 <    FSortBy := stExtDesc;
369 >    FSortBy := ST_ExtDesc;
370    LoadFileNames;
371   end;
372  
# Line 393 | Line 382 | var
382   begin
383    if filelist.ItemIndex > -1 then
384    begin
385 <    fileid := FConnection.ExtractFileID(
385 >    fileid := ConManager.Connection[FConnectionID].ExtractFileIDOfName(
386            filelist.Items.Strings[filelist.ItemIndex]);
387 <    FSelectedFile := FConnection.GetFileInfo(fileid);
387 >    FSelectedFile := ConManager.Connection[FConnectionID].GetFileInfo(fileid);
388      if Assigned(FOnNewFileSelected) then
389        FOnNewFileSelected(FSelectedFile);
390    end;
# Line 408 | Line 397 | var
397   begin
398    pt.X := x;
399    pt.Y := y;
400 < //  filelist.ItemIndex := filelist.ItemAtPos(pt, true);
401 < //  Self.listClick(Self);
400 >  if Shift = [ssRight] then
401 >  begin
402 >    filelist.ItemIndex := filelist.ItemAtPos(pt, true);
403 >    Self.listClick(Self);
404 >  end;
405   end;
406  
407  
408  
409 < procedure TForm_ToolTemplate.SelectFileID(connection: TOniData; id: Integer);
418 < var
419 <  i: Integer;
420 <  name: String;
421 <  nstart, nend: Integer;
409 > procedure TForm_ToolTemplate.SelectConnection(ConnectionID: Integer);
410   begin
411 <  for i := 0 to combo_connection.Items.Count - 1 do
411 >  if FConnectionID <> ConnectionID then
412    begin
413 <    name := combo_connection.Items.Strings[i];
414 <    nstart := Pos(' ', name) + 1;
427 <    nend := Pos('(', name) - 1;
428 <    name := MidStr(name, nstart, nend - nstart);
429 <
430 <    if ExtractFileName(connection.FileName) = name then
431 <    begin
432 <      combo_connection.ItemIndex := i;
433 <      combo_connectionChange(Self);
434 <    end;
413 >    combo_connection.ItemIndex := ConManager.ConnectionIndexByID[ConnectionID];
414 >    combo_connectionChange(Self);
415    end;
416 + end;
417 +
418 + procedure TForm_ToolTemplate.SelectFileID(ConnectionID, FileID: Integer);
419 + var
420 +  i: Integer;
421 + begin
422 +  if FConnectionID <> ConnectionID then
423 +    SelectConnection(ConnectionID);
424  
425    filelist.ItemIndex := -1;
426    if filelist.Items.Count > 0 then
427      for i := 0 to filelist.Items.Count - 1 do
428 <      if FConnection.ExtractFileID(filelist.Items.Strings[i]) = id then
428 >      if ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[i]) = FileID then
429        begin
430          filelist.ItemIndex := i;
431          Break;
# Line 445 | Line 433 | begin
433    Self.listClick(Self);
434   end;
435  
436 < procedure TForm_ToolTemplate.SelectFileName(connection: TOniData; filename: String);
436 > procedure TForm_ToolTemplate.SelectFileName(ConnectionID: Integer; filename: String);
437   var
438    i: Integer;
451  name: String;
452  nstart, nend: Integer;
439   begin
440 <  for i := 0 to combo_connection.Items.Count - 1 do
441 <  begin
456 <    name := combo_connection.Items.Strings[i];
457 <    nstart := Pos(' ', name) + 1;
458 <    nend := Pos('(', name) - 1;
459 <    name := MidStr(name, nstart, nend - nstart);
460 <
461 <    if ExtractFileName(connection.FileName) = name then
462 <    begin
463 <      combo_connection.ItemIndex := i;
464 <      combo_connectionChange(Self);
465 <    end;
466 <  end;
440 >  if FConnectionID <> ConnectionID then
441 >    SelectConnection(ConnectionID);
442  
443    filelist.ItemIndex := -1;
444    if filelist.Items.Count > 0 then
# Line 508 | Line 483 | begin
483   end;
484  
485  
486 < procedure TForm_ToolTemplate.FormResize(Sender: TObject);
486 > function TForm_ToolTemplate.GetToolCloseable: Boolean;
487   begin
488 <  if Self.Width < 300 then
489 <    Self.Width := 300;
490 <  if Self.Height < 200 then
491 <    Self.Height := 200;
517 < end;
518 <
519 <
520 <
521 < procedure TForm_ToolTemplate.FormCreate(Sender: TObject);
522 < begin
523 <  Self.Width  := 260;
524 <  Self.Height := 300;
525 <  FOnNewFileSelected := nil;
526 <  FOnNewConnection := nil;
527 <  FAllowedExts := '';
528 <  FAllowMultiSelect := False;
488 >  if Assigned(FOnCheckCloseable) then
489 >    Result := FOnCheckCloseable
490 >  else
491 >    Result := True;
492   end;
493  
494   procedure TForm_ToolTemplate.FormActivate(Sender: TObject);

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)