Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…alendarSync into release

Resolves #1389
  • Loading branch information
phw198 committed Apr 3, 2022
2 parents 1d9defb + 443e5d9 commit d46787a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 27 deletions.
8 changes: 7 additions & 1 deletion src/OutlookGoogleCalendarSync/Extensions/ColourPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ public void AddStandardColours() {
/// Add just the colours associated with categories
/// </summary>
public void AddCategoryColours() {
if (OutlookOgcs.Factory.OutlookVersionName != OutlookOgcs.Factory.OutlookVersionNames.Outlook2003)
if (OutlookOgcs.Factory.OutlookVersionName == OutlookOgcs.Factory.OutlookVersionNames.Outlook2003) return;

try {
Items.AddRange(OutlookOgcs.Calendar.Categories.DropdownItems().ToArray());
} catch {
OutlookOgcs.Calendar.Categories.ValidateCategories();
Items.AddRange(OutlookOgcs.Calendar.Categories.DropdownItems().ToArray());
}
}

public void ColourPicker_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) {
Expand Down
14 changes: 10 additions & 4 deletions src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ private Event createCalendarEntry(AppointmentItem ai) {
ev.Location = ai.Location;
ev.Visibility = getPrivacy(ai.Sensitivity, null);
ev.Transparency = getAvailability(ai.BusyStatus, null);
ev.ColorId = getColour(ai.Categories, null).Id;
ev.ColorId = getColour(ai.Categories, null)?.Id ?? EventColour.Palette.NullPalette.Id;

ev.Attendees = new List<Google.Apis.Calendar.v3.Data.EventAttendee>();
if (profile.AddAttendees && ai.Recipients.Count > 1 && !APIlimitReached_attendee) { //Don't add attendees if there's only 1 (me)
Expand Down Expand Up @@ -770,8 +770,12 @@ public Event UpdateCalendarEntry(AppointmentItem ai, Event ev, ref int itemModif
if (profile.AddColours || profile.SetEntriesColour) {
EventColour.Palette gColour = this.ColourPalette.GetColour(ev.ColorId);
EventColour.Palette oColour = getColour(ai.Categories, gColour);
if (Sync.Engine.CompareAttribute("Colour", Sync.Direction.OutlookToGoogle, gColour.Name, oColour.Name, sb, ref itemModified)) {
ev.ColorId = oColour.Id;
if (!string.IsNullOrEmpty(ai.Categories) && oColour == null)
log.Warn("Not comparing colour as there is a problem with the mapping.");
else {
if (Sync.Engine.CompareAttribute("Colour", Sync.Direction.OutlookToGoogle, gColour.Name, oColour.Name, sb, ref itemModified)) {
ev.ColorId = oColour.Id;
}
}
}

Expand Down Expand Up @@ -1595,7 +1599,9 @@ private EventColour.Palette getColour(String aiCategories, EventColour.Palette g
} else {
getOutlookCategoryColour(aiCategories, ref categoryColour);
}
if (categoryColour == null || categoryColour == OlCategoryColor.olCategoryColorNone)
if (categoryColour == null)
return null;
else if (categoryColour == OlCategoryColor.olCategoryColorNone)
return EventColour.Palette.NullPalette;
else
return GetColour((OlCategoryColor)categoryColour);
Expand Down
62 changes: 40 additions & 22 deletions src/OutlookGoogleCalendarSync/OutlookOgcs/OutlookCategories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookGoogleCalendarSync.OutlookOgcs {
Expand Down Expand Up @@ -155,25 +156,46 @@ public void BuildPicker(ref System.Windows.Forms.CheckedListBox clb) {
/// <returns>The Outlook category type</returns>
public Outlook.OlCategoryColor? OutlookColour(String categoryName) {
if (string.IsNullOrEmpty(categoryName)) log.Warn("Category name is empty.");

try {
if (this.categories != null && this.categories.Count > 0) { }
} catch (System.Exception ex) {
OGCSexception.Analyse("Categories are not accessible!", OGCSexception.LogAsFail(ex));
this.categories = null;
}

if (this.categories == null || OutlookOgcs.Calendar.Categories == null) {
OutlookOgcs.Calendar.Instance.IOutlook.RefreshCategories();
this.categories = Calendar.Categories.categories;
}
foreach (Outlook.Category category in this.categories) {
if (category.Name == categoryName.Trim()) return category.Color;
}

foreach (Outlook.Category category in this.categories) {
if (category.Name == categoryName.Trim()) return category.Color;
}
log.Warn("Could not convert category name '" + categoryName + "' into Outlook category type.");
return null;
}

log.Warn("Could not convert category name '" + categoryName + "' into Outlook category type.");
return null;
/// <summary>
/// Check all the Outlook categories can still be accessed. If not, refresh them.
/// </summary>
public void ValidateCategories() {
try {
if (this.categories != null && this.categories.Count > 0) { }
} catch (System.Exception ex) {
OGCSexception.Analyse("Categories are not accessible!", OGCSexception.LogAsFail(ex));
this.categories = null;
}
if (this.categories == null || OutlookOgcs.Calendar.Categories == null) {
OutlookOgcs.Calendar.Instance.IOutlook.RefreshCategories();
} else {
String catName = "";
try {
foreach (Outlook.Category cat in this.categories) {
catName = cat.Name;
Outlook.OlCategoryColor catColour = cat.Color;
}
} catch (System.Exception ex) {
if (OGCSexception.GetErrorCode(ex, 0x0000FFFF) == "0x00004005" && ex.TargetSite.Name == "get_Color") {
log.Warn("Outlook category '" + catName + "' seems to have changed!");
OutlookOgcs.Calendar.Instance.IOutlook.RefreshCategories();
} else {
OGCSexception.Analyse("Could not access all the Outlook categories.", ex);
this.categories = null;
return;
}
}
}
this.categories = Calendar.Categories.categories;
}

/// <summary>
Expand All @@ -195,10 +217,6 @@ private List<String> getNames() {
/// <returns>List to be used in dropdown, for example</returns>
public List<OutlookOgcs.Categories.ColourInfo> DropdownItems() {
List<OutlookOgcs.Categories.ColourInfo> items = new List<OutlookOgcs.Categories.ColourInfo>();
if (this.categories == null) {
OutlookOgcs.Calendar.Instance.IOutlook.RefreshCategories();
this.categories = Calendar.Categories.categories;
}
if (this.categories != null) {
foreach (Outlook.Category category in this.categories) {
items.Add(new OutlookOgcs.Categories.ColourInfo(category.Color, Categories.Map.RgbColour(category.Color), category.Name));
Expand Down Expand Up @@ -247,8 +265,8 @@ public String FindName(Outlook.OlCategoryColor? olCategory, String categoryName
log.Debug("Did not find Outlook category " + olCategory.ToString() + (categoryName == null ? "" : " \"" + categoryName + "\""));
String newCategoryName = "OGCS " + FriendlyCategoryName(olCategory);
if (!createMissingCategory) {
createMissingCategory = System.Windows.Forms.OgcsMessageBox.Show("There is no matching Outlook category.\r\nWould you like to create one of the form '" + newCategoryName + "'?",
"Create new Outlook category?", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes;
createMissingCategory = OgcsMessageBox.Show("There is no matching Outlook category.\r\nWould you like to create one of the form '" + newCategoryName + "'?",
"Create new Outlook category?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
}
if (createMissingCategory) {
Outlook.Category newCategory = categories.Add(newCategoryName, olCategory);
Expand Down
11 changes: 11 additions & 0 deletions src/OutlookGoogleCalendarSync/Sync/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,17 @@ private SyncResult synchronize() {
OutlookOgcs.Calendar.Instance.ReclaimOrphanCalendarEntries(ref outlookEntries, ref googleEntries);
if (Sync.Engine.Instance.CancellationPending) return SyncResult.UserCancelled;

if (this.Profile.AddColours || this.Profile.SetEntriesColour) OutlookOgcs.Calendar.Categories.ValidateCategories();
if (this.Profile.ColourMaps.Count > 0) {
this.Profile.ColourMaps.ToList().ForEach(c => {
if (OutlookOgcs.Calendar.Categories.OutlookColour(c.Key) == null) {
if (OgcsMessageBox.Show("There is a problem with your colour mapping configuration.\r\nColours may not get synced as intended.\r\nReview maps now for missing Outlook colours?",
"Invalid colour map", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Error) == DialogResult.Yes)
new Forms.ColourMap().ShowDialog();
}
});
}

//Sync
if (this.Profile.SyncDirection.Id != Direction.GoogleToOutlook.Id) {
success = outlookToGoogle(outlookEntries, googleEntries, ref bubbleText);
Expand Down

0 comments on commit d46787a

Please sign in to comment.