unit AHMTMailAccount;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,AHMMailClass;
type
  TAHMAccountHolder = class;
  TAHMMailAccounts = class;
  TAHMMailAccount = class(TCollectionItem)
  private
    FAHMAccountHolder : TAHMAccountHolder;
    FInclude: Boolean;
    FTimeout: Integer;
    FFullName: String;
    FAccountName: String;
    FOrganization: String;
    FUserName: String;
    FPassword: String;
    FHeaderOnly : Boolean;
    FPop3Port: Integer;
    FPop3Server: String;
    FSMTPPort: Integer;
    FReplyMail: String;
    FFromMail: String;
    FSMTPServer: String;
    FDefault: Boolean;
    FProgressStep: Integer;
    FDeleteOffServer: Boolean;
    procedure SetTimeOut(const Value: Integer);
    procedure SetDefault(const Value: Boolean);
    procedure SetProgressStep(const Value: Integer);
  protected
    function GetDisplayName: string; override;
  public
    constructor Create(Collection: TCollection); override;
    procedure Assign(Source: TPersistent); override;
    property Owner : TAHMAccountHolder read FAHMAccountHolder;
    property DisplayName : String read GetDisplayName;
  published
    Property ProgressStep : Integer read FProgressStep write SetProgressStep;
    Property Pop3Server : String read FPop3Server write FPop3Server;
    Property SMTPServer : String read FSMTPServer write FSMTPServer;
    Property Fullname : String read  FFullName write FFullName;
    Property Organization : String read FOrganization write FOrganization;
    Property Username : String read FUserName write FUserName;
    Property Password : String read FPassword write FPassword;
    Property Pop3Port : Integer read FPop3Port write FPop3Port default 110;
    Property SMTPPort : Integer read FSMTPPort write FSMTPPort default 25;
    Property TimeOut : Integer read FTimeout write SetTimeOut default 60;
    Property FromMail : String read FFromMail write FFromMail;
    Property ReplyMail : String read FReplyMail write FReplyMail;
    Property AccountName : String read FAccountName write FAccountName;
    Property Include : Boolean read FInclude write FInclude default True;
    Property Default : Boolean read FDefault write SetDefault default True;
    Property HeaderOnly : Boolean read FHeaderOnly write FHeaderOnly default False;
    Property DeleteOffServer : Boolean read FDeleteOffServer write FDeleteOffServer default False;
  end;
  TAHMMailAccounts = class(TCollection)
  private
    FAHMAccountHolder : TAHMAccountHolder;
    function GetAHMMailAccount(Index: Integer): TAHMMailAccount;
    procedure SetAHMMailAccount(Index: Integer; Value: TAHMMailAccount);
  protected
    function GetOwner : TPersistent; override;
  public
    constructor Create(AOwner: TAHMAccountHolder);
    function Add : TAHMMailAccount;
    function Delete(Index :Integer) :Boolean;
    property Owner : TAHMAccountHolder read FAHMAccountHolder;
    property Items[Index: Integer]: TAHMMailAccount
             read GetAHMMailAccount write SetAHMMailAccount; default;
  end;
  TAHMAccountHolder = class(TAHMMailsComponent)
  private
    FAHMMailAccounts : TAHMMailAccounts;
    FDateTimeFormat: String;
    procedure SetAHMMailAccounts(Value: TAHMMailAccounts);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Accounts: TAHMMailAccounts read FAHMMailAccounts write SetAHMMailAccounts;
    property DateTimeFormat : String read FDateTimeFormat write FDateTimeFormat;
  end;

implementation

constructor TAHMMailAccount.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  FAHMAccountHolder :=(((Collection as TAHMMailAccounts).GetOwner) as TAHMAccountHolder);
  FDefault:=true;
  FInclude:=True;
  FPop3Port:=110;
  FDeleteOffServer:=False;
  FHeaderOnly:=False;
  FSMTPPort:=25;
  FTimeOut:=60;
end;

function TAHMMailAccount.GetDisplayName: string;
begin
  if FAccountName='' then Result := inherited GetDisplayName
  else Result:=FAccountName;
end;

function TAHMMailAccounts.GetOwner: TPersistent;
begin
  Result := FAHMAccountHolder;
end;

procedure TAHMMailAccount.Assign(Source: TPersistent);
begin
  if Source is TAHMMailAccount then
  begin
    FInclude := TAHMMailAccount(Source).FInclude;
    FTimeout := TAHMMailAccount(Source).FTimeOut;
    FFullName := TAHMMailAccount(Source).FFullName;
    FAccountName := TAHMMailAccount(Source).FAccountName;
    FOrganization := TAHMMailAccount(Source).FOrganization;
    FUserName := TAHMMailAccount(Source).FUserName;
    FPassword := TAHMMailAccount(Source).FPassword;
    FPop3Port := TAHMMailAccount(Source).FPop3Port;
    FPop3Server := TAHMMailAccount(Source).FPop3Server;
    FSMTPPort := TAHMMailAccount(Source).FSMTPPort;
    FReplyMail := TAHMMailAccount(Source).FReplyMail;
    FFromMail := TAHMMailAccount(Source).FFromMail;
    FSMTPServer := TAHMMailAccount(Source).FSMTPServer;
    FDeleteOffServer := TAHMMailAccount(Source).FDeleteOffServer;
    FHeaderOnly := TAHMMailAccount(Source).FHeaderOnly;
    Exit;
  end;
  inherited Assign(Source);
end;

constructor TAHMMailAccounts.Create(AOwner: TAHMAccountHolder);
begin
  inherited Create(TAHMMailAccount);
  FAHMAccountHolder := AOwner;
end;

function TAHMMailAccounts.Add: TAHMMailAccount;
begin
  Result := TAHMMailAccount(inherited Add);
end;

function TAHMMailAccounts.GetAHMMailAccount(Index: Integer): TAHMMailAccount;
begin
  Result := TAHMMailAccount(inherited GetItem(Index));
end;

procedure TAHMMailAccounts.SetAHMMailAccount(Index: Integer; Value: TAHMMailAccount);
begin
  inherited SetItem(Index, Value);
end;

constructor TAHMAccountHolder.Create(AOwner: TComponent);
begin
  inherited create(AOwner);
  FDateTimeFormat := LongDateFormat + ', ' + LongTimeFormat;
  FAHMMailAccounts:=TAHMMailAccounts.Create(self);
end;

destructor TAHMAccountHolder.Destroy;
begin
  FAHMMailAccounts.Free;
  inherited Destroy;
end;

procedure TAHMAccountHolder.SetAHMMailAccounts(Value: TAHMMailAccounts);
begin
  FAHMMailAccounts.Assign(Value);
end;

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

procedure TAHMMailAccount.SetTimeOut(const Value: Integer);
begin
  if Value>0 then FTimeout := Value else FTimeOut:=1;
end;

procedure TAHMMailAccount.SetDefault(const Value: Boolean);
Var I : Integer;
begin
  if Value then
  for I:=0 to FAHMAccountHolder.Accounts.Count-1 do FAHMAccountHolder.Accounts[I].FDefault:=False;
  FDefault := Value;
end;

procedure TAHMMailAccount.SetProgressStep(const Value: Integer);
begin
  if (Value>0) and (Value<=100) then FProgressStep := Value else FProgressStep:=1;
end;

end.
