unit _MetaManager; interface uses _MetaTypes; type TMetaManager = class protected FFiles: array of TFile; FRoot: TExtensions; FConnectionID: Integer; FDataAccess: TObject; function GetFileCount: Integer; function GetFileById(Id: Integer): TFile; function GetExt(Ext: String): TExtension; procedure AddExt(Ext: String); private procedure InitExts; public constructor Create(ConnectionID: Integer; DataAccess: TObject); procedure InitExtFiles(Ext: String); procedure InitFile(id: Integer); procedure InitFileFields(id: Integer); property Root: TExtensions read FRoot; property RootExt[Ext: String]: TExtension read GetExt; property FileCount: Integer read GetFileCount; property FileById[Id: Integer]: TFile read GetFileById; end; implementation uses Classes, ConnectionManager, Access_OniArchive, TypeDefs, Dialogs, SysUtils, StrUtils, DataAccess; { TFileManager } constructor TMetaManager.Create(ConnectionID: Integer; DataAccess: TObject); begin FConnectionID := ConnectionID; FDataAccess := DataAccess; SetLength(FFiles, TDataAccess(DataAccess).GetFileCount); InitExts; end; function TMetaManager.GetExt(Ext: String): TExtension; var i: Integer; begin Result := nil; if Length(FRoot) > 0 then for i := 0 to High(FRoot) do if FRoot[i].Ext = Ext then begin Result := FRoot[i]; Break; end; end; procedure TMetaManager.AddExt(Ext: String); var i: Integer; begin SetLength(FRoot, Length(FRoot) + 1); for i := High(FRoot) downto 1 do begin if FRoot[i-1].Ext < Ext then begin FRoot[i] := TExtension.Create(FConnectionID, Ext); Break; end else FRoot[i] := FRoot[i-1]; end; if i = 0 then FRoot[0] := TExtension.Create(FConnectionID, Ext); end; function TMetaManager.GetFileById(Id: Integer): TFile; begin Result := FFiles[Id]; end; function TMetaManager.GetFileCount: Integer; begin Result := Length(FFiles); end; procedure TMetaManager.InitFile(id: Integer); var typei: Integer; finfo: TFileInfo; begin if id < ConManager.Connection[FConnectionID].GetFileCount then begin if not Assigned(FFiles[id]) then begin finfo := ConManager.Connection[FConnectionID].GetFileInfo(id); if finfo.Size > 0 then begin for typei := 0 to High(FileDescs) do begin if FileDescs[typei].ext = finfo.Extension then begin FFiles[id] := TFileClass(FileDescs[typei].ftype).Create(FConnectionID, id); Break; end; end; if typei > High(FileDescs) then FFiles[id] := TFile_Empty.Create(FConnectionID, id); end else FFiles[id] := TFile_Empty.Create(FConnectionID, id); Exit; end; end; end; procedure TMetaManager.InitFileFields(id: Integer); begin if id < ConManager.Connection[FConnectionID].GetFileCount then begin if not Assigned(FFiles[id]) then begin InitFile(id); if not (FFiles[id] is TFile_Empty) then FFiles[id].InitDataFields; end; end; end; procedure TMetaManager.InitExtFiles(Ext: String); var files: TStrings; i: Integer; typei: Integer; fid: Integer; finfo: TFileInfo; begin if ConManager.Connection[FConnectionID] is TAccess_OniArchive then TAccess_OniArchive(ConManager.Connection[FConnectionID]).UnloadWhenUnused := False; files := TStringList.Create; files := ConManager.Connection[FConnectionID].GetFilesList('', '', False, ST_IDAsc); SetLength(FFiles, ConManager.Connection[FConnectionID].GetFileCount); for i := 0 to High(FFiles) do FFiles[i] := nil; if files.Count > 0 then begin for i := 0 to files.Count - 1 do begin fid := StrToInt(MidStr(files.Strings[i], 1, 5)); finfo := ConManager.Connection[FConnectionID].GetFileInfo(fid); if Length(finfo.Name) > 0 then begin if finfo.Size > 0 then begin for typei := 0 to High(FileDescs) do begin if FileDescs[typei].ext = finfo.Extension then begin FFiles[fid] := TFileClass(FileDescs[typei].ftype).Create(FConnectionID, fid); Break; end; end; end else FFiles[fid] := TFile_Empty.Create(FConnectionID, fid); end; end; end; files.Free; if ConManager.Connection[FConnectionID] is TAccess_OniArchive then TAccess_OniArchive(ConManager.Connection[FConnectionID]).UnloadWhenUnused := True; end; procedure TMetaManager.InitExts; var files: TStrings; i: Integer; fid: Integer; finfo: TFileInfo; begin files := TStringList.Create; files := TDataAccess(FDataAccess).GetFilesList('', '', False, ST_IDAsc); SetLength(FRoot, 0); if files.Count > 0 then begin for i := 0 to files.Count - 1 do begin fid := StrToInt(MidStr(files.Strings[i], 1, 5)); finfo := TDataAccess(FDataAccess).GetFileInfo(fid); if Length(finfo.Name) > 0 then if not Assigned(GetExt(finfo.Extension)) then AddExt(finfo.Extension); end; end; files.Free; end; end.