home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / nastroje / d5 / MFTP.ZIP / src / FtpMsg.pas < prev    next >
Pascal/Delphi Source File  |  2000-05-23  |  8KB  |  309 lines

  1. unit FtpMsg;
  2.  
  3. interface
  4.  
  5. uses
  6.    Windows, Messages, SysUtils, Classes, Graphics, Controls, RichEdit,
  7.    StdCtrls, ComCtrls, Ftp;
  8.  
  9. {$I mftp.inc}
  10.  
  11. type
  12.    TMessengerStyle = (msgClassic, msgMFtp, msgMFtpTime);
  13.    TMessageType  = (msgCommand, {$ifndef NODEBUG}msgDebug,{$endif} msgError, msgException, msgReply, msgStatus);
  14.  
  15.    TMessengerShowEvent = procedure (Sender: TObject; info: FtpInfo; var Show: Boolean) of object;
  16.    TMessengerShowEventE = procedure (Sender: TObject; error: FtpError; var Show: Boolean) of object;
  17.  
  18.    TMFtpMessenger = class(TCustomRichEdit)
  19.    private
  20.       FFtp:                      TMFtp;
  21.  
  22.       FStyle:                    TMessengerStyle;
  23.       FShowEvent:                TMessengerShowEvent;
  24.       FShowEventE:               TMessengerShowEventE;
  25.  
  26.       FAScroll:                  Boolean;
  27.       ShowDisconnect:            Boolean;
  28.  
  29.       HOnFtpInfo:                Integer;
  30.       HOnFtpError:               Integer;
  31.  
  32.       procedure AddLine(const S: String);
  33.  
  34.       procedure NewOnFtpInfo(Sender: TObject; info: FtpInfo; addinfo: String);
  35.       procedure NewOnFtpError(Sender: TObject; error: FtpError; addinfo: String);
  36.  
  37.       procedure SetClient(NewFtp: TMFtp);
  38.       procedure SetStyle(NewStyle: TMessengerStyle);
  39.    protected
  40.       NoBlankLine:               Boolean;
  41.    public
  42.       constructor Create(AOwner: TComponent); override;
  43.  
  44.       procedure AddMessage(Msg: String; MsgType: TMessageType);
  45.    published
  46.       property AutoScroll: Boolean read FAScroll write FAScroll;
  47.       property Client: TMFtp read FFtp write SetClient;
  48.       property Style: TMessengerStyle read FStyle write SetStyle;
  49.  
  50.       property OnErrorShow: TMessengerShowEventE read FShowEventE write FShowEventE;
  51.       property OnMessageShow: TMessengerShowEvent read FShowEvent write FShowEvent;
  52.  
  53.       property Align;
  54.       property HideScrollBars;
  55.       property HideSelection;
  56.       property PopupMenu;
  57.       property ScrollBars;
  58.       property WordWrap;
  59.  
  60.       property OnClick;
  61.       property OnDblClick;
  62.       property OnEnter;
  63.       property OnExit;
  64.       property OnMouseDown;
  65.       property OnMouseMove;
  66.       property OnMouseUp;
  67.  
  68.       property Anchors;
  69.       property BiDiMode;
  70.       property BorderWidth;
  71.       property Constraints;
  72.       property DragKind;
  73.       property DragMode;
  74.       property ParentBiDiMode;
  75.  
  76.       property OnEndDock;
  77.       property OnMouseWheel;
  78.       property OnMouseWheelDown;
  79.       property OnMouseWheelUp;
  80.       property OnStartDock;
  81.    end;
  82.  
  83. implementation
  84.  
  85. { TMFtpMessanger }
  86.  
  87. constructor TMFtpMessenger.Create;
  88. begin
  89.    inherited Create(AOwner);
  90.  
  91.    FAScroll := True;
  92.    NoBlankLine := False;
  93.    ImeMode := imClose;
  94.    ReadOnly := True;
  95.    WordWrap := False;
  96.  
  97.    ShowDisconnect := False;
  98. end;
  99.  
  100. procedure TMFtpMessenger.SetClient;
  101. begin
  102.    if FFtp = NewFtp then Exit;
  103.  
  104.    if Assigned(FFtp) then
  105.    begin
  106.       with FFtp do
  107.       begin
  108.          UnRegisterInfoEvent(HOnFtpInfo);
  109.          UnRegisterInfoEvent(HOnFtpError);
  110.       end;
  111.    end;
  112.  
  113.    FFtp := NewFtp;
  114.  
  115.    if not Assigned(FFtp) then Exit;
  116.  
  117.    with FFtp do
  118.    begin
  119.       HOnFtpInfo := RegisterInfoEvent(NewOnFtpInfo);
  120.       HOnFtpError := RegisterErrorEvent(NewOnFtpError);
  121.    end;
  122. end;
  123.  
  124. procedure TMFtpMessenger.SetStyle;
  125. begin
  126.    FStyle := NewStyle;
  127.  
  128.    if FStyle = msgClassic then
  129.       Font.Name := 'Courier New'
  130.    else
  131.       Font.Name := 'MS Sans Serif';
  132. end;
  133.  
  134. procedure TMFtpMessenger.AddLine;
  135. begin
  136.    if NoBlankLine then
  137.       if Trim(S) = '' then Exit;
  138.  
  139.    Lines.Add(S);
  140.    
  141.    if FAScroll then
  142.       SendMessage(Handle, EM_LINESCROLL, 0, 1);
  143. end;
  144.  
  145. procedure TMFtpMessenger.AddMessage;
  146. var C: TColor;
  147.     P: String;
  148.     i: Integer;
  149. begin
  150.    if Trim(Msg) = '' then Exit;
  151.  
  152.    C := clBlack;    {to make compiler happy :-)}
  153.  
  154.    if FStyle = msgClassic then
  155.    begin
  156.       case MsgType of
  157.          msgCommand:
  158.          begin
  159.             C := clGreen;
  160.             P := msgCommandx;
  161.          end;
  162.  
  163.          {$ifndef NODEBUG}
  164.          msgDebug:
  165.          begin
  166.             C := clRed;
  167.             P := 'DEBUG:>     ';
  168.          end;
  169.          {$endif}
  170.  
  171.          msgError:
  172.          begin
  173.             C := clRed;
  174.             P := msgErrorx;
  175.          end;
  176.  
  177.          msgException:
  178.          begin
  179.             C := clRed;
  180.             P := msgExceptionx;
  181.          end;
  182.  
  183.          msgReply:
  184.          begin
  185.             //C := clBlack;
  186.             P := msgReplyx;
  187.          end;
  188.  
  189.          msgStatus:
  190.          begin
  191.             C := clBlue;
  192.             P := msgStatusx;
  193.          end;
  194.       end;
  195.  
  196.       SelStart := -1;
  197.       SelAttributes.Color := C;
  198.       Msg := P + Msg;
  199.       i := Pos(#13#10, Msg);
  200.       if i = 0 then
  201.       begin
  202.          AddLine(Msg)
  203.       end
  204.       else
  205.       begin
  206.          AddLine(Copy(Msg, 1, i - 1));
  207.          Delete(Msg, 1, i + 1);
  208.          i := Pos(#13#10, Msg);
  209.  
  210.          while i <> 0 do
  211.          begin
  212.             AddLine('            ' + Copy(Msg, 1, i - 1));
  213.             Delete(Msg, 1, i + 1);
  214.             i := Pos(#13#10, Msg);
  215.          end;
  216.  
  217.          AddLine('            ' + Msg)
  218.       end;
  219.    end
  220.    else
  221.    begin
  222.       case MsgType of
  223.          msgCommand:   C := clBlue;
  224.          {$ifndef NODEBUG}
  225.          msgDebug:     C := clRed;
  226.          {$endif}
  227.          msgError:     C := clRed;
  228.          msgException: C := clRed;
  229.          msgReply:     C := clGreen;
  230.          msgStatus:    C := clPurple;
  231.       end;
  232.  
  233.       SelStart := -1;
  234.       SelAttributes.Color := C;
  235.  
  236.       if (Style = msgMFtpTime) and (MsgType <> msgReply) then
  237.          Msg:='[' + DateTimetoStr(Now) + '] ' + Msg;
  238.       AddLine(Msg);
  239.    end;
  240. end;
  241.  
  242. procedure TMFtpMessenger.NewOnFtpInfo;
  243. var Show: Boolean;
  244. begin
  245.    if Assigned(FShowEvent) then
  246.    begin
  247.       Show := True;
  248.       FShowEvent(Self, info, Show);
  249.       if not Show then Exit;
  250.    end;
  251.  
  252.    case info of
  253.       ftpServerConnected:
  254.       begin
  255.          with FFtp do
  256.          begin
  257.             if (Server = '') and (addinfo = '') then
  258.                AddMessage(msgConnectTo + msgSpecifiedS + msgWelcome, msgStatus)
  259.             else
  260.             if (Server <> '') and (Server <> addinfo) then
  261.             begin
  262.                if addinfo <> '' then
  263.                   AddMessage(msgConnectTo + Server + '(ip: ' + addinfo + ')' + msgWelcome, msgStatus)
  264.                else
  265.                   AddMessage(msgConnectTo + Server + msgWelcome, msgStatus);
  266.             end
  267.             else
  268.             begin
  269.                AddMessage(msgConnectTo + addinfo + msgWelcome, msgStatus);
  270.             end;
  271.          end;
  272.          ShowDisconnect := True;
  273.       end;
  274.       {$ifndef NODEBUG}
  275.       ftpDebug:    AddMessage(addinfo, msgDebug);
  276.       {$endif}
  277.       ftpTraceIn:  AddMessage(addinfo, msgReply);
  278.       ftpTraceOut: AddMessage(addinfo, msgCommand);
  279.       ftpRetrying: AddMessage(addinfo + #13#10, msgStatus);
  280.       ftpNotSupportResume: AddMessage(addinfo + #13#10, msgStatus);
  281.       ftpSupportResume:    AddMessage(addinfo + #13#10, msgStatus);
  282.       ftpServerDisconnected:
  283.       begin
  284.          if ShowDisconnect then
  285.          begin
  286.             AddMessage(msgDisconnected + #13#10, msgStatus);
  287.             ShowDisconnect := False;
  288.          end;
  289.       end;
  290.       ftpNothing: AddMessage(addinfo + #13#10, msgStatus);
  291.       ftpLoggedIn: AddMessage(msgLoginS + #13#10, msgStatus);
  292.    end;
  293. end;
  294.  
  295. procedure TMFtpMessenger.NewOnFtpError;
  296. var Show: Boolean;
  297. begin
  298.    if Assigned(FShowEventE) then
  299.    begin
  300.       Show := True;
  301.       FShowEventE(Self, error, Show);
  302.       if not Show then Exit;
  303.    end;
  304.  
  305.    AddMessage(addinfo + #13#10, msgError);
  306. end;
  307.  
  308. end.
  309.