--- oup/rewrite/Tools/Template.pas 2007/01/18 17:15:59 93 +++ oup/current/Tools/Template.pas 2007/02/26 22:57:17 117 @@ -5,11 +5,12 @@ interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, StrUtils, - ConnectionManager, {Functions,} Data, TypeDefs, Menus, Buttons; + Data, TypeDefs, Menus, Buttons; type TNewFileSelectedEvent = procedure(FileInfo: TFileInfo) of object; TNewConnectionEvent = procedure(Connection: Integer) of object; + TCheckCloseableEvent = function: Boolean of object; TForm_ToolTemplate = class(TForm) panel_files: TPanel; @@ -39,12 +40,14 @@ type Label3: TLabel; combo_connection: TComboBox; Bevel1: TBevel; - procedure RecreateList; - procedure UpdateList; + popup_linkshere: TMenuItem; + popup_separator2: TMenuItem; procedure RecreateExtList; + procedure UpdateConList; procedure LoadFileNames; - procedure SelectFileName(connection: Integer; FileName: String); - procedure SelectFileID(connection: Integer; FileID: Integer); + procedure SelectFileName(ConnectionID: Integer; FileName: String); + procedure SelectFileID(ConnectionID, FileID: Integer); + procedure SelectConnection(ConnectionID: Integer); procedure check_filternameClick(Sender: TObject); procedure check_zerobyteClick(Sender: TObject); procedure combo_extensionClick(Sender: TObject); @@ -52,8 +55,6 @@ type procedure listMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); - procedure FormResize(Sender: TObject); - procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure popup_importClick(Sender: TObject); procedure popup_exportClick(Sender: TObject); @@ -62,26 +63,31 @@ type procedure btn_sortClick(Sender: TObject); procedure FormActivate(Sender: TObject); procedure combo_connectionChange(Sender: TObject); + procedure popup_linkshereClick(Sender: TObject); private FSortBy: TSortType; FOnNewFileSelected: TNewFileSelectedEvent; FOnNewConnection: TNewConnectionEvent; + FOnCheckCloseable: TCheckCloseableEvent; FAllowedExts: String; FAllowMultiSelect: Boolean; FSelectedFile: TFileInfo; - FConnection: Integer; + FConnectionID: Integer; procedure SetAllowedExts(exts: String); procedure SetMultiSelect(allow: Boolean); + function GetToolCloseable: Boolean; public constructor Create(AOwner: TComponent); override; procedure SetFileFilters(pattern, extension: String; zerobytes: Boolean); published property OnNewFileSelected: TNewFileSelectedEvent read FOnNewFileSelected write FOnNewFileSelected; property OnNewConnection: TNewConnectionEvent read FOnNewConnection write FOnNewConnection; + property OnCheckCloseable: TCheckCloseableEvent read FOnCheckCloseable write FOnCheckCloseable; property AllowedExts: String read FAllowedExts write SetAllowedExts; property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect; property SelectedFile: TFileInfo read FSelectedFile; - property Connection: Integer read FConnection; + property ConnectionID: Integer read FConnectionID; + property Closeable: Boolean read GetToolCloseable; end; var @@ -90,34 +96,32 @@ procedure AddToolListEntry(context, name implementation {$R *.dfm} -uses Main, Exporters; +uses Main, ConnectionManager, Exporters, Functions, WhatLinksHere; -procedure TForm_ToolTemplate.UpdateList; +procedure TForm_ToolTemplate.UpdateConList; var i: Integer; fn, datatype, boxstring: String; level: Integer; - oldcon: String; begin - oldcon := combo_connection.Items.Strings[combo_connection.ItemIndex]; combo_connection.ItemIndex := -1; combo_connection.Items.Clear; - if Length(DataConnections) > 0 then + if ConManager.Count > 0 then begin - for i := 0 to High(DataConnections) do + for i := 0 to ConManager.Count - 1 do begin - level := DataConnections[i].LevelInfo.LevelNumber; - fn := ExtractFileName(DataConnections[i].FileName); - if DataConnections[i].Backend = ODB_Dat then + level := ConManager.ConnectionByIndex[i].LevelNumber; + fn := ExtractFileName(ConManager.ConnectionByIndex[i].FileName); + if ConManager.ConnectionByIndex[i].Backend = DB_ONI then datatype := 'ONI-.dat: ' - else if DataConnections[i].Backend = ODB_ADB then + else if ConManager.ConnectionByIndex[i].Backend = DB_ADB then datatype := 'OUP-DB: ' else datatype := 'Unknown: '; - boxstring := datatype + fn + ' (Level: ' + IntToStr(level) + ')'; + boxstring := datatype + fn + ' (Level: ' + IntToStr(level) + ') [' + IntToStr(ConManager.ConnectionByIndex[i].ConnectionID) + ']'; combo_connection.Items.Add(boxstring); - if oldcon = boxstring then + if ConManager.ConnectionByIndex[i].ConnectionID = FConnectionID then combo_connection.ItemIndex := combo_connection.Items.Count - 1; end; if combo_connection.ItemIndex = -1 then @@ -128,72 +132,39 @@ begin end else begin - FConnection := nil; + FConnectionID := 0; filelist.Items.Clear; combo_extension.Items.Clear; combo_connectionChange(Self); - FSelectedFile := GetEmptyFileInfo; + FSelectedFile.ID := -1; if Assigned(FOnNewFileSelected) then FOnNewFileSelected(FSelectedFile); end; end; -procedure TForm_ToolTemplate.RecreateList; -var - i: Integer; - fn, datatype: String; - level: Integer; -begin - combo_connection.Items.Clear; - if Length(DataConnections) > 0 then - begin - for i := 0 to High(DataConnections) do - begin - level := DataConnections[i].LevelInfo.LevelNumber; - fn := ExtractFileName(DataConnections[i].FileName); - if DataConnections[i].Backend = ODB_Dat then - datatype := 'ONI-.dat: ' - else if DataConnections[i].Backend = ODB_ADB then - datatype := 'OUP-DB: ' - else - datatype := 'Unknown: '; - combo_connection.Items.Add(datatype + fn + ' (Level: ' + IntToStr(level) + ')'); - end; - FConnection := DataConnections[0]; - combo_connection.ItemIndex := 0; - combo_connectionChange(Self); - end - else - begin - FConnection := nil; - filelist.Items.Clear; - combo_extension.Items.Clear; - combo_connectionChange(Self); - end; -end; - procedure TForm_ToolTemplate.RecreateExtList; var - i: LongWord; - exts: TStringArray; + i: Integer; + exts: TStrings; begin combo_extension.Items.Clear; - if FConnection <> nil then + if FConnectionID > -1 then begin combo_extension.Items.Add('_All files_ (' + - IntToStr(FConnection.GetFilesCount) + ')'); - exts := FConnection.GetExtensionsList; - for i := 0 to High(exts) do + IntToStr(ConManager.Connection[FConnectionID].GetFileCount) + ')'); + exts := nil; + exts := ConManager.Connection[FConnectionID].GetExtensionsList(EF_ExtCount); + for i := 0 to exts.Count - 1 do if Length(FAllowedExts) > 0 then begin - if Pos(MidStr(exts[i],1,4), FAllowedExts) > 0 then - begin - combo_extension.Items.Add(exts[i]); - end; - end else - combo_extension.Items.Add(exts[i]); + if Pos(MidStr(exts.Strings[i],1,4), FAllowedExts) > 0 then + combo_extension.Items.Add(exts.Strings[i]); + end + else + combo_extension.Items.Add(exts.Strings[i]); combo_extension.ItemIndex := 0; combo_extensionClick(Self); + exts.Free; end; end; @@ -205,10 +176,9 @@ var Extension: String; no_zero_bytes: Boolean; pattern: String; - files: TStringArray; - i: LongWord; + files: TStrings; begin - if FConnection <> nil then + if FConnectionID > -1 then begin Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4); no_zero_bytes := not check_zerobyte.Checked; @@ -221,13 +191,13 @@ begin else Extension := ''; - files := FConnection.GetFilesList(extension, pattern, no_zero_bytes, FSortBy); + files := nil; + files := ConManager.Connection[FConnectionID].GetFilesList(extension, pattern, no_zero_bytes, FSortBy); filelist.Visible := False; filelist.Items.Clear; - if Length(files) > 0 then - for i := 0 to High(files) do - filelist.Items.Add(files[i]); + if files.Count > 0 then + filelist.Items.AddStrings(files); filelist.Visible := True; end; end; @@ -238,12 +208,12 @@ var id: Integer; ext: String; begin - id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]); + id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]); ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4); exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*'; exportd.DefaultExt := ext; if exportd.Execute then - ExportDatFile(FConnection, id, exportd.FileName); + ExportDatFile(FConnectionID, id, exportd.FileName); end; procedure TForm_ToolTemplate.popup_importClick(Sender: TObject); @@ -251,70 +221,79 @@ var id: Integer; finfo: TFileInfo; fs: TFileStream; - data: TData; begin - id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]); - finfo := FConnection.GetFileInfo(id); + if CR_EditDat in ConManager.Connection[FConnectionID].ChangeRights then + begin + id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]); + finfo := ConManager.Connection[FConnectionID].GetFileInfo(id); - importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' + - finfo.Extension + '|All files|*.*'; - if importd.Execute then - begin - fs := TFileStream.Create(importd.FileName, fmOpenRead); - if fs.Size <> finfo.Size then - ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) + - ', file has to have same size as file in .dat.' + CrLf + - 'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf + - 'Size of chosen file: ' + FormatFileSize(fs.Size)) - else begin - SetLength(data, fs.Size); - fs.Read(data[0], fs.Size); - FConnection.UpdateDatFile(id, data); + importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' + + finfo.Extension + '|All files|*.*'; + if importd.Execute then + begin + fs := TFileStream.Create(importd.FileName, fmOpenRead); + if fs.Size <> finfo.Size then + begin + if not (CR_ResizeDat in ConManager.Connection[FConnectionID].ChangeRights) then + begin + ShowMessage('Can''t import ' + ExtractFilename(importd.FileName) + + ', file has to have same size as file in .dat with this backend.' + CrLf + + 'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf + + 'Size of chosen file: ' + FormatFileSize(fs.Size)); + Exit; + end else begin + if MessageBox(Self.Handle, + PChar('File has different size from the file in the .dat.' + CrLf + + 'Size of file in .dat: ' + FormatFileSize(finfo.Size) + CrLf + + 'Size of chosen file: ' + FormatFileSize(fs.Size) + CrLf + + 'Replace anyway?'), PChar('Different size'), MB_YESNO + MB_ICONWARNING) = ID_NO then + begin + Exit; + end; + end; + end; + ConManager.Connection[FConnectionID].UpdateDatFile(id, fs); Self.listClick(Self); + fs.Free; end; - fs.Free; + end else begin + ShowMessage('Editing .dat-contents not allowed with this backend.'); end; end; +procedure TForm_ToolTemplate.popup_linkshereClick(Sender: TObject); +begin + Form_WhatLinksHere.ConID := FConnectionID; + Form_WhatLinksHere.FileID := FSelectedFile.ID; + Form_WhatLinksHere.SenderForm := Self; + Form_WhatLinksHere.Show; +end; + procedure TForm_ToolTemplate.popup_opentool(Sender: TObject); var sender_name, context: String; id: Integer; begin sender_name := TComponent(Sender).Name; - id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]); + id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]); context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name)); - Form_Main.open_child(context, FConnection, id); + Form_Main.open_child(context, FConnectionID, id); end; procedure TForm_ToolTemplate.combo_connectionChange(Sender: TObject); var name: String; - nstart, nend: Integer; - i: Integer; begin if combo_connection.ItemIndex >= 0 then begin name := combo_connection.Items.Strings[combo_connection.ItemIndex]; - nstart := Pos(' ', name) + 1; - nend := Pos('(', name) - 1; - name := MidStr(name, nstart, nend - nstart); - - for i := 0 to High(DataConnections) do - begin - if ExtractFileName(DataConnections[i].FileName) = name then - begin - FConnection := DataConnections[i]; - Break; - end; - end; - if i = Length(DataConnections) then - FConnection := nil; - - RecreateExtList; - if Assigned(FOnNewConnection) then - FOnNewConnection(FConnection); - end; + FConnectionID := StrToInt(MidStr(name, Pos('[', name) + 1, Pos(']', name) - Pos('[', name) - 1)); + end + else + FConnectionID := -1; + RecreateExtList; + if Assigned(FOnNewConnection) then + FOnNewConnection(FConnectionID); end; procedure TForm_ToolTemplate.combo_extensionClick(Sender: TObject); @@ -329,8 +308,16 @@ var item: TMenuItem; begin inherited; - RecreateList; - FSelectedFile := GetEmptyFileInfo; + Self.Width := 260; + Self.Height := 300; + FAllowedExts := ''; + FAllowMultiSelect := False; + FOnNewFileSelected := nil; + FOnNewConnection := nil; + FOnCheckCloseable := nil; + FConnectionID := -1; + FSelectedFile.ID := -1; + UpdateConList; if Length(ToolList) > 0 then begin for i := 0 to High(ToolList) do @@ -367,17 +354,17 @@ end; procedure TForm_ToolTemplate.btn_sortClick(Sender: TObject); begin if btn_sort_id_asc.Down then - FSortBy := stIDAsc + FSortBy := ST_IDAsc else if btn_sort_id_desc.Down then - FSortBy := stIDDesc + FSortBy := ST_IDDesc else if btn_sort_name_asc.Down then - FSortBy := stNameAsc + FSortBy := ST_NameAsc else if btn_sort_name_desc.Down then - FSortBy := stNameDesc + FSortBy := ST_NameDesc else if btn_sort_ext_asc.Down then - FSortBy := stExtAsc + FSortBy := ST_ExtAsc else if btn_sort_ext_desc.Down then - FSortBy := stExtDesc; + FSortBy := ST_ExtDesc; LoadFileNames; end; @@ -393,9 +380,9 @@ var begin if filelist.ItemIndex > -1 then begin - fileid := FConnection.ExtractFileID( + fileid := ConManager.Connection[FConnectionID].ExtractFileIDOfName( filelist.Items.Strings[filelist.ItemIndex]); - FSelectedFile := FConnection.GetFileInfo(fileid); + FSelectedFile := ConManager.Connection[FConnectionID].GetFileInfo(fileid); if Assigned(FOnNewFileSelected) then FOnNewFileSelected(FSelectedFile); end; @@ -408,36 +395,35 @@ var begin pt.X := x; pt.Y := y; -// filelist.ItemIndex := filelist.ItemAtPos(pt, true); -// Self.listClick(Self); + if Shift = [ssRight] then + begin + filelist.ItemIndex := filelist.ItemAtPos(pt, true); + Self.listClick(Self); + end; end; -procedure TForm_ToolTemplate.SelectFileID(connection: TOniData; id: Integer); -var - i: Integer; - name: String; - nstart, nend: Integer; +procedure TForm_ToolTemplate.SelectConnection(ConnectionID: Integer); begin - for i := 0 to combo_connection.Items.Count - 1 do + if FConnectionID <> ConnectionID then begin - name := combo_connection.Items.Strings[i]; - nstart := Pos(' ', name) + 1; - nend := Pos('(', name) - 1; - name := MidStr(name, nstart, nend - nstart); - - if ExtractFileName(connection.FileName) = name then - begin - combo_connection.ItemIndex := i; - combo_connectionChange(Self); - end; + combo_connection.ItemIndex := ConManager.ConnectionIndexByID[ConnectionID]; + combo_connectionChange(Self); end; +end; + +procedure TForm_ToolTemplate.SelectFileID(ConnectionID, FileID: Integer); +var + i: Integer; +begin + if FConnectionID <> ConnectionID then + SelectConnection(ConnectionID); filelist.ItemIndex := -1; if filelist.Items.Count > 0 then for i := 0 to filelist.Items.Count - 1 do - if FConnection.ExtractFileID(filelist.Items.Strings[i]) = id then + if ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[i]) = FileID then begin filelist.ItemIndex := i; Break; @@ -445,25 +431,12 @@ begin Self.listClick(Self); end; -procedure TForm_ToolTemplate.SelectFileName(connection: TOniData; filename: String); +procedure TForm_ToolTemplate.SelectFileName(ConnectionID: Integer; filename: String); var i: Integer; - name: String; - nstart, nend: Integer; begin - for i := 0 to combo_connection.Items.Count - 1 do - begin - name := combo_connection.Items.Strings[i]; - nstart := Pos(' ', name) + 1; - nend := Pos('(', name) - 1; - name := MidStr(name, nstart, nend - nstart); - - if ExtractFileName(connection.FileName) = name then - begin - combo_connection.ItemIndex := i; - combo_connectionChange(Self); - end; - end; + if FConnectionID <> ConnectionID then + SelectConnection(ConnectionID); filelist.ItemIndex := -1; if filelist.Items.Count > 0 then @@ -508,24 +481,12 @@ begin end; -procedure TForm_ToolTemplate.FormResize(Sender: TObject); +function TForm_ToolTemplate.GetToolCloseable: Boolean; begin - if Self.Width < 300 then - Self.Width := 300; - if Self.Height < 200 then - Self.Height := 200; -end; - - - -procedure TForm_ToolTemplate.FormCreate(Sender: TObject); -begin - Self.Width := 260; - Self.Height := 300; - FOnNewFileSelected := nil; - FOnNewConnection := nil; - FAllowedExts := ''; - FAllowMultiSelect := False; + if Assigned(FOnCheckCloseable) then + Result := FOnCheckCloseable + else + Result := True; end; procedure TForm_ToolTemplate.FormActivate(Sender: TObject);