[KOE-168] Added checking of and sending with stored send-as address.

This commit is contained in:
Patrick Simpson 2018-04-13 13:50:04 +03:00
parent d4dc876bbe
commit 7a6075a6d9
8 changed files with 95 additions and 24 deletions

View File

@ -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<SharedFolder> shares)
{
SyncState.SyncState state = ThisAddIn.Instance.GetFeature<FeatureSyncState>()?.GetSyncState(zpush);
foreach (SharedFolder folder in shares)
{
if (!folder.FlagSendAsOwner)

View File

@ -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)

View File

@ -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; }

View File

@ -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
/// <summary>
/// Sends and receives all accounts, or a specific account.
/// </summary>
void SendReceive(IAccount account = null);
void SendReceive(IAccount account = null, AcaciaTask after = null);
/// <summary>
/// Restarts the application

View File

@ -72,9 +72,9 @@ namespace Acacia.Stubs.OutlookWrappers
/// <summary>
/// Triggers an Outlook send/receive operation for this account.
/// </summary>
public void SendReceive()
public void SendReceive(AcaciaTask after = null)
{
ThisAddIn.Instance.SendReceive(this);
ThisAddIn.Instance.SendReceive(this, after);
}
#region Properties

View File

@ -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

View File

@ -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();
}
/// <summary>
/// Delegate for an account-specific task.
/// </summary>
@ -499,6 +501,20 @@ namespace Acacia.ZPush
}
}
private readonly ConcurrentQueue<AcaciaTask> _endTasks = new ConcurrentQueue<AcaciaTask>();
/// <summary>
/// Invoked after an explicit send and receive is performed, invokes any tasks.
/// </summary>
private void SyncObject_SyncEnd()
{
AcaciaTask task;
while (_endTasks.TryDequeue(out task))
{
Tasks.Task(task, false);
}
}
#endregion
}
}

View File

@ -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());
}