Exemple #1
0
        /// <summary>
        /// Downloads data from server, and uploads local data to server for all linked Google accounts.
        /// </summary>
        private void GlobalResync(CancellationToken token)
        {
            DateTime lastSync = Settings.LastSuccessfulSync;

            try
            {
                GoogleAccount[] accounts = GoogleAccounts.AllAccounts();

                if (accounts == null || accounts.Length == 0)
                {
                    Done = true;
                    return;
                }

                ThrowIfNetworkUnavailable();

                // Lock the last sync in now; any events created during the
                // sync process will be ignored, and will be handled by the
                // next sync.
                Settings.LastSuccessfulSync = DateTime.UtcNow;

                SyncObject[] upload = SyncDatabase.AllSyncObjects();

                token.ThrowIfCancellationRequested();

                //
                // Calendars
                //
                Status = "Downloading calendar list";

                int calendars = SyncCalendars(accounts, token);
                _total    = calendars * 3 + 2;
                _progress = 1;
                RaiseEvent(ProgressChangedEvent);

                token.ThrowIfCancellationRequested();

                //
                // Events
                //
                foreach (GoogleAccount gAccount in accounts)
                {
                    string email = gAccount.Email;

                    foreach (string feedUrl in gAccount.LinkedCalendars)
                    {
                        //
                        // Update new account
                        //
                        if (SyncDatabase.GetSyncObject(email) != null)
                        {
                            Status = "Syncing " + email;

                            CalendarHelper.MergeCalendar(email, gAccount.Password, feedUrl, token);
                            SyncDatabase.Delete(email);

                            token.ThrowIfCancellationRequested();

                            _progress += 3;
                            RaiseEvent(ProgressChangedEvent);
                        }

                        //
                        // Update existing account
                        //
                        else
                        {
                            CalendarService calendarService = CalendarHelper.GetService(GlobalData.GoogleDataAppName,
                                                                                        email, gAccount.Password);

                            Status = "Uploading local calendar";

                            foreach (SyncObject each in upload)
                            {
                                switch (each.SyncType)
                                {
                                case SyncType.Create:
                                case SyncType.Modify:
                                {
                                    if (each.OldUrl != "")
                                    {
                                        // We don't know which account owned this calendar; try to delete it
                                        // from every account.
                                        try
                                        {
                                            IEnumerable <EventEntry> entries = CalendarHelper.GetElementsByDaytimerID(
                                                each.ReferenceID, calendarService, each.OldUrl,
                                                token);

                                            if (entries != null)
                                            {
                                                foreach (EventEntry entry in entries)
                                                {
                                                    entry.Delete();
                                                }
                                            }
                                        }
                                        catch { }
                                    }

                                    Appointment appt = AppointmentDatabase.GetAppointment(each.ReferenceID);

                                    //if (appt != null && appt.Sync && (appt.Owner == "" || appt.Owner == email))
                                    //	CalendarHelper.AddEvent(calendarService, feedUrl, appt);

                                    // Uncomment the following method after UI is implemented
                                    // which allows user to select where to upload event.

                                    if (appt != null && appt.Sync && appt.Owner == email)                                                    // && (appt.Owner == "" || appt.Owner == email))
                                    {
                                        CalendarHelper.AddEvent(calendarService,
                                                                appt.CalendarUrl != "" ? appt.CalendarUrl : feedUrl, appt,
                                                                token);
                                    }
                                }
                                break;

                                case SyncType.Delete:
                                {
                                    IEnumerable <EventEntry> entries = CalendarHelper.GetElementsByDaytimerID(
                                        each.ReferenceID, calendarService, each.Url != "" ? each.Url : feedUrl,
                                        token);

                                    if (entries != null)
                                    {
                                        foreach (EventEntry entry in entries)
                                        {
                                            entry.Delete();
                                        }
                                    }
                                }
                                break;

                                default:
                                    break;
                                }
                            }

                            token.ThrowIfCancellationRequested();

                            _progress++;
                            RaiseEvent(ProgressChangedEvent);

                            Status = "Downloading " + email;

                            IEnumerable <EventEntry> download = CalendarHelper.GetAllEventsModifiedSince(calendarService, feedUrl, lastSync, token);

                            token.ThrowIfCancellationRequested();

                            _progress++;
                            RaiseEvent(ProgressChangedEvent);

                            CalendarHelper.DownloadMergeCalendar(download, email, calendarService, feedUrl, token);

                            token.ThrowIfCancellationRequested();

                            _progress++;
                            RaiseEvent(ProgressChangedEvent);
                        }
                    }
                }

                Status = "Cleaning up";

                // Clear sync queue
                foreach (SyncObject each in upload)
                {
                    SyncDatabase.Delete(each);
                }

                _progress++;
                RaiseEvent(ProgressChangedEvent);
            }
            catch (Exception exc)
            {
                _error = exc;
                Settings.LastSuccessfulSync = lastSync;
            }
            finally
            {
                ReminderQueue.Populate();
                Done = true;
            }
        }