From 95c6150c90be9e950b7d84187ad006b8bb5cbe02 Mon Sep 17 00:00:00 2001 From: Patrick Simpson Date: Fri, 10 Feb 2017 13:30:22 +0100 Subject: [PATCH] Small cleanups. Made HookItemEvents disabled by default. Restored DebugSupport buttons. --- .../DebugSupport/FeatureDebugSupport.cs | 12 ++--- .../AcaciaZPushPlugin/GlobalOptions.cs | 2 +- .../AcaciaZPushPlugin/Stubs/IExplorer.cs | 6 +++ .../AcaciaZPushPlugin/Stubs/IFolder.cs | 4 ++ .../Stubs/OutlookWrappers/AddInWrapper.cs | 5 +- .../Stubs/OutlookWrappers/ExplorerWrapper.cs | 10 ++++ .../Stubs/OutlookWrappers/FolderWrapper.cs | 6 +++ .../Stubs/OutlookWrappers/StoreWrapper.cs | 18 +++++-- .../AcaciaZPushPlugin/Stubs/Wrappers.cs | 5 +- .../AcaciaZPushPlugin/ZPush/ZPushAccount.cs | 1 - .../AcaciaZPushPlugin/ZPush/ZPushAccounts.cs | 2 +- .../AcaciaZPushPlugin/ZPush/ZPushFolder.cs | 16 ++----- .../AcaciaZPushPlugin/ZPush/ZPushWatcher.cs | 47 +++++++------------ 13 files changed, 73 insertions(+), 61 deletions(-) diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/DebugSupport/FeatureDebugSupport.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/DebugSupport/FeatureDebugSupport.cs index 53d5855..e1600a0 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/DebugSupport/FeatureDebugSupport.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/DebugSupport/FeatureDebugSupport.cs @@ -48,16 +48,16 @@ namespace Acacia.Features.DebugSupport public override void AfterStartup() { - //ShowAbout(); + /*DebugDialog dd = new DebugDialog(); + dd.Show(); + dd.DebugCycle(5);*/ } #region About dialog public void ShowAbout() { - DebugDialog dd = new DebugDialog(); - dd.Show(); - dd.DebugCycle(5); + new AboutDialog().ShowDialog(); } #endregion @@ -79,9 +79,7 @@ namespace Acacia.Features.DebugSupport public void ShowSettings() { - //new SettingsDialog().ShowDialog(); - ThisAddIn.Instance.GetFeature()?.FullResync(); - Tasks.Task(this, "GC", () => Util.GarbageCollect()); + new SettingsDialog().ShowDialog(); } public override FeatureSettings GetSettings() diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/GlobalOptions.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/GlobalOptions.cs index e413474..43e9a82 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/GlobalOptions.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/GlobalOptions.cs @@ -125,7 +125,7 @@ namespace Acacia get { return GetOption(null, HOOK_ITEM_EVENTS); } set { SetOption(null, HOOK_ITEM_EVENTS, value); } } - private static readonly BoolOption HOOK_ITEM_EVENTS = new BoolOption("HookItemEvents", true); + private static readonly BoolOption HOOK_ITEM_EVENTS = new BoolOption("HookItemEvents", false); #region UI Options diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IExplorer.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IExplorer.cs index e4b4474..33ec004 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IExplorer.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IExplorer.cs @@ -13,5 +13,11 @@ namespace Acacia.Stubs /// /// The command bars. The caller is responsible for disposing. ICommandBars GetCommandBars(); + + /// + /// Returns the currently selected folder, or null if no folder is selected. + /// + /// The folder. The caller is responsible for disposing. + IFolder GetCurrentFolder(); } } diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IFolder.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IFolder.cs index 7164be6..54cdbd5 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IFolder.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IFolder.cs @@ -106,5 +106,9 @@ namespace Acacia.Stubs /// function prevents creating lots of wrappers. /// bool IsAtDepth(int depth); + + // TODO: remove this. It's a quick hack to find the events associated with this folder for ZPushWatcher. + // make event watching part of the folder instead + ZPushFolder ZPush { get; set; } } } diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AddInWrapper.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AddInWrapper.cs index 6efb688..3facf93 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AddInWrapper.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AddInWrapper.cs @@ -88,10 +88,7 @@ namespace Acacia.Stubs.OutlookWrappers public IExplorer GetActiveExplorer() { - using (ComRelease com = new ComRelease()) - { - return new ExplorerWrapper(com.Add(_thisAddIn.Application).ActiveExplorer()); - } + return new ExplorerWrapper(_app.ActiveExplorer()); } #region Window handle diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/ExplorerWrapper.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/ExplorerWrapper.cs index cd7556a..6350abe 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/ExplorerWrapper.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/ExplorerWrapper.cs @@ -14,9 +14,19 @@ namespace Acacia.Stubs.OutlookWrappers { } + protected override void DoRelease() + { + base.DoRelease(); + } + public ICommandBars GetCommandBars() { return new CommandBarsWrapper(_item.CommandBars); } + + public IFolder GetCurrentFolder() + { + return _item.CurrentFolder.Wrap(); + } } } diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/FolderWrapper.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/FolderWrapper.cs index a2b217e..ed55a67 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/FolderWrapper.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/FolderWrapper.cs @@ -467,5 +467,11 @@ namespace Acacia.Stubs.OutlookWrappers { get { return (ItemType)(int)_item.DefaultItemType; } } + + public ZPushFolder ZPush + { + get; + set; + } } } diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/StoreWrapper.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/StoreWrapper.cs index 10d46f8..11d4499 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/StoreWrapper.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/StoreWrapper.cs @@ -36,18 +36,30 @@ namespace Acacia.Stubs.OutlookWrappers return new FolderWrapper((NSOutlook.Folder)_item.GetRootFolder()); } + private NSOutlook.MAPIFolder GetDefaultFolderObj(DefaultFolder folder) + { + try + { + return (NSOutlook.Folder)_item.GetDefaultFolder((NSOutlook.OlDefaultFolders)(int)folder); + } + catch(Exception) + { + return null; + } + } + public IFolder GetDefaultFolder(DefaultFolder folder) { // FolderWrapper manages the returned Folder - return new FolderWrapper((NSOutlook.Folder)_item.GetDefaultFolder((NSOutlook.OlDefaultFolders)(int)folder)); + return GetDefaultFolderObj(folder).Wrap(); } public string GetDefaultFolderId(DefaultFolder folder) { - NSOutlook.MAPIFolder mapiFolder = _item.GetDefaultFolder((NSOutlook.OlDefaultFolders)(int)folder); + NSOutlook.MAPIFolder mapiFolder = GetDefaultFolderObj(folder); try { - return mapiFolder.EntryID; + return mapiFolder?.EntryID; } finally { diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/Wrappers.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/Wrappers.cs index 4ee284c..6b86ddd 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/Wrappers.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/Wrappers.cs @@ -1,4 +1,5 @@ -using System; +using Acacia.Stubs.OutlookWrappers; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -11,7 +12,7 @@ namespace Acacia.Stubs { public static IFolder Wrap(this NSOutlook.MAPIFolder obj) { - throw new NotImplementedException(); // TODO + return Mapping.WrapOrDefault(obj); } } } diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushAccount.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushAccount.cs index e6d8a93..41355ab 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushAccount.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushAccount.cs @@ -28,7 +28,6 @@ using System.Security; using System.Text; using System.Threading; using System.Threading.Tasks; -using NSOutlook = Microsoft.Office.Interop.Outlook; namespace Acacia.ZPush { diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushAccounts.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushAccounts.cs index 8797bfe..22a50b1 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushAccounts.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushAccounts.cs @@ -293,7 +293,7 @@ namespace Acacia.ZPush /// The account. The caller is responsible for releasing this. /// The associated ZPushAccount /// If the registry key cannot be found - // TODO: check management of account + // TODO: check owner private ZPushAccount CreateFromRegistry(NSOutlook.Account account) { // TODO: check that caller releases account everywhere diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushFolder.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushFolder.cs index c8ec64e..c5941c4 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushFolder.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushFolder.cs @@ -50,14 +50,16 @@ namespace Acacia.ZPush this._parent = parent; this._watcher = watcher; this._folder = folder; + folder.ZPush = this; } protected override void DoRelease() { + Cleanup(); _folder.Dispose(); } - public IFolder Folder { get; } + public IFolder Folder { get { return _folder; } } public string Name { get { return _folder.Name; } } private void Initialise() @@ -75,16 +77,6 @@ namespace Acacia.ZPush } } - // TODO - /*public override void Dispose() - { - Logger.Instance.Trace(this, "Disposing folder: {0}", _item.Name); - Cleanup(); - base.Dispose(); - ComRelease.Release(_items); - ComRelease.Release(_subFolders); - }*/ - internal ItemsWatcher ItemsWatcher() { ItemsWatcher watcher = new ItemsWatcher(); @@ -168,7 +160,7 @@ namespace Acacia.ZPush } // Release the folder if not used - ComRelease.Release(child); + child.Dispose(); } } } diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushWatcher.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushWatcher.cs index f486810..2fe58db 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushWatcher.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushWatcher.cs @@ -37,24 +37,27 @@ namespace Acacia.ZPush /// public class ZPushWatcher { + private readonly IAddIn _addIn; + // TODO: remove private readonly NSOutlook.Application _app; public readonly ZPushAccounts Accounts; public readonly ZPushSync Sync; - private NSOutlook.Explorer _explorer; + private NSOutlook.Explorer _explorer2; #region Setup public ZPushWatcher(IAddIn addIn) { + this._addIn = addIn; this._app = addIn.RawApp; Sync = new ZPushSync(this, _app); Accounts = new ZPushAccounts(this, _app); // Need to keep a link to keep receiving events - _explorer = _app.ActiveExplorer(); - _explorer.SelectionChange += Explorer_SelectionChange; + _explorer2 = _app.ActiveExplorer(); + _explorer2.SelectionChange += Explorer_SelectionChange; } /// @@ -208,17 +211,15 @@ namespace Acacia.ZPush { if (ActiveFolderChange != null) { - NSOutlook.MAPIFolder active = _explorer.CurrentFolder; - if (active != null) + + using (IFolder folder = _addIn.GetActiveExplorer()?.GetCurrentFolder()) { - using (IFolder folder = Mapping.Wrap(active)) + try { - try - { + if (folder != null) ActiveFolderChange(folder); - } - catch (System.Exception e) { Logger.Instance.Error(this, "Exception in Explorer_SelectionChange.ActiveFolderChange: {0}", e); } } + catch (System.Exception e) { Logger.Instance.Error(this, "Exception in Explorer_SelectionChange.ActiveFolderChange: {0}", e); } } } // TODO: cache value @@ -236,18 +237,13 @@ namespace Acacia.ZPush public ZPushAccount CurrentZPushAccount() { - if (_explorer.CurrentFolder == null) - return null; - - NSOutlook.MAPIFolder folder = _explorer.CurrentFolder; - try + using (IExplorer explorer = _addIn.GetActiveExplorer()) + using (IFolder folder = explorer?.GetCurrentFolder()) { + if (folder == null) + return null; return Accounts.GetAccount(folder); } - finally - { - ComRelease.Release(folder); - } } #endregion @@ -392,17 +388,8 @@ namespace Acacia.ZPush if (!DebugOptions.GetOption(null, DebugOptions.WATCHER_ENABLED)) return; - // Must have a ZPush folder to watch events, create one if necessary - ZPushFolder zPushFolder; - if (!(folder is ZPushFolder)) - { - // TODO - throw new NotImplementedException(); - } - else - { - zPushFolder = (ZPushFolder)folder; - } + // Must have a ZPush folder to watch events. + ZPushFolder zPushFolder = folder.ZPush; // Register the handlers ItemsWatcher watcher = zPushFolder.ItemsWatcher();