public static void SetOutlookLastSync(AppointmentsSynchronizer sync, Outlook.AppointmentItem outlookAppointment)
        {
            //save sync datetime
            Outlook.UserProperties userProperties = null;
            Outlook.UserProperty   prop           = null;
            try
            {
                userProperties = outlookAppointment.UserProperties;
                prop           = userProperties[sync.OutlookPropertyNameSynced];
                if (prop == null)
                {
                    prop = userProperties.Add(sync.OutlookPropertyNameSynced, Outlook.OlUserPropertyType.olDateTime, false);
                }

                prop.Value = DateTime.Now;
            }
            finally
            {
                if (prop != null)
                {
                    Marshal.ReleaseComObject(prop);
                }
                if (userProperties != null)
                {
                    Marshal.ReleaseComObject(userProperties);
                }
            }
        }
예제 #2
0
        public ConflictResolution Resolve(Outlook.AppointmentItem outlookAppointment, Event googleAppointment, AppointmentsSynchronizer sync, bool isNewMatch)
        {
            string name = string.Empty;

            _form.OutlookItemTextBox.Text = string.Empty;
            _form.GoogleItemTextBox.Text  = string.Empty;
            if (outlookAppointment != null)
            {
                name = outlookAppointment.Subject + " - " + outlookAppointment.Start;
                _form.OutlookItemTextBox.Text += outlookAppointment.Body;
            }

            if (googleAppointment != null)
            {
                name = googleAppointment.Summary + " - " + AppointmentsSynchronizer.GetTime(googleAppointment);
                _form.GoogleItemTextBox.Text += googleAppointment.Description;
            }

            if (isNewMatch)
            {
                _form.messageLabel.Text =
                    "This is the first time these appointments \"" + name +
                    "\" are synced. Choose which you would like to keep.";
                _form.skip.Text = "Keep both";
            }
            else
            {
                _form.messageLabel.Text =
                    "Both the Outlook and Google Appointment \"" + name +
                    "\" have been changed. Choose which you would like to keep.";
            }

            return(Resolve());
        }
        public static string GetOutlookGoogleAppointmentId(AppointmentsSynchronizer sync, Outlook.AppointmentItem outlookAppointment)
        {
            string id = null;

            Outlook.UserProperties userProperties = null;
            Outlook.UserProperty   idProp         = null;

            try
            {
                userProperties = outlookAppointment.UserProperties;
                idProp         = userProperties[sync.OutlookPropertyNameId];
                if (idProp != null)
                {
                    id = (string)idProp.Value;
                }
            }
            finally
            {
                if (idProp != null)
                {
                    Marshal.ReleaseComObject(idProp);
                }
                if (userProperties != null)
                {
                    Marshal.ReleaseComObject(userProperties);
                }
            }
            return(id);
        }
예제 #4
0
        public void TestExtendedProps()
        {
            sync.SyncOption = SyncOption.MergeOutlookWins;

            // create new appointment to sync
            Outlook.AppointmentItem outlookAppointment = AppointmentsSynchronizer.CreateOutlookAppointmentItem(AppointmentsSynchronizer.SyncAppointmentsFolder);
            outlookAppointment.Subject     = name;
            outlookAppointment.Start       = DateTime.Now;
            outlookAppointment.Start       = DateTime.Now;
            outlookAppointment.AllDayEvent = true;

            outlookAppointment.Save();

            var googleAppointment = Factory.NewEvent();

            sync.appointmentsSynchronizer.UpdateAppointment(outlookAppointment, ref googleAppointment);

            Assert.AreEqual(name, googleAppointment.Summary);

            // read appointment from google
            googleAppointment = null;
            MatchAppointments(sync);
            AppointmentsMatcher.SyncAppointments(sync.appointmentsSynchronizer);

            AppointmentMatch match = FindMatch(outlookAppointment);

            Assert.IsNotNull(match);
            Assert.IsNotNull(match.GoogleAppointment);

            // get extended prop
            Assert.AreEqual(AppointmentPropertiesUtils.GetOutlookId(outlookAppointment), AppointmentPropertiesUtils.GetGoogleOutlookAppointmentId(sync.SyncProfile, match.GoogleAppointment));

            DeleteTestAppointments(match);
        }
        public static DateTime?GetOutlookLastSync(AppointmentsSynchronizer sync, Outlook.AppointmentItem outlookAppointment)
        {
            DateTime?result = null;

            Outlook.UserProperties userProperties = null;
            Outlook.UserProperty   prop           = null;

            try
            {
                userProperties = outlookAppointment.UserProperties;
                prop           = userProperties[sync.OutlookPropertyNameSynced];
                if (prop != null)
                {
                    result = (DateTime)prop.Value;
                }
            }
            finally
            {
                if (prop != null)
                {
                    Marshal.ReleaseComObject(prop);
                }
                if (userProperties != null)
                {
                    Marshal.ReleaseComObject(userProperties);
                }
            }
            return(result);
        }
        public static void ResetOutlookGoogleAppointmentId(AppointmentsSynchronizer sync, Outlook.AppointmentItem outlookAppointment)
        {
            Outlook.UserProperties userProperties = null;

            try
            {
                userProperties = outlookAppointment.UserProperties;

                for (var i = userProperties.Count; i > 0; i--)
                {
                    Outlook.UserProperty p = null;
                    try
                    {
                        p = userProperties[i];
                        if (p.Name == sync.OutlookPropertyNameId || p.Name == sync.OutlookPropertyNameSynced)
                        {
                            userProperties.Remove(i);
                        }
                    }
                    finally
                    {
                        if (p != null)
                        {
                            Marshal.ReleaseComObject(p);
                        }
                    }
                }
            }
            finally
            {
                Marshal.ReleaseComObject(userProperties);
            }
        }
        /// <inheritdoc />
        protected override void Execute(CodeActivityContext context)
        {
            var data     = context.GetValue(Data);
            var schedule = AppointmentsSynchronizer.PrepareSchedule(data);
            var pattern  = AppointmentsSynchronizer.DeterminePattern(schedule);

            context.SetValue(WeeklyPatterns, pattern);
        }
예제 #8
0
        private static void SyncAppointmentNoOutlook(AppointmentMatch match, AppointmentsSynchronizer sync)
        {
            string outlookAppointmenttId = AppointmentPropertiesUtils.GetGoogleOutlookAppointmentId(sync.SyncProfile, match.GoogleAppointment);

            if (!string.IsNullOrEmpty(outlookAppointmenttId))
            {
                if (sync.SyncOption == SyncOption.GoogleToOutlookOnly || !sync.SyncDelete)
                {
                    return;
                }
                else if (!sync.PromptDelete)
                {
                    sync.DeleteGoogleResolution = DeleteResolution.DeleteGoogleAlways;
                }
                else if (sync.DeleteGoogleResolution != DeleteResolution.DeleteGoogleAlways &&
                         sync.DeleteGoogleResolution != DeleteResolution.KeepGoogleAlways)
                {
                    using (var r = new ConflictResolver())
                    {
                        sync.DeleteGoogleResolution = r.ResolveDelete(match.GoogleAppointment);
                    }
                }
                switch (sync.DeleteGoogleResolution)
                {
                case DeleteResolution.KeepGoogle:
                case DeleteResolution.KeepGoogleAlways:
                    AppointmentPropertiesUtils.ResetGoogleOutlookAppointmentId(sync.SyncProfile, match.GoogleAppointment);
                    break;

                case DeleteResolution.DeleteGoogle:
                case DeleteResolution.DeleteGoogleAlways:
                    //Avoid recreating a OutlookAppointment already existing
                    //==> Delete this e instead if previous match existed but no match exists anymore
                    return;

                default:
                    throw new ApplicationException("Cancelled");
                }
            }


            if (sync.SyncOption == SyncOption.OutlookToGoogleOnly)
            {
                sync.SkippedCount++;
                Logger.Log(string.Format("Google appointment not added to Outlook, because of SyncOption " + sync.SyncOption.ToString() + ": {0}", match.GoogleAppointment.Summary), EventType.Information);
                return;
            }

            //create a Outlook appointment from Google appointment
            match.OutlookAppointment = AppointmentsSynchronizer.CreateOutlookAppointmentItem(AppointmentsSynchronizer.SyncAppointmentsFolder);

            sync.UpdateAppointment(ref match.GoogleAppointment, match.OutlookAppointment, match.GoogleAppointmentExceptions);
        }
예제 #9
0
        public DeleteResolution ResolveDelete(Event googleAppointment)
        {
            _form.Text = "Outlook appointment deleted";
            _form.messageLabel.Text =
                "Outlook appointment \"" + googleAppointment.Summary + " - " + AppointmentsSynchronizer.GetTime(googleAppointment) +
                "\" doesn't exist anymore. Do you want to delete it also on Google side?";

            _form.OutlookItemTextBox.Text = string.Empty;
            _form.GoogleItemTextBox.Text += googleAppointment.Description;


            _form.keepOutlook.Text = "Keep Google";
            _form.keepGoogle.Text  = "Delete Google";
            _form.skip.Enabled     = false;

            return(ResolveDeletedOutlook());
        }
예제 #10
0
        public void TestSync_Time()
        {
            sync.SyncOption = SyncOption.MergeOutlookWins;

            // create new appointment to sync
            Outlook.AppointmentItem outlookAppointment = AppointmentsSynchronizer.CreateOutlookAppointmentItem(AppointmentsSynchronizer.SyncAppointmentsFolder);
            outlookAppointment.Subject     = name;
            outlookAppointment.Start       = DateTime.Now;
            outlookAppointment.End         = DateTime.Now.AddHours(1);
            outlookAppointment.AllDayEvent = false;

            outlookAppointment.Save();

            sync.SyncOption = SyncOption.OutlookToGoogleOnly;

            var googleAppointment = Factory.NewEvent();

            sync.appointmentsSynchronizer.UpdateAppointment(outlookAppointment, ref googleAppointment);

            googleAppointment = null;

            sync.SyncOption = SyncOption.GoogleToOutlookOnly;
            //load the same appointment from google.
            MatchAppointments(sync);
            AppointmentMatch match = FindMatch(outlookAppointment);

            Assert.IsNotNull(match);
            Assert.IsNotNull(match.GoogleAppointment);
            Assert.IsNotNull(match.OutlookAppointment);

            Outlook.AppointmentItem recreatedOutlookAppointment = AppointmentsSynchronizer.CreateOutlookAppointmentItem(AppointmentsSynchronizer.SyncAppointmentsFolder);
            sync.appointmentsSynchronizer.UpdateAppointment(ref match.GoogleAppointment, recreatedOutlookAppointment, match.GoogleAppointmentExceptions);
            Assert.IsNotNull(outlookAppointment);
            Assert.IsNotNull(recreatedOutlookAppointment);
            // match recreatedOutlookAppointment with outlookAppointment

            Assert.AreEqual(outlookAppointment.Subject, recreatedOutlookAppointment.Subject);

            Assert.AreEqual(outlookAppointment.Start, recreatedOutlookAppointment.Start);
            Assert.AreEqual(outlookAppointment.End, recreatedOutlookAppointment.End);
            Assert.AreEqual(outlookAppointment.AllDayEvent, recreatedOutlookAppointment.AllDayEvent);
            //ToDo: Check other properties

            DeleteTestAppointments(match);
            recreatedOutlookAppointment.Delete();
        }
예제 #11
0
        /// <inheritdoc />
        protected override void Execute(CodeActivityContext context)
        {
            var synchronizer = new AppointmentsSynchronizer(context.GetValue(OrganizerEmail), context.GetValue(OrganizerPassword), context.GetValue(ExchangeUrl))
            {
                Subject             = context.GetValue(Subject),
                Body                = context.GetValue(Body),
                BodyIsHtml          = context.GetValue(IsBodyHtml),
                BodyCheckSubstrings = context.GetValue(BodyCheckSubstrings),
                RequiredAttendees   = context.GetValue(RequiredAtendees),
                OptionalAttendees   = context.GetValue(OptionalAtendees),
                Data                = context.GetValue(Data)
            };

            synchronizer.Sync();

            context.SetValue(MasterPatterns, synchronizer.WeeklyPatterns);
            context.SetValue(MasterIds, synchronizer.MasterIds);
        }
        /// <summary>
        /// Sets the syncId of the Outlook Appointment and the last sync date.
        /// Please assure to always call this function when saving OutlookItem
        /// </summary>
        /// <param name="sync"></param>
        /// <param name="outlookAppointment"></param>
        /// <param name="googleAppointment"></param>
        public static void SetOutlookGoogleAppointmentId(AppointmentsSynchronizer sync, Outlook.AppointmentItem outlookAppointment, Event googleAppointment)
        {
            if (googleAppointment.Id == null)
            {
                throw new NullReferenceException("GoogleAppointment must have a valid Id");
            }

            //check if outlook Appointment aready has google id property.
            Outlook.UserProperties userProperties = null;
            Outlook.UserProperty   prop           = null;
            try
            {
                userProperties = outlookAppointment.UserProperties;
                prop           = userProperties[sync.OutlookPropertyNameId];
                if (prop == null)
                {
                    prop = userProperties.Add(sync.OutlookPropertyNameId, Outlook.OlUserPropertyType.olText, false);
                }

                prop.Value = googleAppointment.Id;
            }
            catch (Exception ex)
            {
                Logger.Log(ex, EventType.Debug);
                Logger.Log("Name: " + sync.OutlookPropertyNameId, EventType.Debug);
                Logger.Log("Value: " + googleAppointment.Id, EventType.Debug);
                throw;
            }
            finally
            {
                if (prop != null)
                {
                    Marshal.ReleaseComObject(prop);
                }
                if (userProperties != null)
                {
                    Marshal.ReleaseComObject(userProperties);
                }
            }
            SetOutlookLastSync(sync, outlookAppointment);
        }
예제 #13
0
 public static void SyncAppointment(AppointmentMatch m, AppointmentsSynchronizer sync)
 {
     if (m.GoogleAppointment == null && m.OutlookAppointment != null)
     {
         //no Google appointment
         SyncAppointmentNoGoogle(m, sync);
     }
     else if (m.OutlookAppointment == null && m.GoogleAppointment != null)
     {
         //no Outlook appointment
         SyncAppointmentNoOutlook(m, sync);
     }
     else if (m.OutlookAppointment != null && m.GoogleAppointment != null)
     {
         SyncAppointmentBothExists(m, sync);
     }
     else
     {
         throw new ArgumentNullException("AppointmenttMatch has all peers null.");
     }
 }
예제 #14
0
        public static void SyncAppointments(AppointmentsSynchronizer sync)
        {
            for (int i = 0; i < sync.Appointments.Count; i++)
            {
                AppointmentMatch match = sync.Appointments[i];
                if (NotificationReceived != null)
                {
                    string name = string.Empty;
                    if (match.OutlookAppointment != null)
                    {
                        name = match.OutlookAppointment.Subject + " - " + match.OutlookAppointment.Start;
                    }
                    else if (match.GoogleAppointment != null)
                    {
                        name = match.GoogleAppointment.Summary + " - " + AppointmentsSynchronizer.GetTime(match.GoogleAppointment);
                    }
                    NotificationReceived(string.Format("Syncing appointment {0} of {1}: {2} ...", i + 1, sync.Appointments.Count, name));
                }

                SyncAppointment(match, sync);
            }
        }
예제 #15
0
        public void TestRemoveOutlookDuplicatedAppointments_02()
        {
            sync.SyncOption = SyncOption.OutlookToGoogleOnly;

            // create new Outlook test appointment
            var ola1 = AppointmentsSynchronizer.CreateOutlookAppointmentItem(AppointmentsSynchronizer.SyncAppointmentsFolder);

            ola1.Subject     = name;
            ola1.Start       = DateTime.Now;
            ola1.End         = DateTime.Now.AddHours(1);
            ola1.AllDayEvent = false;
            ola1.ReminderSet = false;
            ola1.Save();

            // create new Google test appointments
            var e1 = Factory.NewEvent();

            sync.appointmentsSynchronizer.UpdateAppointment(ola1, ref e1);

            var ola2 = AppointmentsSynchronizer.CreateOutlookAppointmentItem(AppointmentsSynchronizer.SyncAppointmentsFolder);

            ola2.Subject     = name;
            ola2.Start       = DateTime.Now;
            ola2.End         = DateTime.Now.AddHours(1);
            ola2.AllDayEvent = false;
            ola2.ReminderSet = false;
            ola2.Save();
            sync.appointmentsSynchronizer.UpdateAppointment(ola2, ref e1);
            AppointmentPropertiesUtils.ResetGoogleOutlookAppointmentId(sync.SyncProfile, e1);
            e1 = sync.appointmentsSynchronizer.SaveGoogleAppointment(e1);

            var gid_ola1 = AppointmentPropertiesUtils.GetOutlookGoogleAppointmentId(sync.appointmentsSynchronizer, ola1);
            var gid_ola2 = AppointmentPropertiesUtils.GetOutlookGoogleAppointmentId(sync.appointmentsSynchronizer, ola2);
            var gid_e1   = AppointmentPropertiesUtils.GetGoogleId(e1);
            var oid_e1   = AppointmentPropertiesUtils.GetGoogleOutlookAppointmentId(sync.SyncProfile, e1);
            var oid_ola1 = AppointmentPropertiesUtils.GetOutlookId(ola1);
            var oid_ola2 = AppointmentPropertiesUtils.GetOutlookId(ola2);

            // assert ola1 points to e1
            Assert.AreEqual(gid_ola1, gid_e1);
            // assert ola2 points to e1
            Assert.AreEqual(gid_ola2, gid_e1);
            // assert appointment e1 does not point to ola1
            Assert.AreNotEqual(oid_e1, oid_ola1);
            // assert appointment e1 does not point to ola2
            Assert.AreNotEqual(oid_e1, oid_ola2);

            sync.appointmentsSynchronizer.LoadAppointments();

            var f_e1   = sync.appointmentsSynchronizer.GetGoogleAppointmentById(gid_e1);
            var f_ola1 = sync.appointmentsSynchronizer.GetOutlookAppointmentById(oid_ola1);
            var f_ola2 = sync.appointmentsSynchronizer.GetOutlookAppointmentById(oid_ola2);

            Assert.IsNotNull(f_e1);
            Assert.IsNotNull(f_ola1);
            Assert.IsNotNull(f_ola2);

            var f_gid_ola1 = AppointmentPropertiesUtils.GetOutlookGoogleAppointmentId(sync.appointmentsSynchronizer, f_ola1);
            var f_gid_ola2 = AppointmentPropertiesUtils.GetOutlookGoogleAppointmentId(sync.appointmentsSynchronizer, f_ola2);
            var f_gid_e1   = AppointmentPropertiesUtils.GetGoogleId(f_e1);
            var f_oid_e1   = AppointmentPropertiesUtils.GetGoogleOutlookAppointmentId(sync.SyncProfile, f_e1);
            var f_oid_ola1 = AppointmentPropertiesUtils.GetOutlookId(f_ola1);
            var f_oid_ola2 = AppointmentPropertiesUtils.GetOutlookId(f_ola2);

            // assert ola1 does not point to e1
            Assert.AreNotEqual(f_gid_ola1, f_gid_e1);
            // assert ola2 does not point to e1
            Assert.AreNotEqual(f_gid_ola2, f_gid_e1);
            // assert appointment e1 does not point to ola1
            Assert.AreNotEqual(f_oid_e1, f_oid_ola1);
            // assert appointment e1 does not point to ola2
            Assert.AreNotEqual(f_oid_e1, f_oid_ola2);

            DeleteTestAppointment(f_ola1);
            DeleteTestAppointment(f_ola2);
            DeleteTestAppointment(f_e1);
        }
예제 #16
0
 public ConflictResolution Resolve(string message, Event googleAppointment, Outlook.AppointmentItem outlookAppointment, AppointmentsSynchronizer sync)
 {
     return(Resolve(message, outlookAppointment, googleAppointment, sync, false, true));
 }
예제 #17
0
        public ConflictResolution Resolve(string message, Outlook.AppointmentItem outlookAppointment, Event googleAppointment, AppointmentsSynchronizer sync, bool keepOutlook, bool keepGoogle)
        {
            // string name = string.Empty;

            _form.OutlookItemTextBox.Text = string.Empty;
            _form.GoogleItemTextBox.Text  = string.Empty;
            if (outlookAppointment != null)
            {
                // name = outlookAppointment.Subject + " - " + outlookAppointment.Start;
                _form.OutlookItemTextBox.Text += outlookAppointment.Body;
            }

            if (googleAppointment != null)
            {
                // name = googleAppointment.Summary + " - " + Synchronizer.GetTime(googleAppointment);
                _form.GoogleItemTextBox.Text += googleAppointment.Description;
            }

            //ToDo: Make it more flexible
            _form.keepGoogle.Enabled  = keepGoogle;
            _form.keepOutlook.Enabled = keepOutlook;
            _form.AllCheckBox.Visible = true;
            _form.messageLabel.Text   = message;

            return(Resolve());
        }
예제 #18
0
        private void DeleteTestAppointments()
        {
            Outlook.MAPIFolder mapiFolder = null;
            Outlook.Items      items      = null;

            if (string.IsNullOrEmpty(AppointmentsSynchronizer.SyncAppointmentsFolder))
            {
                mapiFolder = Synchronizer.OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
            }
            else
            {
                mapiFolder = Synchronizer.OutlookNameSpace.GetFolderFromID(AppointmentsSynchronizer.SyncAppointmentsFolder);
            }
            Assert.NotNull(mapiFolder);

            try
            {
                items = mapiFolder.Items;
                Assert.NotNull(items);

                object item = items.GetFirst();
                while (item != null)
                {
                    if (item is Outlook.AppointmentItem)
                    {
                        var ola = item as Outlook.AppointmentItem;
                        if (ola.Subject == name)
                        {
                            var s = ola.Subject + " - " + ola.Start;
                            ola.Delete();
                            Logger.Log("Deleted Outlook test appointment: " + s, EventType.Information);
                        }
                        Marshal.ReleaseComObject(ola);
                    }
                    Marshal.ReleaseComObject(item);
                    item = items.GetNext();
                }
            }
            finally
            {
                if (mapiFolder != null)
                {
                    Marshal.ReleaseComObject(mapiFolder);
                }
                if (items != null)
                {
                    Marshal.ReleaseComObject(items);
                }
            }

            var    query = sync.appointmentsSynchronizer.EventRequest.List(AppointmentsSynchronizer.SyncAppointmentsGoogleFolder);
            Events feed;
            string pageToken = null;

            do
            {
                query.PageToken = pageToken;
                feed            = query.Execute();
                foreach (Event e in feed.Items)
                {
                    if (!e.Status.Equals("cancelled") && e.Summary != null && e.Summary == name)
                    {
                        sync.appointmentsSynchronizer.EventRequest.Delete(AppointmentsSynchronizer.SyncAppointmentsGoogleFolder, e.Id).Execute();
                        Logger.Log("Deleted Google test appointment: " + e.Summary + " - " + AppointmentsSynchronizer.GetTime(e), EventType.Information);
                    }
                }
                pageToken = feed.NextPageToken;
            }while (pageToken != null);

            sync.appointmentsSynchronizer.LoadAppointments();
            Assert.AreEqual(0, sync.appointmentsSynchronizer.GoogleAppointments.Count);
            Assert.AreEqual(0, sync.appointmentsSynchronizer.OutlookAppointments.Count);
        }
예제 #19
0
        private static void SyncAppointmentBothExists(AppointmentMatch m, AppointmentsSynchronizer sync)
        {
            //ToDo: Check how to overcome appointment recurrences, which need more than 60 seconds to update and therefore get updated again and again because of time tolerance 60 seconds violated again and again

            //merge appointment details

            //determine if this appointment pair were synchronized
            //DateTime? lastUpdated = GetOutlookPropertyValueDateTime(match.OutlookAppointment, sync.OutlookPropertyNameUpdated);
            DateTime?lastSynced = AppointmentPropertiesUtils.GetOutlookLastSync(sync, m.OutlookAppointment);

            if (lastSynced.HasValue)
            {
                //appointment pair was syncronysed before.

                //determine if Google appointment was updated since last sync

                //lastSynced is stored without seconds. take that into account.
                DateTime lastUpdatedOutlook = m.OutlookAppointment.LastModificationTime.AddSeconds(-m.OutlookAppointment.LastModificationTime.Second);
                DateTime lastUpdatedGoogle  = m.GoogleAppointment.Updated.Value.AddSeconds(-m.GoogleAppointment.Updated.Value.Second);
                //consider GoogleAppointmentExceptions, because if they are updated, the master appointment doesn't have a new Saved TimeStamp
                foreach (var e in m.GoogleAppointmentExceptions)
                {
                    if (e.Updated != null)//happens for cancelled events
                    {
                        DateTime lastUpdatedGoogleException = e.Updated.Value.AddSeconds(-e.Updated.Value.Second);
                        if (lastUpdatedGoogleException > lastUpdatedGoogle)
                        {
                            lastUpdatedGoogle = lastUpdatedGoogleException;
                        }
                    }
                    else if (m.OutlookAppointment.IsRecurring && m.OutlookAppointment.RecurrenceState == Outlook.OlRecurrenceState.olApptMaster)
                    {
                        Outlook.AppointmentItem outlookRecurrenceException = null;
                        try
                        {
                            var slaveRecurrence = m.OutlookAppointment.GetRecurrencePattern();
                            if (e.OriginalStartTime != null && !string.IsNullOrEmpty(e.OriginalStartTime.Date))
                            {
                                outlookRecurrenceException = slaveRecurrence.GetOccurrence(DateTime.Parse(e.OriginalStartTime.Date));
                            }
                            else if (e.OriginalStartTime != null && e.OriginalStartTime.DateTime != null)
                            {
                                outlookRecurrenceException = slaveRecurrence.GetOccurrence(e.OriginalStartTime.DateTime.Value);
                            }
                        }
                        catch (Exception ignored)
                        {
                            Logger.Log("Google Appointment with OriginalEvent found, but Outlook occurrence not found: " + e.Summary + " - " + e.OriginalStartTime.DateTime + ": " + ignored, EventType.Debug);
                        }

                        if (outlookRecurrenceException != null && outlookRecurrenceException.MeetingStatus != Outlook.OlMeetingStatus.olMeetingCanceled)
                        {
                            lastUpdatedGoogle = DateTime.Now;
                            break; //no need to search further, already newest date set
                        }
                    }
                }

                //check if both outlok and Google appointments where updated sync last sync
                if ((int)lastUpdatedOutlook.Subtract(lastSynced.Value).TotalSeconds > TimeTolerance &&
                    (int)lastUpdatedGoogle.Subtract(lastSynced.Value).TotalSeconds > TimeTolerance)
                {
                    //both appointments were updated.
                    //options: 1) ignore 2) loose one based on SyncOption
                    //throw new Exception("Both appointments were updated!");

                    switch (sync.SyncOption)
                    {
                    case SyncOption.MergeOutlookWins:
                    case SyncOption.OutlookToGoogleOnly:
                        //overwrite Google appointment
                        Logger.Log("Outlook and Google appointment have been updated, Outlook appointment is overwriting Google because of SyncOption " + sync.SyncOption + ": " + m.OutlookAppointment.Subject + ".", EventType.Information);
                        sync.UpdateAppointment(m.OutlookAppointment, ref m.GoogleAppointment);
                        break;

                    case SyncOption.MergeGoogleWins:
                    case SyncOption.GoogleToOutlookOnly:
                        //overwrite outlook appointment
                        Logger.Log("Outlook and Google appointment have been updated, Google appointment is overwriting Outlook because of SyncOption " + sync.SyncOption + ": " + m.GoogleAppointment.Summary + ".", EventType.Information);
                        sync.UpdateAppointment(ref m.GoogleAppointment, m.OutlookAppointment, m.GoogleAppointmentExceptions);
                        break;

                    case SyncOption.MergePrompt:
                        //promp for sync option
                        if (sync.ConflictResolution != ConflictResolution.GoogleWinsAlways &&
                            sync.ConflictResolution != ConflictResolution.OutlookWinsAlways &&
                            sync.ConflictResolution != ConflictResolution.SkipAlways)
                        {
                            using (var r = new ConflictResolver())
                            {
                                sync.ConflictResolution = r.Resolve(m.OutlookAppointment, m.GoogleAppointment, sync, false);
                            }
                        }
                        switch (sync.ConflictResolution)
                        {
                        case ConflictResolution.Skip:
                        case ConflictResolution.SkipAlways:
                            Logger.Log(string.Format("User skipped appointment ({0}).", m.ToString()), EventType.Information);
                            sync.SkippedCount++;
                            break;

                        case ConflictResolution.OutlookWins:
                        case ConflictResolution.OutlookWinsAlways:
                            sync.UpdateAppointment(m.OutlookAppointment, ref m.GoogleAppointment);
                            break;

                        case ConflictResolution.GoogleWins:
                        case ConflictResolution.GoogleWinsAlways:
                            sync.UpdateAppointment(ref m.GoogleAppointment, m.OutlookAppointment, m.GoogleAppointmentExceptions);
                            break;

                        default:
                            throw new ApplicationException("Cancelled");
                        }
                        break;
                    }
                    return;
                }


                //check if Outlook appointment was updated (with X second tolerance)
                if (sync.SyncOption != SyncOption.GoogleToOutlookOnly &&
                    ((int)lastUpdatedOutlook.Subtract(lastSynced.Value).TotalSeconds > TimeTolerance ||
                     (int)lastUpdatedGoogle.Subtract(lastSynced.Value).TotalSeconds > TimeTolerance &&
                     sync.SyncOption == SyncOption.OutlookToGoogleOnly
                    )
                    )
                {
                    //Outlook appointment was changed or changed Google appointment will be overwritten

                    if ((int)lastUpdatedGoogle.Subtract(lastSynced.Value).TotalSeconds > TimeTolerance &&
                        sync.SyncOption == SyncOption.OutlookToGoogleOnly)
                    {
                        Logger.Log("Google appointment has been updated since last sync, but Outlook appointment is overwriting Google because of SyncOption " + sync.SyncOption + ": " + m.OutlookAppointment.Subject + ".", EventType.Information);
                    }

                    sync.UpdateAppointment(m.OutlookAppointment, ref m.GoogleAppointment);

                    //at the moment use Outlook as "master" source of appointments - in the event of a conflict Google appointment will be overwritten.
                    //TODO: control conflict resolution by SyncOption
                    return;
                }

                //check if Google appointment was updated (with X second tolerance)
                if (sync.SyncOption != SyncOption.OutlookToGoogleOnly &&
                    ((int)lastUpdatedGoogle.Subtract(lastSynced.Value).TotalSeconds > TimeTolerance ||
                     (int)lastUpdatedOutlook.Subtract(lastSynced.Value).TotalSeconds > TimeTolerance &&
                     sync.SyncOption == SyncOption.GoogleToOutlookOnly
                    )
                    )
                {
                    //google appointment was changed or changed Outlook appointment will be overwritten

                    if ((int)lastUpdatedOutlook.Subtract(lastSynced.Value).TotalSeconds > TimeTolerance &&
                        sync.SyncOption == SyncOption.GoogleToOutlookOnly)
                    {
                        Logger.Log("Outlook appointment has been updated since last sync, but Google appointment is overwriting Outlook because of SyncOption " + sync.SyncOption + ": " + m.OutlookAppointment.Subject + ".", EventType.Information);
                    }

                    sync.UpdateAppointment(ref m.GoogleAppointment, m.OutlookAppointment, m.GoogleAppointmentExceptions);
                }
            }
            else
            {
                //appointments were never synced.
                //merge appointments.
                switch (sync.SyncOption)
                {
                case SyncOption.MergeOutlookWins:
                case SyncOption.OutlookToGoogleOnly:
                    //overwrite Google appointment
                    sync.UpdateAppointment(m.OutlookAppointment, ref m.GoogleAppointment);
                    break;

                case SyncOption.MergeGoogleWins:
                case SyncOption.GoogleToOutlookOnly:
                    //overwrite outlook appointment
                    sync.UpdateAppointment(ref m.GoogleAppointment, m.OutlookAppointment, m.GoogleAppointmentExceptions);
                    break;

                case SyncOption.MergePrompt:
                    //promp for sync option
                    if (sync.ConflictResolution != ConflictResolution.GoogleWinsAlways &&
                        sync.ConflictResolution != ConflictResolution.OutlookWinsAlways &&
                        sync.ConflictResolution != ConflictResolution.SkipAlways)
                    {
                        using (var r = new ConflictResolver())
                        {
                            sync.ConflictResolution = r.Resolve(m.OutlookAppointment, m.GoogleAppointment, sync, true);
                        }
                    }
                    switch (sync.ConflictResolution)
                    {
                    case ConflictResolution.Skip:
                    case ConflictResolution.SkipAlways:         //Keep both, Google AND Outlook
                        sync.Appointments.Add(new AppointmentMatch(m.OutlookAppointment, null));
                        sync.Appointments.Add(new AppointmentMatch(null, m.GoogleAppointment));
                        break;

                    case ConflictResolution.OutlookWins:
                    case ConflictResolution.OutlookWinsAlways:
                        sync.UpdateAppointment(m.OutlookAppointment, ref m.GoogleAppointment);
                        break;

                    case ConflictResolution.GoogleWins:
                    case ConflictResolution.GoogleWinsAlways:
                        sync.UpdateAppointment(ref m.GoogleAppointment, m.OutlookAppointment, m.GoogleAppointmentExceptions);
                        break;

                    default:
                        throw new ApplicationException("Canceled");
                    }
                    break;
                }
            }
        }
예제 #20
0
        /// <summary>
        /// Matches outlook and Google appointment by a) id b) properties.
        /// </summary>
        /// <param name="sync">Syncronizer instance</param>
        /// <returns>Returns a list of match pairs (outlook appointment + Google appointment) for all appointment. Those that weren't matche will have it's peer set to null</returns>
        public static List <AppointmentMatch> MatchAppointments(AppointmentsSynchronizer sync)
        {
            Logger.Log("Matching Outlook and Google appointments...", EventType.Information);
            var result = new List <AppointmentMatch>();

            var googleAppointmentExceptions = new List <Event>();

            //for each outlook appointment try to get Google appointment id from user properties
            //if no match - try to match by properties
            //if no match - create a new match pair without Google appointment.
            //foreach (Outlook._AppointmentItem olc in outlookAppointments)
            var OutlookAppointmentsWithoutSyncId = new Collection <Outlook.AppointmentItem>();

            #region Match first all outlookAppointments by sync id
            for (int i = 1; i <= sync.OutlookAppointments.Count; i++)
            {
                Outlook.AppointmentItem ola = null;

                try
                {
                    ola = sync.OutlookAppointments[i] as Outlook.AppointmentItem;

                    if (ola == null || string.IsNullOrEmpty(ola.Subject) && ola.Start == AppointmentSync.outlookDateMin)
                    {
                        Logger.Log("Empty Outlook appointment found. Skipping", EventType.Warning);
                        sync.SkippedCount++;
                        sync.SkippedCountNotMatches++;
                        continue;
                    }
                    else if (ola.MeetingStatus == Outlook.OlMeetingStatus.olMeetingCanceled || ola.MeetingStatus == Outlook.OlMeetingStatus.olMeetingReceivedAndCanceled)
                    {
                        Logger.Log("Skipping Outlook appointment found because it is cancelled: " + ola.Subject + " - " + ola.Start, EventType.Debug);
                        //sync.SkippedCount++;
                        //sync.SkippedCountNotMatches++;
                        continue;
                    }
                    else if (AppointmentsSynchronizer.TimeMin != null &&
                             (ola.IsRecurring && ola.GetRecurrencePattern().PatternEndDate < AppointmentsSynchronizer.TimeMin ||
                              !ola.IsRecurring && ola.End < AppointmentsSynchronizer.TimeMin) ||
                             AppointmentsSynchronizer.TimeMax != null &&
                             (ola.IsRecurring && ola.GetRecurrencePattern().PatternStartDate > AppointmentsSynchronizer.TimeMax ||
                              !ola.IsRecurring && ola.Start > AppointmentsSynchronizer.TimeMax))
                    {
                        Logger.Log("Skipping Outlook appointment because it is out of months range to sync:" + ola.Subject + " - " + ola.Start, EventType.Debug);
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    //this is needed because some appointments throw exceptions
                    if (ola != null && !string.IsNullOrEmpty(ola.Subject))
                    {
                        Logger.Log("Accessing Outlook appointment: " + ola.Subject + " threw and exception. Skipping: " + ex.Message, EventType.Warning);
                    }
                    else
                    {
                        Logger.Log("Accessing Outlook appointment threw and exception. Skipping: " + ex.Message, EventType.Warning);
                    }
                    sync.SkippedCount++;
                    sync.SkippedCountNotMatches++;
                    continue;
                }

                NotificationReceived?.Invoke(string.Format("Matching appointment {0} of {1} by id: {2} ...", i, sync.OutlookAppointments.Count, ola.Subject));

                // Create our own info object to go into collections/lists, so we can free the Outlook objects and not run out of resources / exceed policy limits.
                //OutlookAppointmentInfo olci = new OutlookAppointmentInfo(ola, sync);

                //try to match this appointment to one of Google appointments

                var gid = AppointmentPropertiesUtils.GetOutlookGoogleAppointmentId(sync, ola);

                if (gid != null)
                {
                    var e     = sync.GetGoogleAppointmentById(gid);
                    var match = new AppointmentMatch(ola, null);

                    if (e != null && !e.Status.Equals("cancelled"))
                    {
                        //we found a match by google id, that is not deleted or cancelled yet
                        match.AddGoogleAppointment(e);
                        result.Add(match);
                        sync.GoogleAppointments.Remove(e);
                    }
                    else
                    {
                        OutlookAppointmentsWithoutSyncId.Add(ola);
                    }
                }
                else
                {
                    OutlookAppointmentsWithoutSyncId.Add(ola);
                }
            }
            #endregion
            #region Match the remaining appointments by properties

            for (int i = 0; i < OutlookAppointmentsWithoutSyncId.Count; i++)
            {
                var ola = OutlookAppointmentsWithoutSyncId[i];

                NotificationReceived?.Invoke(string.Format("Matching appointment {0} of {1} by unique properties: {2} ...", i + 1, OutlookAppointmentsWithoutSyncId.Count, ola.Subject));

                //no match found by id => match by subject/title
                //create a default match pair with just outlook appointment.
                var match = new AppointmentMatch(ola, null);

                //foreach Google appointment try to match and create a match pair if found some match(es)
                for (int j = sync.GoogleAppointments.Count - 1; j >= 0; j--)
                {
                    var e = sync.GoogleAppointments[j];
                    // only match if there is a appointment targetBody, else
                    // a matching Google appointment will be created at each sync
                    if (!e.Status.Equals("cancelled") && ola.Subject == e.Summary && e.Start.DateTime != null && ola.Start == e.Start.DateTime)
                    {
                        match.AddGoogleAppointment(e);
                        sync.GoogleAppointments.Remove(e);
                    }
                }

                if (match.GoogleAppointment == null)
                {
                    Logger.Log(string.Format("No match found for outlook appointment ({0}) => {1}", match.OutlookAppointment.Subject + " - " + match.OutlookAppointment.Start, (AppointmentPropertiesUtils.GetOutlookGoogleAppointmentId(sync, match.OutlookAppointment) != null ? "Delete from Outlook" : "Add to Google")), EventType.Information);
                }

                result.Add(match);
            }
            #endregion


            //for each Google appointment that's left (they will be nonmatched) create a new match pair without outlook appointment.
            for (int i = 0; i < sync.GoogleAppointments.Count; i++)
            {
                var googleAppointment = sync.GoogleAppointments[i];

                NotificationReceived?.Invoke(string.Format("Adding new Google appointment {0} of {1} by unique properties: {2} ...", i + 1, sync.GoogleAppointments.Count, googleAppointment.Summary));

                if (googleAppointment.RecurringEventId != null)
                {
                    sync.SkippedCountNotMatches++;
                    googleAppointmentExceptions.Add(googleAppointment);
                }
                else if (googleAppointment.Status.Equals("cancelled"))
                {
                    Logger.Log("Skipping Google appointment found because it is cancelled: " + googleAppointment.Summary + " - " + AppointmentsSynchronizer.GetTime(googleAppointment), EventType.Debug);
                    //sync.SkippedCount++;
                    //sync.SkippedCountNotMatches++;
                }
                else if (string.IsNullOrEmpty(googleAppointment.Summary) && (googleAppointment.Start == null || googleAppointment.Start.DateTime == null && googleAppointment.Start.Date == null))
                {
                    // no title or time
                    sync.SkippedCount++;
                    sync.SkippedCountNotMatches++;
                    Logger.Log("Skipped GoogleAppointment because no unique property found (Subject or StartDate):" + googleAppointment.Summary + " - " + AppointmentsSynchronizer.GetTime(googleAppointment), EventType.Warning);
                }
                else
                {
                    Logger.Log(string.Format("No match found for Google appointment ({0}) => {1}", googleAppointment.Summary + " - " + AppointmentsSynchronizer.GetTime(googleAppointment), (!string.IsNullOrEmpty(AppointmentPropertiesUtils.GetGoogleOutlookAppointmentId(sync.SyncProfile, googleAppointment)) ? "Delete from Google" : "Add to Outlook")), EventType.Information);
                    var match = new AppointmentMatch(null, googleAppointment);
                    result.Add(match);
                }
            }

            //for each Google appointment exception, assign to proper match
            for (int i = 0; i < googleAppointmentExceptions.Count; i++)
            {
                var e = googleAppointmentExceptions[i];
                NotificationReceived?.Invoke(string.Format("Adding Google appointment exception {0} of {1} : {2} ...", i + 1, googleAppointmentExceptions.Count, e.Summary + " - " + AppointmentsSynchronizer.GetTime(e)));

                //Search for original recurrent event in matches
                bool found = false;
                foreach (var m in result)
                {
                    if (m.GoogleAppointment != null && e.RecurringEventId.Equals(m.GoogleAppointment.Id))
                    {
                        m.GoogleAppointmentExceptions.Add(e);
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    Logger.Log(string.Format("No match found for Google appointment exception: {0}", e.Summary + " - " + AppointmentsSynchronizer.GetTime(e)), EventType.Debug);
                }
            }

            return(result);
        }
예제 #21
0
        private static void SyncAppointmentNoGoogle(AppointmentMatch match, AppointmentsSynchronizer sync)
        {
            string googleAppointmentId = AppointmentPropertiesUtils.GetOutlookGoogleAppointmentId(sync, match.OutlookAppointment);

            if (!string.IsNullOrEmpty(googleAppointmentId))
            {
                //Redundant check if exist, but in case an error occurred in MatchAppointments or not all appointments have been loaded (e.g. because months before/after constraint)
                Event matchingGoogleAppointment = null;
                if (sync.AllGoogleAppointments != null)
                {
                    matchingGoogleAppointment = sync.GetGoogleAppointmentById(googleAppointmentId);
                }
                else
                {
                    matchingGoogleAppointment = sync.LoadGoogleAppointments(googleAppointmentId, null, null, null, null);
                }
                if (matchingGoogleAppointment == null)
                {
                    if (sync.SyncOption == SyncOption.OutlookToGoogleOnly || !sync.SyncDelete)
                    {
                        return;
                    }
                    else if (!sync.PromptDelete && match.OutlookAppointment.Recipients.Count == 0)
                    {
                        sync.DeleteOutlookResolution = DeleteResolution.DeleteOutlookAlways;
                    }
                    else if (sync.DeleteOutlookResolution != DeleteResolution.DeleteOutlookAlways &&
                             sync.DeleteOutlookResolution != DeleteResolution.KeepOutlookAlways)
                    {
                        using (var r = new ConflictResolver())
                        {
                            sync.DeleteOutlookResolution = r.ResolveDelete(match.OutlookAppointment);
                        }
                    }
                    switch (sync.DeleteOutlookResolution)
                    {
                    case DeleteResolution.KeepOutlook:
                    case DeleteResolution.KeepOutlookAlways:
                        AppointmentPropertiesUtils.ResetOutlookGoogleAppointmentId(sync, match.OutlookAppointment);
                        break;

                    case DeleteResolution.DeleteOutlook:
                    case DeleteResolution.DeleteOutlookAlways:

                        if (match.OutlookAppointment.Recipients.Count > 1)
                        {
                            //ToDo:Maybe find as better way, e.g. to ask the user, if he wants to overwrite the invalid appointment
                            Logger.Log("Outlook Appointment not deleted, because multiple participants found,  invitation maybe NOT sent by Google: " + match.OutlookAppointment.Subject + " - " + match.OutlookAppointment.Start, EventType.Information);
                            AppointmentPropertiesUtils.ResetOutlookGoogleAppointmentId(sync, match.OutlookAppointment);
                            break;
                        }
                        else
                        {
                            //Avoid recreating a GoogleAppointment already existing
                            //==> Delete this OutlookAppointment instead if previous match existed but no match exists anymore
                            return;
                        }

                    default:
                        throw new ApplicationException("Cancelled");
                    }
                }
                else
                {
                    sync.SkippedCount++;
                    match.GoogleAppointment = matchingGoogleAppointment;
                    Logger.Log("Outlook Appointment not deleted, because still existing on Google side, maybe because months restriction: " + match.OutlookAppointment.Subject + " - " + match.OutlookAppointment.Start, EventType.Information);
                    return;
                }
            }

            if (sync.SyncOption == SyncOption.GoogleToOutlookOnly)
            {
                sync.SkippedCount++;
                Logger.Log(string.Format("Outlook appointment not added to Google, because of SyncOption " + sync.SyncOption.ToString() + ": {0}", match.OutlookAppointment.Subject), EventType.Information);
                return;
            }

            //create a Google appointment from Outlook appointment
            match.GoogleAppointment = Factory.NewEvent();

            sync.UpdateAppointment(match.OutlookAppointment, ref match.GoogleAppointment);
        }