(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)
unit AHMTFileHolder;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,AHMSystemClass;
type
  TAHMFileHolder = class;
  TAHMStoredFiles = class;
  TAHMStoredFile = class(TCollectionItem)
  private
    FFileData: TMemoryStream;
    FFileDate: TDateTime;
    FFileName: String;
    FFilePath: String;
    FFileSize: LongInt;
    FAHMFileHolder : TAHMFileHolder;
    procedure ReadData(Stream: TStream);
    procedure WriteData(Stream: TStream);
    function Empty: boolean;
    procedure SetFileDate(const Value: TDateTime);
    procedure SetFileName(const Value: String);
    procedure SetFilePath(const Value: String);
    procedure SetFileSize(const Value: LongInt);
  protected
    function GetDisplayName: string; override;
    procedure DefineProperties( Filer: TFiler ); override;
  public
    procedure Clear;
    procedure LoadFromFile( FileName: string );
    procedure SaveToFile( FileName: string );
    procedure LoadFromStream( Stream: TStream );
    procedure SaveToStream( Stream: TStream );
    function SaveToTempFile: string;
    constructor Create(Collection: TCollection); override;
    procedure Assign(Source: TPersistent); override;
    property Owner : TAHMFileHolder read FAHMFileHolder;
    property DisplayName : String read GetDisplayName;
    destructor Destroy; override;
  published
    property FileDate: TDateTime read FFileDate write SetFileDate;
    property NameOfFile: String read FFileName write SetFileName;
    property FilePath: String read FFilePath write SetFilePath;
    property FileSize: LongInt read FFileSize write SetFileSize;
  end;
  TAHMStoredFiles = class(TCollection)
  private
    FAHMFileHolder : TAHMFileHolder;
    function GetAHMStoredFile(Index: Integer): TAHMStoredFile;
    procedure SetAHMStoredFile(Index: Integer; Value: TAHMStoredFile);
  protected
    function GetOwner : TPersistent; override;
  public
    constructor Create(AOwner: TAHMFileHolder);
    function Add : TAHMStoredFile;
    function Delete(Index :Integer) :Boolean;
    property Owner : TAHMFileHolder read FAHMFileHolder;
    property Items[Index: Integer]: TAHMStoredFile
             read GetAHMStoredFile write SetAHMStoredFile; default;
  end;
  TAHMFileHolder = class(TAHMSystemsComponent)
  private
    FAHMStoredFiles : TAHMStoredFiles;
    procedure SetAHMStoredFiles(Value: TAHMStoredFiles);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function SaveToTempFile(Index : Integer) : String;
    procedure SaveToFile(Index : Integer; Filename : String);
  published
    property Files: TAHMStoredFiles read FAHMStoredFiles write SetAHMStoredFiles;
  end;

implementation

constructor TAHMStoredFile.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  FAHMFileHolder :=(((Collection as TAHMStoredFiles).GetOwner) as TAHMFileHolder);
  FFileData := TMemoryStream.Create;
end;

Destructor TAHMStoredFile.Destroy;
begin
  FFileData.Free;
  inherited Destroy;
end;

function TAHMStoredFile.GetDisplayName: string;
begin
  if FFileName<>'' then Result := FFileName else Result := 'No File Stored';
end;

function TAHMStoredFiles.GetOwner: TPersistent;
begin
  Result := FAHMFileHolder;
end;

procedure TAHMStoredFile.Assign(Source: TPersistent);
var TS: TMemoryStream;
begin
  ts := TMemoryStream.Create;
  fFileName := TAHMStoredFile(Source).NameOfFile;
  fFileSize := TAHMStoredFile(Source).FileSize;
  fFilePath := TAHMStoredFile(Source).FilePath;
  fFileDate := TAHMStoredFile(Source).FileDate;
  TAHMStoredFile(Source).SaveToStream(Ts);
  FFileData.LoadFromStream(ts);
  Ts.Destroy;
end;

constructor TAHMStoredFiles.Create(AOwner: TAHMFileHolder);
begin
  inherited Create(TAHMStoredFile);
  FAHMFileHolder := AOwner;
end;

function TAHMStoredFiles.Add: TAHMStoredFile;
begin
  Result := TAHMStoredFile(inherited Add);
end;

function TAHMStoredFiles.GetAHMStoredFile(Index: Integer): TAHMStoredFile;
begin
  Result := TAHMStoredFile(inherited GetItem(Index));
end;

procedure TAHMStoredFiles.SetAHMStoredFile(Index: Integer; Value: TAHMStoredFile);
begin
  inherited SetItem(Index, Value);
end;

constructor TAHMFileHolder.Create(AOwner: TComponent);
begin
  inherited create(AOwner);
  FAHMStoredFiles:=TAHMStoredFiles.Create(self);
end;

destructor TAHMFileHolder.Destroy;
begin
  FAHMStoredFiles.Free;
  inherited Destroy;
end;

procedure TAHMFileHolder.SaveToFile(Index: Integer; Filename: String);
begin
  if (Index>-1) and (Index<FAHMStoredFiles.Count) then
  FAHMStoredFiles[Index].SaveToFile(FileName);
end;

function TAHMFileHolder.SaveToTempFile(Index: Integer): String;
begin
  if (Index>-1) and (Index<FAHMStoredFiles.Count) then
  result:=FAHMStoredFiles[Index].SaveToTempFile
  else result:='';
end;

procedure TAHMFileHolder.SetAHMStoredFiles(Value: TAHMStoredFiles);
begin
  FAHMStoredFiles.Assign(Value);
end;

function TAHMStoredFiles.Delete(Index: Integer): Boolean;
begin
  result := true;
  Try
    Items[Index].free;
  Except
    result := false;
    Application.handleexception(self);
  End;
end;

procedure TAHMStoredFile.DefineProperties(Filer: TFiler);
begin
  Filer.DefineBinaryProperty( 'FileContents',ReadData,WriteData,not Empty);
end;

procedure TAHMStoredFile.LoadFromFile(FileName: string);
var SearchRec: TSearchRec;
begin
  if FileExists( FileName ) then
  begin
    FindFirst( FileName, faAnyFile, SearchRec );
    FFileData.LoadFromFile( FileName );
    FFileDate := FileDateToDateTime( SearchRec.Time );
    FFileName := ExtractFileName( FileName );
    FFilePath := copy( ExtractFilePath( FileName ), 1, length( ExtractFilePath( FileName )) - 1 );
    FFileSize := SearchRec.Size;
  end;
end;

procedure TAHMStoredFile.LoadFromStream(Stream: TStream);
begin
  FFileData.LoadFromStream(Stream);
end;

procedure TAHMStoredFile.ReadData(Stream: TStream);
begin
  FFileData.LoadFromStream( Stream );
end;

procedure TAHMStoredFile.SaveToFile(FileName: string);
begin
  if not(Empty) then FFileData.SaveToFile( FileName );
end;

procedure TAHMStoredFile.SaveToStream(Stream: TStream);
begin
  TMemoryStream( Stream ).LoadFromStream(FFileData);
end;

function TAHMStoredFile.SaveToTempFile: string;
var aFileName: array[ 0..512 ] of char;
begin
  GetTempPath( 512, @aFileName );
  GetTempFileName( @aFileName ,'tmp', 0, @aFileName );
  FFileData.SaveToFile(StrPas(aFileName));
  Result := StrPas( aFileName );
end;

procedure TAHMStoredFile.WriteData(Stream: TStream);
begin
  FFileData.SaveToStream(Stream);
end;

function TAHMStoredFile.Empty: boolean;
begin
  Result :=FFileData.Size = 0;
end;

procedure TAHMStoredFile.Clear;
begin
  FFileData.Clear;
  FFileDate := 0;
  FFileName := '';
  FFilePath := '';
  FFileSize := 0;
end;

procedure TAHMStoredFile.SetFileDate(const Value: TDateTime);
begin
  { Can not change ... designtime viewing only}
  if (csLoading in FAHMFileHolder.Componentstate) then FFileDate:=Value;
end;

procedure TAHMStoredFile.SetFileName(const Value: String);
begin
  { Can not change ... designtime viewing only}
  if (csLoading in FAHMFileHolder.Componentstate) then FFileName:=Value;
end;

procedure TAHMStoredFile.SetFilePath(const Value: String);
begin
  { Can not change ... designtime viewing only}
  if (csLoading in FAHMFileHolder.Componentstate) then FFilePath:=Value;
end;

procedure TAHMStoredFile.SetFileSize(const Value: LongInt);
begin
  { Can not change ... designtime viewing only}
  if (csLoading in FAHMFileHolder.Componentstate) then FFileSize:=Value;  
end;

end.
