home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / intmail2 / popview.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-07-27  |  4.1 KB  |  170 lines

  1. unit popview;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Menus, ExtCtrls, StdCtrls, ComCtrls, msmsg,
  8. {$IFDEF VER120}
  9.   ImgList,
  10. {$ENDIF}
  11.   ShellApi;
  12.  
  13. type
  14.   TMsgViewDlg = class(TForm)
  15.     Panel1: TPanel;
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     Label3: TLabel;
  19.     FromLabel: TLabel;
  20.     DateLabel: TLabel;
  21.     SubjectLabel: TLabel;
  22.     AttListView: TListView;
  23.     BodyMemo: TMemo;
  24.     ImageList1: TImageList;
  25.     PopupMenu1: TPopupMenu;
  26.     Execute1: TMenuItem;
  27.     Save1: TMenuItem;
  28.     SaveDialog1: TSaveDialog;
  29.     procedure FormCreate(Sender: TObject);
  30.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  31.     procedure AttListViewDblClick(Sender: TObject);
  32.     procedure Save1Click(Sender: TObject);
  33.     procedure PopupMenu1Popup(Sender: TObject);
  34.   private
  35.     { Private declarations }
  36.     TempPath : string;
  37.     FMessage : TmsCustomMessage;
  38.     procedure AddFileToListView(const FileName : string);
  39.     procedure SetMessage(Value : TmsCustomMessage);
  40.   public
  41.     { Public declarations }
  42.     property MailMessage : TmsCustomMessage write SetMessage;
  43.   end;
  44.  
  45. var
  46.   MsgViewDlg: TMsgViewDlg;
  47.  
  48. implementation
  49.  
  50. {$R *.DFM}
  51.  
  52. uses msUtils;
  53.  
  54. procedure TMsgViewDlg.AddFileToListView(const FileName : string);
  55. var
  56.   Item : TListItem;
  57.   Icon : TIcon;
  58.   j : word;
  59. begin
  60.   Item:=AttListView.Items.Add;
  61.   Item.Caption:=ExtractFileName(FileName);
  62.   Icon:=TIcon.Create;
  63.   j:=0;
  64.   Icon.Handle:=ExtractAssociatedIcon(hInstance,PChar(FileName),j);
  65.   if Icon.Handle<>0 then
  66.     Item.ImageIndex:=ImageList1.AddIcon(Icon);
  67. end;
  68.  
  69. procedure TMsgViewDlg.SetMessage(Value : TmsCustomMessage);
  70. var
  71.   i : Integer;
  72.   Path : string;
  73.   s1, s2 : string;
  74.   DT : TDateTime;
  75.   TZ: ShortString;
  76. begin
  77.   FMessage:=Value;
  78.   BodyMemo.Lines:=FMessage.Body;
  79.   s1:=FMessage.Headers.GetFieldBody('From');
  80.   SplitAddress(s1,s1,s2);
  81.   if s2<>'' then
  82.     FromLabel.Caption:=s2
  83.   else
  84.     FromLabel.Caption:=s1;
  85.   s1:=FMessage.Headers.GetFieldBody('Date');
  86.   MailDateToDateTime(s1,DT,TZ);
  87.   if DT<>0 then
  88.     DateLabel.Caption:=DateTimeToStr(DT)+' '+TZ
  89.   else
  90.     DateLabel.Caption:='Unknown';
  91.   SubjectLabel.Caption:=FMessage.Headers.GetFieldBody('Subject');
  92.   if FMessage.Attachments.Count=0 then
  93.   begin
  94.     AttListView.Visible:=false;
  95.   end;
  96.   for i:=0 to FMessage.Attachments.Count-1 do
  97.   begin
  98.     FMessage.SaveAttachment(i,TempPath);
  99.     Path:=TempPath+FMessage.Attachments[i].FileName;
  100.     AddFileToListView(Path);
  101.   end;
  102. end;
  103.  
  104. procedure TMsgViewDlg.FormCreate(Sender: TObject);
  105. var
  106.   Buf : PChar;
  107. begin
  108.   Buf:=StrAlloc(255);
  109.   GetTempPath(255,Buf);
  110.   TempPath:=StrPas(Buf);
  111.   StrDispose(Buf);
  112. end;
  113.  
  114. procedure TMsgViewDlg.FormClose(Sender: TObject; var Action: TCloseAction);
  115. var
  116.   i : Integer;
  117.   F : file;
  118. begin
  119.   if Assigned(FMessage) then
  120.   begin
  121.     for i:=0 to FMessage.Attachments.Count-1 do
  122.     begin
  123.       AssignFile(F,TempPath+FMessage.Attachments[i].FileName);
  124.       System.Erase(F);
  125.     end;
  126.   end;
  127. end;
  128.  
  129. procedure TMsgViewDlg.AttListViewDblClick(Sender: TObject);
  130. var
  131.   s : string;
  132.   TheHandle : THandle;
  133.   i : Integer;
  134. begin
  135.   if AttListView.Selected<>nil then
  136.   begin
  137.     i:=AttListView.Selected.Index;
  138.     s:=TempPath+AttListView.Items[i].Caption;
  139.     TheHandle:=ShellExecute(Application.Handle,'Open',PChar(s),nil,nil,SW_SHOW);
  140.     if TheHandle<=HINSTANCE_ERROR then
  141.       raise Exception.Create('Unable to run ShellExec. Error code='+IntToStr(TheHandle));
  142.   end;
  143. end;
  144.  
  145. procedure TMsgViewDlg.Save1Click(Sender: TObject);
  146. begin
  147.   if AttListView.Selected<>nil then
  148.   begin
  149.     SaveDialog1.FileName:=AttListView.Items[AttListView.Selected.Index].Caption;
  150.     if SaveDialog1.Execute then
  151.       FMessage.Attachments[AttListView.Selected.Index].Contents.SaveToFile(SaveDialog1.FileName);
  152.   end;
  153. end;
  154.  
  155. procedure TMsgViewDlg.PopupMenu1Popup(Sender: TObject);
  156. begin
  157.   if AttListView.Selected=nil then
  158.   begin
  159.     Execute1.Enabled:=false;
  160.     Save1.Enabled:=false;
  161.   end
  162.   else
  163.   begin
  164.     Execute1.Enabled:=true;
  165.     Save1.Enabled:=true;
  166.   end;
  167. end;
  168.  
  169. end.
  170.