diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SendAs/FeatureSendAs.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SendAs/FeatureSendAs.cs index 446ccf2..49cb32f 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SendAs/FeatureSendAs.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SendAs/FeatureSendAs.cs @@ -27,6 +27,7 @@ using Acacia.ZPush.API.SharedFolders; using static Acacia.DebugOptions; using Acacia.Features.GAB; using Acacia.Features.SyncState; +using System.Windows.Forms; namespace Acacia.Features.SendAs { @@ -108,7 +109,6 @@ namespace Acacia.Features.SendAs Logger.Instance.Trace(this, "Responding to mail, checking"); using (IStore store = mail.GetStore()) { - /* ZPushAccount zpush = Watcher.Accounts.GetAccount(store); Logger.Instance.Trace(this, "Checking ZPush: {0}", zpush); if (zpush == null) @@ -127,7 +127,7 @@ namespace Acacia.Features.SendAs { response.SetSender(address); } - }*/ + } } } @@ -159,11 +159,57 @@ namespace Acacia.Features.SendAs #region Address resolving + private IRecipient FindSendAsSender(ZPushAccount zpush, IFolder folder) + { + SyncId syncId = folder.SyncId; + if (syncId != null) + { + string address = zpush.GetSendAsAddress(syncId); + if (address == null) + { + // Check if it should have an address + SharedFolder shared = _sharedFolders?.GetSharedFolder(folder); + if (shared?.FlagSendAsOwner == true) + { + // See if we can get it now + address = FindSendAsAddress(zpush, shared); + + if (address == null) + { + // Should have it, error + MessageBox.Show(ThisAddIn.Instance.Window, + Properties.Resources.SharedFolders_SendAsFailed_Label, + Properties.Resources.SharedFolders_SendAsFailed_Title, + MessageBoxButtons.OK, + MessageBoxIcon.Error + ); + } + else + { + + } + } + } + + if (address != null) + { + IRecipient resolved = ThisAddIn.Instance.ResolveRecipient(address); + if (resolved != null) + return resolved; + } + } + return null; + } + public string FindSendAsAddress(ZPushAccount zpush, SharedFolder folder) { string address = folder.SendAsAddress; if (!string.IsNullOrWhiteSpace(address)) + { + // Make sure it's in the registry + StoreSyncIdAddress(zpush, folder); return address; + } // Check the registry string addressSync = zpush.GetSendAsAddress(folder.SyncId); @@ -171,20 +217,23 @@ namespace Acacia.Features.SendAs // If we have no address on sync id, or it differs from the one on backend id, backend id wins, as that's the one set by the dialog if (string.IsNullOrWhiteSpace(addressSync) || !addressSync.Equals(addressBackend)) { - address = addressBackend; + folder.SendAsAddress = address = addressBackend; // Resolved now, store on sync id - if (folder.SyncId.IsCustom) - zpush.SetSendAsAddress(folder.SyncId, address); + StoreSyncIdAddress(zpush, folder); } else address = addressSync; return address; } + private void StoreSyncIdAddress(ZPushAccount zpush, SharedFolder folder) + { + if (!string.IsNullOrWhiteSpace(folder.SyncId?.ToString()) && !folder.SyncId.Equals(folder.BackendId)) + zpush.SetSendAsAddress(folder.SyncId, folder.SendAsAddress); + } + internal void UpdateSendAsAddresses(ZPushAccount zpush, ICollection shares) { - SyncState.SyncState state = ThisAddIn.Instance.GetFeature()?.GetSyncState(zpush); - foreach (SharedFolder folder in shares) { if (!folder.FlagSendAsOwner) diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/FeatureSharedFolders.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/FeatureSharedFolders.cs index 6c64d86..7b0e709 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/FeatureSharedFolders.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/FeatureSharedFolders.cs @@ -148,8 +148,7 @@ namespace Acacia.Features.SharedFolders public void Sync(ZPushAccount account) { - Watcher.Sync.Resync(); - account.Account.SendReceive(); + account.Account.SendReceive(new AcaciaTask(null, this, "SyncShares", () => SyncShares(account))); } private void SyncShares(ZPushAccount account) diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IAccount.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IAccount.cs index 3240b00..0887310 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IAccount.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IAccount.cs @@ -1,6 +1,4 @@ - -using Acacia.Native.MAPI; -/// Copyright 2017 Kopano b.v. +/// Copyright 2018 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, @@ -21,6 +19,8 @@ using System.Linq; using System.Security; using System.Text; using System.Threading.Tasks; +using Acacia.Native.MAPI; +using Acacia.Utils; namespace Acacia.Stubs { @@ -32,7 +32,7 @@ namespace Acacia.Stubs IStore Store { get; } - void SendReceive(); + void SendReceive(AcaciaTask after = null); string DisplayName { get; } diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IAddIn.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IAddIn.cs index 960c338..90dd36f 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IAddIn.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/IAddIn.cs @@ -1,4 +1,4 @@ -/// Copyright 2017 Kopano b.v. +/// Copyright 2018 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, @@ -62,7 +62,7 @@ namespace Acacia.Stubs /// /// Sends and receives all accounts, or a specific account. /// - void SendReceive(IAccount account = null); + void SendReceive(IAccount account = null, AcaciaTask after = null); /// /// Restarts the application diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AccountWrapper.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AccountWrapper.cs index 847932d..a7087e0 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AccountWrapper.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AccountWrapper.cs @@ -72,9 +72,9 @@ namespace Acacia.Stubs.OutlookWrappers /// /// Triggers an Outlook send/receive operation for this account. /// - public void SendReceive() + public void SendReceive(AcaciaTask after = null) { - ThisAddIn.Instance.SendReceive(this); + ThisAddIn.Instance.SendReceive(this, after); } #region Properties diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AddInWrapper.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AddInWrapper.cs index 34e9c41..0e52811 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AddInWrapper.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Stubs/OutlookWrappers/AddInWrapper.cs @@ -137,8 +137,13 @@ namespace Acacia.Stubs.OutlookWrappers } } - public void SendReceive(IAccount account) + public void SendReceive(IAccount account, AcaciaTask after) { + if (after != null) + { + Watcher.Sync.AddEndTaskOnce(after); + } + // TODO: send/receive specific account NSOutlook.NameSpace session = _app.Session; try diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushSync.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushSync.cs index 0d5ce13..759f3e9 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushSync.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushSync.cs @@ -1,4 +1,4 @@ -/// Copyright 2017 Kopano b.v. +/// Copyright 2018 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, @@ -19,6 +19,7 @@ using Acacia.Stubs; using Acacia.Utils; using Acacia.ZPush.Connect; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; @@ -223,6 +224,11 @@ namespace Acacia.ZPush } } + public void AddEndTaskOnce(AcaciaTask after) + { + _endTasks.Enqueue(after); + } + #endregion #region Setup @@ -261,6 +267,7 @@ namespace Acacia.ZPush // Need to keep a reference to keep receiving events _syncObject = addIn.GetSyncObject(); _syncObject.SyncStart += SyncObject_SyncStart; + _syncObject.SyncEnd += SyncObject_SyncEnd; watcher.AccountDiscovered += Watcher_AccountDiscovered; } } @@ -282,11 +289,6 @@ namespace Acacia.ZPush _started = true; } - public void Resync() - { - LastSyncTime = new DateTime(); - } - /// /// Delegate for an account-specific task. /// @@ -499,6 +501,20 @@ namespace Acacia.ZPush } } + private readonly ConcurrentQueue _endTasks = new ConcurrentQueue(); + + /// + /// Invoked after an explicit send and receive is performed, invokes any tasks. + /// + private void SyncObject_SyncEnd() + { + AcaciaTask task; + while (_endTasks.TryDequeue(out task)) + { + Tasks.Task(task, false); + } + } + #endregion } } diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushTypes.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushTypes.cs index 94656e6..b81e512 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushTypes.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/ZPush/ZPushTypes.cs @@ -63,6 +63,8 @@ namespace Acacia.ZPush public override bool Equals(object obj) { + if (obj == null) + return false; return (this.GetType() == obj.GetType()) && ((ZPushId)obj)._id.ToLower().Equals(_id.ToLower()); }