public bool Validate(StringBuilder errorMessageBuilder)
        {
            bool result = OptionTasks.ValidateCalendarUrl(_serverSettingsControl.CalendarUrl, errorMessageBuilder, true);

            result &= _outlookFolderControl.Validate(errorMessageBuilder);
            result &= OptionTasks.ValidateEmailAddress(errorMessageBuilder, _serverSettingsControl.EmailAddress);
            return(result);
        }
        private async Task TestServerConnection()
        {
            _testConnectionButton.Enabled = false;
            try
            {
                StringBuilder errorMessageBuilder = new StringBuilder();
                if (!OptionTasks.ValidateCalendarUrl(_calenderUrlTextBox.Text, errorMessageBuilder, false))
                {
                    MessageBox.Show(errorMessageBuilder.ToString(), "The CalDav/CardDav Url is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                var enteredUri   = new Uri(_calenderUrlTextBox.Text);
                var webDavClient = CreateWebDavClient();

                Uri          autoDiscoveredUrl;
                ResourceType autoDiscoveredResourceType;

                if (ConnectionTester.RequiresAutoDiscovery(enteredUri))
                {
                    var autodiscoveryResult = await OptionTasks.DoAutoDiscovery(enteredUri, webDavClient, true, true, _dependencies.OutlookFolderType);

                    if (autodiscoveryResult.WasCancelled)
                    {
                        return;
                    }
                    if (autodiscoveryResult.RessourceUrl != null)
                    {
                        autoDiscoveredUrl          = autodiscoveryResult.RessourceUrl;
                        autoDiscoveredResourceType = autodiscoveryResult.ResourceType;
                    }
                    else
                    {
                        var autodiscoveryResult2 = await OptionTasks.DoAutoDiscovery(enteredUri.AbsolutePath.EndsWith ("/")?enteredUri : new Uri(enteredUri.ToString() + "/"), webDavClient, false, false, _dependencies.OutlookFolderType);

                        if (autodiscoveryResult2.WasCancelled)
                        {
                            return;
                        }
                        if (autodiscoveryResult2.RessourceUrl != null)
                        {
                            autoDiscoveredUrl          = autodiscoveryResult2.RessourceUrl;
                            autoDiscoveredResourceType = autodiscoveryResult2.ResourceType;
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                else
                {
                    var result = await ConnectionTester.TestConnection(enteredUri, webDavClient, ResourceType.None);

                    if (result.ResourceType != ResourceType.None)
                    {
                        _settingsFaultFinder.FixSynchronizationMode(result);

                        OptionTasks.DisplayTestReport(
                            result,
                            _dependencies.SelectedSynchronizationModeRequiresWriteableServerResource,
                            _dependencies.SelectedSynchronizationModeDisplayName,
                            _dependencies.OutlookFolderType);
                        return;
                    }
                    else
                    {
                        var autodiscoveryResult = await OptionTasks.DoAutoDiscovery(enteredUri, webDavClient, false, false, _dependencies.OutlookFolderType);

                        if (autodiscoveryResult.WasCancelled)
                        {
                            return;
                        }
                        if (autodiscoveryResult.RessourceUrl != null)
                        {
                            autoDiscoveredUrl          = autodiscoveryResult.RessourceUrl;
                            autoDiscoveredResourceType = autodiscoveryResult.ResourceType;
                        }
                        else
                        {
                            var autodiscoveryResult2 = await OptionTasks.DoAutoDiscovery(enteredUri, webDavClient, true, true, _dependencies.OutlookFolderType);

                            if (autodiscoveryResult2.RessourceUrl != null)
                            {
                                autoDiscoveredUrl          = autodiscoveryResult2.RessourceUrl;
                                autoDiscoveredResourceType = autodiscoveryResult2.ResourceType;
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                }

                _calenderUrlTextBox.Text = autoDiscoveredUrl.ToString();

                var finalResult = await ConnectionTester.TestConnection(autoDiscoveredUrl, webDavClient, autoDiscoveredResourceType);

                _settingsFaultFinder.FixSynchronizationMode(finalResult);

                OptionTasks.DisplayTestReport(
                    finalResult,
                    _dependencies.SelectedSynchronizationModeRequiresWriteableServerResource,
                    _dependencies.SelectedSynchronizationModeDisplayName,
                    _dependencies.OutlookFolderType);
                ;
            }
            catch (Exception x)
            {
                s_logger.Error("Exception while testing the connection.", x);
                string message = null;
                for (Exception ex = x; ex != null; ex = ex.InnerException)
                {
                    message += ex.Message + Environment.NewLine;
                }
                MessageBox.Show(message, OptionTasks.ConnectionTestCaption);
            }
            finally
            {
                _testConnectionButton.Enabled = true;
            }
        }