(************************************************************************
*    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 AHMTDsgInetMail;

interface
{$I AHM.INC}
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls,AHMTIEMails,DsgnIntf,Buttons,LibIntf,DsgnWnds,AHMMailConst;

type
  TAHMInetProperty = class(TClassProperty)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;
  TAHMAttachmentProperty = class(TClassProperty)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;

procedure ConfigureInetMail(AHMInetMessages : TAHMMessages;FormDsgn:{$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF});

implementation
uses ComCtrls,AHMTMailDsgnWnd;
type
 TInetMailConfig = class(TAHMMailDsgnWnd)
 private
    ListBox1: TListBox;
    ListBox2: TListBox;
    SpeedButton1:TSpeedButton;
    SpeedButton2:TSpeedButton;
    Button2:Tbutton;
    Button5:TButton;
    SaveDialog1: TSaveDialog;
    OpenDialog1 : TOpenDialog;
    FeedBGauge : TProgressbar;
    FeedBPanel : TPanel;
    FeedBEdit : TEdit;
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure AHMDsgnSMTPMailSendProgress(Sender: TObject; Percent: Word);
    procedure AHMDsgnPopMailRetrProgress(Sender: TObject; Percent: Word);
    procedure AHMDsgnMailOnConnect(Sender :TObject;Const Server : String; Port : Integer);
    procedure AHMDsgnMailError(Sender :TObject;Status,MsgText :String;IsError : Boolean);
    procedure AHMDsgnMailOnDisconnect(Sender :TObject;Const Server : String; Port : Integer);
    procedure AHMDsgnServerMessage(Sender: TObject; Status,Msg: String);
  protected
    procedure Activated; override;
  public
    MsgString : String;
    FAHMInetMessages : TAHMMessages;
    {$IFDEF AHMINPRISE4}
    procedure ComponentDeleted(Component: IPersistent); override;
    {$ELSE}
    procedure ComponentDeleted(Component:TComponent); override;
    {$ENDIF}
    procedure SetupForm;
    {$IFDEF AHMINPRISE3}
    procedure FormClosed(AForm:TCustomForm); override;
    {$ELSE}
    procedure FormClosed(AForm:TForm); override;
    {$ENDIF}
    destructor Destroy; override;
  end;

Var InetMailConfig : TInetMailConfig;

procedure TAHMAttachmentProperty.Edit;
begin
  if InetMailConfig=nil then Showmessage(AHMRcMailDsgMsg1) else
  InetMailConfig.BringToFront;
end;

procedure TInetMailConfig.SetupForm;
begin
  Left := 356;
  Top := 140;
  HorzScrollBar.Visible := False;
  BorderStyle := bsToolWindow;
  Caption := AHMRcMailMsgStr;
  ClientHeight := 360;
  ClientWidth := 235;
  Color := clBtnFace;
  Position := poScreenCenter;
  OnClose := FormClose;
  PixelsPerInch := 96;
  with TBevel.Create(self) do
  begin
    Parent:=self;
    Left := 4;
    Top := 167;
    Width := 225;
    Height := 10;
    Shape := bsTopLine;
  end;
  with TLabel.Create(self) do
  begin
    Parent:=self;
    Left := 4;
    Top := 33;
    Width := 105;
    Height := 13;
    AutoSize := False;
    Caption := AHMRcMailMsgStr;
    Transparent := True;
  end;
  with TBevel.Create(Self) do
  begin
    Parent:=self;
    Left := 3;
    Top := 3;
    Width := 229;
    Height := 30;
    Shape := bsFrame;
  end;
  SpeedButton1:=TSpeedButton.Create(Self);
  with Speedbutton1 do
  begin
    Parent:=Self;
    Left := 7;
    Top := 7;
    Width := 23;
    Height := 23;
    if ((FAHMInetMessages.Owner as TAHMMailHost) is TAHMSmtpMail) then
    Glyph.LoadFromResourceName(HInstance,'TAHMSMTPMail') else
    Glyph.LoadFromResourceName(HInstance,'TAHMPOPMail');
    OnClick := SpeedButton1Click;
  end;
  SpeedButton2:=TSpeedButton.Create(Self);
  with SpeedButton2 do
  begin
    Parent:=Self;
    Left := 33;
    Top := 7;
    Width := 59;
    Height := 23;
    Caption := '>>';
    NumGlyphs := 2;
    OnClick := SpeedButton2Click;
  end;
  with TLabel.Create(Self) do
  begin
    Parent:=Self;
    Left := 4;
    Top := 168;
    Width := 99;
    Height := 13;
    AutoSize := False;
    Caption := AHMRcMailFileStr;
    Transparent := True;
  end;
  with TButton.Create(Self) do
  begin
    Parent:=Self;
    Left := 156;
    Top := 76;
    Width := 75;
    Height := 26;
    Caption := AHMRcMailDelStr;
    TabOrder := 0;
    OnClick := Button1Click;
  end;
  ListBox1:=TListBox.Create(Self);
  with Listbox1 do
  begin
    Parent:=Self;
    Left := 4;
    Top := 49;
    Width := 145;
    Height := 113;
    ItemHeight := 13;
    TabOrder := 1;
    OnClick := ListBox1Click;
    OnMouseDown := ListBox1MouseDown;
  end;
  Listbox2:=TListbox.Create(Self);
  with Listbox2 do
  begin
    Parent:=Self;
    Left := 4;
    Top := 184;
    Width := 225;
    Height := 105;
    ItemHeight := 13;
    TabOrder := 2;
    OnMouseDown := ListBox2MouseDown;
  end;
  Button2:=TButton.Create(Self);
  with Button2 do
  begin
    Parent:=Self;
    Left := 156;
    Top := 49;
    Width := 75;
    Height := 24;
    Caption := AHMRcMailAddStr;
    TabOrder := 3;
    OnClick := Button2Click;
  end;
  with TButton.Create(self) do
  begin
    Parent:=Self;
    Left := 4;
    Top := 297;
    Width := 75;
    Height := 25;
    Caption := AHMRcMailAddStr;
    TabOrder := 4;
    OnClick := Button3Click;
  end;
  with TButton.Create(Self) do
  begin
    Parent:=Self;
    Left := 80;
    Top := 297;
    Width := 75;
    Height := 25;
    Caption := AHMRcMailDelStr;
    TabOrder := 5;
    OnClick := Button4Click;
  end;
  Button5:=TButton.Create(Self);
  with Button5 do
  begin
    Parent:=Self;
    Left := 156;
    Top := 297;
    Width := 75;
    Height := 25;
    Caption := AHMRcMailSaveToStr;
    TabOrder := 6;
    OnClick := Button5Click;
  end;
  OpenDialog1:=TOpenDialog.Create(Self);
  with OpenDialog1 do
  begin
    DefaultExt := AHMRcMailDefExt;
    Filter := AHMRcMailDefFilter;
    Title := AHMRcMailDefTitle;
  end;
  SaveDialog1:=TSaveDialog.Create(Self);
  with SaveDialog1 do Title := AHMRcMailDefSaveAttAs;
  FeedBPanel:=TPanel.Create(Self);
  with FeedBPanel do
  begin
    Parent:=Self;
    Align:=alBottom;
    Height:=20;
    BevelOuter:=bvNone;
  end;
  FeedBGauge:=TProgressbar.Create(Self);
  with FeedBGauge do
  begin
    Parent:=Self;
    Align:=alBottom;
  end;
  FeedBEdit:=TEdit.Create(FeedBPanel);
  with FeedBEdit do
  begin
    Parent:=FeedBPanel;
    Align:=alClient;
    Text:='';
    Font.Name:='Arial';
    Font.Style:=[fsItalic];
    ReadOnly:=True;
  end;
end;

function TAHMAttachmentProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog,paReadOnly];
end;

procedure ConfigureInetMail(AHMInetMessages : TAHMMessages;FormDsgn:{$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF});
Var I : Integer;
begin
  {$WARNINGS OFF}
  if InetMailConfig<>nil then InetMailConfig.BringToFront else
  begin
    InetMailConfig:=TInetMailConfig.Create(Application);
    InetMailConfig.FAHMInetMessages:=AHMInetMessages;
    InetMailConfig.Setupform;
  end;
  {$WARNINGS ON}
  with InetMailConfig do
  begin
    Listbox1.Clear;
    Listbox2.Clear;
    if ((AHMInetMessages.Owner as TAHMMailHost) is TAHMSmtpMail) then
    begin
      Button2.Enabled:=True;
      SpeedButton2.Visible:=True;
      SpeedButton1.Visible:=True;
      Button5.Visible:=False;
      MsgString:=AHMRcMailOutgoingMsg;
    end else
    begin
      Button2.Enabled:=False;
      SpeedButton2.Visible:=True;
      SpeedButton2.Enabled:=True;
      SpeedButton1.Visible:=True;
      Button5.Visible:=True;
      MsgString:=AHMRcMailIncomingMsg;
    end;
    InetMailConfig.Designer:=FormDsgn;
    For I:=0 to AHMInetMessages.Count-1 do
    if AHMInetMessages[I].FromName<>'' then
    listbox1.items.add(AHMInetMessages[I].FromName) else listbox1.items.add(MsgString);
    If Listbox1.Items.Count>0 then Listbox1.Itemindex:=0 else
    if (FAHMInetMessages.Owner is TAHMSMTPMail) then SpeedButton2.enabled:=False;
    If Listbox1.Items.Count>0 then ListBox1Click(AHMInetMessages);
    show;
  end;
end;

procedure TAHMInetProperty.Edit;
begin
  ConfigureInetMail(TAHMMessages(GetOrdValue),Self.Designer);
end;

function TAHMInetProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog,paReadOnly];
end;

procedure TInetMailConfig.Button3Click(Sender: TObject);
begin
  if (Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0) then
  begin
    if OpenDialog1.Execute then
    begin
       Listbox2.Items.add(OpenDialog1.Filename);
       FAHMInetMessages[Listbox1.ItemIndex].Attachments.Add(OpenDIalog1.Filename);
       Listbox2.Itemindex:=Listbox2.Items.Count-1;
       ListBox2MouseDown(self,mbLeft,[ssAlt],0,0);
    end;
  end;
end;

procedure TInetMailConfig.Activated;
begin
//  SpeedButton1Click(self);
end;

destructor TInetMailConfig.Destroy;
begin
  Listbox1.Clear;
  Listbox2.Clear;
  if self=InetMailConfig then InetMailConfig:=nil;
  inherited Destroy;
end;

{$IFDEF AHMINPRISE4}
procedure TInetMailConfig.ComponentDeleted(Component: IPersistent);
{$ELSE}
procedure TInetMailConfig.ComponentDeleted(Component:TComponent);
{$ENDIF}
begin
  {$IFDEF AHMINPRISE4}
  if ExtractPersistent(Component)=(FAHMInetMessages.Owner as TPersistent) then
  {$ELSE}
  if Component=FAHMInetMessages.Owner then
  {$ENDIF}
  begin
    FAHMInetMessages:=nil;
    Release;
  end;
  inherited;
end;

{$IFDEF AHMINPRISE3}
procedure TInetMailConfig.FormClosed(AForm:TCustomForm);
{$ELSE}
procedure TInetMailConfig.FormClosed(AForm:TForm);
{$ENDIF}
begin
  if (FAHMInetMessages.Owner <> nil) and (AForm = ((FAHMInetMessages.Owner) as TAHMMailHost).Owner) then Free;
  inherited;
end;

procedure TInetMailConfig.Button4Click(Sender: TObject);
begin
  if ((Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0)) and ((Listbox2.ItemIndex>-1) and (Listbox2.Items.Count>0)) then
  begin
     FAHMInetMessages[Listbox1.Itemindex].Attachments[Listbox2.ItemIndex].Free;
     Listbox2.Items.Delete(Listbox2.ItemIndex);
     if Listbox2.Items.count>0 then Listbox2.Itemindex:=0 else Listbox2.Itemindex:=-1;
     ListBox2MouseDown(self,mbLeft,[ssAlt],0,0);
  end;
end;

procedure TInetMailConfig.Button5Click(Sender: TObject);
begin
  if ((Listbox2.ItemIndex>-1) and (Listbox2.Items.Count>0)) then
  begin
    SaveDialog1.Filename:=Listbox2.Items[Listbox2.ItemIndex];
    SaveDialog1.Execute;
    FAHMInetMessages[Listbox1.ItemIndex].Attachments[Listbox2.ItemIndex].Savetofile(SaveDialog1.Filename);
  end;
end;

procedure TInetMailConfig.Button2Click(Sender: TObject);
begin
  FAHMInetMessages.Add;
  Listbox1.items.Add(MsgString);
  Listbox1.ItemIndex:=Listbox1.Items.Count-1;
  ListBox1MouseDown(self,mbLeft,[ssAlt],0,0);
  SpeedButton2.enabled:=True;
end;

procedure TInetMailConfig.ListBox1MouseDown(Sender: TObject;Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var FSelectList: {$IFDEF AHMINPRISE5}TDesignerSelectionList;{$ELSE}TComponentList;{$ENDIF}
begin
  if Button=mbRight then exit;
  if ((Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0)) then
  begin
    FSelectList:={$IFDEF AHMINPRISE5}TDesignerSelectionList.Create;{$ELSE}TComponentList.Create;{$ENDIF}
    if (FAHMInetMessages[Listbox1.ItemIndex] as TAHMMessage)<>nil then
    FSelectList.Add(FAHMInetMessages[Listbox1.ItemIndex] as TAHMMessage)
    else FSelectList.Add(FAHMInetMessages);
    {$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF}(Designer).SetSelections(FSelectList);
  end else SpeedButton1Click(self);
end;

procedure TInetMailConfig.FormClose(Sender: TObject;var Action: TCloseAction);
begin
  InetMailConfig:=nil;
  Action:=caFree;
end;

procedure TInetMailConfig.Button1Click(Sender: TObject);
begin
  if ((Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0)) then
  begin
    if (FAHMInetMessages.Owner is TAHMPopMail) then
    TAHMPopMail(FAHMInetMessages.Owner).MessageIDs.Delete(TAHMPopMail(FAHMInetMessages.Owner).MessageIDS.IndexOf(FAHMInetMessages[ListBox1.ItemIndex].MessageID));
    FAHMInetMessages[Listbox1.ItemIndex].Free;
    ListBox1.Items.Delete(Listbox1.ItemIndex);
    Listbox2.Clear;
    if Listbox1.Items.count>0 then Listbox1.Itemindex:=0;
    ListBox1MouseDown(self,mbLeft,[ssLeft],0,0);
  end;
  if Listbox1.Items.count=0 then
  if (FAHMInetMessages.Owner is TAHMSMTPMail) then SpeedButton2.enabled:=False;
end;

procedure TInetMailConfig.ListBox1Click(Sender: TObject);
Var I : Integer;
begin
  if ((Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0)) then
  begin
    Listbox2.Clear;
    For I:=0 to FAHMInetMessages[Listbox1.ItemIndex].Attachments.Count-1 do
    Listbox2.Items.Add(FAHMInetMessages[Listbox1.ItemIndex].Attachments[i].PhysFile);
  end;
end;

procedure TInetMailConfig.SpeedButton1Click(Sender: TObject);
var FSelectList: {$IFDEF AHMINPRISE5}TDesignerSelectionList;{$ELSE}TComponentList;{$ENDIF}
begin
  FSelectList:={$IFDEF AHMINPRISE5}TDesignerSelectionList.Create;{$ELSE}TComponentList.Create;{$ENDIF}
  with FSelectList do
  begin
    Add(FAHMInetMessages.Owner as TAHMMailHost);
    {$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF}(Designer).SetSelections(FSelectList);
  end;
end;

procedure TInetMailConfig.SpeedButton2Click(Sender: TObject);
begin
  Self.Enabled:=False;
  try
    if (FAHMInetMessages.Owner is TAHMSMTPMail) then
      TAHMSMTPMail(FAHMInetMessages.Owner).OnSendProgress:=AHMDsgnSMTPMailSendProgress
    else
    if (FAHMInetMessages.Owner is TAHMPopMail) then
      TAHMPopMail(FAHMInetMessages.Owner).OnRetrieveProgress:=AHMDsgnPopMailRetrProgress;
    TAHMMailHost(FAHMInetMessages.Owner).OnServerMessage:=AHMDsgnServerMessage;
    TAHMMailHost(FAHMInetMessages.Owner).OnConnect:=AHMDsgnMailOnConnect;
    if (FAHMInetMessages.Owner is TAHMPopMail) then
    begin
      TAHMPopMail(FAHMInetMessages.Owner).OnDisconnect:=AHMDsgnMailOnDisconnect;
      TAHMPopMail(FAHMInetMessages.Owner).OnServerResponse:=AHMDsgnMailError;
    end else
    if (FAHMInetMessages.Owner is TAHMSMTPMail) then
    begin
      TAHMSMTPMail(FAHMInetMessages.Owner).OnDisconnect:=AHMDsgnMailOnDisconnect;
      TAHMSMTPMail(FAHMInetMessages.Owner).OnServerResponse:=AHMDsgnMailError;
    end;
    (FAHMInetMessages.Owner as TAHMMailHost).Execute;
  finally
    Self.Enabled:=True;
  end;
end;

procedure TInetMailConfig.ListBox2MouseDown(Sender: TObject;Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var FSelectList: {$IFDEF AHMINPRISE5}TDesignerSelectionList;{$ELSE}TComponentList;{$ENDIF}
begin
  if Button=mbRight then exit;
  if ((Listbox1.ItemIndex>-1) and (Listbox1.Items.Count>0)) and
    ((Listbox2.ItemIndex>-1) and (Listbox2.Items.Count>0)) then
  begin
    FSelectList:={$IFDEF AHMINPRISE5}TDesignerSelectionList.Create;{$ELSE}TComponentList.Create;{$ENDIF}
    FSelectList.Add((FAHMInetMessages[Listbox1.ItemIndex] as TAHMMessage).Attachments[Listbox2.ItemIndex]);
    {$IFDEF AHMINPRISE4}IFormDesigner{$ELSE}TFormDesigner{$ENDIF}(Designer).SetSelections(FSelectList);
  end else ListBox1MouseDown(self,mbLeft,[ssAlt],0,0);
end;

procedure TInetMailConfig.AHMDsgnSMTPMailSendProgress(Sender: TObject; Percent: Word);
begin
  FeedBGauge.Position:=Percent;
  FeedBEdit.Text:=AHMRcMailDsgSending;
  Application.ProcessMessages;
end;

procedure TInetMailConfig.AHMDsgnPopMailRetrProgress(Sender: TObject; Percent: Word);
begin
  FeedBGauge.Position:=Percent;
  FeedBEdit.Text:=AHMRcMailDsgRetrieving;
  Application.ProcessMessages;
end;

procedure TInetMailConfig.AHMDsgnMailOnConnect(Sender :TObject;Const Server : String; Port : Integer);
begin
  SpeedButton2.Enabled:=False;
  FeedBGauge.Position:=0;
  FeedBEdit.Text:=AHMRcMailDsgConnecting+Server+AHMRcMailDsgOnPort+Inttostr(Port);
  Application.ProcessMessages;
end;

procedure TInetMailConfig.AHMDsgnMailOnDisconnect(Sender :TObject;Const Server : String; Port : Integer);
Var I : Integer;
begin
  FeedBGauge.Position:=0;
  FeedBEdit.Text:=AHMRcMailDsgDisconnecting+Server;
  Listbox1.Clear;
  Listbox2.Clear;
  For I:=0 to FAHMInetMessages.Count-1 do
  if FAHMInetMessages[I].FromName<>'' then listbox1.items.add(FAHMInetMessages[I].FromName)
                                      else listbox1.items.add(MsgString);
  If Listbox1.Items.Count>0 then Listbox1.Itemindex:=0 else
  if (FAHMInetMessages.Owner is TAHMSMTPMail) then SpeedButton2.enabled:=False;
  If Listbox1.Items.Count>0 then ListBox1Click(FAHMInetMessages);
  SpeedButton2.Enabled:=True;
  Application.ProcessMessages;
end;

procedure TInetMailConfig.AHMDsgnServerMessage(Sender: TObject; Status,Msg: String);
begin
  FeedBEdit.Text:=Status+' - '+Msg;
  Application.ProcessMessages;
end;

procedure TInetMailConfig.AHMDsgnMailError(Sender :TObject;Status,MsgText :String;IsError : Boolean);
begin
  FeedBGauge.Position:=0;
  FeedBEdit.Text:=Status+' - '+MsgText;
  Application.ProcessMessages;
end;

end.
