Пример #1
0
        public static void MergeCalendar(string username, string password, string feedUrl, CancellationToken token)
        {
            CalendarService          calendarService = CalendarHelper.GetService(GlobalData.GoogleDataAppName, username, password);
            IEnumerable <EventEntry> entries         = CalendarHelper.GetAllEvents(calendarService, feedUrl, null, token);

            token.ThrowIfCancellationRequested();

            DownloadMergeCalendar(entries, username, calendarService, feedUrl, token);
            //	UploadMergeCalendar(calendarService, feedUrl);
        }
Пример #2
0
        private void SignIn()
        {
            message.Text = "Checking your credentials...";

            retryButton.IsEnabled = false;

            animation.Start();

            userDataGrid.Visibility = Visibility.Collapsed;

            string email    = emailBox.Text;
            string password = passwordBox.Password;

            Thread thread = new Thread(() =>
            {
                try
                {
                    if (!CalendarHelper.Verify(email, password))
                    {
                        Dispatcher.BeginInvoke(() =>
                        {
                            if (!IsLoaded)
                            {
                                return;
                            }

                            TaskDialog td = new TaskDialog(this, "Credentials",
                                                           "It doesn't look like these credentials work. Try and enter them again.",
                                                           MessageType.Error);
                            td.ShowDialog();
                            passwordBox.SelectAll();
                            signInButton.Visibility = Visibility.Visible;

                            animation.Stop();
                            userDataGrid.Visibility = Visibility.Visible;

                            message.Text = "Enter your Google account credentials below.";
                        });

                        return;
                    }

                    SecureStorage secure = new SecureStorage("Google", email);

                    if (!_updateMode)
                    {
                        secure.Load();

                        if (secure.Username != null || secure.Password != null)
                        {
                            Dispatcher.BeginInvoke(() =>
                            {
                                if (!IsLoaded)
                                {
                                    return;
                                }

                                TaskDialog td = new TaskDialog(this, "Account Exists",
                                                               "You already added this account to " + GlobalAssemblyInfo.AssemblyName + ".", MessageType.Error);
                                td.ShowDialog();
                                emailBox.SelectAll();
                                signInButton.Visibility = Visibility.Visible;

                                animation.Stop();
                                userDataGrid.Visibility = Visibility.Visible;
                            });

                            return;
                        }
                    }

                    secure.Username = email;
                    secure.Password = password;
                    secure.Save();

                    Dispatcher.BeginInvoke(() =>
                    {
                        if (!IsLoaded)
                        {
                            return;
                        }

                        DialogResult = true;
                    });
                }
                catch
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        if (!IsLoaded)
                        {
                            return;
                        }

                        TaskDialog td = new TaskDialog(this, "Sign In",
                                                       "We're having trouble signing into your Google account. Check your Internet connection and try again.",
                                                       MessageType.Error);
                        td.ShowDialog();

                        retryButton.IsEnabled  = true;
                        retryButton.Visibility = Visibility.Visible;
                        closeButton.Visibility = Visibility.Visible;

                        animation.Stop();
                        userDataGrid.Visibility = Visibility.Visible;
                        message.Text            = "Enter your Google account credentials below.";
                    });
                }
            });

            thread.Start();
        }
Пример #3
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;
            }
        }
Пример #4
0
        /// <summary>
        /// Download all calendars, and returns an integer with number of calendars.
        /// </summary>
        /// <param name="accounts"></param>
        /// <returns></returns>
        private int SyncCalendars(GoogleAccount[] accounts, CancellationToken token)
        {
            int count = 0;

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

                // Get calendars linked to this account
                IEnumerable <CalendarEntry> linkedCalendars = CalendarHelper.GetAllCalendars(
                    CalendarHelper.GetService(GlobalData.GoogleDataAppName, email, gAccount.Password),
                    null, token);

                PersistentGoogleCalendar[] savedCalendars = PersistentGoogleCalendars.AllCalendars();

                if (savedCalendars != null)
                {
                    foreach (PersistentGoogleCalendar each in savedCalendars)
                    {
                        bool found = false;

                        foreach (CalendarEntry entry in linkedCalendars)
                        {
                            if (GetEventFeedRel(entry.Links) == each.Url)
                            {
                                found = true;
                                break;
                            }
                        }

                        if (!found)
                        {
                            PersistentGoogleCalendars.Delete(each);
                        }
                    }
                }

                List <string> stringCals = new List <string>();

                foreach (CalendarEntry calendar in linkedCalendars)
                {
                    string uri = GetEventFeedRel(calendar.Links);

                    if (uri != null)
                    {
                        stringCals.Add(uri);

                        PersistentGoogleCalendars.Save(new PersistentGoogleCalendar(email,
                                                                                    uri, calendar.Title.Text, calendar.Color));

                        count++;
                    }
                }

                gAccount.LinkedCalendars = stringCals.ToArray();

                token.ThrowIfCancellationRequested();
            }

            return(count);
        }