Skip to content

Commit

Permalink
Settings.Profile class
Browse files Browse the repository at this point in the history
  • Loading branch information
phw198 committed Oct 9, 2021
1 parent 8d1bf1b commit 2246264
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/OutlookGoogleCalendarSync/GoogleOgcs/EventColour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public EventColour() { }
/// </summary>
public List<Palette> ActivePalette {
get {
SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();
List<Palette> activePalette = new List<Palette>();
if (profile.UseGoogleCalendar == null) return activePalette;

Expand Down
8 changes: 4 additions & 4 deletions src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public Event GetCalendarEntry(String eventId) {
}

public List<Event> GetCalendarEntriesInRange() {
SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();
return GetCalendarEntriesInRange(profile.SyncStart, profile.SyncEnd);
}

Expand All @@ -270,7 +270,7 @@ public List<Event> GetCalendarEntriesInRange(DateTime from, DateTime to) {
String pageToken = null;
Int16 pageNum = 1;

SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();

log.Debug("Retrieving all events from Google: " + from.ToShortDateString() + " -> " + to.ToShortDateString());
do {
Expand Down Expand Up @@ -1485,7 +1485,7 @@ public void GetSettings() {
}
}
private void getCalendarSettings() {
SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();

if (!profile.AddReminders || !profile.UseGoogleDefaultReminder) return;

Expand Down Expand Up @@ -1604,7 +1604,7 @@ private EventColour.Palette getColour(String aiCategories, EventColour.Palette g
public EventColour.Palette GetColour(OlCategoryColor categoryColour) {
EventColour.Palette gColour = null;

SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();
if (profile.ColourMaps.Count > 0) {
KeyValuePair<String, String> kvp = profile.ColourMaps.FirstOrDefault(cm => OutlookOgcs.Calendar.Categories.OutlookColour(cm.Key) == categoryColour);
if (kvp.Key != null) {
Expand Down
4 changes: 2 additions & 2 deletions src/OutlookGoogleCalendarSync/OutlookOgcs/OutlookCalendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public List<AppointmentItem> FilterCalendarEntries(SettingsStore.Calendar profil
if (OutlookItems != null) {
log.Fine(OutlookItems.Count + " calendar items exist.");
if (profile is null)
profile = Settings.Instance.ProfileInPlay();
profile = Settings.Profile.InPlay();

OutlookItems.Sort("[Start]", Type.Missing);
OutlookItems.IncludeRecurrences = false;
Expand Down Expand Up @@ -947,7 +947,7 @@ private String getColour(String gColourId, String oColour) {
public String GetCategoryColour(String gColourId, Boolean createMissingCategory = true) {
OlCategoryColor? outlookColour = null;

SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();
if (profile.ColourMaps.Count > 0) {
KeyValuePair<String, String> kvp = profile.ColourMaps.FirstOrDefault(cm => cm.Value == gColourId);
if (kvp.Key != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void Dispose() {
public void Get(Outlook.Application oApp, Outlook.MAPIFolder calendar) {
Outlook.Store store = null;
try {
if (Settings.Instance.ProfileInPlay().OutlookService == OutlookOgcs.Calendar.Service.DefaultMailbox)
if (Settings.Profile.InPlay().OutlookService == OutlookOgcs.Calendar.Service.DefaultMailbox)
this.categories = oApp.Session.Categories;
else {
try {
Expand Down
10 changes: 5 additions & 5 deletions src/OutlookGoogleCalendarSync/OutlookOgcs/OutlookNew.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void Connect() {

oNS = GetCurrentUser(oNS);

SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();

if (!profile.OutlookGalBlocked && currentUserName == "Unknown") {
log.Info("Current username is \"Unknown\"");
Expand Down Expand Up @@ -209,7 +209,7 @@ public NameSpace GetCurrentUser(NameSpace oNS) {
Recipient currentUser = null;
try {
DateTime triggerOOMsecurity = DateTime.Now;
SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();
try {
currentUser = oNS.CurrentUser;
if (!Forms.Main.Instance.IsHandleCreated && (DateTime.Now - triggerOOMsecurity).TotalSeconds > 1) {
Expand Down Expand Up @@ -284,7 +284,7 @@ public NameSpace GetCurrentUser(NameSpace oNS) {

private MAPIFolder getCalendarStore(NameSpace oNS) {
MAPIFolder defaultCalendar = null;
SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();
if (profile.OutlookService == OutlookOgcs.Calendar.Service.AlternativeMailbox && profile.MailboxName != "") {
log.Debug("Finding Alternative Mailbox calendar folders");
Folders binFolders = null;
Expand Down Expand Up @@ -428,7 +428,7 @@ private MAPIFolder getSharedCalendar(NameSpace oNS, String sharedURI, Boolean in
private void getDefaultCalendar(NameSpace oNS, ref MAPIFolder defaultCalendar) {
log.Debug("Finding default Mailbox calendar folders");
try {
SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();
Boolean updateGUI = profile.Equals(Forms.Main.Instance.ActiveCalendarProfile);
if (updateGUI) {
Forms.Main.Instance.rbOutlookDefaultMB.CheckedChanged -= Forms.Main.Instance.rbOutlookDefaultMB_CheckedChanged;
Expand Down Expand Up @@ -705,7 +705,7 @@ public void RefreshCategories() {
Extensions.OutlookColourPicker outlookColours = new Extensions.OutlookColourPicker();
outlookColours.AddColourItems();

if (Settings.Instance.ProfileInPlay().Equals(Forms.Main.Instance.ActiveCalendarProfile)) {
if (Settings.Profile.InPlay().Equals(Forms.Main.Instance.ActiveCalendarProfile)) {
Forms.Main.Instance.ddOutlookColour = outlookColours;
foreach (OutlookOgcs.Categories.ColourInfo cInfo in Forms.Main.Instance.ddOutlookColour.Items) {
if (cInfo.OutlookCategory.ToString() == Forms.Main.Instance.ActiveCalendarProfile.SetEntriesColourValue &&
Expand Down
10 changes: 5 additions & 5 deletions src/OutlookGoogleCalendarSync/OutlookOgcs/OutlookOld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Connect() {
log.Info("Exchange connection mode: " + exchangeConnectionMode.ToString());

oNS = GetCurrentUser(oNS);
SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();

if (!profile.OutlookGalBlocked && currentUserName == "Unknown") {
log.Info("Current username is \"Unknown\"");
Expand Down Expand Up @@ -178,7 +178,7 @@ public OlExchangeConnectionMode ExchangeConnectionMode() {
private const String PR_IPM_WASTEBASKET_ENTRYID = "http://schemas.microsoft.com/mapi/proptag/0x35E30102";

public NameSpace GetCurrentUser(NameSpace oNS) {
SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();

//We only need the current user details when syncing meeting attendees.
//If GAL had previously been detected as blocked, let's always try one attempt to see if it's been opened up
Expand Down Expand Up @@ -243,7 +243,7 @@ public NameSpace GetCurrentUser(NameSpace oNS) {

private MAPIFolder getCalendarStore(NameSpace oNS) {
MAPIFolder defaultCalendar = null;
SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();
if (profile.OutlookService == OutlookOgcs.Calendar.Service.DefaultMailbox) {
getDefaultCalendar(oNS, ref defaultCalendar);
}
Expand Down Expand Up @@ -283,7 +283,7 @@ private MAPIFolder getSharedCalendar(NameSpace oNS, String sharedURI) {
private void getDefaultCalendar(NameSpace oNS, ref MAPIFolder defaultCalendar) {
log.Debug("Finding default Mailbox calendar folders");
try {
SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();
Boolean updateGUI = profile.Equals(Forms.Main.Instance.ActiveCalendarProfile);
if (updateGUI) {
Forms.Main.Instance.rbOutlookDefaultMB.CheckedChanged -= Forms.Main.Instance.rbOutlookDefaultMB_CheckedChanged;
Expand Down Expand Up @@ -395,7 +395,7 @@ public List<Object> FilterItems(Items outlookItems, String filter) {
AppointmentItem ai = null;
if (outlookItems[i] is AppointmentItem) {
ai = outlookItems[i] as AppointmentItem;
SettingsStore.Calendar profile = Settings.Instance.ProfileInPlay();
SettingsStore.Calendar profile = Settings.Profile.InPlay();
if (ai.IsRecurring && ai.Start.Date < profile.SyncStart && ai.End.Date < profile.SyncStart)
o2003recurring.Add(outlookItems[i]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/OutlookGoogleCalendarSync/SettingsStore/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void LogSettings() {
public static SettingsStore.Calendar GetCalendarProfile(Object settingsStore) {
if (settingsStore is SettingsStore.Calendar)
return settingsStore as SettingsStore.Calendar;
else throw new ArgumentException("Expected calendar settings, received " + Settings.GetProfileType(settingsStore));
else throw new ArgumentException("Expected calendar settings, received " + Settings.Profile.GetType(settingsStore));
}

#region Override Methods
Expand Down
85 changes: 49 additions & 36 deletions src/OutlookGoogleCalendarSync/SettingsStore/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,48 +412,61 @@ public static void configureLoggingLevel(string logLevel) {
((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).RaiseConfigurationChanged(EventArgs.Empty);
}

public enum ProfileType {
Calendar,
Global,
Unknown
}
public static ProfileType GetProfileType(Object settingsStore) {
switch (settingsStore.GetType().ToString()) {
case "OutlookGoogleCalendarSync.Settings": return ProfileType.Global;
case "OutlookGoogleCalendarSync.SettingsStore.Calendar": return ProfileType.Calendar;
/// <summary>
/// Deregister all profiles from Push Sync
/// </summary>
public void DeregisterAllForPushSync() {
foreach (SettingsStore.Calendar calendar in Settings.Instance.Calendars) {
calendar.DeregisterForPushSync();
}
log.Warn("Unknown profile type: " + settingsStore.GetType().ToString());
return ProfileType.Unknown;
}

public SettingsStore.Calendar ProfileInPlay() {
StackTrace stackTrace = new StackTrace();
StackFrame[] stackFrames = stackTrace.GetFrames().Reverse().ToArray();
public class Profile {
public enum Type {
Calendar,
Global,
Unknown
}
public static Type GetType(Object settingsStore) {
switch (settingsStore.GetType().ToString()) {
case "OutlookGoogleCalendarSync.Settings": return Type.Global;
case "OutlookGoogleCalendarSync.SettingsStore.Calendar": return Type.Calendar;
}
log.Warn("Unknown profile type: " + settingsStore.GetType().ToString());
return Type.Unknown;
}

String[] FormMethods = new String[] { "updateGUIsettings", "UpdateGUIsettings_Profile", "<StartSync>b__0", "miCatRefresh_Click", "GetMyGoogleCalendars_Click",
"btColourMap_Click", "ColourPicker_Enter", "ddGoogleColour_SelectedIndexChanged" };
String[] TimerMethods = new String[] { "OnTick" };

foreach (StackFrame frame in stackFrames) {
//log.Debug("Frame:" + frame.GetMethod().Name);
if (FormMethods.Contains(frame.GetMethod().Name))
return Forms.Main.Instance.ActiveCalendarProfile;
else if (TimerMethods.Contains(frame.GetMethod().Name))
return Sync.Engine.Calendar.Instance.Profile;
public static String Name(Object settingsStore) {
switch (GetType(settingsStore)) {
case Type.Calendar: return (settingsStore as SettingsStore.Calendar)._ProfileName;
}
return "";
}
String stackString = "";
stackFrames.Reverse().ToList().ForEach(sf => stackString += sf.GetMethod().Name + " < ");
log.Warn(stackString);
log.Error("Unknown profile being referenced.");
return Forms.Main.Instance.ActiveCalendarProfile;
}

/// <summary>
/// Deregister all profiles from Push Sync
/// </summary>
public void DeregisterAllForPushSync() {
foreach(SettingsStore.Calendar calendar in Settings.Instance.Calendars) {
calendar.DeregisterForPushSync();
/// <summary>
/// Dynamically determine which profile is being used.
/// </summary>
/// <returns>Currently hard-coded to a Calendar profile</returns>
public static SettingsStore.Calendar InPlay() {
StackTrace stackTrace = new StackTrace();
StackFrame[] stackFrames = stackTrace.GetFrames().Reverse().ToArray();

String[] FormMethods = new String[] { "updateGUIsettings", "UpdateGUIsettings_Profile", "<StartSync>b__0", "miCatRefresh_Click", "GetMyGoogleCalendars_Click",
"btColourMap_Click", "ColourPicker_Enter", "ddGoogleColour_SelectedIndexChanged" };
String[] TimerMethods = new String[] { "OnTick" };

foreach (StackFrame frame in stackFrames) {
//log.Debug("Frame:" + frame.GetMethod().Name);
if (FormMethods.Contains(frame.GetMethod().Name))
return Forms.Main.Instance.ActiveCalendarProfile;
else if (TimerMethods.Contains(frame.GetMethod().Name))
return Sync.Engine.Calendar.Instance.Profile;
}
String stackString = "";
stackFrames.Reverse().ToList().ForEach(sf => stackString += sf.GetMethod().Name + " < ");
log.Warn(stackString);
log.Error("Unknown profile being referenced.");
return Forms.Main.Instance.ActiveCalendarProfile;
}
}
}
Expand Down
32 changes: 17 additions & 15 deletions src/OutlookGoogleCalendarSync/Sync/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,6 @@ public void StartSync(Boolean updateSyncSchedule = true) {

this.Profile.OgcsTimer.NextSyncDateText = "In progress...";

StringBuilder sb = new StringBuilder();
Forms.Main.Instance.Console.BuildOutput("Sync version: " + System.Windows.Forms.Application.ProductVersion, ref sb);
Forms.Main.Instance.Console.BuildOutput("Profile: " + this.Profile._ProfileName, ref sb);
Forms.Main.Instance.Console.BuildOutput((Sync.Engine.Instance.ManualForceCompare ? "Full s" : "S") + "ync started at " + DateTime.Now.ToString(), ref sb);
Forms.Main.Instance.Console.BuildOutput("Syncing from " + this.Profile.SyncStart.ToShortDateString() +
" to " + this.Profile.SyncEnd.ToShortDateString(), ref sb);
mainFrm.Console.BuildOutput(this.Profile.SyncDirection.Name, ref sb);

//Make the clock emoji show the right time
int minsPastHour = DateTime.Now.Minute;
minsPastHour = (int)minsPastHour - (minsPastHour % 30);
sb.Insert(0, ":clock" + DateTime.Now.ToString("hh").TrimStart('0') + (minsPastHour == 00 ? "" : "30") + ":");
mainFrm.Console.Update(sb);

if (this.Profile.OutlookPush) this.Profile.DeregisterForPushSync();

Sync.Engine.SyncResult syncResult = Sync.Engine.SyncResult.Fail;
Expand All @@ -110,10 +96,26 @@ public void StartSync(Boolean updateSyncSchedule = true) {
log.Info("User opted to abandon further syncs.");
syncResult = Sync.Engine.SyncResult.Abandon;
break;
} else
} else {
log.Info("User opted to retry sync straight away.");
mainFrm.Console.Clear();
}
}

StringBuilder sb = new StringBuilder();
mainFrm.Console.BuildOutput("Sync version: " + System.Windows.Forms.Application.ProductVersion, ref sb);
mainFrm.Console.BuildOutput("Profile: " + this.Profile._ProfileName, ref sb);
mainFrm.Console.BuildOutput((Sync.Engine.Instance.ManualForceCompare ? "Full s" : "S") + "ync started at " + DateTime.Now.ToString(), ref sb);
mainFrm.Console.BuildOutput("Syncing from " + this.Profile.SyncStart.ToShortDateString() +
" to " + this.Profile.SyncEnd.ToShortDateString(), ref sb);
mainFrm.Console.BuildOutput(this.Profile.SyncDirection.Name, ref sb);

//Make the clock emoji show the right time
int minsPastHour = DateTime.Now.Minute;
minsPastHour = (int)minsPastHour - (minsPastHour % 30);
sb.Insert(0, ":clock" + DateTime.Now.ToString("hh").TrimStart('0') + (minsPastHour == 00 ? "" : "30") + ":");
mainFrm.Console.Update(sb);

//Kick off the sync in the background thread
Sync.Engine.Instance.bwSync.DoWork += new DoWorkEventHandler(
delegate (object o, DoWorkEventArgs args) {
Expand Down
Loading

0 comments on commit 2246264

Please sign in to comment.