using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 커스텀 애드인
/// </summary>
public partial class CustomAddIn
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 폴더 컨테이너 딕셔너리
/// </summary>
private Dictionary<string, FolderContainer> folderContainerDictionary = new Dictionary<string, FolderContainer>();
/// <summary>
/// 필터 폴더명
/// </summary>
private readonly string filterFolderName = "Sales Force";
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 커스텀 애드인 시작시 처리하기 - CustomAddIn_Startup(sender, e)
/// <summary>
/// 커스텀 애드인 시작시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void CustomAddIn_Startup(object sender, EventArgs e)
{
BuildFolderContainerDictionary();
}
#endregion
#region 커스텀 애드인 셧다운시 처리하기 - CustomAddIn_Shutdown(sender, e)
/// <summary>
/// 커스텀 애드인 셧다운시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void CustomAddIn_Shutdown(object sender, EventArgs e)
{
}
#endregion
#region 소스 폴더 폴더 이동 전 처리하기 - sourceFolder_BeforeFolderMove(targetFolder, cancel)
/// <summary>
/// 소스 폴더 폴더 이동 전 처리하기
/// </summary>
/// <param name="targetFolder">타겟 폴더</param>
/// <param name="cancel">취소 여부</param>
private void sourceFolder_BeforeFolderMove(MAPIFolder targetFolder, ref bool cancel)
{
cancel = true;
}
#endregion
#region 소스 폴더 항목 이동 전 처리하기 - sourceFolder_BeforeItemMove(sourceItem, targetFolder, cancel)
/// <summary>
/// 소스 폴더 항목 이동 전 처리하기
/// </summary>
/// <param name="sourceItem">소스 항목</param>
/// <param name="targetFolder">타겟 폴더</param>
/// <param name="cancel">취소 여부</param>
private void sourceFolder_BeforeItemMove(object sourceItem, MAPIFolder targetFolder, ref bool cancel)
{
cancel = true;
}
#endregion
#region 소스 폴더 컬렉션 폴더 추가시 처리하기 - sourceFolders_FolderAdd(folder)
/// <summary>
/// 소스 폴더 컬렉션 폴더 추가시 처리하기
/// </summary>
/// <param name="folder">폴더</param>
private void sourceFolders_FolderAdd(MAPIFolder folder)
{
Debug.WriteLine("BEGIN ADD FOLDER");
try
{
ClearFolderContainerDictionaryData();
BuildFolderContainerDictionary();
}
catch(System.Exception exception)
{
Debug.WriteLine(exception.ToString());
Debug.WriteLine("STOP ADD FOLDER");
return;
}
Debug.WriteLine("END ADD FOLDER");
}
#endregion
#region 소스 폴더 컬렉션 폴더 변경시 처리하기 - sourceFolders_FolderChange(folder)
/// <summary>
/// 소스 폴더 컬렉션 폴더 변경시 처리하기
/// </summary>
/// <param name="folder">폴더</param>
private void sourceFolders_FolderChange(MAPIFolder folder)
{
Debug.WriteLine("BEGIN CHANGE FOLDER");
try
{
ClearFolderContainerDictionaryData();
BuildFolderContainerDictionary();
}
catch(System.Exception exception)
{
Debug.WriteLine(exception.ToString());
Debug.WriteLine("STOP CHANGE FOLDER");
return;
}
Debug.WriteLine("END CHANGE FOLDER");
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region VSTO에서 생성한 코드
/// <summary>
/// 디자이너 지원에 필요한 메서드입니다.
/// 이 메서드의 내용을 코드 편집기로 수정하지 마세요.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(CustomAddIn_Startup );
this.Shutdown += new System.EventHandler(CustomAddIn_Shutdown);
}
#endregion
#region 폴더 컨테이너 딕셔너리 구축하기 - BuildFolderContainerDictionary()
/// <summary>
/// 폴더 컨테이너 딕셔너리 구축하기
/// </summary>
private void BuildFolderContainerDictionary()
{
foreach(Folder rootfolder in Application.ActiveExplorer().Session.Folders)
{
foreach(Folder folder in rootfolder.Folders)
{
if(!folder.FullFolderPath.EndsWith(this.filterFolderName))
{
continue;
}
AddFolderContainer(null, folder);
}
}
}
#endregion
#region 폴터 컨테이너 추가하기 - AddFolderContainer(parentContainer, folder)
/// <summary>
/// 폴터 컨테이너 추가하기
/// </summary>
/// <param name="parentContainer">부모 컨테이너</param>
/// <param name="folder">폴더</param>
private void AddFolderContainer(FolderContainer parentContainer, Folder folder)
{
string sourceKey = folder.FullFolderPath.Substring(2);
string[] sourceKeyItemArray = sourceKey.Split('\\');
if(sourceKeyItemArray.Length < 2 || sourceKeyItemArray[1] != this.filterFolderName)
{
return;
}
FolderContainer container = new FolderContainer();
container.Parent = parentContainer;
container.SourceKey = sourceKey;
container.SourceFolder = folder;
container.SourceFolders = folder.Folders;
container.SourceFolder.BeforeFolderMove -= sourceFolder_BeforeFolderMove;
container.SourceFolder.BeforeItemMove -= sourceFolder_BeforeItemMove;
container.SourceFolders.FolderAdd -= sourceFolders_FolderAdd;
container.SourceFolders.FolderChange -= sourceFolders_FolderChange;
container.SourceFolder.BeforeFolderMove += sourceFolder_BeforeFolderMove;
container.SourceFolder.BeforeItemMove += sourceFolder_BeforeItemMove;
container.SourceFolders.FolderAdd += sourceFolders_FolderAdd;
container.SourceFolders.FolderChange += sourceFolders_FolderChange;
if(parentContainer != null)
{
parentContainer.ChildList.Add(container);
}
this.folderContainerDictionary.Add(container.SourceKey, container);
Debug.WriteLine(container.SourceKey);
foreach(Folder childFolder in folder.Folders)
{
AddFolderContainer(container, childFolder);
}
}
#endregion
#region 폴더 컨테이너 딕셔너리 데이터 지우기 - ClearFolderContainerDictionaryData()
/// <summary>
/// 폴더 컨테이너 딕셔너리 데이터 지우기
/// </summary>
private void ClearFolderContainerDictionaryData()
{
foreach(KeyValuePair<string, FolderContainer> keyValuePair in this.folderContainerDictionary)
{
string sourceKey = keyValuePair.Key;
FolderContainer container = keyValuePair.Value;
try
{
container.SourceFolder.BeforeFolderMove -= sourceFolder_BeforeFolderMove;
container.SourceFolder.BeforeItemMove -= sourceFolder_BeforeItemMove;
container.SourceFolders.FolderAdd -= sourceFolders_FolderAdd;
container.SourceFolders.FolderChange -= sourceFolders_FolderChange;
}
catch
{
}
container.Parent = null;
container.SourceKey = null;
container.SourceFolder = null;
container.SourceFolders = null;
container.ChildList.Clear();
}
this.folderContainerDictionary.Clear();
}
#endregion
}
}