Test Message Smart Filter
 
unit unMain;
 
 
interface
 
 
{$I IMI.INC}
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, StdCtrls, ImgList, ExtendedMAPI, IMIEMTypes;
 
type
  TfrmMain = class(TForm)
    FolderListIcons: TImageList;
    MailboxTreeView: TTreeView;
    MessageListIcons: TImageList;
    MessageListView: TListView;
    Panel1: TPanel;
    btLogOff: TButton;
    cbVirtualMsgList: TCheckBox;
    Splitter1: TSplitter;
    StatusBar: TStatusBar;
    btMsgFilter: TButton;
    procedure btLogOffClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure MailboxTreeViewExpanding(Sender: TObject; Node: TTreeNode;
      var AllowExpansion: Boolean);
    procedure MailboxTreeViewCollapsing(Sender: TObject; Node: TTreeNode;
      var AllowCollapse: Boolean);
    procedure MailboxTreeViewChange(Sender: TObject; Node: TTreeNode);
    procedure MessageListViewColumnClick(Sender: TObject; Column: TListColumn);
    procedure cbVirtualMsgListClick(Sender: TObject);
    procedure btMsgFilterClick(Sender: TObject);
    procedure MessageListViewData(Sender: TObject; Item: TListItem);
    procedure MessageListViewDataHint(Sender: TObject;
      StartIndex, EndIndex: Integer);
  private
    { Private declarations }
    hr: HRESULT;
    MAPISession: IMAPISession;
    MAPIStore: IMsgStore;
    MAPITable: IMAPITable;
    FRestriction: PSRestriction;
    procedure AddStores;
    procedure CheckStore(StoreID: TBytes);
    procedure ReleaseAll;
    procedure ExpandLevel(Node: TTreeNode);
    procedure CollapseLevel(Node: TTreeNode);
    //
    procedure GetMessages(P: Pointer; const FromFilter: Boolean = False);
    procedure GetVirtualMessages(P: Pointer; const FromFilter: Boolean = False);
  public
    { Public declarations }
  end;
 
 

var
  frmMain: TfrmMain;
 
 
implementation
 
{$R *.dfm}
 
uses
  MAPIUtils, MAPIException, MAPISessionUtils, MAPIFldUtils, MAPISmartFilter,
  SmartFilterDlgFrm;
 
 
var
  StoreList: TStoresHeadList = nil;
  MsgHeadList: TMsgHeadList = nil;
  MsgHeadListCount: Integer = 0;
 
  ColumnIndex: Integer = -1;
 
  SortAscending: Boolean = True;
  ColumnTag: Cardinal = 0;
 
  SortAscendingOld: Boolean = True;
  ColumnTagOld: Cardinal = 0;
 
  MinIndx: Integer = 0;
  MaxIndx: Integer = 50;
  FolderID: TBytes;
 
const
  strLogOff: array [Boolean] of string = ('Log On', 'Log Off');
 
procedure TfrmMain.btLogOffClick(Sender: TObject);
begin
  if btLogOff.Tag = 0 then
  // Log On
  begin
    // Get MAPI Session
    MAPISession := GetMAPISession(Self.Handle, '', MAPI_LOGON_UI);
    if Assigned(MAPISession) then
      StoreList := GetMAPIStores(MAPISession);
    AddStores;
  end
  else
  // Log Off
  begin
    ReleaseAll;
    // Close and clear MAPI Session
    ReleaseMapiSession(MAPISession);
  end;
  btLogOff.Tag := Integer(Assigned(MAPISession));
  btLogOff.Caption := strLogOff[Bool(btLogOff.Tag)];
end;
 
 
procedure TfrmMain.btMsgFilterClick(Sender: TObject);
var
  SmartFilter: TSmartFilter;
  mr: Integer;
begin
  if not Assigned(MailboxTreeView.Selected) or
    not Assigned(MailboxTreeView.Selected.Data) or
    (MailboxTreeView.Selected.Level = 0) then
    Exit;
 
  if not Assigned(MAPITable) then
    Exit;
 
  SmartFilter := TSmartFilter.Create(MAPITable);
  try
    with TFrmSmartFilterDialog.Create(Self) do
      try
 
        mr := ShowModal;
 
        if mr = mrOK then
        begin
          SmartFilter.Subject := Subject;
          SmartFilter.Size := MessageSize;
          SmartFilter.TimeAfter := TimeAfter;
          SmartFilter.TimeBefore := TimeBefore;
          SmartFilter.Sender := Sender;
          SmartFilter.Recipient := Recipient;
          SmartFilter.Unread := Unread;
          SmartFilter.BodyText := BodyText;
          SmartFilter.IsOrRelation := IsOrRelation;
          SmartFilter.SetFilter;
        end
        else if mr = mrAbort then
          SmartFilter.SetFilter(True);
 
        if Assigned(FRestriction) then
          MapiFreeBuffer(FRestriction);
        FRestriction := nil;
 
        if Assigned(SmartFilter.Filter) then
          MAPICopyRestriction(SmartFilter.Filter, nil, FRestriction);
 
      finally
        Free;
      end;
 
  finally
    FreeAndNil(SmartFilter);
  end;
 
  if not cbVirtualMsgList.Checked then
  begin
    GetMessages(MailboxTreeView.Selected.Data, True);
  end
  else
  begin
    GetVirtualMessages(MailboxTreeView.Selected.Data, True);
    MessageListView.Refresh;
  end;
 
  if mr = mrOK then
    StatusBar.Panels[0].Text := StatusBar.Panels[0].Text + ' || Filtered';
end;
 
 
procedure TfrmMain.cbVirtualMsgListClick(Sender: TObject);
begin
  if not Assigned(MailboxTreeView.Selected) then
    Exit;
 
  if MailboxTreeView.Selected.Level < 1 then
    Exit;
 
  if not cbVirtualMsgList.Checked then
    GetMessages(MailboxTreeView.Selected.Data)
  else
    GetVirtualMessages(MailboxTreeView.Selected.Data);
 
  MessageListView.Refresh;
end;
 
 
procedure TfrmMain.AddStores;
var
  iCount: Integer;
  LenA: Integer;
  TreeNode: TTreeNode;
begin
 
  LenA := Length(StoreList);
  if LenA < 1 then
    Exit;
 
  for iCount := 0 to LenA - 1 do
  begin
 
    if StoreList[iCount].IsDefault then
      TreeNode := MailboxTreeView.Items.AddObjectFirst(nil,
        StoreList[iCount].DisplayName, @StoreList[iCount])
    else
      TreeNode := MailboxTreeView.Items.AddObject(nil,
        StoreList[iCount].DisplayName, @StoreList[iCount]);
 
    // Image Index
    if StoreList[iCount].IsDefault then
      TreeNode.ImageIndex := 0
    else
      TreeNode.ImageIndex := 1;
 
    TreeNode.SelectedIndex := TreeNode.ImageIndex;
    TreeNode.Expanded := False;
 
    // Add Dummy Node for [+] Icon
    MailboxTreeView.Items.AddChildObjectFirst(TreeNode, '', nil);
  end;
 
end;
 
 
procedure TfrmMain.CheckStore(StoreID: TBytes);
begin
  if Assigned(MAPIStore) and not IsSameMAPIObject(MAPISession, MAPIStore,
    StoreID) then
  begin
    ReleaseMsgStore(MAPIStore);
    MAPIStore := nil;
  end;
 
  if not Assigned(MAPIStore) then
    MAPIStore := GetMAPIStore(MAPISession, StoreID);
end;
 
 
procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  // Clear all MAPI Interfaces
  ReleaseAll;
  // Close and clear MAPI Session
  ReleaseMapiSession(MAPISession);
end;
 
 
procedure TfrmMain.FormCreate(Sender: TObject);
begin
{$IF DEFINED (WIN64)}
  Self.Caption := Self.Caption + ' - WIN64';
{$ELSE}
  Self.Caption := Self.Caption + ' - WIN32';
{$IFEND}
  MessageListView.Items.Count := 0;
 
  FRestriction := nil;
  MAPITable := nil;
  MAPIStore := nil;
  MAPISession := nil;
 
  FolderID := nil;
 
  // set Property_Tag for Columns sort
  MessageListView.Columns.Items[0].Tag := PR_ICON_INDEX;
  MessageListView.Columns.Items[1].Tag := PR_HASATTACH;
  MessageListView.Columns.Items[2].Tag := PR_SENDER_NAME;
  MessageListView.Columns.Items[3].Tag := PR_NORMALIZED_SUBJECT;
  MessageListView.Columns.Items[4].Tag := PR_MESSAGE_DELIVERY_TIME;
  MessageListView.Columns.Items[5].Tag := PR_MESSAGE_SIZE;
 
end;
 
procedure TfrmMain.MessageListViewData(Sender: TObject; Item: TListItem);
var
  MsgHead: PMsgHead;
begin
  if not cbVirtualMsgList.Checked then
  begin
 
    if Item.Index > MsgHeadListCount - 1 then
      Exit;
 
    Item.ImageIndex := GetMsgIconIndex(MsgHeadList[Item.Index]);
    Item.SubItems.Add('');
    if MsgHeadList[Item.Index].HasAttachment then
      Item.SubItemImages[0] := 187
    else
      Item.SubItemImages[0] := -1;
    Item.SubItems.Add(MsgHeadList[Item.Index].Sender);
 
    Item.SubItems.Add(MsgHeadList[Item.Index].Subject);
 
    Item.SubItems.Add(DateTimeToStr(MsgHeadList[Item.Index].SentTime));
    Item.SubItems.Add(ShowCustomSize(MsgHeadList[Item.Index].Size));
  end
  else
  begin
    MsgHead := ShellItem(Item.Index);
    if not Assigned(MsgHead) then
    begin
      MinIndx := Item.Index;
      MaxIndx := Item.Index + 50;
      GetVirtualMessages(MailboxTreeView.Selected.Data);
      MsgHead := ShellItem(Item.Index);
    end;
    if not Assigned(MsgHead) then
      Exit;
 
    Item.ImageIndex := GetMsgIconIndex(MsgHead^);
    Item.SubItems.Add('');
    if MsgHead^.HasAttachment then
      Item.SubItemImages[0] := 187
    else
      Item.SubItemImages[0] := -1;
    Item.SubItems.Add(MsgHead^.Sender);
 
    Item.SubItems.Add(MsgHead^.Subject);
 
    Item.SubItems.Add(DateTimeToStr(MsgHead^.SentTime));
    Item.SubItems.Add(ShowCustomSize(MsgHead^.Size));
  end;
end;
 
 
procedure TfrmMain.MessageListViewDataHint(Sender: TObject;
  StartIndex, EndIndex: Integer);
begin
  if not cbVirtualMsgList.Checked then
    Exit;
  if (StartIndex < MinIndx) or (EndIndex > MaxIndx) then
  begin
    MinIndx := StartIndex;
    MaxIndx := EndIndex;
    GetVirtualMessages(MailboxTreeView.Selected.Data);
  end;
end;
 
 
 
 
end.

 

 

Copyright © 1999 - 2021 IMIBO
Privacy Statement