Skip to content

Commit

Permalink
Stopped storing MAPIFolder in Outlook.Calendar class.
Browse files Browse the repository at this point in the history
GetFolderByID() function to find MAPIFolder by profile entryID.
Make sure using the correct Engine profile during sync.
#1307
  • Loading branch information
phw198 committed Oct 27, 2021
1 parent 605c900 commit 7fb7c1c
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 61 deletions.
24 changes: 15 additions & 9 deletions src/OutlookGoogleCalendarSync/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ private void ddMailboxName_SelectedIndexChanged(object sender, EventArgs e) {

public void cbOutlookCalendar_SelectedIndexChanged(object sender, EventArgs e) {
KeyValuePair<String, MAPIFolder> calendar = (KeyValuePair<String, MAPIFolder>)cbOutlookCalendars.SelectedItem;
OutlookOgcs.Calendar.Instance.UseOutlookCalendar = calendar.Value;
ActiveCalendarProfile.UseOutlookCalendar = new OutlookCalendarListEntry(calendar.Value);

log.Warn("Outlook calendar selection changed to: " + ActiveCalendarProfile.UseOutlookCalendar.ToString());
}
Expand Down Expand Up @@ -1263,14 +1263,20 @@ private void tbOutlookDateFormat_Leave(object sender, EventArgs e) {

private void btTestOutlookFilter_Click(object sender, EventArgs e) {
log.Debug("Testing the Outlook filter string.");
int filterCount = OutlookOgcs.Calendar.Instance.FilterCalendarEntries(this.ActiveCalendarProfile, OutlookOgcs.Calendar.Instance.UseOutlookCalendar.Items, false).Count();
OutlookOgcs.Calendar.Disconnect(true);
String msg = "The format '" + tbOutlookDateFormat.Text + "' returns " + filterCount + " calendar items within the date range ";
msg += ActiveCalendarProfile.SyncStart.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern);
msg += " and " + ActiveCalendarProfile.SyncEnd.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern);

log.Info(msg);
OgcsMessageBox.Show(msg, "Date-Time Format Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
try {
MAPIFolder calendar = OutlookOgcs.Calendar.Instance.IOutlook.GetFolderByID(this.ActiveCalendarProfile.UseOutlookCalendar.Id+"1");
int filterCount = OutlookOgcs.Calendar.Instance.FilterCalendarEntries(this.ActiveCalendarProfile, false).Count();
OutlookOgcs.Calendar.Disconnect(true);
String msg = "The format '" + tbOutlookDateFormat.Text + "' returns " + filterCount + " calendar items within the date range ";
msg += ActiveCalendarProfile.SyncStart.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern);
msg += " and " + ActiveCalendarProfile.SyncEnd.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern);

log.Info(msg);
OgcsMessageBox.Show(msg, "Date-Time Format Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
} catch (System.Exception ex) {
OGCSexception.Analyse("Profile '" + Settings.Profile.Name(ActiveCalendarProfile) + "', calendar ID " + ActiveCalendarProfile.UseOutlookCalendar.Id, ex);
OgcsMessageBox.Show(ex.Message, "Unable to perform test", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void urlDateFormats_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
Expand Down
10 changes: 5 additions & 5 deletions src/OutlookGoogleCalendarSync/GoogleOgcs/CustomProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static String metadataIdKeyName(MetadataId id) {

//For backward compatibility, always default to key names with no set number appended
if (calendarKeys.Count == 0||
(calendarKeys.Count == 1 && calendarKeys.ContainsKey(calendarKeyName)) && calendarKeys[calendarKeyName] == OutlookOgcs.Calendar.Instance.UseOutlookCalendar.EntryID)
(calendarKeys.Count == 1 && calendarKeys.ContainsKey(calendarKeyName)) && calendarKeys[calendarKeyName] == Sync.Engine.Calendar.Instance.Profile.UseOutlookCalendar.Id)
{
maxSet = -1;
return returnVal;
Expand All @@ -156,7 +156,7 @@ private static String metadataIdKeyName(MetadataId id) {
if (matches[0].Groups[1].Value != "")
appendedNos = Convert.ToInt16(matches[0].Groups[1].Value);
if (appendedNos - maxSet == 1) maxSet = appendedNos;
if (kvp.Value == OutlookOgcs.Calendar.Instance.UseOutlookCalendar.EntryID)
if (kvp.Value == Sync.Engine.Calendar.Instance.Profile.UseOutlookCalendar.Id)
returnSet = (matches[0].Groups[1].Value == "") ? "0" : matches[0].Groups[1].Value;
}
}
Expand Down Expand Up @@ -198,9 +198,9 @@ public static Boolean Exists(Event ev, MetadataId searchId, out String searchKey
int? keySet = getKeySet(ev, out maxSet);
if (keySet.HasValue && keySet.Value != 0) searchKey += "-" + keySet.Value.ToString("D2");
if (searchId == MetadataId.oCalendarId)
return ev.ExtendedProperties.Private__.ContainsKey(searchKey) && ev.ExtendedProperties.Private__[searchKey] == OutlookOgcs.Calendar.Instance.UseOutlookCalendar.EntryID;
return ev.ExtendedProperties.Private__.ContainsKey(searchKey) && ev.ExtendedProperties.Private__[searchKey] == Sync.Engine.Calendar.Instance.Profile.UseOutlookCalendar.Id;
else
return ev.ExtendedProperties.Private__.ContainsKey(searchKey) && Get(ev, MetadataId.oCalendarId) == OutlookOgcs.Calendar.Instance.UseOutlookCalendar.EntryID;
return ev.ExtendedProperties.Private__.ContainsKey(searchKey) && Get(ev, MetadataId.oCalendarId) == Sync.Engine.Calendar.Instance.Profile.UseOutlookCalendar.Id;
}

public static Boolean ExistsAny(Event ev) {
Expand All @@ -214,7 +214,7 @@ public static Boolean ExistsAny(Event ev) {
/// Add the Outlook appointment IDs into Google event.
/// </summary>
public static void AddOutlookIDs(ref Event ev, AppointmentItem ai) {
Add(ref ev, MetadataId.oCalendarId, OutlookOgcs.Calendar.Instance.UseOutlookCalendar.EntryID);
Add(ref ev, MetadataId.oCalendarId, Sync.Engine.Calendar.Instance.Profile.UseOutlookCalendar.Id);
Add(ref ev, MetadataId.oEntryId, ai.EntryID);
Add(ref ev, MetadataId.oGlobalApptId, OutlookOgcs.Calendar.Instance.IOutlook.GetGlobalApptID(ai));
CustomProperty.LogProperties(ev, log4net.Core.Level.Debug);
Expand Down
4 changes: 2 additions & 2 deletions src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ public void IdentifyEventDifferences(
}
}
if (!foundMatch && profile.MergeItems &&
GoogleOgcs.CustomProperty.Get(google[g], CustomProperty.MetadataId.oCalendarId) != OutlookOgcs.Calendar.Instance.UseOutlookCalendar.EntryID)
GoogleOgcs.CustomProperty.Get(google[g], CustomProperty.MetadataId.oCalendarId) != profile.UseOutlookCalendar.Id)
google.Remove(google[g]);

} else if (profile.MergeItems) {
Expand Down Expand Up @@ -1278,7 +1278,7 @@ public static Boolean ItemIDsMatch(ref Event ev, AppointmentItem ai) {
{
log.Fine("Comparing Outlook CalendarID");
gCompareID = CustomProperty.Get(ev, CustomProperty.MetadataId.oCalendarId);
if (gCompareID == OutlookOgcs.Calendar.Instance.UseOutlookCalendar.EntryID) {
if (gCompareID == profile.UseOutlookCalendar.Id) {

//But...if an appointment is copied within ones own calendar, the DATA part is the same (only the creation time changes)!
//So now compare the Entry ID too.
Expand Down
40 changes: 18 additions & 22 deletions src/OutlookGoogleCalendarSync/OutlookOgcs/OutlookCalendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ public static Calendar Instance {
public static Boolean OOMsecurityInfo = false;
private static List<String> alreadyRedirectedToWikiForComError = new List<String>();
public const String GlobalIdPattern = "040000008200E00074C5B7101A82E008";
public MAPIFolder UseOutlookCalendar {
get { return IOutlook.UseOutlookCalendar(); }
set {
IOutlook.UseOutlookCalendar(value);
Forms.Main.Instance.ActiveCalendarProfile.UseOutlookCalendar = new OutlookCalendarListEntry(value);
}
}
public Folders Folders {
get { return IOutlook.Folders(); }
}
Expand Down Expand Up @@ -104,7 +97,7 @@ public static void Disconnect(Boolean onlyWhenNoGUI = false) {
public List<AppointmentItem> GetCalendarEntriesInRange(SettingsStore.Calendar profile, Boolean suppressAdvisories) {
List<AppointmentItem> filtered = new List<AppointmentItem>();
try {
filtered = FilterCalendarEntries(profile, UseOutlookCalendar.Items, suppressAdvisories: suppressAdvisories);
filtered = FilterCalendarEntries(profile, suppressAdvisories: suppressAdvisories);
} catch (System.Runtime.InteropServices.InvalidComObjectException ex) {
if (OGCSexception.GetErrorCode(ex) == "0x80131527") { //COM object separated from underlying RCW
log.Warn(ex.Message);
Expand All @@ -120,18 +113,10 @@ public List<AppointmentItem> GetCalendarEntriesInRange(SettingsStore.Calendar pr
}
throw;

} catch (System.NullReferenceException ex) {
if (Instance.UseOutlookCalendar == null) {
OGCSexception.LogAsFail(ref ex);
OGCSexception.Analyse(ex);
OutlookOgcs.Calendar.Instance.Reset();
filtered = FilterCalendarEntries(profile, Instance.UseOutlookCalendar.Items, suppressAdvisories: suppressAdvisories);
} else throw;

} catch (System.ArgumentNullException ex) {
OGCSexception.Analyse("It seems that Outlook has just been closed.", OGCSexception.LogAsFail(ex));
OutlookOgcs.Calendar.Instance.Reset();
filtered = FilterCalendarEntries(profile, Instance.UseOutlookCalendar.Items, suppressAdvisories: suppressAdvisories);
filtered = FilterCalendarEntries(profile, suppressAdvisories: suppressAdvisories);

} catch (System.Exception) {
if (!suppressAdvisories) Forms.Main.Instance.Console.Update("Unable to access the Outlook calendar.", Console.Markup.error);
Expand All @@ -140,16 +125,27 @@ public List<AppointmentItem> GetCalendarEntriesInRange(SettingsStore.Calendar pr
return filtered;
}

public List<AppointmentItem> FilterCalendarEntries(SettingsStore.Calendar profile, Items OutlookItems, Boolean filterBySettings = true,
public List<AppointmentItem> FilterCalendarEntries(SettingsStore.Calendar profile, Boolean filterBySettings = true,
Boolean noDateFilter = false, String extraFilter = "", Boolean suppressAdvisories = false) {
//Filtering info @ https://msdn.microsoft.com/en-us/library/cc513841%28v=office.12%29.aspx

List<AppointmentItem> result = new List<AppointmentItem>();
Items OutlookItems = null;

if (profile is null)
profile = Settings.Profile.InPlay();

try {
MAPIFolder thisUseOutlookCalendar = IOutlook.GetFolderByID(profile.UseOutlookCalendar.Id);
OutlookItems = thisUseOutlookCalendar.Items;
} catch {
log.Fail("Could not open '" + Settings.Profile.Name(profile) + "' profile calendar folder with ID " + profile.UseOutlookCalendar.Id);
throw;
}

if (OutlookItems != null) {
log.Fine(OutlookItems.Count + " calendar items exist.");
if (profile is null)
profile = Settings.Profile.InPlay();


OutlookItems.Sort("[Start]", Type.Missing);
OutlookItems.IncludeRecurrences = false;

Expand Down Expand Up @@ -1265,7 +1261,7 @@ private static string exportToCSV(AppointmentItem ai) {
csv.Append(ai.ReminderSet + ",");
csv.Append(ai.ReminderMinutesBeforeStart.ToString() + ",");
csv.Append(OutlookOgcs.Calendar.Instance.IOutlook.GetGlobalApptID(ai) + ",");
csv.Append(ai.EntryID + "," + OutlookOgcs.Calendar.instance.UseOutlookCalendar.EntryID + ",");
csv.Append(ai.EntryID + "," + Sync.Engine.Calendar.Instance.Profile.UseOutlookCalendar.Id + ",");
csv.Append((CustomProperty.Get(ai, CustomProperty.MetadataId.gEventID) ?? "") + ",");
csv.Append((CustomProperty.Get(ai, CustomProperty.MetadataId.gCalendarId) ?? "") + ",");
csv.Append(CustomProperty.GetOGCSlastModified(ai).ToString() + ",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public interface Interface {
/// <param name="filter">The logic by which to perform filter</param>
/// <returns>Filtered items</returns>
List<Object> FilterItems(Items outlookItems, String filter);
MAPIFolder GetFolderByID(String entryID);
void GetAppointmentByID(String entryID, out AppointmentItem ai);
}
}
20 changes: 17 additions & 3 deletions src/OutlookGoogleCalendarSync/OutlookOgcs/OutlookNew.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,24 @@ public List<Object> FilterItems(Items outlookItems, String filter) {
return restrictedItems;
}

public MAPIFolder GetFolderByID(String entryID) {
NameSpace ns = null;
try {
ns = oApp.GetNamespace("mapi");
return ns.GetFolderFromID(entryID);
} finally {
ns = (NameSpace)OutlookOgcs.Calendar.ReleaseObject(ns);
}
}

public void GetAppointmentByID(String entryID, out AppointmentItem ai) {
NameSpace ns = oApp.GetNamespace("mapi");
ai = ns.GetItemFromID(entryID) as AppointmentItem;
ns = (NameSpace)OutlookOgcs.Calendar.ReleaseObject(ns);
NameSpace ns = null;
try {
ns = oApp.GetNamespace("mapi");
ai = ns.GetItemFromID(entryID) as AppointmentItem;
} finally {
ns = (NameSpace)OutlookOgcs.Calendar.ReleaseObject(ns);
}
}

public String GetRecipientEmail(Recipient recipient) {
Expand Down
21 changes: 17 additions & 4 deletions src/OutlookGoogleCalendarSync/OutlookOgcs/OutlookOld.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Google.Apis.Calendar.v3.Data;
using log4net;
using Microsoft.Office.Interop.Outlook;
using NodaTime;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -409,10 +408,24 @@ public List<Object> FilterItems(Items outlookItems, String filter) {
return restrictedItems;
}

public MAPIFolder GetFolderByID(String entryID) {
NameSpace ns = null;
try {
ns = oApp.GetNamespace("mapi");
return ns.GetFolderFromID(entryID);
} finally {
ns = (NameSpace)OutlookOgcs.Calendar.ReleaseObject(ns);
}
}

public void GetAppointmentByID(String entryID, out AppointmentItem ai) {
NameSpace ns = oApp.GetNamespace("mapi");
ai = ns.GetItemFromID(entryID) as AppointmentItem;
ns = (NameSpace)OutlookOgcs.Calendar.ReleaseObject(ns);
NameSpace ns = null;
try {
oApp.GetNamespace("mapi");
ai = ns.GetItemFromID(entryID) as AppointmentItem;
} finally {
ns = (NameSpace)OutlookOgcs.Calendar.ReleaseObject(ns);
}
}

public String GetRecipientEmail(Recipient recipient) {
Expand Down
4 changes: 2 additions & 2 deletions src/OutlookGoogleCalendarSync/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ public static Boolean CalledByProcess(String callingProcessNames) {
return false;
}

/*public static void StackTraceToString() {
public static void StackTraceToString() {
try {
String stackString = "";
List<System.Diagnostics.StackFrame> stackFrames = new System.Diagnostics.StackTrace().GetFrames().ToList();
Expand All @@ -567,6 +567,6 @@ public static Boolean CalledByProcess(String callingProcessNames) {
} catch (System.Exception ex) {
OGCSexception.Analyse(ex);
}
}*/
}
}
}
2 changes: 1 addition & 1 deletion src/OutlookGoogleCalendarSync/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion("2.9.*")]
[assembly: AssemblyFileVersion("2.9.1.10")]
[assembly: AssemblyFileVersion("2.9.1.11")]

[assembly: AssemblyMetadata("SquirrelAwareVersion", "1")]
13 changes: 10 additions & 3 deletions src/OutlookGoogleCalendarSync/SettingsStore/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,21 @@ public static String Name(Object settingsStore) {
/// </summary>
/// <returns>Currently hard-coded to a Calendar profile</returns>
public static SettingsStore.Calendar InPlay() {
SettingsStore.Calendar aProfile = null;
SettingsStore.Calendar aProfile;

if (Program.CalledByProcess("OnTick")) {
if (Program.CalledByProcess("manualSynchronize,updateGUIsettings,UpdateGUIsettings_Profile,miCatRefresh_Click," +
"GetMyGoogleCalendars_Click,btColourMap_Click,ColourPicker_Enter,ddGoogleColour_SelectedIndexChanged")) {
aProfile = Forms.Main.Instance.ActiveCalendarProfile;
log.Fine("Using profile Forms.Main.Instance.ActiveCalendarProfile");

} else if (Program.CalledByProcess("synchronize,OnTick")) {
aProfile = Sync.Engine.Calendar.Instance.Profile;
log.Fine("Using profile Sync.Engine.Calendar.Instance.Profile");

} else {
Program.StackTraceToString();
log.Error("Unknown profile being referenced.");
aProfile = Forms.Main.Instance.ActiveCalendarProfile;
log.Fine("Using profile Forms.Main.Instance.ActiveCalendarProfile");
}
return aProfile;
}
Expand Down
9 changes: 7 additions & 2 deletions src/OutlookGoogleCalendarSync/Sync/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Calendar() {
Profile = Sync.Engine.Instance.ActiveProfile as SettingsStore.Calendar;
}

public void StartSync(Boolean updateSyncSchedule = true) {
public void StartSync(Boolean manualIgnition, Boolean updateSyncSchedule = true) {
Forms.Main mainFrm = Forms.Main.Instance;
mainFrm.bSyncNow.Text = "Stop Sync";
mainFrm.NotificationTray.UpdateItem("sync", "&Stop Sync");
Expand Down Expand Up @@ -121,7 +121,7 @@ public void StartSync(Boolean updateSyncSchedule = true) {
delegate (object o, DoWorkEventArgs args) {
BackgroundWorker b = o as BackgroundWorker;
try {
syncResult = synchronize();
syncResult = manualIgnition ? manualSynchronize() : synchronize();
} catch (System.Exception ex) {
String hResult = OGCSexception.GetErrorCode(ex);

Expand Down Expand Up @@ -264,6 +264,11 @@ private void skipCorruptedItem(ref List<AppointmentItem> outlookEntries, Appoint
}
}

private SyncResult manualSynchronize() {
//This function is just a shim to determine how the sync was triggered when looking at the call stack
return synchronize();
}

private SyncResult synchronize() {
Console console = Forms.Main.Instance.Console;
console.Update("Finding Calendar Entries", Console.Markup.mag_right, newLine: false);
Expand Down
Loading

0 comments on commit 7fb7c1c

Please sign in to comment.