{*****************************************************************************
 *
 *  FMOnline.pas - Fetch and send mails  (12-August-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 FMOnline;

interface

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

type
  TOnlineForm = class(TdfsGradientForm)
    Panel1: TPanel;
    Notebook1: TNotebook;
    Label1: TLabel;
    Image3: TImage;
    BtmDetail: TButton;
    GroupBox3: TGroupBox;
    Label2: TLabel;
    Label6: TLabel;
    Image4: TImage;
    Button3: TButton;
    Button5: TButton;
    GroupBox4: TGroupBox;
    Label7: TLabel;
    Label5: TLabel;
    Image5: TImage;
    Button1: TButton;
    Button6: TButton;
    GroupBox5: TGroupBox;
    Label17: TLabel;
    Edit1: TEdit;
    Label18: TLabel;
    Image6: TImage;
    GroupBox6: TGroupBox;
    Button10: TButton;
    Edit2: TEdit;
    Button11: TButton;
    Label20: TLabel;
    Panel2: TPanel;
    Label9: TLabel;
    Label10: TLabel;
    Label13: TLabel;
    Label15: TLabel;
    Splitter1: TSplitter;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label11: TLabel;
    Edit3: TEdit;
    Label12: TLabel;
    btnStop: TButton;
    ProgressBar1: TProgressBar;
    menLabel14: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure BtmDetailClick(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure FormDestroy(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure dfsGradientFormShow(Sender: TObject);
    procedure btnHideClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    CommandList: TStringList;
    OnlineTyp: Byte;
    procedure SetCancelOpportunitiesFalse;
    procedure ShowStatusMessage;
    procedure DialUpNetwork;
  end;

var
  OnlineForm: TOnlineForm;


implementation

{$R *.DFM}

uses
  Main, TDOnline, ShowError;

procedure TOnlineForm.SetCancelOpportunitiesFalse;
begin
  with MainForm do begin
    StatusBar1.Panels[2].Text := '';
    StatusBar1.Panels[3].Text := '';
    Label5.Caption := '';
    Label4.Caption := '';
    MenCancelOnlineOperation1.Enabled := False;
  end;
end;



//TOGO -oMjg:display status after send
procedure TOnlineForm.ShowStatusMessage;
var
  S1, S2: string;
begin
  try
    SetCancelOpportunitiesFalse;
    Screen.Cursor := crDefault;
    try
      if bDUNNormalQuit then
        MainForm.DialUp1.GoOffline;
    except
    end;
    if MainForm.Terminating or ((not bAskToGoOffline) and (Abort)) then begin
      MainForm.OnlineThreadCounter := 0;
      MainForm.OnlineThreadTerminated := True;
      Exit;
    end;
    if bViewStatusAtEnd or bAskToGoOffline then
    begin
      PlaySound(sSoundFile3);
      if AvailableMailCount > 0 then begin
        S1 := Format(GetMessageString(59), [IntToStr(FetchedMailCount),   //%s/%s新邮件
          IntToStr(AvailableMailCount)]);
      end
      else
        S1 := GetMessageString(60);        //没有新邮件
      if SendMailCount = 1 then
        S2 := GetMessageString(61)        //1封邮件已发送
      else if SendMailCount > 1 then
        S2 := Format(GetMessageString(62), [IntToStr(SendMailCount)])   //%s封邮件已发送
      else
        S2 := GetMessageString(63);                        //没有新邮件

      if OnlineTyp = 1 then
      begin
       Label7.Caption := S1;
       MainForm.Label5.Caption :=Label7.Caption;
      end
      else if OnlineTyp in [2, 8] then // AHRK74 -- Add Selective send.
      begin
        Label7.Caption := S2;
        MainForm.Label5.Caption :=Label7.Caption;
      end

      else if OnlineTyp in [3,4, 7] then
        begin
        //Label7.Caption := S1 + ' ' + S2;
        Label7.Caption := S1;
        MainForm.Label5.Caption :=Label7.Caption;
        end;

      if bAskToGoOffline then begin
        Label6.Caption := Label9.Caption;
        Label5.Visible := True;
        Button5.Visible := True;
        Button3.Caption := Label10.Caption;
      end
      else begin
        Label6.Caption := Label13.Caption;
        Label5.Visible := False;
        Button5.Visible := False;
//        Button3.Caption := Button4.Caption;
      end;
//      NoteBook1.ActivePage := 'Offline';
//      ShowModal;
    end;
    FrmShowError.ProgressBar1.Position:=100;
  except
  end;
  MainForm.OnlineThreadCounter := 0;
  MainForm.OnlineThreadTerminated := True;
end;

procedure TOnlineForm.DialUpNetwork;
begin
  Screen.Cursor := crDefault;
  try
    OnlineThread.Name := GetMessageString(80);
    if MainForm.DialUp1.PossibleConnections.IndexOf(sDUNConnection) <> -1 then
      if MainForm.DialUp1.CurrentConnection = '' then begin //if offline
        MainForm.DialUp1.ConnectTo := sDUNConnection;
        MainForm.DialUp1.Password := sDUNPassword;
        if bDUNAskForPassword then
          MainForm.DialUp1.Password := '';
        MainForm.DialUp1.Username := sDUNUsername;
        OnlineThread.AddToLog(ltStatus, 'Connecting to Dial Up Network...');
        if MainForm.DialUp1.Password = '' then begin
          Notebook1.ActivePage := 'DUN';
          ActiveControl := Edit2;
          if ShowModal = mrCancel then begin
            MainForm.DialUp1.GoOffline;
            OnlineThread.ShowError(GetMessageString(82));
            Exit;
          end
          else
            MainForm.DialUp1.Password := Edit1.Text;
        end;
        MainForm.DialUp1.GoOnline;
        repeat
          Application.HandleMessage;
          if Abort then begin
            MainForm.DialUp1.GoOffline;
            Exit;
          end;
        until (MainForm.DialUp1.CurrentConnection <> '');
      end;
  except
    on Error: Exception do begin
      MainForm.DialUp1.GoOffline;
      OnlineThread.ShowError(Error.Message);
    end;
  end;
end;

procedure TOnlineForm.FormCreate(Sender: TObject);

  procedure SetBitmapToImage(var S: TImage; I: Integer);
  var
    Bitmap: TBitmap;
  begin
    Bitmap := TBitmap.Create;
    MainForm.ImageList3.GetBitmap(I, Bitmap);
    S.Picture.Bitmap.Assign(Bitmap);
    Bitmap.Free;
  end;

begin
  GroupBox3.Caption := Label15.Caption;
  if ColorScheme or LowColor then begin
//    SetBitmapToImage(Image3, 12);
//    SetBitmapToImage(Image4, 10);
//    SetBitmapToImage(Image5, 11);
//    Image3.Transparent := True;
//    Image4.Transparent := True;
//    Image5.Transparent := True;
//    Image6.Transparent := True;
  end;
//  Image6.Picture.Bitmap.Assign(Image4.Picture.Bitmap);
  Image3.Width := 28;
  Image4.Width := 28;
  Image5.Width := 28;
  Image6.Width := 28;
  Edit1.PasswordChar := '*';
  Edit2.PasswordChar := '*';
  CommandList := TStringList.Create;
end;

procedure TOnlineForm.BtmDetailClick(Sender: TObject);
begin
  if FileExists(sProtocolFile) then
    ShellExecute(Handle, 'open', PChar(sProtocolFile), '',
      PChar(ExtractFilePath(sProtocolFile)), SW_SHOWNORMAL);
end;

procedure TOnlineForm.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then begin
    Key := #0;
    ModalResult := mrOK;
  end;
end;

procedure TOnlineForm.FormDestroy(Sender: TObject);
begin
  CommandList.Free;
end;

procedure TOnlineForm.Button5Click(Sender: TObject);
begin
  MainForm.DialUp1.GoOffline;
end;

procedure TOnlineForm.dfsGradientFormShow(Sender: TObject);
begin
  Caption := 'Smail';

end;

procedure TOnlineForm.btnHideClick(Sender: TObject);
begin
  CommandList.Free;
  close;
end;

procedure TOnlineForm.btnStopClick(Sender: TObject);
begin
  close;
end;

initialization
  OnlineForm := nil;
  PMRegistry.AddCustomClass(TOnlineForm, 'TOnlineForm');

end.
