Skip to content

Commit

Permalink
Some progress made, but potential version mess.
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-brooke committed Dec 1, 2017
1 parent a4717e0 commit e21299c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Expand Up @@ -65,8 +65,8 @@ SuiteCRMAddIn/Documentation/latex/

*.pcapng

*.vsp
*.xlsx

*.psess

*.xlsx
*.vsp
47 changes: 2 additions & 45 deletions SuiteCRMAddIn/BusinessLogic/AppointmentSyncState.cs
Expand Up @@ -29,6 +29,8 @@ namespace SuiteCRMAddIn.BusinessLogic
using Outlook = Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using SuiteCRMClient.Logging;

public class AppointmentSyncState: SyncState<Outlook.AppointmentItem>
{
Expand Down Expand Up @@ -87,51 +89,6 @@ public override void DeleteItem()
this.OutlookItem.Delete();
}


/// <summary>
/// An appointment may be changed because its recipients have changed.
/// </summary>
/// <returns>True if the underlying item has changed, or its recipients have changed.</returns>
protected override bool ReallyChanged()
{
var result = base.ReallyChanged();

ProtoItem older = this.Cache as ProtoAppointment;

if (older != null)
{
var currentAddresses = new HashSet<string>();
var olderAddresses = new List<string>();
olderAddresses.AddRange(((ProtoAppointment)older).RecipientAddresses);

foreach (Outlook.Recipient recipient in olItem.Recipients)
{
currentAddresses.Add(recipient.GetSmtpAddress());
}
if (currentAddresses.Count == olderAddresses.Count)
{
var sorted = new List<string>();
sorted.AddRange(currentAddresses);
sorted.Sort();

for (int i = 0; i < sorted.Count; i++)
{
if (sorted[i] != olderAddresses[i])
{
result = true;
break;
}
}
}
else
{
result = true;
}
}

return result;
}

/// <summary>
/// Construct a JSON-serialisable representation of my appointment item.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions SuiteCRMAddIn/BusinessLogic/ContactSyncState.cs
Expand Up @@ -26,6 +26,8 @@ namespace SuiteCRMAddIn.BusinessLogic
using SuiteCRMAddIn.ProtoItems;
using Extensions;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;
using SuiteCRMClient.Logging;

/// <summary>
/// A SyncState for Contact items.
Expand Down Expand Up @@ -54,6 +56,7 @@ public override string Description
}
}


/// <summary>
/// Don't actually delete contact items from Outlook; instead, mark them private so they
/// don't get copied back to CRM.
Expand Down
9 changes: 6 additions & 3 deletions SuiteCRMAddIn/BusinessLogic/SyncState.cs
Expand Up @@ -69,20 +69,23 @@ public bool IsDeletedInOutlook
{
get
{
bool result;
if (_wasDeleted) return true;
// TODO: Make this logic more robust. Perhaps check HRESULT of COMException?
try
{
// Has the side-effect of throwing an exception if the item has been deleted:
var entryId = OutlookItemEntryId;
return false;
result = false;
}
catch (COMException com)
{
Globals.ThisAddIn.Log.Debug($"Object has probably been deleted: {com.ErrorCode}, {com.Message}");
Globals.ThisAddIn.Log.Debug($"Object has probably been deleted: {com.ErrorCode}, {com.Message}; HResult {com.HResult}");
_wasDeleted = true;
return true;
result = true;
}

return result;
}
}

Expand Down
1 change: 1 addition & 0 deletions SuiteCRMAddIn/BusinessLogic/SyncState{1}.cs
Expand Up @@ -25,6 +25,7 @@ namespace SuiteCRMAddIn.BusinessLogic
using SuiteCRMAddIn.ProtoItems;
using SuiteCRMClient.Logging;
using System;
using System.Runtime.InteropServices;

/// <summary>
/// The sync state of an item of the specified type.
Expand Down
42 changes: 34 additions & 8 deletions SuiteCRMAddIn/BusinessLogic/Synchroniser.cs
Expand Up @@ -786,18 +786,44 @@ protected void EnsureSynchronisationPropertiesForOutlookItem(OutlookItemType olI
/// <param name="value">The value.</param>
protected abstract void EnsureSynchronisationPropertyForOutlookItem(OutlookItemType olItem, string name, string value);

/// <summary>
/// Returns true iff user is currently focussed on this (Contacts/Appointments/Tasks) tab.
/// </summary>
/// <remarks>
/// This is used in determining whether an item is in fact newly created by the user;
/// it has a certain code smell to it.
/// </remarks>
protected abstract bool IsCurrentView { get; }

/// <summary>
/// Returns true iff local (Outlook) deletions should be propagated to the server.
/// </summary>
/// <remarks>TODO: Why should this ever be false?</remarks>
protected abstract bool PropagatesLocalDeletions { get; }

/// <summary>
/// Deal, in CRM, with items deleted in Outlook.
/// </summary>
protected void RemoveDeletedItems()
{
// Make a copy of the list to avoid mutation error while iterating:
var syncStatesCopy = new List<SyncState<OutlookItemType>>(ItemsSyncState);
foreach (var syncState in syncStatesCopy)
if (IsCurrentView && PropagatesLocalDeletions)
{
var shouldDeleteFromCrm = syncState.IsDeletedInOutlook || !syncState.ShouldSyncWithCrm;
if (shouldDeleteFromCrm) RemoveFromCrm(syncState);
if (syncState.IsDeletedInOutlook) ItemsSyncState.Remove(syncState);
// Make a copy of the list to avoid mutation error while iterating:
var syncStatesCopy = new List<SyncState<OutlookItemType>>(ItemsSyncState);
foreach (var syncState in syncStatesCopy)
{
var shouldDeleteFromCrm = syncState.IsDeletedInOutlook || !syncState.ShouldSyncWithCrm;
if (shouldDeleteFromCrm) RemoveFromCrm(syncState);
if (syncState.IsDeletedInOutlook) ItemsSyncState.Remove(syncState);
}
}
else
{
var items = ItemsSyncState.Where(x => x.IsDeletedInOutlook).Count();
if (items > 0)
{
Log.Error($"Possibly bug #95: was attempting to delete {items} items from CRM");
}
}
}

Expand Down Expand Up @@ -1071,7 +1097,7 @@ protected virtual void OutlookItemAdded(OutlookItemType olItem)
{
lock (enqueueingLock)
{
if (this.GetExistingSyncState(olItem) == null)
if (IsCurrentView && this.GetExistingSyncState(olItem) == null)
{
SyncState<OutlookItemType> state = this.AddOrGetSyncState(olItem);
DaemonWorker.Instance.AddTask(new TransmitNewAction<OutlookItemType>(this, state, this.DefaultCrmModule));
Expand Down Expand Up @@ -1179,7 +1205,7 @@ protected void RemoveItemSyncState(SyncState<OutlookItemType> item)
/// <returns>True if this synchroniser relates to the current tab and the timing logic is satisfied.</returns>
protected bool ShouldPerformSyncNow(SyncState<OutlookItemType> syncState)
{
return (syncState.ShouldPerformSyncNow());
return (IsCurrentView && syncState.ShouldPerformSyncNow());
}
}

Expand Down
2 changes: 2 additions & 0 deletions SuiteCRMAddIn/BusinessLogic/TaskSyncState.cs
Expand Up @@ -26,6 +26,7 @@ namespace SuiteCRMAddIn.BusinessLogic
using ProtoItems;
using Extensions;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;

/// <summary>
/// A SyncState for Contact items.
Expand All @@ -52,6 +53,7 @@ public override string Description
}
}


public override void DeleteItem()
{
this.OutlookItem.Delete();
Expand Down

0 comments on commit e21299c

Please sign in to comment.