Commit 78ac1768 authored by Thomas's avatar Thomas
Browse files

Adjust algorithm to find patch mail item

parent c72ce70a
...@@ -143,18 +143,21 @@ namespace pEp.DPE ...@@ -143,18 +143,21 @@ namespace pEp.DPE
/// <returns>The patch mail item or null if not found.</returns> /// <returns>The patch mail item or null if not found.</returns>
public Outlook.MailItem GetPatchMailItem(Patch patch) public Outlook.MailItem GetPatchMailItem(Patch patch)
{ {
Outlook.Folder inbox = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder; Outlook.Folder inbox = null;
Outlook.Items folderItems = inbox?.Items; Outlook.Items items = null;
if (folderItems?.Count > 0) try
{ {
Log.Verbose("GetPatchMailItem: " + folderItems.Count + " items found."); inbox = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
items = inbox?.Items;
try if (items?.Count > 0)
{ {
Log.Verbose("GetPatchMailItem: " + items.Count + " items found in " + inbox.FolderPath);
// First filter out older items // First filter out older items
Outlook.Items filteredInboxItems = folderItems.Restrict("[ReceivedTime] >= '" + patch.CreationDate.ToString("g") + "'"); items = items.Restrict("[ReceivedTime] >= '" + patch.CreationDate.ToString("g") + "'");
Log.Verbose("GetPatchMailItem: Items since receival of patch: " + filteredInboxItems?.Count ?? "0"); Log.Verbose("GetPatchMailItem: Items since receival of patch: " + items?.Count ?? "0");
Outlook.UserDefinedProperties up = inbox.UserDefinedProperties; Outlook.UserDefinedProperties up = inbox.UserDefinedProperties;
if (up.Find(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_ID) == null) if (up.Find(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_ID) == null)
...@@ -164,16 +167,34 @@ namespace pEp.DPE ...@@ -164,16 +167,34 @@ namespace pEp.DPE
// Now find the one with the respective patch id // Now find the one with the respective patch id
string filter = string.Format("[{0}] = '{1}'", MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_ID, patch.Id); string filter = string.Format("[{0}] = '{1}'", MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_ID, patch.Id);
return filteredInboxItems.Find(filter) as Outlook.MailItem; items = items.Restrict(filter);
if (items.Count > 1)
{
Log.Error("GetPatchMailItem: More than one item found with patch id " + patch.Id);
}
else if (items.Count < 1)
{
Log.Error("GetPatchMailItem: No item found with patch id " + patch.Id);
}
else
{
return items[1] as Outlook.MailItem;
}
} }
catch (Exception ex) else
{ {
Log.Error("GetPatchMailItem: Error occured. " + ex); Log.Verbose("GetPatchMailItem: No items were found or folder was null.");
} }
} }
else catch (Exception ex)
{
Log.Error("GetPatchMailItem: Error occured. " + ex);
}
finally
{ {
Log.Verbose("GetPatchMailItem: No items were found or folder was null."); inbox = null;
items = null;
} }
return null; return null;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment