Files
ayanova7/source/Plugins/AyaNovaOL/Connect.cs
2018-06-29 19:47:36 +00:00

235 lines
8.7 KiB
C#

namespace AyaNovaOL
{
using System;
using Extensibility;
using System.Runtime.InteropServices;
using Outlook=Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Core;
using System.Windows.Forms;
using System.Reflection;
using System.Collections.Generic;
#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the AyaNovaOLSetup project,
// right click the project in the Solution Explorer, then choose install.
#endregion
/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("E40A95F9-61D2-4B47-AF80-54BCC5AC9D7F"), ProgId("AyaNovaOL.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
// Our UI will consist of a single CommandBarButton
// private CommandBarButton btnAyaNova;
private Outlook.Application _OutlookApplication;
private object _addInInstance;
// Wrapper stuff
Outlook.Inspectors _Inspectors;
Outlook.Explorers _Explorers;
// This dictionary holds our Wrapped Inspectors, Explorers, Folders, Items, Whatever
Dictionary<Guid, WrapperClass> _WrappedObjects;
/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}
/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
Util.db("OLI -> OnConnection()");
//MessageBox.Show("OnConnection");
_OutlookApplication = application as Outlook.Application;
_addInInstance = addInInst;
if (connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}
}
/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2 interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
//MessageBox.Show("OnDisconnection");
if (disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
//MessageBox.Show("OnDisconnection completed");
}
/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}
/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2 interface.
/// Receives notification that the host application has completed loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
_WrappedObjects = new Dictionary<Guid, WrapperClass>();
//// Inspectors stuff
_Inspectors = _OutlookApplication.Inspectors;
_Inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(_Inspectors_NewInspector);
// Are there any open Inspector after Startup ?
for (int i = _Inspectors.Count; i >= 1; i--)
{
// Wrap the Inspector and do some usefull with it
WrapInspector(_Inspectors[i]);
}
//// Explorer stuff
_Explorers = _OutlookApplication.Explorers;
_Explorers.NewExplorer += new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(_Explorers_NewExplorer);
// Are there any open Explorers after Startup ?
for (int i = _Explorers.Count; i >= 1; i--)
{
// Wrap the Explorer and do some usefull with it
WrapExplorer(_Explorers[i]);
}
}
/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
//MessageBox.Show("Cleanup starting in OnBeginShutdown");
CleanUp();
//MessageBox.Show("Cleanup completed in OnBeginShutdown");
}
#region Wrapper specific
void CleanUp()
{
if (_OutlookApplication != null)
{
_WrappedObjects.Clear();
_Inspectors.NewInspector -= new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(_Inspectors_NewInspector);
_Inspectors = null;
_Explorers.NewExplorer -= new Outlook.ExplorersEvents_NewExplorerEventHandler(_Explorers_NewExplorer);
_Explorers = null;
_addInInstance = null;
_OutlookApplication = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
void _Explorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer)
{
WrapExplorer(Explorer);
}
void WrapExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer)
{
//Util.d("WrapExplorer:" + Explorer.Caption);
ExplorerWrapper wrappedExplorer = new ExplorerWrapper(Explorer);
wrappedExplorer.Closed += new WrapperClosedDelegate(wrappedObject_Closed);
_WrappedObjects[wrappedExplorer.Id] = wrappedExplorer;
}
void _Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
if (Inspector.CurrentItem is Outlook.MailItem)
{
InspectorWrapper wrappedInspector = new InspectorWrapper(Inspector);
wrappedInspector.Closed += new WrapperClosedDelegate(wrappedObject_Closed);
_WrappedObjects[wrappedInspector.Id] = wrappedInspector;
}
}
/// <summary>
/// The Inspector is "wrapped" and used in the application.
/// </summary>
/// <param name="inspector">The new Inspector instance</param>
void WrapInspector(Outlook.Inspector inspector)
{
if (inspector.CurrentItem is Outlook.MailItem)
{
InspectorWrapper wrappedInspector = new InspectorWrapper(inspector);
wrappedInspector.Closed += new WrapperClosedDelegate(wrappedObject_Closed);
_WrappedObjects[wrappedInspector.Id] = wrappedInspector;
}
}
void wrappedObject_Closed(Guid id)
{
_WrappedObjects.Remove(id);
}
#endregion
}
}