private void deleteAllSyncItems() { DateTime syncStarted = DateTime.Now; OutlookCalendar ocal = null; try { logboxout("Sync started at " + syncStarted.ToString()); logboxout("--------------------------------------------------"); ocal = new OutlookCalendar(); logboxout("Reading Google Calendar Entries..."); GoogleCalendar gcal = new GoogleCalendar(); List <Event> GoogleEntries = gcal.getCalendarEntriesInRange(syncStarted); List <Event> GoogleEntriesToDelete = new List <Event>(); foreach (Event ev in GoogleEntries) { if (!string.IsNullOrEmpty(ev.Description) && ev.Description.Contains("Added by OutlookGoogleSync (" + ocal.AccountName + "):")) { GoogleEntriesToDelete.Add(ev); } } logboxout("Deleting " + GoogleEntriesToDelete.Count + " Google Calendar Sync Entries..."); foreach (Event ev in GoogleEntriesToDelete) { if (!string.IsNullOrEmpty(ev.Description) && ev.Description.Contains("Added by OutlookGoogleSync (" + ocal.AccountName + "):")) { gcal.deleteCalendarEntry(ev); } } logboxout("Done."); logboxout("--------------------------------------------------"); DateTime syncFinished = DateTime.Now; TimeSpan elapsed = syncFinished - syncStarted; logboxout("Sync finished at " + syncFinished.ToString()); logboxout("Time needed: " + elapsed.Minutes + " min " + elapsed.Seconds + " s"); } catch (System.Exception ex) { logboxout("Error Syncing:\r\n" + ex.ToString()); } freeCOMResources(ocal, null); }
private void syncWorker_DoWork(object sender, DoWorkEventArgs e) { if (e.Argument is string && "DELETE".Equals((string)e.Argument)) { deleteAllSyncItems(); return; } AppointmentItemCache appointmentItemCache = new AppointmentItemCache(); EventCache eventCache = new EventCache(); DateTime syncStarted = DateTime.Now; OutlookCalendar ocal = null; try { logboxout("Sync started at " + syncStarted.ToString()); logboxout("--------------------------------------------------"); logboxout("Reading Outlook Calendar Entries..."); ocal = new OutlookCalendar(); List <AppointmentItemCacheEntry> OutlookEntries = new List <AppointmentItemCacheEntry>(); foreach (AppointmentItem a in ocal.getCalendarEntriesInRange(syncStarted)) { OutlookEntries.Add(appointmentItemCache.GetAppointmentItemCacheEntry(a, ocal.AccountName)); } if (checkBoxCreateFiles.Checked) { using (TextWriter tw = new StreamWriter("export_found_in_outlook.txt")) { foreach (AppointmentItemCacheEntry ai in OutlookEntries) { tw.WriteLine(ai.Signature); } } } logboxout("Found " + OutlookEntries.Count + " Outlook Calendar Entries."); logboxout("--------------------------------------------------"); logboxout("Reading Google Calendar Entries..."); string accountName = "(Empty)"; if (ocal != null || !string.IsNullOrEmpty(ocal.AccountName)) { accountName = ocal.AccountName; } GoogleCalendar gcal = new GoogleCalendar(); List <EventCacheEntry> GoogleEntries = new List <EventCacheEntry>(); foreach (Event ev in gcal.getCalendarEntriesInRange(syncStarted)) { GoogleEntries.Add(eventCache.GetEventCacheEntry(ev, accountName)); } if (checkBoxCreateFiles.Checked) { using (TextWriter tw = new StreamWriter("export_found_in_google.txt")) { foreach (EventCacheEntry ev in GoogleEntries) { tw.WriteLine(ev.Signature); } } } logboxout("Found " + GoogleEntries.Count + " Google Calendar Entries."); logboxout("--------------------------------------------------"); List <EventCacheEntry> GoogleEntriesToBeDeleted = identifyGoogleEntriesToBeDeleted(OutlookEntries, GoogleEntries, accountName); if (checkBoxCreateFiles.Checked) { using (TextWriter tw = new StreamWriter("export_to_be_deleted.txt")) { foreach (EventCacheEntry ev in GoogleEntriesToBeDeleted) { tw.WriteLine(ev.Signature); } } } logboxout(GoogleEntriesToBeDeleted.Count + " Google Calendar Entries to be deleted."); //OutlookEntriesToBeCreated ...in Google! List <AppointmentItemCacheEntry> OutlookEntriesToBeCreated = identifyOutlookEntriesToBeCreated(OutlookEntries, GoogleEntries); if (checkBoxCreateFiles.Checked) { using (TextWriter tw = new StreamWriter("export_to_be_created.txt")) { foreach (AppointmentItemCacheEntry ai in OutlookEntriesToBeCreated) { tw.WriteLine(ai.Signature); } } } logboxout(OutlookEntriesToBeCreated.Count + " Entries to be created in Google."); logboxout("--------------------------------------------------"); if (GoogleEntriesToBeDeleted.Count > 0) { logboxout("Deleting " + GoogleEntriesToBeDeleted.Count + " Google Calendar Entries..."); foreach (EventCacheEntry ev in GoogleEntriesToBeDeleted) { gcal.deleteCalendarEntry(ev.Event); } logboxout("Done."); logboxout("--------------------------------------------------"); } if (OutlookEntriesToBeCreated.Count > 0) { logboxout("Creating " + OutlookEntriesToBeCreated.Count + " Entries in Google..."); foreach (AppointmentItemCacheEntry aice in OutlookEntriesToBeCreated) { AppointmentItem ai = aice.AppointmentItem; Event ev = new Event(); ev.Start = new EventDateTime(); ev.End = new EventDateTime(); if (ai.AllDayEvent) { ev.Start.Date = ai.Start.ToString("yyyy-MM-dd"); ev.End.Date = ai.End.ToString("yyyy-MM-dd"); } else { ev.Start.DateTime = GoogleCalendar.GoogleTimeFrom(ai.Start); ev.End.DateTime = GoogleCalendar.GoogleTimeFrom(ai.End); } ev.Summary = ai.Subject; if (checkBoxAddDescription.Checked) { try { ev.Description = ai.Body; } catch (System.Exception ex) { string startDt = ai.AllDayEvent ? ai.Start.ToShortDateString() : ai.Start.ToString(); string endDt = ai.AllDayEvent ? ai.End.ToShortDateString() : ai.End.ToString(); logboxout("Error accessing the body of Outlook item. Body will be empty.\r\n Subject: [" + ev.Summary + "]\r\n Start: [" + startDt + "]\r\n End: [" + endDt + "]\r\n Error: " + ex.Message); } } ev.Location = ai.Location; //consider the reminder set in Outlook if (checkBoxAddReminders.Checked && ai.ReminderSet) { ev.Reminders = new Event.RemindersData(); ev.Reminders.UseDefault = false; EventReminder reminder = new EventReminder(); reminder.Method = "popup"; reminder.Minutes = ai.ReminderMinutesBeforeStart; ev.Reminders.Overrides = new List <EventReminder>(); ev.Reminders.Overrides.Add(reminder); } else { ev.Reminders = new Event.RemindersData(); ev.Reminders.UseDefault = false; } if (checkBoxAddAttendees.Checked) { ev.Description += Environment.NewLine; ev.Description += Environment.NewLine + "=============================================="; ev.Description += Environment.NewLine + "Added by OutlookGoogleSync (" + accountName + "):" + Environment.NewLine; ev.Description += Environment.NewLine + "ORGANIZER: " + Environment.NewLine + ai.Organizer + Environment.NewLine; ev.Description += Environment.NewLine + "REQUIRED: " + Environment.NewLine + splitAttendees(ai.RequiredAttendees) + Environment.NewLine; if (ai.OptionalAttendees != null) { ev.Description += Environment.NewLine + "OPTIONAL: " + Environment.NewLine + splitAttendees(ai.OptionalAttendees); } ev.Description += Environment.NewLine + "=============================================="; } gcal.addEntry(ev); } logboxout("Done."); logboxout("--------------------------------------------------"); } DateTime syncFinished = DateTime.Now; TimeSpan elapsed = syncFinished - syncStarted; logboxout("Sync finished at " + syncFinished.ToString()); logboxout("Time needed: " + elapsed.Minutes + " min " + elapsed.Seconds + " s"); } catch (System.Exception ex) { logboxout("Error Syncing:\r\n" + ex.ToString()); } eventCache.Clear(); freeCOMResources(ocal, appointmentItemCache); }