1
0
mirror of https://github.com/Kopano-dev/kopano-ol-extension.git synced 2023-10-10 13:37:40 +02:00

[KOE-168] Added storage of send-as address in registry

This commit is contained in:
Patrick Simpson 2018-04-10 14:47:52 +03:00
parent 607e189b37
commit d4dc876bbe
11 changed files with 194 additions and 41 deletions

View File

@ -26,6 +26,7 @@ using Acacia.Features.SharedFolders;
using Acacia.ZPush.API.SharedFolders; using Acacia.ZPush.API.SharedFolders;
using static Acacia.DebugOptions; using static Acacia.DebugOptions;
using Acacia.Features.GAB; using Acacia.Features.GAB;
using Acacia.Features.SyncState;
namespace Acacia.Features.SendAs namespace Acacia.Features.SendAs
{ {
@ -158,6 +159,42 @@ namespace Acacia.Features.SendAs
#region Address resolving #region Address resolving
public string FindSendAsAddress(ZPushAccount zpush, SharedFolder folder)
{
string address = folder.SendAsAddress;
if (!string.IsNullOrWhiteSpace(address))
return address;
// Check the registry
string addressSync = zpush.GetSendAsAddress(folder.SyncId);
string addressBackend = zpush.GetSendAsAddress(folder.BackendId);
// 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;
// Resolved now, store on sync id
if (folder.SyncId.IsCustom)
zpush.SetSendAsAddress(folder.SyncId, address);
}
else address = addressSync;
return address;
}
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)
continue;
// Resolve it
FindSendAsAddress(zpush, folder);
}
}
public string FindSendAsAddress(ZPushAccount zpush, GABUser user) public string FindSendAsAddress(ZPushAccount zpush, GABUser user)
{ {
GABHandler handler = FeatureGAB.FindGABForAccount(zpush); GABHandler handler = FeatureGAB.FindGABForAccount(zpush);

View File

@ -1,7 +1,4 @@
 /// Copyright 2018 Kopano b.v.
using Acacia.Features.SecondaryContacts;
using Acacia.Features.SendAs;
/// Copyright 2016 Kopano b.v.
/// ///
/// This program is free software: you can redistribute it and/or modify /// 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, /// it under the terms of the GNU Affero General Public License, version 3,
@ -16,6 +13,9 @@ using Acacia.Features.SendAs;
/// along with this program.If not, see<http://www.gnu.org/licenses/>. /// along with this program.If not, see<http://www.gnu.org/licenses/>.
/// ///
/// Consult LICENSE file for details /// Consult LICENSE file for details
using Acacia.Features.SecondaryContacts;
using Acacia.Features.SendAs;
using Acacia.Stubs; using Acacia.Stubs;
using Acacia.UI; using Acacia.UI;
using Acacia.UI.Outlook; using Acacia.UI.Outlook;
@ -143,9 +143,20 @@ namespace Acacia.Features.SharedFolders
private void AdditionalFolders_Sync(ZPushConnection connection) private void AdditionalFolders_Sync(ZPushConnection connection)
{ {
using (SharedFoldersManager manager = Manage(connection.Account)) SyncShares(connection.Account);
}
public void Sync(ZPushAccount account)
{ {
Logger.Instance.Debug(this, "Starting sync for account {0}", connection.Account); Watcher.Sync.Resync();
account.Account.SendReceive();
}
private void SyncShares(ZPushAccount account)
{
using (SharedFoldersManager manager = Manage(account))
{
Logger.Instance.Debug(this, "Starting sync for account {0}", account);
// Fetch the current shares // Fetch the current shares
ICollection<SharedFolder> shares = manager.GetCurrentShares(null); ICollection<SharedFolder> shares = manager.GetCurrentShares(null);
@ -157,11 +168,10 @@ namespace Acacia.Features.SharedFolders
// Store any send-as properties // Store any send-as properties
FeatureSendAs sendAs = ThisAddIn.Instance.GetFeature<FeatureSendAs>(); FeatureSendAs sendAs = ThisAddIn.Instance.GetFeature<FeatureSendAs>();
// TODO sendAs?.UpdateSendAsAddresses(account, shares);
//sendAs?.UpdateSendAsAddresses(connection.Account, shares);
// Store with the account // Store with the account
connection.Account.SetFeatureData(this, KEY_SHARES, dict); account.SetFeatureData(this, KEY_SHARES, dict);
} }
} }

View File

@ -256,6 +256,35 @@ namespace Acacia.Features.SharedFolders
private void dialogButtons_Apply(object sender, EventArgs e) private void dialogButtons_Apply(object sender, EventArgs e)
{ {
// Check if all fields are properly set
foreach (StoreTreeNode storeNode in _userFolders.Values)
{
// Check modified folders
if (storeNode.IsDirty)
{
foreach(SharedFolder folder in storeNode.CurrentShares)
{
// Check if the send-as address has been resolved (or entered) correctly
if (folder.FlagSendAsOwner && string.IsNullOrWhiteSpace(folder.SendAsAddress))
{
// Select the node if we can
KTreeNode folderNode = storeNode.FindNode(folder);
if (folderNode != null)
{
// If the node is already selected, explicitly warn about the send-as address
// Otherwise, selecting it will pop up the warning
if (folderNode.IsSelected)
TryInitSendAsAddress();
else
FocusNode(folderNode, false);
}
return;
}
}
}
}
BusyText = Properties.Resources.SharedFolders_Applying_Label; BusyText = Properties.Resources.SharedFolders_Applying_Label;
KUITask.New((ctx) => KUITask.New((ctx) =>
{ {
@ -270,6 +299,10 @@ namespace Acacia.Features.SharedFolders
ctx.AddBusy(1); ctx.AddBusy(1);
++state.folders; ++state.folders;
// Check removed shares
_folders.RemoveSharesForStore(storeNode.User, storeNode.RemovedShares);
// Set shares
_folders.SetSharesForStore(storeNode.User, storeNode.CurrentShares, ctx.CancellationToken); _folders.SetSharesForStore(storeNode.User, storeNode.CurrentShares, ctx.CancellationToken);
} }
@ -343,17 +376,17 @@ namespace Acacia.Features.SharedFolders
foreach (StoreTreeNode storeNode in _userFolders.Values) foreach (StoreTreeNode storeNode in _userFolders.Values)
storeNode.ChangesApplied(); storeNode.ChangesApplied();
CheckDirty(); CheckDirty();
}
if (state.folders != 0) if (state.folders != 0)
{ {
// Sync account // Sync account
_account.Account.SendReceive(); _feature.Sync(_account);
// Show success // Show success
ShowCompletion(Properties.Resources.SharedFolders_Applying_Success); ShowCompletion(Properties.Resources.SharedFolders_Applying_Success);
} }
}
}, true) }, true)
.OnError((x) => .OnError((x) =>
{ {
@ -438,7 +471,8 @@ namespace Acacia.Features.SharedFolders
kTreeFolders.SelectNode(node, KTree.ScrollMode.Top); kTreeFolders.SelectNode(node, KTree.ScrollMode.Top);
// Start loading folders if requested // Start loading folders if requested
node.IsExpanded = expand; if (expand)
node.IsExpanded = true;
// Clear any selected user // Clear any selected user
gabLookup.SelectedUser = null; gabLookup.SelectedUser = null;
@ -837,25 +871,21 @@ namespace Acacia.Features.SharedFolders
private void TryInitSendAsAddress() private void TryInitSendAsAddress()
{ {
string email = null; // Initialise to the send-as address specified for an existing share, or a simple GAB lookup otherwise
/*_featureSendAs?.FindSendAsAddress(_account, null, string email =
_optionSendAsNodes[0].AvailableFolder.BackendId, _optionSendAsNodes[0].SharedFolder?.SendAsAddress ??
_optionSendAsNodes[0].AvailableFolder.Store;)*/ _featureSendAs?.FindSendAsAddress(_account, _optionSendAsNodes[0].AvailableFolder.Store);
if (email != null) if (!string.IsNullOrEmpty(email))
{ {
textSendAsAddress.Text = email; textSendAsAddress.Text = email;
if (!string.IsNullOrEmpty(email) && _optionSendAsNodes[0].IsShared)
{
_optionSendAsNodes[0].SharedFolder.SendAsAddress = email; _optionSendAsNodes[0].SharedFolder.SendAsAddress = email;
} }
}
else if (checkSendAs.Checked) else if (checkSendAs.Checked)
{ {
// TODO: resource string MessageBox.Show(Properties.Resources.SharedFolders_SendAsFailed_Label,
MessageBox.Show("Unable to determine the email address for the folder. " + Properties.Resources.SharedFolders_SendAsFailed_Title,
"Send-as will only work if you specify the email address manually.", MessageBoxButtons.OK, MessageBoxIcon.Information);
"Shared Folders", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
} }

View File

@ -8,6 +8,7 @@ using Acacia.ZPush;
using Acacia.ZPush.API.SharedFolders; using Acacia.ZPush.API.SharedFolders;
using System.Threading; using System.Threading;
using Acacia.Native.MAPI; using Acacia.Native.MAPI;
using Acacia.Features.SendAs;
namespace Acacia.Features.SharedFolders namespace Acacia.Features.SharedFolders
{ {
@ -51,6 +52,16 @@ namespace Acacia.Features.SharedFolders
#region API #region API
public void RemoveSharesForStore(GABUser store, ICollection<SharedFolder> removed)
{
foreach(SharedFolder folder in removed)
{
if (folder.SyncId != null)
_account.SetSendAsAddress(folder.SyncId, null);
_account.SetSendAsAddress(folder.BackendId, null);
}
}
/// <summary> /// <summary>
/// Sets all shares for the specified store. /// Sets all shares for the specified store.
/// </summary> /// </summary>
@ -94,6 +105,15 @@ namespace Acacia.Features.SharedFolders
); );
} }
// Patch in the send-as addresses
foreach (SharedFolder folder in shares)
{
if (folder.FlagSendAsOwner && string.IsNullOrWhiteSpace(folder.SendAsAddress))
{
folder.SendAsAddress = ThisAddIn.Instance.GetFeature<FeatureSendAs>()?.FindSendAsAddress(_account, folder);
}
}
// Commit changes // Commit changes
if (_query != null) if (_query != null)
_query.Commit(); _query.Commit();

View File

@ -74,7 +74,7 @@ namespace Acacia.Features.SharedFolders
this._initialShares = currentFolders; this._initialShares = currentFolders;
// Patch in send as address // Patch in send as address
foreach (SharedFolder share in _initialShares.Values) foreach (SharedFolder share in _initialShares.Values)
if (share.SendAsAddress == null) if (string.IsNullOrWhiteSpace(share.SendAsAddress))
share.SendAsAddress = sendAsAddress; share.SendAsAddress = sendAsAddress;
this._feature = folders.Feature; this._feature = folders.Feature;
this._featureSendAs = ThisAddIn.Instance.GetFeature<FeatureSendAs>(); this._featureSendAs = ThisAddIn.Instance.GetFeature<FeatureSendAs>();
@ -191,6 +191,20 @@ namespace Acacia.Features.SharedFolders
} }
} }
internal ICollection<SharedFolder> RemovedShares
{
get
{
List<SharedFolder> removed = new List<SharedFolder>();
foreach(SharedFolder folder in _initialShares.Values)
{
if (!_currentShares.ContainsKey(folder.BackendId))
removed.Add(folder);
}
return removed;
}
}
internal string DefaultNameForFolder(AvailableFolder folder) internal string DefaultNameForFolder(AvailableFolder folder)
{ {
// Default include the store name in root folders // Default include the store name in root folders
@ -239,6 +253,7 @@ namespace Acacia.Features.SharedFolders
state = state.WithName(DefaultNameForFolder(folder)); state = state.WithName(DefaultNameForFolder(folder));
} }
if (string.IsNullOrWhiteSpace(state.SendAsAddress))
state = state.WithSendAsAddress(_sendAsAddress); state = state.WithSendAsAddress(_sendAsAddress);
return state; return state;
} }

View File

@ -1166,6 +1166,24 @@ namespace Acacia.Properties {
} }
} }
/// <summary>
/// Looks up a localized string similar to Unable to determine the email address for the folder. Send-as will only work if you specify the email address manually..
/// </summary>
internal static string SharedFolders_SendAsFailed_Label {
get {
return ResourceManager.GetString("SharedFolders_SendAsFailed_Label", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Shared Folders.
/// </summary>
internal static string SharedFolders_SendAsFailed_Title {
get {
return ResourceManager.GetString("SharedFolders_SendAsFailed_Title", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to There are unsaved changes. Do you really want to to discard these?. /// Looks up a localized string similar to There are unsaved changes. Do you really want to to discard these?.
/// </summary> /// </summary>

View File

@ -530,4 +530,10 @@ Please contact your system administrator for any required changes.</value>
<data name="SharedFolders_WholeStoreRestart_Title" xml:space="preserve"> <data name="SharedFolders_WholeStoreRestart_Title" xml:space="preserve">
<value>Open stores</value> <value>Open stores</value>
</data> </data>
<data name="SharedFolders_SendAsFailed_Label" xml:space="preserve">
<value>Unable to determine the email address for the folder. Send-as will only work if you specify the email address manually.</value>
</data>
<data name="SharedFolders_SendAsFailed_Title" xml:space="preserve">
<value>Shared Folders</value>
</data>
</root> </root>

View File

@ -58,5 +58,11 @@ namespace Acacia.Stubs
string RegistryBaseKey { get; } string RegistryBaseKey { get; }
void SetAccountProp(PropTag prop, object value); void SetAccountProp(PropTag prop, object value);
string this[string name]
{
get;
set;
}
} }
} }

View File

@ -147,7 +147,7 @@ namespace Acacia.ZPush.API.SharedFolders
{ {
SoapData newData = _data; SoapData newData = _data;
newData.name = name; newData.name = name;
return new SharedFolder(newData); return DoClone(newData);
} }
#endregion #endregion
@ -163,6 +163,11 @@ namespace Acacia.ZPush.API.SharedFolders
{ {
SoapData newData = _data; SoapData newData = _data;
newData.flags = flags; newData.flags = flags;
return DoClone(newData);
}
private SharedFolder DoClone(SoapData newData)
{
SharedFolder clone = new SharedFolder(newData); SharedFolder clone = new SharedFolder(newData);
clone.SendAsAddress = SendAsAddress; clone.SendAsAddress = SendAsAddress;
return clone; return clone;

View File

@ -390,26 +390,27 @@ namespace Acacia.ZPush
#region Send as #region Send as
private const string PREFIX_SEND_AS = "KOE SendAs "; private const string PREFIX_SEND_AS_SYNC = "KOE SendAs Sync ";
private const string PREFIX_SEND_AS_BACKEND = "KOE SendAs Backend ";
public void SetSendAsAddress(BackendId id, string sendAsAddress) public void SetSendAsAddress(BackendId id, string sendAsAddress)
{ {
RegistryUtil.SetValueString(Account.RegistryBaseKey, PREFIX_SEND_AS + id.ToString(), sendAsAddress); _account[PREFIX_SEND_AS_BACKEND + id] = sendAsAddress;
} }
public string GetSendAsAddress(BackendId id) public string GetSendAsAddress(BackendId id)
{ {
return RegistryUtil.GetValueString(Account.RegistryBaseKey, PREFIX_SEND_AS + id.ToString(), null); return _account[PREFIX_SEND_AS_BACKEND + id];
} }
public void SetSendAsAddress(SyncId id, string sendAsAddress) public void SetSendAsAddress(SyncId id, string sendAsAddress)
{ {
RegistryUtil.SetValueString(Account.RegistryBaseKey, PREFIX_SEND_AS + id.ToString(), sendAsAddress); _account[PREFIX_SEND_AS_SYNC + id] = sendAsAddress;
} }
public string GetSendAsAddress(SyncId id) public string GetSendAsAddress(SyncId id)
{ {
return RegistryUtil.GetValueString(Account.RegistryBaseKey, PREFIX_SEND_AS + id.ToString(), null); return _account[PREFIX_SEND_AS_SYNC + id];
} }
#endregion #endregion

View File

@ -282,6 +282,11 @@ namespace Acacia.ZPush
_started = true; _started = true;
} }
public void Resync()
{
LastSyncTime = new DateTime();
}
/// <summary> /// <summary>
/// Delegate for an account-specific task. /// Delegate for an account-specific task.
/// </summary> /// </summary>