Exemplo n.º 1
0
        public EventMappingConfigurationViewModel(IReadOnlyList <string> availableCategories, ICurrentOptions currentOptions)
        {
            if (availableCategories == null)
            {
                throw new ArgumentNullException(nameof(availableCategories));
            }
            if (currentOptions == null)
            {
                throw new ArgumentNullException(nameof(currentOptions));
            }

            AvailableCategories           = availableCategories;
            _currentOptions               = currentOptions;
            SetServerCalendarColorCommand = new DelegateCommand(_ =>
            {
                ComponentContainer.EnsureSynchronizationContext();
                SetServerCalendarColorAsync();
            });
            GetServerCalendarColorCommand = new DelegateCommand(_ =>
            {
                ComponentContainer.EnsureSynchronizationContext();
                GetServerCalendarColorAsync();
            });

            _customPropertyMappingViewModel = new CustomPropertyMappingViewModel();
            Items = new[] { _customPropertyMappingViewModel };
        }
    public ServerSettingsViewModel (ISettingsFaultFinder settingsFaultFinder, ICurrentOptions currentOptions, IOutlookAccountPasswordProvider outlookAccountPasswordProvider)
    {
      if (settingsFaultFinder == null)
        throw new ArgumentNullException (nameof (settingsFaultFinder));
      if (currentOptions == null)
        throw new ArgumentNullException (nameof (currentOptions));

      _settingsFaultFinder = settingsFaultFinder;
      _currentOptions = currentOptions;
      _outlookAccountPasswordProvider = outlookAccountPasswordProvider;

      _testConnectionCommand = new DelegateCommandWithoutCanExecuteDelegation (_ =>
      {
        ComponentContainer.EnsureSynchronizationContext();
        TestConnectionAsync();
      });
      _getAccountSettingsCommand = new DelegateCommandWithoutCanExecuteDelegation (_ =>
      {
        ComponentContainer.EnsureSynchronizationContext();
        GetAccountSettings();
      });
      _createDavResourceCommand = new DelegateCommandWithoutCanExecuteDelegation (_ =>
      {
        ComponentContainer.EnsureSynchronizationContext();
        CreateDavResource();
      });
    }
        public ServerSettingsViewModel(ISettingsFaultFinder settingsFaultFinder, ICurrentOptions currentOptions, IOutlookAccountPasswordProvider outlookAccountPasswordProvider)
        {
            if (settingsFaultFinder == null)
            {
                throw new ArgumentNullException(nameof(settingsFaultFinder));
            }
            if (currentOptions == null)
            {
                throw new ArgumentNullException(nameof(currentOptions));
            }

            _settingsFaultFinder            = settingsFaultFinder;
            _currentOptions                 = currentOptions;
            _outlookAccountPasswordProvider = outlookAccountPasswordProvider;

            _testConnectionCommand = new DelegateCommandWithoutCanExecuteDelegation(_ =>
            {
                ComponentContainer.EnsureSynchronizationContext();
                TestConnectionAsync();
            });
            _getAccountSettingsCommand = new DelegateCommandWithoutCanExecuteDelegation(_ =>
            {
                ComponentContainer.EnsureSynchronizationContext();
                GetAccountSettings();
            });
            _createDavResourceCommand = new DelegateCommandWithoutCanExecuteDelegation(_ =>
            {
                ComponentContainer.EnsureSynchronizationContext();
                CreateDavResource();
            });
        }
        private static async Task <string> TestGoogleContactsConnection(ICurrentOptions currentOptions, OlItemType outlookFolderType, string url)
        {
            var service = await GoogleHttpClientFactory.LoginToContactsService(currentOptions.EmailAddress, currentOptions.GetProxyIfConfigured());

            try
            {
                await Task.Run(() => service.GetGroups());
            }
            catch (Exception x)
            {
                s_logger.Error(null, x);
                MessageBox.Show(x.Message, ConnectionTestCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(url);
            }
            TestResult result = new TestResult(
                ResourceType.AddressBook,
                CalendarProperties.None,
                AddressBookProperties.AddressBookAccessSupported | AddressBookProperties.IsWriteable);

            DisplayTestReport(
                result,
                currentOptions.SynchronizationMode,
                currentOptions.SynchronizationModeDisplayName,
                outlookFolderType);
            return(string.Empty);
        }
        public static async Task <string> CreateDavResource(ICurrentOptions environment, string url)
        {
            if (environment.OutlookFolderType == null)
            {
                MessageBox.Show("Please select an Outlook folder to specify the item type for this profile", ConnectionTestCaption);
                return(url);
            }

            var outlookFolderType = environment.OutlookFolderType.Value;

            string        serverUrl           = url;
            StringBuilder errorMessageBuilder = new StringBuilder();

            if (!ValidateWebDavUrl(serverUrl, errorMessageBuilder, false))
            {
                MessageBox.Show(errorMessageBuilder.ToString(), "The CalDav/CardDav Url is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(url);
            }

            var enteredUri     = new Uri(serverUrl);
            var webDavClient   = environment.CreateWebDavClient(enteredUri);
            var newResourceUri = await AddResource(enteredUri, webDavClient, outlookFolderType);

            return(newResourceUri.ToString());
        }
 public MappingConfigurationViewModelFactory (IReadOnlyList<string> availableCategories, ICurrentOptions currentOptions)
 {
   if (availableCategories == null)
     throw new ArgumentNullException (nameof (availableCategories));
   if (currentOptions == null)
     throw new ArgumentNullException (nameof (currentOptions));
   _availableCategories = availableCategories;
   _currentOptions = currentOptions;
 }
        private static async Task <string> TestGoogleTaskConnection(ICurrentOptions currentOptions, StringBuilder errorMessageBuilder, OlItemType outlookFolderType, string url)
        {
            var service = await GoogleHttpClientFactory.LoginToGoogleTasksService(currentOptions.EmailAddress, currentOptions.GetProxyIfConfigured());

            string connectionTestUrl;

            if (string.IsNullOrEmpty(url))
            {
                TaskLists taskLists = await service.Tasklists.List().ExecuteAsync();

                if (taskLists.Items.Any())
                {
                    var selectedTaskList = SelectTaskList(taskLists.Items.Select(i => new TaskListData(i.Id, i.Title)).ToArray());
                    if (selectedTaskList != null)
                    {
                        connectionTestUrl = selectedTaskList.Id;
                    }
                    else
                    {
                        return(url);
                    }
                }
                else
                {
                    connectionTestUrl = url;
                }
            }
            else
            {
                connectionTestUrl = url;
            }

            try
            {
                await service.Tasklists.Get(connectionTestUrl).ExecuteAsync();
            }
            catch (Exception x)
            {
                s_logger.Error(null, x);
                errorMessageBuilder.AppendFormat("The tasklist with id '{0}' is invalid.", connectionTestUrl);
                MessageBox.Show(errorMessageBuilder.ToString(), "The tasklist is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(url);
            }
            TestResult result = new TestResult(ResourceType.TaskList, CalendarProperties.None, AddressBookProperties.None);

            DisplayTestReport(
                result,
                currentOptions.SynchronizationMode,
                currentOptions.SynchronizationModeDisplayName,
                outlookFolderType);
            return(connectionTestUrl);
        }
Exemplo n.º 8
0
 public MappingConfigurationViewModelFactory(IReadOnlyList <string> availableCategories, ICurrentOptions currentOptions)
 {
     if (availableCategories == null)
     {
         throw new ArgumentNullException(nameof(availableCategories));
     }
     if (currentOptions == null)
     {
         throw new ArgumentNullException(nameof(currentOptions));
     }
     _availableCategories = availableCategories;
     _currentOptions      = currentOptions;
 }
Exemplo n.º 9
0
        private static async Task TestGoogleTaskConnection(ICurrentOptions currentOptions, StringBuilder errorMessageBuilder, OlItemType outlookFolderType)
        {
            var service = await GoogleHttpClientFactory.LoginToGoogleTasksService(currentOptions.EmailAddress, currentOptions.GetProxyIfConfigured());

            if (string.IsNullOrEmpty(currentOptions.ServerUrl))
            {
                TaskLists taskLists = await service.Tasklists.List().ExecuteAsync();

                if (taskLists.Items.Any())
                {
                    using (SelectResourceForm listCalendarsForm =
                               new SelectResourceForm(
                                   new Tuple <Uri, string, ArgbColor?>[] { },
                                   new Tuple <Uri, string>[] { },
                                   taskLists.Items.Select(i => Tuple.Create(i.Id, i.Title)).ToArray(),
                                   ResourceType.TaskList))
                    {
                        if (listCalendarsForm.ShowDialog() == DialogResult.OK)
                        {
                            currentOptions.ServerUrl = listCalendarsForm.SelectedUrl;
                        }
                        else
                        {
                            return;
                        }
                    }
                }
            }

            try
            {
                await service.Tasklists.Get(currentOptions.ServerUrl).ExecuteAsync();
            }
            catch (Exception)
            {
                errorMessageBuilder.AppendFormat("The tasklist with id '{0}' is invalid.", currentOptions.ServerUrl);
                MessageBox.Show(errorMessageBuilder.ToString(), "The tasklist is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            TestResult result = new TestResult(ResourceType.TaskList, CalendarProperties.None, AddressBookProperties.None);

            DisplayTestReport(
                result,
                currentOptions.SynchronizationMode,
                currentOptions.SynchronizationModeDisplayName,
                outlookFolderType);
        }
    public GoogleServerSettingsViewModel (ISettingsFaultFinder settingsFaultFinder, ICurrentOptions currentOptions)
    {
      if (settingsFaultFinder == null)
        throw new ArgumentNullException (nameof (settingsFaultFinder));
      if (currentOptions == null)
        throw new ArgumentNullException (nameof (currentOptions));

      _settingsFaultFinder = settingsFaultFinder;
      _currentOptions = currentOptions;
      _currentOptions.OutlookFolderTypeChanged += CurrentOptions_OutlookFolderTypeChanged;
      _doAutoDiscoveryCommand = new DelegateCommandWithoutCanExecuteDelegation (_ => DoAutoDiscovery());
      _testConnectionCommand = new DelegateCommandWithoutCanExecuteDelegation (_ =>
      {
        ComponentContainer.EnsureSynchronizationContext();
        TestConnectionAsync (CalenderUrl);
      });
    }
        public ServerSettingsViewModel(ISettingsFaultFinder settingsFaultFinder, ICurrentOptions currentOptions)
        {
            if (settingsFaultFinder == null)
            {
                throw new ArgumentNullException(nameof(settingsFaultFinder));
            }
            if (currentOptions == null)
            {
                throw new ArgumentNullException(nameof(currentOptions));
            }

            _settingsFaultFinder   = settingsFaultFinder;
            _currentOptions        = currentOptions;
            _testConnectionCommand = new DelegateCommandWithoutCanExecuteDelegation(_ =>
            {
                ComponentContainer.EnsureSynchronizationContext();
                TestConnectionAsync();
            });
        }
Exemplo n.º 12
0
        public GoogleServerSettingsViewModel(ISettingsFaultFinder settingsFaultFinder, ICurrentOptions currentOptions)
        {
            if (settingsFaultFinder == null)
            {
                throw new ArgumentNullException(nameof(settingsFaultFinder));
            }
            if (currentOptions == null)
            {
                throw new ArgumentNullException(nameof(currentOptions));
            }

            _settingsFaultFinder = settingsFaultFinder;
            _currentOptions      = currentOptions;
            _currentOptions.OutlookFolderTypeChanged += CurrentOptions_OutlookFolderTypeChanged;
            _doAutoDiscoveryCommand = new DelegateCommandWithoutCanExecuteDelegation(_ => DoAutoDiscovery());
            _testConnectionCommand  = new DelegateCommandWithoutCanExecuteDelegation(_ =>
            {
                ComponentContainer.EnsureSynchronizationContext();
                TestConnectionAsync(CalenderUrl);
            });
        }
 IMappingConfigurationViewModelFactory CreateMappingConfigurationViewModelFactory(ICurrentOptions currentOptions)
 {
     return(new MappingConfigurationViewModelFactory(_availableCategories, currentOptions));
 }
 IServerSettingsViewModel CreateServerSettingsViewModel(ISettingsFaultFinder settingsFaultFinder, ICurrentOptions currentOptions)
 {
     return(new ServerSettingsViewModel(settingsFaultFinder, currentOptions, _outlookAccountPasswordProvider));
 }
 IServerSettingsViewModel CreateGoogleServerSettingsViewModel(ISettingsFaultFinder settingsFaultFinder, ICurrentOptions currentOptions)
 {
     return(new GoogleServerSettingsViewModel(settingsFaultFinder, currentOptions));
 }
Exemplo n.º 16
0
        public static async Task TestGoogleConnection(ICurrentOptions currentOptions, ISettingsFaultFinder settingsFaultFinder)
        {
            if (currentOptions.OutlookFolderType == null)
            {
                MessageBox.Show("Please select an Outlook folder to specify the item type for this profile", ConnectionTestCaption);
                return;
            }

            var outlookFolderType = currentOptions.OutlookFolderType.Value;

            StringBuilder errorMessageBuilder = new StringBuilder();

            if (!ValidateGoogleEmailAddress(errorMessageBuilder, currentOptions.EmailAddress))
            {
                MessageBox.Show(errorMessageBuilder.ToString(), "The Email Address is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (outlookFolderType == OlItemType.olTaskItem)
            {
                await TestGoogleTaskConnection(currentOptions, errorMessageBuilder, outlookFolderType);

                return;
            }

            if (!ValidateWebDavUrl(currentOptions.ServerUrl, errorMessageBuilder, false))
            {
                MessageBox.Show(errorMessageBuilder.ToString(), "The CalDav/CardDav Url is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var enteredUri   = new Uri(currentOptions.ServerUrl);
            var webDavClient = currentOptions.CreateWebDavClient();

            Uri          autoDiscoveredUrl;
            ResourceType autoDiscoveredResourceType;

            if (ConnectionTester.RequiresAutoDiscovery(enteredUri))
            {
                var calDavDataAccess = new CalDavDataAccess(enteredUri, webDavClient);
                var foundCaldendars  = await calDavDataAccess.GetUserCalendarsNoThrow(false);

                var cardDavDataAccess = new CardDavDataAccess(enteredUri, webDavClient);
                IReadOnlyList <Tuple <Uri, string> > foundAddressBooks = await cardDavDataAccess.GetUserAddressBooksNoThrow(true);

                if (foundCaldendars.Count > 0 || foundAddressBooks.Count > 0)
                {
                    ResourceType initalResourceType;
                    if (currentOptions.OutlookFolderType == OlItemType.olContactItem)
                    {
                        initalResourceType = ResourceType.AddressBook;
                    }
                    else if (currentOptions.OutlookFolderType == OlItemType.olTaskItem)
                    {
                        initalResourceType = ResourceType.TaskList;
                    }
                    else
                    {
                        initalResourceType = ResourceType.Calendar;
                    }

                    using (SelectResourceForm listCalendarsForm =
                               new SelectResourceForm(
                                   foundCaldendars,
                                   foundAddressBooks,
                                   new Tuple <string, string>[] {},
                                   initalResourceType))
                    {
                        if (listCalendarsForm.ShowDialog() == DialogResult.OK)
                        {
                            if (listCalendarsForm.ResourceType == ResourceType.TaskList)
                            {
                                autoDiscoveredUrl        = null;
                                currentOptions.ServerUrl = listCalendarsForm.SelectedUrl;
                            }
                            else
                            {
                                autoDiscoveredUrl = new Uri(enteredUri.GetLeftPart(UriPartial.Authority) + listCalendarsForm.SelectedUrl);
                            }
                            autoDiscoveredResourceType = listCalendarsForm.ResourceType;
                        }
                        else
                        {
                            autoDiscoveredUrl          = null;
                            autoDiscoveredResourceType = ResourceType.None;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("No resources were found via autodiscovery!", ConnectionTestCaption);
                    autoDiscoveredUrl          = null;
                    autoDiscoveredResourceType = ResourceType.None;
                }
            }
            else
            {
                var result = await ConnectionTester.TestConnection(enteredUri, webDavClient, ResourceType.None);

                if (result.ResourceType != ResourceType.None)
                {
                    settingsFaultFinder.FixSynchronizationMode(result);
                }
                DisplayTestReport(
                    result,
                    currentOptions.SynchronizationMode,
                    currentOptions.SynchronizationModeDisplayName,
                    outlookFolderType);
                return;
            }

            if (autoDiscoveredUrl != null)
            {
                currentOptions.ServerUrl = autoDiscoveredUrl.ToString();
                var finalResult =
                    await ConnectionTester.TestConnection(autoDiscoveredUrl, webDavClient, autoDiscoveredResourceType);

                settingsFaultFinder.FixSynchronizationMode(finalResult);

                DisplayTestReport(
                    finalResult,
                    currentOptions.SynchronizationMode,
                    currentOptions.SynchronizationModeDisplayName,
                    outlookFolderType);
            }
            else if (outlookFolderType == OlItemType.olTaskItem)
            {
                TestResult result = new TestResult(ResourceType.TaskList, CalendarProperties.None, AddressBookProperties.None);

                DisplayTestReport(
                    result,
                    currentOptions.SynchronizationMode,
                    currentOptions.SynchronizationModeDisplayName,
                    outlookFolderType);
            }
        }
    public EventMappingConfigurationViewModel (IReadOnlyList<string> availableCategories, ICurrentOptions currentOptions)
    {
      if (availableCategories == null)
        throw new ArgumentNullException (nameof (availableCategories));
      if (currentOptions == null)
        throw new ArgumentNullException (nameof (currentOptions));

      AvailableCategories = availableCategories;
      _currentOptions = currentOptions;
      SetServerCalendarColorCommand = new DelegateCommand (_ =>
      {
        ComponentContainer.EnsureSynchronizationContext();
        SetServerCalendarColorAsync();
      });
      GetServerCalendarColorCommand = new DelegateCommand (_ =>
      {
        ComponentContainer.EnsureSynchronizationContext();
        GetServerCalendarColorAsync();
      });

      _customPropertyMappingViewModel = new CustomPropertyMappingViewModel();
      Items = new[] {_customPropertyMappingViewModel};
    }
Exemplo n.º 18
0
        public static async Task TestWebDavConnection(ICurrentOptions environment, ISettingsFaultFinder settingsFaultFinder)
        {
            if (environment.OutlookFolderType == null)
            {
                MessageBox.Show("Please select an Outlook folder to specify the item type for this profile", ConnectionTestCaption);
                return;
            }

            var outlookFolderType = environment.OutlookFolderType.Value;

            StringBuilder errorMessageBuilder = new StringBuilder();

            if (!ValidateWebDavUrl(environment.ServerUrl, errorMessageBuilder, false))
            {
                MessageBox.Show(errorMessageBuilder.ToString(), "The CalDav/CardDav Url is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var enteredUri   = new Uri(environment.ServerUrl);
            var webDavClient = environment.CreateWebDavClient();

            Uri          autoDiscoveredUrl;
            ResourceType autoDiscoveredResourceType;

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

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

                    if (autodiscoveryResult2.WasCancelled)
                    {
                        return;
                    }
                    if (autodiscoveryResult2.RessourceUrl != null)
                    {
                        autoDiscoveredUrl          = autodiscoveryResult2.RessourceUrl;
                        autoDiscoveredResourceType = autodiscoveryResult2.ResourceType;
                    }
                    else
                    {
                        MessageBox.Show("No resources were found via autodiscovery!", ConnectionTestCaption);
                        return;
                    }
                }
            }
            else
            {
                var result = await ConnectionTester.TestConnection(enteredUri, webDavClient, ResourceType.None);

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

                    DisplayTestReport(
                        result,
                        environment.SynchronizationMode,
                        environment.SynchronizationModeDisplayName,
                        outlookFolderType);
                    return;
                }
                else
                {
                    var autodiscoveryResult = await DoAutoDiscovery(enteredUri, webDavClient, false, false, environment.OutlookFolderType);

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

                        if (autodiscoveryResult2.RessourceUrl != null)
                        {
                            autoDiscoveredUrl          = autodiscoveryResult2.RessourceUrl;
                            autoDiscoveredResourceType = autodiscoveryResult2.ResourceType;
                        }
                        else
                        {
                            MessageBox.Show("No resources were found via autodiscovery!", ConnectionTestCaption);
                            return;
                        }
                    }
                }
            }

            environment.ServerUrl = autoDiscoveredUrl.ToString();

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

            settingsFaultFinder.FixSynchronizationMode(finalResult);

            DisplayTestReport(
                finalResult,
                environment.SynchronizationMode,
                environment.SynchronizationModeDisplayName,
                outlookFolderType);
        }
        public static async Task <string> TestGoogleConnection(ICurrentOptions currentOptions, ISettingsFaultFinder settingsFaultFinder, string url)
        {
            if (currentOptions.OutlookFolderType == null)
            {
                MessageBox.Show("Please select an Outlook folder to specify the item type for this profile", ConnectionTestCaption);
                return(url);
            }

            var outlookFolderType = currentOptions.OutlookFolderType.Value;

            StringBuilder errorMessageBuilder = new StringBuilder();

            if (!ValidateGoogleEmailAddress(errorMessageBuilder, currentOptions.EmailAddress))
            {
                MessageBox.Show(errorMessageBuilder.ToString(), "The Email Address is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(url);
            }

            if (outlookFolderType == OlItemType.olTaskItem)
            {
                return(await TestGoogleTaskConnection(currentOptions, errorMessageBuilder, outlookFolderType, url));
            }

            if (outlookFolderType == OlItemType.olContactItem && currentOptions.ServerAdapterType == ServerAdapterType.GoogleContactApi)
            {
                return(await TestGoogleContactsConnection(currentOptions, outlookFolderType, url));
            }

            if (!ValidateWebDavUrl(url, errorMessageBuilder, false))
            {
                MessageBox.Show(errorMessageBuilder.ToString(), "The CalDav/CardDav Url is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(url);
            }

            var enteredUri   = new Uri(url);
            var webDavClient = currentOptions.CreateWebDavClient(enteredUri);

            Uri autoDiscoveredUrl;

            if (ConnectionTester.RequiresAutoDiscovery(enteredUri))
            {
                var autoDiscoveryResult = await DoAutoDiscovery(enteredUri, webDavClient, false, true, outlookFolderType);

                switch (autoDiscoveryResult.Status)
                {
                case AutoDiscoverResultStatus.UserCancelled:
                    return(url);

                case AutoDiscoverResultStatus.ResourceSelected:
                    autoDiscoveredUrl = autoDiscoveryResult.RessourceUrl;
                    break;

                default:
                    autoDiscoveredUrl = null;
                    break;
                }
            }
            else
            {
                autoDiscoveredUrl = null;
            }


            var finalUrl = autoDiscoveredUrl?.ToString() ?? url;

            var result = await ConnectionTester.TestConnection(new Uri(finalUrl), webDavClient);

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

            if (outlookFolderType == OlItemType.olContactItem)
            {
                // Google Addressbook doesn't have any properties. As long as there doesn't occur an exception, the test is successful.
                MessageBox.Show("Connection test successful.", ConnectionTestCaption);
            }
            else
            {
                DisplayTestReport(
                    result,
                    currentOptions.SynchronizationMode,
                    currentOptions.SynchronizationModeDisplayName,
                    outlookFolderType);
            }

            return(finalUrl);
        }
        public static async Task <string> TestWebDavConnection(ICurrentOptions environment, ISettingsFaultFinder settingsFaultFinder, string url)
        {
            if (environment.OutlookFolderType == null)
            {
                MessageBox.Show("Please select an Outlook folder to specify the item type for this profile", ConnectionTestCaption);
                return(url);
            }

            var outlookFolderType = environment.OutlookFolderType.Value;

            StringBuilder errorMessageBuilder = new StringBuilder();

            if (!ValidateWebDavUrl(url, errorMessageBuilder, false))
            {
                MessageBox.Show(errorMessageBuilder.ToString(), "The CalDav/CardDav Url is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(url);
            }

            var enteredUri   = new Uri(url);
            var webDavClient = environment.CreateWebDavClient(enteredUri);

            Uri autoDiscoveredUrl;

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

                switch (autodiscoveryResult.Status)
                {
                case AutoDiscoverResultStatus.UserCancelled:
                    return(url);

                case AutoDiscoverResultStatus.ResourceSelected:
                    autoDiscoveredUrl = autodiscoveryResult.RessourceUrl;
                    break;

                case AutoDiscoverResultStatus.NoResourcesFound:
                    var autodiscoveryResult2 = await DoAutoDiscovery(enteredUri.AbsolutePath.EndsWith ("/")?enteredUri : new Uri(enteredUri.ToString() + "/"), webDavClient, false, false, outlookFolderType);

                    switch (autodiscoveryResult2.Status)
                    {
                    case AutoDiscoverResultStatus.UserCancelled:
                        return(url);

                    case AutoDiscoverResultStatus.ResourceSelected:
                        autoDiscoveredUrl = autodiscoveryResult2.RessourceUrl;
                        break;

                    case AutoDiscoverResultStatus.NoResourcesFound:
                        MessageBox.Show("No resources were found via autodiscovery!", ConnectionTestCaption);
                        return(url);

                    default:
                        throw new NotImplementedException(autodiscoveryResult2.Status.ToString());
                    }
                    break;

                default:
                    throw new NotImplementedException(autodiscoveryResult.Status.ToString());
                }
            }
            else
            {
                var result = await ConnectionTester.TestConnection(enteredUri, webDavClient);

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

                    DisplayTestReport(
                        result,
                        environment.SynchronizationMode,
                        environment.SynchronizationModeDisplayName,
                        outlookFolderType);
                    return(url);
                }
                else
                {
                    var autodiscoveryResult = await DoAutoDiscovery(enteredUri, webDavClient, false, false, outlookFolderType);

                    switch (autodiscoveryResult.Status)
                    {
                    case AutoDiscoverResultStatus.UserCancelled:
                        return(url);

                    case AutoDiscoverResultStatus.ResourceSelected:
                        autoDiscoveredUrl = autodiscoveryResult.RessourceUrl;
                        break;

                    case AutoDiscoverResultStatus.NoResourcesFound:
                        var autodiscoveryResult2 = await DoAutoDiscovery(enteredUri, webDavClient, true, true, outlookFolderType);

                        switch (autodiscoveryResult2.Status)
                        {
                        case AutoDiscoverResultStatus.UserCancelled:
                            return(url);

                        case AutoDiscoverResultStatus.ResourceSelected:
                            autoDiscoveredUrl = autodiscoveryResult2.RessourceUrl;
                            break;

                        case AutoDiscoverResultStatus.NoResourcesFound:
                            MessageBox.Show("No resources were found via autodiscovery!", ConnectionTestCaption);
                            return(url);

                        default:
                            throw new NotImplementedException(autodiscoveryResult2.Status.ToString());
                        }
                        break;

                    default:
                        throw new NotImplementedException(autodiscoveryResult.Status.ToString());
                    }
                }
            }


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

            settingsFaultFinder.FixSynchronizationMode(finalResult);

            DisplayTestReport(
                finalResult,
                environment.SynchronizationMode,
                environment.SynchronizationModeDisplayName,
                outlookFolderType);

            return(autoDiscoveredUrl.ToString());
        }