/// Copyright 2017 Kopano b.v.
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License, version 3,
/// as published by the Free Software Foundation.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
/// GNU Affero General Public License for more details.
///
/// You should have received a copy of the GNU Affero General Public License
/// along with this program.If not, see.
///
/// Consult LICENSE file for details
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Acacia.Stubs;
using Acacia.Stubs.OutlookWrappers;
using System.Reflection;
using System.Threading;
using System.Collections.Concurrent;
namespace Acacia.Utils
{
///
/// Handles registration for events on mail items. To register for these, each individual MailItem must be registered,
/// which can be done in the Application.ItemLoad event. This class hides that implementation and also ensures the
/// event registrations are removed when the item is unloaded, to prevent resource leaks.
///
/// TODO: this name is now wrong
public class MailEvents
{
#region Events
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);
///
/// Hooks into Reply(All) and Forward events
///
public event MailResponseEventHandler Respond;
public event MailResponseEventHandler Reply;
private void OnReply(IMailItem mail, IMailItem response)
{
try
{
if ((Reply != null || Respond != null) && mail != null && response != null)
{
if (Reply != null)
Reply(mail, response);
if (Respond != null)
Respond(mail, response);
}
}
catch (System.Exception e)
{
Logger.Instance.Error(this, "OnReply: {0}", e);
}
}
public event MailResponseEventHandler ReplyAll;
private void OnReplyAll(IMailItem mail, IMailItem response)
{
try
{
if ((ReplyAll != null || Respond != null) && mail != null && response != null)
{
if (ReplyAll != null)
ReplyAll(mail, response);
if (Respond != null)
Respond(mail, response);
}
}
catch (System.Exception e)
{
Logger.Instance.Error(this, "OnReplyAll: {0}", e);
}
}
public event MailResponseEventHandler Forward;
private void OnForward(IMailItem mail, IMailItem response)
{
try
{
if ((Forward != null || Respond != null) && mail != null && response != null)
{
if (Forward != null)
Forward(mail, response);
if (Respond != null)
Respond(mail, response);
}
}
catch (System.Exception e)
{
Logger.Instance.Error(this, "OnForward: {0}", e);
}
}
public event MailEventHandler Read;
private void OnRead(IMailItem mail)
{
try
{
if (Read != null && mail != null)
{
Read(mail);
}
}
catch (System.Exception e)
{
Logger.Instance.Error(this, "OnRead: {0}", e);
}
}
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)
{
try
{
if (BeforeDelete != null && item != null)
{
BeforeDelete(item, ref cancel);
}
}
catch(System.Exception e)
{
Logger.Instance.Error(this, "OnBeforeDelete: {0}", e);
}
}
// TODO: should this be CancellableMailItemEventHandler?
public event CancellableItemEventHandler Write;
private void OnWrite(IItem item, ref bool cancel)
{
try
{
if (Write != null && item != null)
{
Write(item, ref cancel);
}
}
catch (System.Exception e)
{
Logger.Instance.Error(this, "OnWrite: {0}", e);
}
}
#region Send
private class Dispatchers
{
public class Dispatcher
{
private readonly Dispatchers _dispatchers;
private bool _failed;
private List