elmimeviewer_plaintext.pas

来自「著名的SecureBlackBox控件完整源码」· PAS 代码 · 共 196 行

PAS
196
字号
// File Version: 2004-04-16
unit ElMimeViewer_PlainText;

interface

uses
  // System units:
  SysUtils, Classes,
  // ElMime units:
  SBMIMETypes,
  SBMIMEUtils,
  SBMIMEClasses,
  SBMIMEStream,
  SBMIME,
  // ElMime Demo units:
  ElMimeViewer_DataCommon,
  // other units:
  Windows, Messages, {$IFDEF D_6_UP}Variants,{$ENDIF} Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type

  TFrame = TElMimePlugFrame;

  TfraPlainText = class(TFrame)
  private
    { Private declarations }
    Memo: TMemo;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    class function IsSupportedThisMessapePart(mp: TElMessagePart; TagInfo: TTagInfo; Node: TTreeNodeInfo): Boolean; override;
    function GetCaption: string; override;
    procedure WriteHTMLCode(sm: TElCustomMemoryStream); override;
  protected
    procedure Init(mp: TElMessagePart; TagInfo: TTagInfo; Node: TTreeNodeInfo; bShow: Boolean); override;
  end;

implementation

{ TfraPlainText }

constructor TfraPlainText.Create(AOwner: TComponent);
begin
  inherited;

  Align := alClient;
  Memo := TMemo.Create(Self);
  Memo.Align := alClient;
  Memo.WordWrap := False;
  Memo.ScrollBars := ssBoth;
  Memo.ReadOnly := True;
  Memo.Parent := Self;
end;

class function TfraPlainText.IsSupportedThisMessapePart(mp: TElMessagePart;
  TagInfo: TTagInfo; Node: TTreeNodeInfo): Boolean;
var
  wsContentSubtype, wsName: TWideString;
  sName: AnsiString;
  function BinaryAutodetect: Boolean;
  var
    i: integer;
    Buffer: TBytes;
    BufferSize: Integer;
    {$IFDEF DELPHI_NET}
    sData: TWideString;
    {$ELSE}
    ws: TWideString;
    sData: string;
    {$ENDIF}
  const
    cHtmlWords: array[0..8]of string = (
      '<!DOCTYPE HTML',
      '<HTML>',
      '</table>',
      '<tr><td>',
      '<table cellspacing=',
      '</td></tr>',
      '</font>',
      '<align=center>',
      'document.write('
    );
  begin
    Result := False;
    if mp.IsText then
    begin
      mp.GetText({$IFDEF DELPHI_NET}sData{$ELSE}ws{$ENDIF});
      {$IFNDEF DELPHI_NET}
      sData := ws;
      {$ENDIF}
    end
    else
    begin
      BufferSize := 0;
      mp.GetData(Buffer, BufferSize);
      {$IFDEF DELPHI_NET}
        sData := StringOf(Buffer);
      {$ELSE}
        GetStringOf(Buffer, sData);
      {$ENDIF IFDEF DELPHI_NET}
    end;

    for i:=Low(cHtmlWords) to High(cHtmlWords) do
    begin
      if (PosExSafe(cHtmlWords[i], sData, 1, 100) > 0) then
      begin
        Result := True;
        Node.ImageIndex := 12;
        exit;
      end;
    end;
  end;
begin
  Result := False;
  if (TagInfo<>tiBody) or (Node=nil) or (mp=nil) or (mp.IsMultipart) then
    exit;
  Result := mp.IsText; // mp.IsTextPlain
  if not Result then
  begin
    if mp.IsApplication then
    begin
      wsContentSubtype := mp.ContentSubtype;
      if WideSameText(wsContentSubtype, 'octet-stream') then
      begin
        wsName := mp.FileName;
        DeleteQuotationMarks(wsName);
        sName := LowerCase(Trim(ExtractFileExtension(wsName)));
        if wsName <> '' then
        begin
          if PosExSafe('\'+sName+'\', '\txt\text\htm\html\shtml\xml\xls\cgi\php\css\js\java\c\cpp\h\hpp\pas\dpr\dof\dsk\')>0 then
          begin
            Result := True;
            Node.ImageIndex := 12;
            exit;
          end;
        end;

        Result := BinaryAutodetect;

      end;
    end;
  end;
end;

procedure TfraPlainText.Init(mp: TElMessagePart; TagInfo: TTagInfo; Node: TTreeNodeInfo; bShow: Boolean);
var
  ws: TWideString;
  Buffer: TBytes;
  BufferSize: Integer;
  {$IFNDEF DELPHI_NET}
  S: AnsiString;
  {$ENDIF}
begin
  inherited;
  if (mp = nil) or (not bShow) then
    exit;
  if mp.IsText then
  begin
    mp.GetText(ws);
    Memo.Text := ws;
  end
  else
  begin
    // Show Attached text file:
    // todo: selected encoding ...
    BufferSize := 0;
    mp.GetData(Buffer, BufferSize);
    {$IFDEF DELPHI_NET}
      Memo.Text := StringOf(Buffer);
    {$ELSE}
      GetStringOf(Buffer, S);
      Memo.Text := S;
    {$ENDIF IFDEF DELPHI_NET}
  end;
end;

procedure TfraPlainText.WriteHTMLCode(sm: TElCustomMemoryStream);
begin
  if (sm=nil) or (fElMessagePart = nil) then
    exit;
  WriteStringToStream('<pre>'#13#10, sm);
  WriteStringToStream(Memo.Text, sm);
  WriteStringToStream(#13#10'</pre>'#13#10, sm);
end;


function TfraPlainText.GetCaption: string;
begin
  Result := 'Text Part';
end;

initialization
  TfraPlainText.RegisterClass(TfraPlainText);
end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?