Example #1
0
        public static void DownloadMergeCalendar(IEnumerable <EventEntry> entries, string owner, CalendarService calendarService, string feedUrl, CancellationToken token)
        {
            if (entries == null)
            {
                return;
            }

            foreach (EventEntry each in entries)
            {
                for (int i = 0; i < MaxTries; i++)
                {
                    try
                    {
                        if (each.Status.Value != EventEntry.EventStatus.CANCELED_VALUE)
                        {
                            if (!each.IsDraft)
                            {
                                // Always get a fresh event, in case it has been modified elsewhere.
                                EventEntry entry = each;

                                try { entry = GetElementByGoogleID(each.EventId, calendarService, feedUrl, each.Etag); }
                                catch (GDataNotModifiedException) { }

                                string   id             = GetDaytimerID(entry);
                                DateTime serverModified = entry.Edited.DateValue;
                                DateTime localModified  = DateTime.MinValue;
                                bool     exists         = false;

                                Appointment appt = new Appointment(false);

                                if (string.IsNullOrEmpty(id))
                                {
                                    appt.ID = IDGenerator.GenerateID();
                                    AddExtendedProperty(entry, DaytimerID, appt.ID);
                                }
                                else
                                {
                                    Appointment existing = AppointmentDatabase.GetAppointment(id);

                                    if (existing != null)
                                    {
                                        localModified = existing.LastModified;
                                        exists        = true;
                                        appt          = new Appointment(existing);
                                    }
                                    else
                                    {
                                        appt.ID = id;
                                    }
                                }

                                if (serverModified > localModified)
                                {
                                    // Prevent hangup where after first sync, all events would have
                                    // the same owner as the first synced account.
                                    if (!exists)
                                    {
                                        appt.Owner       = owner;
                                        appt.CalendarUrl = feedUrl;
                                    }

                                    if (entry.Times.Count > 0)
                                    {
                                        When when = entry.Times[0];

                                        appt.StartDate = when.StartTime;                                    //.ToLocalTime();
                                        appt.EndDate   = when.EndTime;                                      //.ToLocalTime();
                                        appt.AllDay    = when.AllDay;
                                    }

                                    if (entry.Reminders.Count > 0)
                                    {
                                        Reminder reminder = GetReminder(entry.Reminders,
                                                                        new Reminder.ReminderMethod[] { Reminder.ReminderMethod.alert, Reminder.ReminderMethod.all });                //entry.Reminders[0];

                                        if (reminder == null)
                                        {
                                            reminder = entry.Reminders[0];
                                        }

                                        appt.Reminder = reminder.Method == Reminder.ReminderMethod.none
                                                                                        ? TimeSpan.FromSeconds(-1)
                                                                                        : new TimeSpan(reminder.Days, reminder.Hours, reminder.Minutes, 0);
                                    }
                                    else
                                    {
                                        appt.Reminder = TimeSpan.FromSeconds(-1);
                                    }

                                    appt.Subject      = entry.Title.Text;
                                    appt.Location     = entry.Locations[0].ValueString;
                                    appt.ReadOnly     = entry.ReadOnly;
                                    appt.LastModified = serverModified;

                                    // Google only supports two "show as" values: Busy and Free.
                                    if (entry.EventTransparency.Value == EventEntry.Transparency.OPAQUE_VALUE)
                                    {
                                        appt.ShowAs = ShowAs.Busy;
                                    }
                                    else
                                    {
                                        if (appt.ShowAs == ShowAs.Busy)
                                        {
                                            appt.ShowAs = ShowAs.Free;
                                        }
                                        else
                                        {
                                            ExtendedProperty showas = GetExtendedProperty(entry, DaytimerShowAs);

                                            if (showas != null)
                                            {
                                                appt.ShowAs = (ShowAs)Enum.Parse(typeof(ShowAs), showas.Value, true);
                                            }
                                        }
                                    }

                                    if (entry.Recurrence != null)
                                    {
                                        appt.SetRecurrenceValues(entry.Recurrence.Value);
                                    }
                                    else
                                    {
                                        appt.IsRepeating = false;
                                    }

                                    if (entry.OriginalEvent != null)
                                    {
                                        EventEntry rEntry = GetElementByGoogleID(entry.OriginalEvent.IdOriginal, calendarService, feedUrl);

                                        string rID = GetDaytimerID(rEntry);

                                        if (string.IsNullOrEmpty(rID))
                                        {
                                            AddExtendedProperty(rEntry, DaytimerID, rID = IDGenerator.GenerateID());
                                        }

                                        appt.RepeatID = rID;
                                    }
                                    else
                                    {
                                        appt.RepeatID = null;
                                    }

                                    //ExtendedProperty details = GetExtendedProperty(entry, DaytimerDetails);
                                    //FlowDocument detailsDocument = null;

                                    //if (details != null)
                                    //	detailsDocument = FlowDocumentDeserialize(details.Value);

                                    if (!string.IsNullOrEmpty(entry.Content.Content))
                                    {
                                        // Since Google does not support text formatting,
                                        // don't change the document unless the text content
                                        // is different.
                                        if (appt.Details != entry.Content.Content)
                                        {
                                            // Edited through the Google UI or another application.
                                            //if (new TextRange(detailsDocument.ContentStart, detailsDocument.ContentEnd).Text != entry.Content.Content)
                                            appt.DetailsDocument = new FlowDocument(new Paragraph(new Run(entry.Content.Content)));

                                            //// Edited through another Daytimer application.
                                            //else
                                            //	appt.DetailsDocument = detailsDocument;
                                        }
                                    }
                                    else
                                    {
                                        //if (detailsDocument != null && string.IsNullOrEmpty(new TextRange(detailsDocument.ContentStart, detailsDocument.ContentEnd).Text))
                                        //	appt.DetailsDocument = detailsDocument;
                                        //else
                                        appt.DetailsDocument = new FlowDocument();
                                    }

                                    //
                                    // Custom attributes
                                    //
                                    ExtendedProperty category = GetExtendedProperty(entry, DaytimerCategory);
                                    ExtendedProperty priority = GetExtendedProperty(entry, DaytimerPriority);

                                    if (category != null)
                                    {
                                        appt.CategoryID = category.Value;
                                    }

                                    if (priority != null)
                                    {
                                        appt.Priority = (Priority)Enum.Parse(typeof(Priority), priority.Value, true);
                                    }


                                    if (exists)
                                    {
                                        AppointmentDatabase.UpdateAppointment(appt, false);
                                    }
                                    else
                                    {
                                        AppointmentDatabase.Add(appt, false);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (each.OriginalEvent != null)
                            {
                                EventEntry rEntry = GetElementByGoogleID(each.OriginalEvent.IdOriginal, calendarService, feedUrl);

                                if (rEntry != null)
                                {
                                    string rID = GetDaytimerID(rEntry);

                                    if (string.IsNullOrEmpty(rID))
                                    {
                                        AddExtendedProperty(rEntry, DaytimerID, rID = IDGenerator.GenerateID());
                                    }

                                    Appointment appt = AppointmentDatabase.GetAppointment(rID);

                                    if (appt == null)
                                    {
                                        appt             = new Appointment(false);
                                        appt.ID          = rID;
                                        appt.Owner       = owner;
                                        appt.CalendarUrl = feedUrl;
                                    }
                                    else if (appt.Owner != owner || (appt.CalendarUrl != "" && appt.CalendarUrl != feedUrl))
                                    {
                                        break;
                                    }

                                    DateTime[] skip = appt.Recurrence.Skip;

                                    if (skip == null)
                                    {
                                        skip = new DateTime[] { each.Times[0].StartTime.Date }
                                    }
                                    ;
                                    else
                                    {
                                        if (Array.IndexOf(skip, each.Times[0].StartTime.Date) == -1)
                                        {
                                            Array.Resize <DateTime>(ref skip, skip.Length + 1);
                                            skip[skip.Length - 1] = each.Times[0].StartTime.Date;
                                        }
                                    }

                                    appt.Recurrence.Skip = skip;
                                    appt.IsRepeating     = true;
                                    AppointmentDatabase.UpdateAppointment(appt, false);


                                    //if (!string.IsNullOrEmpty(rID))
                                    //{
                                    //// If the appointment is null, it will be synced later; we don't have to worry about
                                    //// reading the skip dates now.
                                    //if (appt != null)
                                    //{
                                    //	DateTime[] skip = appt.RepeatSkip;

                                    //	if (skip == null)
                                    //		skip = new DateTime[] { each.Times[0].StartTime.Date };
                                    //	else
                                    //	{
                                    //		if (Array.IndexOf(skip, each.Times[0].StartTime.Date) == -1)
                                    //		{
                                    //			Array.Resize<DateTime>(ref skip, skip.Length + 1);
                                    //			skip[skip.Length - 1] = each.Times[0].StartTime.Date;
                                    //		}
                                    //	}

                                    //	appt.RepeatSkip = skip;
                                    //	AppointmentDatabase.UpdateAppointment(appt, false);
                                    //}
                                    //}
                                }
                                else
                                {
                                    string id = GetDaytimerID(each);

                                    if (id != null)
                                    {
                                        Appointment del = AppointmentDatabase.GetAppointment(id);

                                        if (del != null && del.Owner == owner && (del.CalendarUrl == feedUrl || del.CalendarUrl == ""))
                                        {
                                            AppointmentDatabase.Delete(id, false);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                string id = GetDaytimerID(each);

                                if (id != null)
                                {
                                    Appointment del = AppointmentDatabase.GetAppointment(id);

                                    if (del != null &&
                                        del.Owner == owner && (del.CalendarUrl == feedUrl || del.CalendarUrl == ""))
                                    {
                                        AppointmentDatabase.Delete(id, false);
                                    }
                                }
                            }
                        }

                        break;
                    }
                    catch
                    {
                        if (i == MaxTries - 1)
                        {
                            throw;
                        }

                        Thread.Sleep(AttemptDelay);
                    }

                    token.ThrowIfCancellationRequested();
                }
            }
        }