Example #1
0
        /// <inheritdoc/>
        public override void FromSettings(ConnectWizard wizard, Xml.InstanceSourceSettingsBase settings)
        {
            if (settings is Xml.LocalInstanceSourceSettings h_local)
            {
                // - Restore instance list.
                // - Restore connecting instance (optional).
                foreach (var h_instance in h_local.ConnectingInstanceList)
                {
                    var connecting_instance = InstanceList.FirstOrDefault(inst => inst.Base.AbsoluteUri == h_instance.Base.AbsoluteUri);
                    if (connecting_instance == null)
                    {
                        // The connecting instance was not found. Could be user entered, or removed from discovery file.
                        connecting_instance = new Instance(h_instance.Base);
                        connecting_instance.RequestAuthorization += wizard.Instance_RequestAuthorization;
                        connecting_instance.ForgetAuthorization  += wizard.Instance_ForgetAuthorization;
                    }
                    connecting_instance.Popularity = h_instance.Popularity;

                    // Restore connecting profiles (optionally).
                    ObservableCollection <Profile> profile_list = null;
                    try
                    {
                        switch (Properties.Settings.Default.ConnectingProfileSelectMode)
                        {
                        case 0:
                        case 2:
                            // This might trigger OAuth.
                            profile_list = connecting_instance.GetProfileList(connecting_instance, Window.Abort.Token);
                            break;
                        }
                    }
                    catch (OperationCanceledException) { throw; }
                    catch
                    {
                        // When profile list could not be obtained from the instance, instance settings should be forgotten to avoid issues next time.
                        connecting_instance.Forget();
                        continue;
                    }
                    switch (Properties.Settings.Default.ConnectingProfileSelectMode)
                    {
                    case 0:
                    {
                        // Restore only profiles user connected to before.
                        foreach (var h_profile in h_instance.ProfileList)
                        {
                            var profile = profile_list.FirstOrDefault(prof => prof.ID == h_profile.ID);
                            if (profile != null)
                            {
                                // Synchronise profile data.
                                h_profile.DisplayName = profile.DisplayName;
                                profile.Popularity    = h_profile.Popularity;
                            }
                            else
                            {
                                // The profile is gone missing. Create an unavailable profile placeholder.
                                profile = new Profile
                                {
                                    Instance    = connecting_instance,
                                    ID          = h_profile.ID,
                                    DisplayName = h_profile.DisplayName,
                                    Popularity  = h_profile.Popularity
                                };
                                profile.RequestAuthorization += (object sender_profile, RequestAuthorizationEventArgs e_profile) => connecting_instance.OnRequestAuthorization(connecting_instance, e_profile);
                            }

                            // Add to the list of connecting profiles.
                            if (ConnectingProfileList.FirstOrDefault(prof => prof.Equals(profile)) == null)
                            {
                                ConnectingProfileList.Add(profile);
                            }
                        }
                    }
                    break;

                    case 2:
                    {
                        // Add all available profiles to the connecting profile list.
                        // Restore popularity on the fly (or leave default to promote newly discovered profiles).
                        foreach (var profile in profile_list)
                        {
                            var h_profile = h_instance.ProfileList.FirstOrDefault(prof => prof.ID == profile.ID);
                            if (h_profile != null)
                            {
                                profile.Popularity = h_profile.Popularity;
                            }

                            ConnectingProfileList.Add(profile);
                        }
                    }
                    break;
                    }

                    var instance = ConnectingInstanceList.FirstOrDefault(inst => inst.Base.AbsoluteUri == connecting_instance.Base.AbsoluteUri);
                    if (instance == null)
                    {
                        ConnectingInstanceList.Add(connecting_instance);
                    }
                }
                ConnectingInstance = SelectConnectingInstance(h_local.ConnectingInstance);
            }
        }
        /// <inheritdoc/>
        public override void FromSettings(ConnectWizard wizard, Xml.InstanceSourceSettingsBase settings)
        {
            if (settings is Xml.DistributedInstanceSourceSettings h_distributed)
            {
                // - Restore authenticating instance.
                // - Restore connecting instance (optional).
                AuthenticatingInstance = h_distributed.AuthenticatingInstance != null?InstanceList.FirstOrDefault(inst => inst.Base.AbsoluteUri == h_distributed.AuthenticatingInstance.AbsoluteUri) : null;

                if (AuthenticatingInstance != null)
                {
                    AuthenticatingInstance.RequestAuthorization += wizard.Instance_RequestAuthorization;
                    AuthenticatingInstance.ForgetAuthorization  += wizard.Instance_ForgetAuthorization;
                    ConnectingInstance = SelectConnectingInstance(h_distributed.ConnectingInstance);
                }
            }
        }