示例#1
0
        public static bool UpdateRecurrenceExceptions(Outlook.AppointmentItem master, Event slave, Synchronizer sync)
        {
            bool ret = false;

            Outlook.Exceptions exceptions = master.GetRecurrencePattern().Exceptions;

            if (exceptions != null && exceptions.Count != 0)
            {
                foreach (Outlook.Exception exception in exceptions)
                {
                    if (!exception.Deleted)
                    {
                        //Add exception time (but only if in given time range
                        if ((Synchronizer.MonthsInPast == 0 || exception.AppointmentItem.End >= DateTime.Now.AddMonths(-Synchronizer.MonthsInPast)) &&
                             (Synchronizer.MonthsInFuture == 0 || exception.AppointmentItem.Start <= DateTime.Now.AddMonths(Synchronizer.MonthsInFuture)))
                        {
                            //slave.Times.Add(new Google.GData.Extensions.When(exception.AppointmentItem.Start, exception.AppointmentItem.Start, exception.AppointmentItem.AllDayEvent));
                            var googleRecurrenceException = Factory.NewEvent();
                            //if (slave.Sequence != null)
                            //    googleRecurrenceException.Sequence = slave.Sequence + 1;
                            googleRecurrenceException.RecurringEventId = slave.Id;
                            //googleRecurrenceException.OriginalEvent.Href = ???
                            googleRecurrenceException.OriginalStartTime = new EventDateTime();
                            if (master.AllDayEvent == true)
                                googleRecurrenceException.OriginalStartTime.Date = exception.OriginalDate.ToString("yyyy-MM-dd");
                            else
                            {
                                //DateTime start = exception.OriginalDate.AddHours(master.Start.Hour).AddMinutes(master.Start.Minute).AddSeconds(master.Start.Second);
                                googleRecurrenceException.OriginalStartTime.DateTime = exception.OriginalDate;
                            }

                            try
                            {
                                sync.UpdateAppointment(exception.AppointmentItem, ref googleRecurrenceException);
                                //googleRecurrenceExceptions.Add(googleRecurrenceException);

                                ret = true;
                            }
                            catch (Exception ex)
                            {
                                //should not happen
                                Logger.Log(ex.Message, EventType.Error);
                            }
                        }
                    }
                    else
                    {//Delete exception time

                    //    //for (int i=slave.Times.Count;i>0;i--)
                    //    //{
                    //    //    When time = slave.Times[i-1];
                    //    //    if (time.StartTime.Equals(exception.AppointmentItem.Start))
                    //    //    {
                    //    //        slave.Times.Remove(time);
                    //    //        ret = true;
                    //    //        break;
                    //    //    }
                    //    //}

                    //    //for (int i = googleRecurrenceExceptions.Count; i > 0;i-- )
                    //    //{
                    //    //    if (googleRecurrenceExceptions[i-1].Times[0].StartTime.Equals(exception.OriginalDate))
                    //    //    {
                    //    //        googleRecurrenceExceptions[i - 1].Delete();
                    //    //        googleRecurrenceExceptions.RemoveAt(i - 1);
                    //    //    }
                    //    //}

                    //    //ToDo: Doesn't work for all recurrences
                    //    var googleRecurrenceException = sync.GetGoogleAppointmentByStartDate(slave.Id, exception.OriginalDate);

                    //    if (googleRecurrenceException != null)
                    //        googleRecurrenceException.Delete();

                        if ((Synchronizer.MonthsInPast == 0 || exception.OriginalDate >= DateTime.Now.AddMonths(-Synchronizer.MonthsInPast)) &&
                             (Synchronizer.MonthsInFuture == 0 || exception.OriginalDate <= DateTime.Now.AddMonths(Synchronizer.MonthsInFuture)))
                        {
                            //First create deleted occurrences, to delete it later again
                            var googleRecurrenceException = Factory.NewEvent();
                            //if (slave.Sequence != null)
                            //    googleRecurrenceException.Sequence = slave.Sequence + 1;
                            googleRecurrenceException.RecurringEventId = slave.Id;
                            //googleRecurrenceException.OriginalEvent.Href = ???
                            DateTime start = exception.OriginalDate.AddHours(master.Start.Hour).AddMinutes(master.Start.Minute).AddSeconds(master.Start.Second);
                            googleRecurrenceException.OriginalStartTime =  new EventDateTime();
                            googleRecurrenceException.OriginalStartTime.TimeZone = slave.Start.TimeZone;

                            if (master.AllDayEvent)
                            {
                                googleRecurrenceException.OriginalStartTime.Date = start.ToString("yyyy-MM-dd");
                                googleRecurrenceException.End.Date = start.AddMinutes(master.Duration).ToString("yyyy-MM-dd");
                            }
                            else
                            {
                                googleRecurrenceException.OriginalStartTime.DateTime = start;
                                googleRecurrenceException.End.DateTime = start.AddMinutes(master.Duration);
                            }
                            googleRecurrenceException.Start = googleRecurrenceException.OriginalStartTime;

                            googleRecurrenceException.Summary = master.Subject;

                            try
                            {
                                googleRecurrenceException = sync.SaveGoogleAppointment(googleRecurrenceException);
                                //googleRecurrenceExceptions.Add(googleRecurrenceException);

                                //ToDo: check promptDeletion and syncDeletion options
                                sync.EventRequest.Delete(Synchronizer.SyncAppointmentsGoogleFolder, googleRecurrenceException.Id).Execute();
                                Logger.Log("Deleted obsolete recurrence exception from Google: " + master.Subject + " - " + exception.OriginalDate, EventType.Information);
                                //sync.DeletedCount++;

                                ret = true;
                            }
                            catch (Exception ex)
                            {
                                //usually only an error is thrown, if an already cancelled event is to be deleted again
                                Logger.Log(ex.Message, EventType.Debug);
                            }

                        }

                    }
                }
            }

            return ret;
        }