[KOE-108] Copying contacts out of the GAB failed as it triggers a write event that is rejected. Fixed by rejecting write events only if there are preceding property changes on the same item. Also fixed a bug that the rejection dialog box was sometimes not shown by setting the parent window explicitly.

This commit is contained in:
Patrick Simpson 2017-05-03 14:15:14 +02:00
parent 28b613af14
commit 4bf4539bda
3 changed files with 55 additions and 4 deletions

View File

@ -74,7 +74,8 @@ namespace Acacia.Features.GAB
if (SuppressModifications && MailEvents != null)
{
MailEvents.BeforeDelete += SuppressEventHandler_Delete;
MailEvents.Write += SuppressEventHandler_Modify;
MailEvents.PropertyChange += SuppressEventHandler_PropertyChange;
MailEvents.Write += SuppressEventHandler_Write;
}
Watcher.AccountDiscovered += AccountDiscovered;
Watcher.AccountRemoved += AccountRemoved;
@ -247,9 +248,27 @@ namespace Acacia.Features.GAB
SuppressEventHandler(item, false, ref cancel);
}
private void SuppressEventHandler_Modify(IItem item, ref bool cancel)
/// <summary>
/// When copying a contact from the GAB to a local folder, Outlook raises the Write event on
/// the original. To detect this, we set this id on every property change (which is signalled before
/// write), and only suppress if anything has actually changed. If we suppress, the flag is cleared again.
/// </summary>
private string _propertyChangeId;
private void SuppressEventHandler_PropertyChange(IItem item, string propertyName)
{
SuppressEventHandler(item, true, ref cancel);
if (_propertyChangeId == item.EntryID)
return;
_propertyChangeId = item.EntryID;
}
private void SuppressEventHandler_Write(IItem item, ref bool cancel)
{
if (_propertyChangeId == item.EntryID)
{
SuppressEventHandler(item, true, ref cancel);
_propertyChangeId = null;
}
}
private void SuppressEventHandler(IItem item, bool findInspector, ref bool cancel)
@ -301,7 +320,10 @@ namespace Acacia.Features.GAB
}*/
// Show message and cancel event
MessageBox.Show(StringUtil.GetResourceString("GABEvent_Body"),
// TODO: show on active inspector if used
// [KOE-108] If the window is not specified as parent, it sometimes doesn't show
var res = MessageBox.Show(ThisAddIn.Instance.Window,
StringUtil.GetResourceString("GABEvent_Body"),
StringUtil.GetResourceString("GABEvent_Title"),
MessageBoxButtons.OK,
MessageBoxIcon.Warning

View File

@ -231,6 +231,10 @@ namespace Acacia.Stubs.OutlookWrappers
#region Window handle
/// <summary>
/// Simple IWin32Window wrapper for a native handle. NativeWindow sometimes refuses to handle
/// these (FromHandle returns null), so use a simple wrapper.
/// </summary>
private class WindowHandle : IWin32Window
{
private IntPtr hWnd;

View File

@ -38,6 +38,7 @@ namespace Acacia.Utils
public delegate void MailEventHandler(IMailItem mail);
public delegate void MailResponseEventHandler(IMailItem mail, IMailItem response);
public delegate void ItemEventHandler(IItem item);
public delegate void PropertyChangeEventHandler(IItem item, string propertyName);
public delegate void CancellableItemEventHandler(IItem item, ref bool cancel);
public delegate void CancellableMailItemEventHandler(IMailItem item, ref bool cancel);
@ -119,6 +120,22 @@ namespace Acacia.Utils
}
}
public event PropertyChangeEventHandler PropertyChange;
private void OnPropertyChange(IItem item, string propertyName)
{
try
{
if (PropertyChange != null && item != null)
{
PropertyChange(item, propertyName);
}
}
catch (System.Exception e)
{
Logger.Instance.Error(this, "OnPropertyChange: {0}", e);
}
}
public event CancellableItemEventHandler BeforeDelete;
private void OnBeforeDelete(IItem item, ref bool cancel)
{
@ -369,6 +386,7 @@ namespace Acacia.Utils
events.BeforeDelete += HandleBeforeDelete;
events.Forward += HandleForward;
events.PropertyChange += HandlePropertyChange;
events.Read += HandleRead;
events.Reply += HandleReply;
events.ReplyAll += HandleReplyAll;
@ -379,6 +397,7 @@ namespace Acacia.Utils
{
events.BeforeDelete -= HandleBeforeDelete;
events.Forward -= HandleForward;
events.PropertyChange -= HandlePropertyChange;
events.Read -= HandleRead;
events.Reply -= HandleReply;
events.ReplyAll -= HandleReplyAll;
@ -400,8 +419,14 @@ namespace Acacia.Utils
_events.OnForward(_item as IMailItem, wrapped as IMailItem);
}
private void HandlePropertyChange(string name)
{
_events.OnPropertyChange(_item, name);
}
private void HandleRead()
{
// TODO: should this not be simply an IItem?
_events.OnRead(_item as IMailItem);
}