unit Download_Unit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ActiveX, AxCtrls, Download_TLB, StdVcl, Psock, NMFtp, ExtCtrls, StdCtrls,
  Label3D, Gauges, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, IdAntiFreezeBase, IdAntiFreeze, Buttons, Shdocvw;

type
  TDownloadWindow = class(TActiveForm, IDownloadWindow)
    Image1: TImage;
    P_Progress: TGauge;
    Label3D1: TLabel3D;
    Label3D2: TLabel3D;
    Label3D3: TLabel3D;
    P_FileByte: TLabel3D;
    P_Address: TPanel;
    P_FileName: TPanel;
    BT_Cancel: TButton;
    Save: TSaveDialog;
    T_Thread: TTimer;
    Connect: TIdFTP;
    IdAntiFreeze: TIdAntiFreeze;
    P_Back: TPanel;
    Title: TLabel3D;
    Load: TOpenDialog;
    BT_Directory: TBitBtn;
    procedure ActiveFormCreate(Sender: TObject);
    procedure T_ThreadTimer(Sender: TObject);
    procedure BT_CancelClick(Sender: TObject);
    procedure ConnectWork(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCount: Integer);
    procedure ConnectWorkBegin(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCountMax: Integer);
    procedure ConnectWorkEnd(Sender: TObject; AWorkMode: TWorkMode);
    procedure BT_DirectoryClick(Sender: TObject);
    procedure Closeing(Sender: TObject);
  private
    { Private declarations }
    FEvents: IDownloadWindowEvents;
    procedure ActivateEvent(Sender: TObject);
    procedure ClickEvent(Sender: TObject);
    procedure CreateEvent(Sender: TObject);
    procedure DblClickEvent(Sender: TObject);
    procedure DeactivateEvent(Sender: TObject);
    procedure DestroyEvent(Sender: TObject);
    procedure KeyPressEvent(Sender: TObject; var Key: Char);
    procedure PaintEvent(Sender: TObject);
    function  Cut_FileName(FSt : String) : String;
  protected
    { Protected declarations }
    procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override;
    procedure EventSinkChanged(const EventSink: IUnknown); override;
    function Get_Active: WordBool; safecall;
    function Get_AutoScroll: WordBool; safecall;
    function Get_AutoSize: WordBool; safecall;
    function Get_AxBorderStyle: TxActiveFormBorderStyle; safecall;
    function Get_Caption: WideString; safecall;
    function Get_Color: OLE_COLOR; safecall;
    function Get_Cursor: Smallint; safecall;
    function Get_DoubleBuffered: WordBool; safecall;
    function Get_DropTarget: WordBool; safecall;
    function Get_Enabled: WordBool; safecall;
    function Get_Font: IFontDisp; safecall;
    function Get_HelpFile: WideString; safecall;
    function Get_KeyPreview: WordBool; safecall;
    function Get_PixelsPerInch: Integer; safecall;
    function Get_PrintScale: TxPrintScale; safecall;
    function Get_Scaled: WordBool; safecall;
    function Get_Visible: WordBool; safecall;
    function Get_VisibleDockClientCount: Integer; safecall;
    procedure _Set_Font(const Value: IFontDisp); safecall;
    procedure Set_AutoScroll(Value: WordBool); safecall;
    procedure Set_AutoSize(Value: WordBool); safecall;
    procedure Set_AxBorderStyle(Value: TxActiveFormBorderStyle); safecall;
    procedure Set_Caption(const Value: WideString); safecall;
    procedure Set_Color(Value: OLE_COLOR); safecall;
    procedure Set_Cursor(Value: Smallint); safecall;
    procedure Set_DoubleBuffered(Value: WordBool); safecall;
    procedure Set_DropTarget(Value: WordBool); safecall;
    procedure Set_Enabled(Value: WordBool); safecall;
    procedure Set_Font(var Value: IFontDisp); safecall;
    procedure Set_HelpFile(const Value: WideString); safecall;
    procedure Set_KeyPreview(Value: WordBool); safecall;
    procedure Set_PixelsPerInch(Value: Integer); safecall;
    procedure Set_PrintScale(Value: TxPrintScale); safecall;
    procedure Set_Scaled(Value: WordBool); safecall;
    procedure Set_Visible(Value: WordBool); safecall;
    function  Get_FileName: WideString; safecall;
    procedure Set_FileName(const Value: WideString); safecall;
    procedure Set_Address(const Value: WideString); safecall;
    function  Get_Address: WideString; safecall;
    function  Get_UserName: WideString; safecall;
    procedure Set_UserName(const Value: WideString); safecall;
    function  Get_Actions: WideString; safecall;
    procedure Set_Actions(const Value: WideString); safecall;
    procedure DestoryForm;
    function  Get_SendFileName: WideString; safecall;
    procedure Set_SendFileName(const Value: WideString); safecall;
    function  Get_Number: WideString; safecall;
    procedure Set_Number(const Value: WideString); safecall;
    function  Get_Assortment: WideString; safecall;
    procedure Set_Assortment(const Value: WideString); safecall;
  public
    { Public declarations }
    procedure Initialize; override;
  end;


Var
  Initialized, Connected, Errored, Aborted : Boolean;
  SendFileName, UserName, FileName, Address, Actions, Number, Assortment : String;
  RecvByte, BytesToTransfer : Integer;

implementation

uses ComObj, ComServ, SafeFactory, Urlmon;

{$R *.DFM}

{ TDownloadWindow }

procedure TDownloadWindow.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage);
begin
  { Define property pages here.  Property pages are defined by calling
    DefinePropertyPage with the class id of the page.  For example,
      DefinePropertyPage(Class_DownloadWindowPage); }
end;

procedure TDownloadWindow.EventSinkChanged(const EventSink: IUnknown);
begin
  FEvents := EventSink as IDownloadWindowEvents;
end;

procedure TDownloadWindow.Initialize;
begin
  inherited Initialize;
  OnActivate := ActivateEvent;
  OnClick := ClickEvent;
  OnCreate := CreateEvent;
  OnDblClick := DblClickEvent;
  OnDeactivate := DeactivateEvent;
  OnDestroy := DestroyEvent;
  OnKeyPress := KeyPressEvent;
  OnPaint := PaintEvent;
end;

function TDownloadWindow.Get_Active: WordBool;
begin
  Result := Active;
end;

function TDownloadWindow.Get_AutoScroll: WordBool;
begin
  Result := AutoScroll;
end;

function TDownloadWindow.Get_AutoSize: WordBool;
begin
  Result := AutoSize;
end;

function TDownloadWindow.Get_AxBorderStyle: TxActiveFormBorderStyle;
begin
  Result := Ord(AxBorderStyle);
end;

function TDownloadWindow.Get_Caption: WideString;
begin
  Result := WideString(Caption);
end;

function TDownloadWindow.Get_Color: OLE_COLOR;
begin
  Result := OLE_COLOR(Color);
end;

function TDownloadWindow.Get_Cursor: Smallint;
begin
  Result := Smallint(Cursor);
end;

function TDownloadWindow.Get_DoubleBuffered: WordBool;
begin
  Result := DoubleBuffered;
end;

function TDownloadWindow.Get_DropTarget: WordBool;
begin
  Result := DropTarget;
end;

function TDownloadWindow.Get_Enabled: WordBool;
begin
  Result := Enabled;
end;

function TDownloadWindow.Get_Font: IFontDisp;
begin
  GetOleFont(Font, Result);
end;

function TDownloadWindow.Get_HelpFile: WideString;
begin
  Result := WideString(HelpFile);
end;

function TDownloadWindow.Get_KeyPreview: WordBool;
begin
  Result := KeyPreview;
end;

function TDownloadWindow.Get_PixelsPerInch: Integer;
begin
  Result := PixelsPerInch;
end;

function TDownloadWindow.Get_PrintScale: TxPrintScale;
begin
  Result := Ord(PrintScale);
end;

function TDownloadWindow.Get_Scaled: WordBool;
begin
  Result := Scaled;
end;

function TDownloadWindow.Get_Visible: WordBool;
begin
  Result := Visible;
end;

function TDownloadWindow.Get_VisibleDockClientCount: Integer;
begin
  Result := VisibleDockClientCount;
end;

procedure TDownloadWindow._Set_Font(const Value: IFontDisp);
begin
  SetOleFont(Font, Value);
end;

procedure TDownloadWindow.ActivateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnActivate;
end;

procedure TDownloadWindow.ClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnClick;
end;

procedure TDownloadWindow.CreateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnCreate;
end;

procedure TDownloadWindow.DblClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDblClick;
end;

procedure TDownloadWindow.DeactivateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDeactivate;
end;

procedure TDownloadWindow.DestroyEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDestroy;
end;

procedure TDownloadWindow.KeyPressEvent(Sender: TObject; var Key: Char);
var
  TempKey: Smallint;
begin
  TempKey := Smallint(Key);
  if FEvents <> nil then FEvents.OnKeyPress(TempKey);
  Key := Char(TempKey);
end;

procedure TDownloadWindow.PaintEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnPaint;
end;

procedure TDownloadWindow.Set_AutoScroll(Value: WordBool);
begin
  AutoScroll := Value;
end;

procedure TDownloadWindow.Set_AutoSize(Value: WordBool);
begin
  AutoSize := Value;
end;

procedure TDownloadWindow.Set_AxBorderStyle(
  Value: TxActiveFormBorderStyle);
begin
  AxBorderStyle := TActiveFormBorderStyle(Value);
end;

procedure TDownloadWindow.Set_Caption(const Value: WideString);
begin
  Caption := TCaption(Value);
end;

procedure TDownloadWindow.Set_Color(Value: OLE_COLOR);
begin
  Color := TColor(Value);
end;

procedure TDownloadWindow.Set_Cursor(Value: Smallint);
begin
  Cursor := TCursor(Value);
end;

procedure TDownloadWindow.Set_DoubleBuffered(Value: WordBool);
begin
  DoubleBuffered := Value;
end;

procedure TDownloadWindow.Set_DropTarget(Value: WordBool);
begin
  DropTarget := Value;
end;

procedure TDownloadWindow.Set_Enabled(Value: WordBool);
begin
  Enabled := Value;
end;

procedure TDownloadWindow.Set_Font(var Value: IFontDisp);
begin
  SetOleFont(Font, Value);
end;

procedure TDownloadWindow.Set_HelpFile(const Value: WideString);
begin
  HelpFile := String(Value);
end;

procedure TDownloadWindow.Set_KeyPreview(Value: WordBool);
begin
  KeyPreview := Value;
end;

procedure TDownloadWindow.Set_PixelsPerInch(Value: Integer);
begin
  PixelsPerInch := Value;
end;

procedure TDownloadWindow.Set_PrintScale(Value: TxPrintScale);
begin
  PrintScale := TPrintScale(Value);
end;

procedure TDownloadWindow.Set_Scaled(Value: WordBool);
begin
  Scaled := Value;
end;

procedure TDownloadWindow.Set_Visible(Value: WordBool);
begin
  Visible := Value;
end;

function TDownloadWindow.Get_FileName: WideString;
begin

 FileName := 'TEST';
 Get_FileName := 'TEST';

end;

procedure TDownloadWindow.Set_FileName(const Value: WideString);
begin

 FileName := Value;

end;

procedure TDownloadWindow.Set_Address(const Value: WideString);
begin

 Address := Value;

end;

procedure TDownloadWindow.ActiveFormCreate(Sender: TObject);
begin

 Initialized        := False;
 Connected          := False;
 Errored            := False;
 Aborted            := False;

 T_Thread.Enabled   := True;

end;

procedure TDownloadWindow.T_ThreadTimer(Sender: TObject);
begin

 T_Thread.Enabled   := False;
 If Errored Then Exit;

 If (Initialized) And (Not Connected) Then
 Begin
   Initialized        := False;
   Connect.Host       := ADDRESS;

   P_Address.Caption  := 'Com21c Upload Module';
   P_Address.Refresh;

   Connected          := True;
   RecvByte           := 0;

   Connect.Connect;
   Sleep(1000);

   If Actions = 'UPLOAD' Then
   Begin
     Connect.Put(Load.FileName, Format('%s/%s', [Assortment, Number]));
    End Else Begin
     BytesToTransfer := Connect.Size(FileName);
     Connect.Get(FileName, Save.FileName, True);
   End;
 End;

 T_Thread.Enabled  := True;

end;

procedure TDownloadWindow.BT_CancelClick(Sender: TObject);
begin

 Aborted := True;

end;

procedure TDownloadWindow.ConnectWork(Sender: TObject;
  AWorkMode: TWorkMode; const AWorkCount: Integer);
begin

 If (Not Aborted) And (Connected) Then
 Begin
   P_FileByte.Caption  := Format('%d / %d Bytes', [AWorkCount, BytesToTransfer]);
   P_Progress.Progress := AWorkCount;

   P_FileByte.Refresh;
   P_Progress.Refresh;
 End;

 If Aborted Then
 Begin
   Try
     Connect.Disconnect;

     T_Thread.Enabled     := False;

     Connected            := False;
     Initialized          := False;
     Errored              := True;
     Aborted              := False;

     Title.Caption        := '파일전송이 취소되었습니다';
     Title.Font.Color     := clMaroon;

     P_Back.Visible       := True;
     BT_Directory.Enabled := False;
     BT_Cancel.Enabled    := False;
   Except
   End;
 End;

end;

procedure TDownloadWindow.ConnectWorkBegin(Sender: TObject;
  AWorkMode: TWorkMode; const AWorkCountMax: Integer);
begin

 If AWorkCountMax > 0 Then
 Begin
   P_Progress.MaxValue := AWorkCountMax;
   BytesToTransfer     := AWorkCountMax;
  End Else Begin
   P_Progress.MaxValue := BytesToTransfer;
 End;

end;

function TDownloadWindow.Get_Address: WideString;
begin

end;

function TDownloadWindow.Get_UserName: WideString;
begin

end;

procedure TDownloadWindow.Set_UserName(const Value: WideString);
begin

 UserName := Value;

end;

procedure TDownloadWindow.ConnectWorkEnd(Sender: TObject;
  AWorkMode: TWorkMode);
begin

 If (Connected) Then
 Begin
   Connected        := False;
   Errored          := True;

   P_Back.Visible   := True;
   Title.Caption    := '파일전송이 완료되었습니다';
   Title.Font.Color := clNavy;
 End;

end;

function TDownloadWindow.Get_Actions: WideString;
begin

end;

procedure TDownloadWindow.Set_Actions(const Value: WideString);
begin

 Actions := Value;

end;

function TDownloadWindow.Get_Number: WideString;
begin

end;

procedure TDownloadWindow.Set_Number(const Value: WideString);
begin

 Number := Value;

end;

function TDownloadWindow.Get_Assortment: WideString;
begin

end;

procedure TDownloadWindow.Set_Assortment(const Value: WideString);
begin

 Assortment := Value;

end;

procedure TDownloadWindow.BT_DirectoryClick(Sender: TObject);
Var Check : Boolean;
    St : String;
begin

 Connect.User       := UserName;
 Load.FileName      := '';
 Save.FileName      := Cut_FileName(Number);
 St                 := '';

 If Actions = 'UPLOAD' Then
 Begin
   Check := Load.Execute;
   Self.Setfocus;
   Self.BringToFront;
   Self.Show;
   St    := Trim(Load.FileName);
   If Not FileExists(St) Then St := '';
  End Else Begin
   Check := Save.Execute;
   Self.Setfocus;
   Self.BringToFront;
   Self.Show;
   St    := Trim(Save.FileName);
 End;

 If (Check) And (St <> '') Then
 Begin
   P_FileName.Caption   := St;
   P_FileName.Refresh;

   Initialized          := True;
   T_Thread.Enabled     := True;

   BT_Directory.Enabled := False;
   BT_Cancel.Enabled    := True;
 End;

end;

procedure TDownloadWindow.DestoryForm;
begin

 HlinkNavigateString(ComObject, 'close.html');

end;

function TDownloadWindow.Cut_FileName(FSt : String) : String;
Var I : Integer;
    St : String;
begin

 St := '';

 For I := Length(FSt) DownTo 1 Do
   If Copy(FSt, I, 1) <> '\' Then St := Copy(FSt, I, 1) + St
                             Else Break;

 Result := Trim(St);

end;

function TDownloadWindow.Get_SendFileName: WideString;
begin

 Result := Cut_FileName(Load.FileName);

end;

procedure TDownloadWindow.Set_SendFileName(const Value: WideString);
begin

 SendFileName := Value;

end;

procedure TDownloadWindow.Closeing(Sender: TObject);
begin

 Show;

end;

Initialization

  TSafeActiveFormFactory.Create(
    ComServer,
    TActiveFormControl,
    TDownloadWindow,
    Class_DownloadWindow,
    1,
    '',
    OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
    tmApartment);

End.
