{*****************************************************************************
 *
 *  FMAccount.pas - Account form  (28-July-1998)
 *
 *  Copyright (c) 1998-99 Michael Haller
 *
 *  Author:     Michael Haller
 *  E-mail:     michael@discountdrive.com
 *  Homepage:   http://www.discountdrive.com/sunrise
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation;
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 *----------------------------------------------------------------------------
 *
 *  Revision history:
 *
 *     DATE     REV                 DESCRIPTION
 *  ----------- --- ----------------------------------------------------------
 *
 *****************************************************************************}

unit FMAccount;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  GradForm,
  StdCtrls, ExtCtrls, ComCtrls, PXStuff, ComboBoxEx, ImgList;

type
  TAccountForm = class(TdfsGradientForm)
    Panel1: TPanel;
    sPageControl1: TPageControl;
    sTabSheet1: TTabSheet;
    sTabSheet2: TTabSheet;
    sTabSheet3: TTabSheet;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Bevel1: TBevel;
    Label3: TLabel;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Bevel2: TBevel;
    Label8: TLabel;
    Bevel3: TBevel;
    Label9: TLabel;
    Edit6: TEdit;
    Label10: TLabel;
    Edit7: TEdit;
    Label11: TLabel;
    Edit8: TEdit;
    Label12: TLabel;
    Edit9: TEdit;
    Label13: TLabel;
    Bevel4: TBevel;
    Label14: TLabel;
    Edit10: TEdit;
    Label15: TLabel;
    Edit11: TEdit;
    Label16: TLabel;
    Bevel5: TBevel;
    sButton1: TButton;
    sButton2: TButton;
    sTabSheet4: TTabSheet;
    Label17: TLabel;
    Bevel6: TBevel;
    Label18: TLabel;
    Edit12: TEdit;
    Label19: TLabel;
    Edit13: TEdit;
    Label20: TLabel;
    Panel2: TPanel;
    Image1: TImage;
    Image2: TImage;
    Label24: TLabel;
    Label21: TLabel;
    Edit14: TEdit;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Label22: TLabel;
    ComboBoxEx1: TPMComboBoxEx;
    ImageList1: TImageList;
    CheckBox3: TCheckBox;
    sTabSheet5: TTabSheet;
    txtSMTPUserName: TEdit;
    txtSMTPPassword: TEdit;
    Label31: TLabel;
    Label32: TLabel;
    chkAuthenticate: TCheckBox;
    btnSameAsPop: TButton;
    chkSmtpAfterPop: TCheckBox;
    Label27: TLabel;
    Label28: TLabel;
    Button3: TButton;
    CheckBox4: TCheckBox;
    Label25: TLabel;
    Bevel7: TBevel;
    Label26: TLabel;
    Memo1: TMemo;
    cboSignatures: TComboBox;
    procedure FormShow(Sender: TObject);
    procedure sButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure cboSignaturesClick(Sender: TObject);
    procedure btnSameAsPopClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    CreateNewAccount: Boolean;
    procedure LoadAccountData(Data: PAccountData);
    procedure SaveAccountData(var Data: PAccountData);
  end;

var
  AccountForm: TAccountForm;

implementation

{$R *.DFM}

uses
  Main;

procedure TAccountForm.LoadAccountData(Data: PAccountData);
begin
  Edit1.Text := Data^.Name;
  Edit2.Text := Data^.FromMail;
  Edit3.Text := Data^.FromDesc;
  Edit4.Text := Data^.ReplyMail;
  Edit5.Text := Data^.ReplyDesc;
  Edit6.Text := Data^.POPServer;
  Edit7.Text := Data^.POPPort;
  Edit8.Text := Data^.POPUser;
  Edit9.Text := Data^.POPPass;
  Edit10.Text := Data^.SMTPServer;
  Edit11.Text := Data^.SMTPPort;
  Edit12.Text := Data^.KeepFilter;
  Edit13.Text := Data^.DeleteFilter;
  Edit14.Text := Data^.Organization;
  chkAuthenticate.Checked := Data^.SMTPAuthenticate;
  txtSMTPUserName.Text := Data^.SMTPUserName;
  txtSMTPPassword.Text := Data^.SMTPPassword;
  Checkbox1.Checked := (Data^.DoPOP = 1);
  Checkbox2.Checked := (Data^.DoSMTP = 1);
  chkSmtpAfterPop.Checked := (Data^.DoSMTPAfterPOP = 1);
  Checkbox3.Checked := (Data^.PromptPassword = 1);
  // AHRK19
  CheckBox4.Checked := (Data^.ShowDownload = 1);
  ComboBoxEx1.Value := IntToStr(Data^.StandardDownload);
  Memo1.Lines.Clear;
  if FileExists(Data^.Path + 'signat.txt') then
    Memo1.Lines.LoadFromfile(Data^.Path + 'signat.txt');
  CheckBox1Click(Self);
end;

procedure TAccountForm.SaveAccountData(var Data: PAccountData);
begin
  Data^.Name := Edit1.Text;
  if CreateNewAccount then begin
    Data^.Path := sAccountRootFolder +
      MainForm.GetUniqueAccountFolder(Data^.Name) + '\';
    CreateFolder(Data^.Path);
  end;
  Data^.FromMail := Edit2.Text;
  Data^.FromDesc := Edit3.Text;
  Data^.ReplyMail := Edit4.Text;
  Data^.ReplyDesc := Edit5.Text;
  Data^.POPServer := Edit6.Text;
  Data^.POPPort := Edit7.Text;
  Data^.POPUser := Edit8.Text;
  Data^.POPPass := Edit9.Text;
  Data^.SMTPServer := Edit10.Text;
  Data^.SMTPPort := Edit11.Text;
  Data^.KeepFilter := Edit12.Text;
  Data^.DeleteFilter := Edit13.Text;
  Data^.Organization := Edit14.Text;
  Data^.SMTPAuthenticate := chkAuthenticate.Checked;
  Data^.SMTPUserName := txtSMTPUserName.Text;
  Data^.SMTPPassword := txtSMTPPassword.Text;
  Data^.DoPOP := Ord(Checkbox1.Checked); //AH16
  Data^.DoSMTP := Ord(Checkbox2.Checked); // AH16
  Data^.DoSMTPAfterPOP := Ord(chkSmtpAfterPop.Checked); //AH16
  Data^.StandardDownload := StrToInt(ComboBoxEx1.Value); //AH16
  Data^.PromptPassword := Ord(Checkbox3.Checked);
  // AHRK19
  Data^.ShowDownload := Ord(CheckBox4.Checked);
  // AH05 delete file...
  if Trim(Memo1.Lines.Text) = '' then
  begin
    if FileExists(Data^.path + 'signat.txt') then
      DeleteFile(Data^.path + 'signat.txt');
  end
  else
    Memo1.Lines.SaveToFile(Data^.Path + 'signat.txt');
  // AH05
end;

procedure TAccountForm.FormShow(Sender: TObject);
begin
  Edit9.PasswordChar := '*'; //for Hackers :-) AH04 Like me? ;-)
  txtSMTPPassword.PasswordChar := '*';
  sPageControl1.ActivePage:=sTabSheet1;

end;

procedure TAccountForm.sButton1Click(Sender: TObject);
begin
  if Edit2.Text = '' then
    MessageDlg(GetMessageString(37), mtwarning, [mbOK], 0)
  else
    ModalResult := mrOK;
end;

procedure TAccountForm.FormCreate(Sender: TObject);
var
  Found: integer;
  SearchRec: TSearchRec;
begin
  Image1.Picture.Bitmap := MainForm.Image5.Picture.Bitmap;
  Label24.Font.Color := lblFontColor; //AH16
  Memo1.Font := MainForm.DefaultFont;
  ImageList1.ResourceLoad(rtBitmap, 'PX501', clFuchsia);

  // signatures
  Found := FindFirst(sSignatureFolder + '*.txt', faAnyFile, SearchRec);
  while Found = 0 do begin
    SetFileAttr(sSignatureFolder + SearchRec.Name, False, True, False, False);
    Delete(SearchRec.Name, Length(SearchRec.Name) - 3, 4);
    cboSignatures.Items.Add(SearchRec.Name);
    Found := FindNext(SearchRec);
  end;
  FindClose(SearchRec);
end;

procedure TAccountForm.CheckBox1Click(Sender: TObject);
var
  B: Boolean;
begin
  B := not CheckBox3.Checked;
  if CheckBox1.Checked = False then
    B := False;
  Edit9.Enabled := B;
  B := CheckBox1.Checked;
  Label9.Enabled := B;
  Label10.Enabled := B;
  Label11.Enabled := B;
  Label12.Enabled := B;
  Edit6.Enabled := B;
  Edit7.Enabled := B;
  Edit8.Enabled := B;
  CheckBox3.Enabled := B; // AHRK19
  CheckBox4.Enabled := B;
  B := CheckBox2.Checked;
  Label14.Enabled := B;
  Label15.Enabled := B;
  Edit10.Enabled := B;
  Edit11.Enabled := B;
  chkAuthenticate.Enabled := B;
  Label32.Enabled := B and chkAuthenticate.Checked;
  Label31.Enabled := B and chkAuthenticate.Checked;
  txtSMTPUserName.Enabled := B and chkAuthenticate.Checked;
  txtSMTPPassword.Enabled := B and chkAuthenticate.Checked;

  chkSmtpAfterPop.Enabled := B and CheckBox1.Checked;
  B := CheckBox1.Checked and chkAuthenticate.Checked and
    chkAuthenticate.Enabled;
  btnSameAsPop.Enabled := B;
end;

procedure TAccountForm.Button3Click(Sender: TObject);
begin
  Application.HelpContext(19);
end;

procedure TAccountForm.cboSignaturesClick(Sender: TObject);
var
  F: TextFile;
  S : string;
begin
  Memo1.Lines.Clear;
  if cboSignatures.ItemIndex > -1 then begin
    AssignFile(F, sSignatureFolder + cboSignatures.Items[cboSignatures.ItemIndex]
      + '.txt');
    Reset(F);
    while not EoF(F) do begin
      ReadLn(F, S);
      Memo1.Lines.Add(S);
    end;
    CloseFile(F);
    Memo1.Enabled := True;
  end;
end;

procedure TAccountForm.btnSameAsPopClick(Sender: TObject);
begin
  txtSMTPUserName.Text := Edit8.Text;
  txtSMTPPassword.Text := Edit9.Text;
end;

initialization
   PMRegistry.AddCustomClass(TAccountForm, 'TAccountForm');

end.
