Inheritance: WebDavDataAccess, ICardDavDataAccess
    public static async Task<TestResult> TestConnection (Uri url, IWebDavClient webDavClient)
    {
      var calDavDataAccess = new CalDavDataAccess (url, webDavClient);
      var cardDavDataAccess = new CardDavDataAccess (url, webDavClient);

      var ressourceType =
          (await calDavDataAccess.IsResourceCalender() ? ResourceType.Calendar : ResourceType.None) |
          (await cardDavDataAccess.IsResourceAddressBook() ? ResourceType.AddressBook : ResourceType.None);


      return new TestResult (
          ressourceType,
          ressourceType.HasFlag (ResourceType.Calendar) ? await GetCalendarProperties (calDavDataAccess) : CalendarProperties.None,
          ressourceType.HasFlag (ResourceType.AddressBook) ? await GetAddressBookProperties (cardDavDataAccess) : AddressBookProperties.None);
    }
    public static async Task<TestResult> TestConnection (Uri url, IWebDavClient webDavClient, ResourceType supposedRessourceType)
    {
      var calDavDataAccess = new CalDavDataAccess (url, webDavClient);
      var cardDavDataAccess = new CardDavDataAccess (url, webDavClient);

      TestResult result;


      switch (supposedRessourceType)
      {
        case ResourceType.None:
          var ressourceType =
              (await calDavDataAccess.IsResourceCalender() ? ResourceType.Calendar : ResourceType.None) |
              (await cardDavDataAccess.IsResourceAddressBook() ? ResourceType.AddressBook : ResourceType.None);

          result = new TestResult (
              ressourceType,
              ressourceType.HasFlag (ResourceType.Calendar) ? await GetCalendarProperties (calDavDataAccess) : CalendarProperties.None,
              ressourceType.HasFlag (ResourceType.AddressBook) ? await GetAddressBookProperties (cardDavDataAccess) : AddressBookProperties.None);
              break;

        case ResourceType.Calendar:
          result = new TestResult (
              supposedRessourceType,
              CalendarProperties.CalendarAccessSupported |
              (await calDavDataAccess.IsWriteable() ? CalendarProperties.IsWriteable : CalendarProperties.None) |
              (await calDavDataAccess.DoesSupportCalendarQuery() ? CalendarProperties.SupportsCalendarQuery : CalendarProperties.None),
              AddressBookProperties.None);
          break;
        
        case ResourceType.AddressBook:
          result = new TestResult (
              supposedRessourceType,
              CalendarProperties.None,
              AddressBookProperties.AddressBookAccessSupported |
              (await cardDavDataAccess.IsWriteable() ? AddressBookProperties.IsWriteable : AddressBookProperties.None));
          break;
        
        default:
          throw new ArgumentOutOfRangeException ("supposedRessourceType");
      }

      return result;
    }
    public static async Task<TestResult> TestConnection (Uri url, IWebDavClient webDavClient, ResourceType autoRessourceType)
    {
      var calDavDataAccess = new CalDavDataAccess (url, webDavClient);
      var cardDavDataAccess = new CardDavDataAccess (url, webDavClient);

      TestResult result;

      if (autoRessourceType == ResourceType.None)
      {
        var ressourceType =
          (await calDavDataAccess.IsResourceCalender() ? ResourceType.Calendar : ResourceType.None) |
          (await cardDavDataAccess.IsResourceAddressBook() ? ResourceType.AddressBook : ResourceType.None);

        result = new TestResult(
          ressourceType,
          ressourceType.HasFlag(ResourceType.Calendar) ? await GetCalendarProperties(calDavDataAccess) : CalendarProperties.None,
          ressourceType.HasFlag(ResourceType.AddressBook) ? await GetAddressBookProperties(cardDavDataAccess) : AddressBookProperties.None);
      }
      else if (autoRessourceType == ResourceType.Calendar)
      {
        result = new TestResult(
          autoRessourceType,
          CalendarProperties.CalendarAccessSupported |
          (await calDavDataAccess.IsWriteable() ? CalendarProperties.IsWriteable : CalendarProperties.None) |
          (await calDavDataAccess.DoesSupportCalendarQuery() ? CalendarProperties.SupportsCalendarQuery : CalendarProperties.None),
          AddressBookProperties.None);
      }
      else
      {
        result = new TestResult(
          autoRessourceType,
          CalendarProperties.None,
          AddressBookProperties.AddressBookAccessSupported |
          (await cardDavDataAccess.IsWriteable() ? AddressBookProperties.IsWriteable : AddressBookProperties.None));
      }

      return result;
    }
    private async Task<AutoDiscoveryResult> DoAutoDiscovery (Uri autoDiscoveryUri, IWebDavClient webDavClient, bool useWellKnownUrl)
    {
      var calDavDataAccess = new CalDavDataAccess (autoDiscoveryUri, webDavClient);
      IReadOnlyList<Tuple<Uri, string>> foundCaldendars = await calDavDataAccess.GetUserCalendarsNoThrow (useWellKnownUrl);

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

      if (foundCaldendars.Count > 0 || foundAddressBooks.Count > 0)
      {
        using (SelectResourceForm listCalendarsForm = new SelectResourceForm (foundCaldendars, foundAddressBooks, _folderType == OlItemType.olContactItem))
        {
          if (listCalendarsForm.ShowDialog() == DialogResult.OK)
            return new AutoDiscoveryResult (new Uri (autoDiscoveryUri.GetLeftPart (UriPartial.Authority) + listCalendarsForm.SelectedUrl), false, listCalendarsForm.ResourceType);
          else
            return new AutoDiscoveryResult (null, true, ResourceType.None);
        }
      }
      else
      {
        MessageBox.Show ("No resources were found via autodiscovery!", c_connectionTestCaption);
        return new AutoDiscoveryResult (null, false, ResourceType.None);
      }
    }
 private static async Task<ServerResources> GetUserResources (CalDavDataAccess calDavDataAccess, CardDavDataAccess cardDavDataAccess)
 {
   var calDavResources = await calDavDataAccess.GetUserResourcesNoThrow (true);
   if (calDavResources.CalendarResources.Count == 0 && calDavResources.TaskListResources.Count == 0)
     calDavResources = await calDavDataAccess.GetUserResourcesNoThrow (false);
   var foundAddressBooks = await cardDavDataAccess.GetUserAddressBooksNoThrow (true);
   if (foundAddressBooks.Count == 0)
     foundAddressBooks = await cardDavDataAccess.GetUserAddressBooksNoThrow (false);
   return new ServerResources (calDavResources.CalendarResources, foundAddressBooks, calDavResources.TaskListResources);
 }
    public async Task<ServerResources> GetServerResources (NetworkSettingsViewModel networkSettings, GeneralOptions generalOptions)
    {
      string caldavUrlString ;
      string carddavUrlString;

      if (string.IsNullOrEmpty (CalenderUrl) && !string.IsNullOrEmpty (EmailAddress))
      {
        bool success;
        caldavUrlString = OptionTasks.DoSrvLookup (EmailAddress, OlItemType.olAppointmentItem, out success);
        carddavUrlString = OptionTasks.DoSrvLookup (EmailAddress, OlItemType.olContactItem, out success);
      }
      else
      {
        caldavUrlString = CalenderUrl;
        carddavUrlString = CalenderUrl;
      }

      var trimmedCaldavUrl = caldavUrlString.Trim();
      var caldavUrl = new Uri (trimmedCaldavUrl.EndsWith ("/") ? trimmedCaldavUrl : trimmedCaldavUrl + "/");

      var trimmedCarddavUrl = carddavUrlString.Trim();
      var carddavUrl = new Uri (trimmedCarddavUrl.EndsWith("/") ? trimmedCarddavUrl : trimmedCarddavUrl + "/");

      var webDavClientCaldav = CreateWebDavClient (networkSettings, generalOptions, trimmedCaldavUrl);
      var webDavClientCarddav = CreateWebDavClient (networkSettings, generalOptions, trimmedCarddavUrl);
      var calDavDataAccess = new CalDavDataAccess (caldavUrl, webDavClientCaldav);
      var cardDavDataAccess = new CardDavDataAccess (carddavUrl, webDavClientCarddav);

      return await GetUserResources (calDavDataAccess, cardDavDataAccess);
    }
 private static async Task<AddressBookProperties> GetAddressBookProperties (CardDavDataAccess cardDavDataAccess)
 {
   return
       (await cardDavDataAccess.IsAddressBookAccessSupported() ? AddressBookProperties.AddressBookAccessSupported : AddressBookProperties.None) |
       (await cardDavDataAccess.IsWriteable() ? AddressBookProperties.IsWriteable : AddressBookProperties.None);
 }
    private async Task TestGoogleConnection ()
    {
      StringBuilder errorMessageBuilder = new StringBuilder ();

      if (!OptionTasks.ValidateGoogleEmailAddress (errorMessageBuilder, _emailAddressTextBox.Text)) 
      {
        MessageBox.Show (errorMessageBuilder.ToString(), "The Email Address is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
      }
      if (UsedServerAdapterType == ServerAdapterType.GoogleTaskApi &&
          !string.IsNullOrWhiteSpace (_calenderUrlTextBox.Text) &&
          _calenderUrlTextBox.Text != c_googleDavBaseUrl)
      {
        var service = await OAuth.Google.GoogleHttpClientFactory.LoginToGoogleTasksService (_emailAddressTextBox.Text);

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

        OptionTasks.DisplayTestReport (result, false, _dependencies.SelectedSynchronizationModeDisplayName, _dependencies.OutlookFolderType, UsedServerAdapterType);
        return;
      }

      if (!OptionTasks.ValidateWebDavUrl (_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 calDavDataAccess = new CalDavDataAccess(enteredUri, webDavClient);
        IReadOnlyList<Tuple<Uri, string, string>> foundCaldendars = await calDavDataAccess.GetUserCalendarsNoThrow(false);

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

        var service = await OAuth.Google.GoogleHttpClientFactory.LoginToGoogleTasksService(_emailAddressTextBox.Text);

        TaskLists taskLists = await service.Tasklists.List().ExecuteAsync();

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

          using (SelectResourceForm listCalendarsForm =
              new SelectResourceForm(
                  foundCaldendars,
                  foundAddressBooks,
                  taskLists.Items.Select(i => Tuple.Create(i.Id, i.Title)).ToArray(),
                  initalResourceType))
          {
            if (listCalendarsForm.ShowDialog() == DialogResult.OK)
            {
              if (listCalendarsForm.ResourceType == ResourceType.TaskList)
              {
                autoDiscoveredUrl = null;
                _calenderUrlTextBox.Text = listCalendarsForm.SelectedUrl;
                UsedServerAdapterType = ServerAdapterType.GoogleTaskApi;
              }
              else
              {
                autoDiscoveredUrl = new Uri(enteredUri.GetLeftPart(UriPartial.Authority) + listCalendarsForm.SelectedUrl);
                UsedServerAdapterType = ServerAdapterType.WebDavHttpClientBasedWithGoogleOAuth;
              }
              autoDiscoveredResourceType = listCalendarsForm.ResourceType;
            }
            else
            {
              autoDiscoveredUrl = null;
              autoDiscoveredResourceType = ResourceType.None;
            }
          }
        }
        else
        {
          MessageBox.Show("No resources were found via autodiscovery!", OptionTasks.ConnectionTestCaption);
          autoDiscoveredUrl = null;
          autoDiscoveredResourceType = ResourceType.None;
        }
      }
      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,
            UsedServerAdapterType);
        return;
      }

      if (autoDiscoveredUrl != null)
      {
        _calenderUrlTextBox.Text = autoDiscoveredUrl.ToString();
        var finalResult =
          await ConnectionTester.TestConnection(autoDiscoveredUrl, webDavClient, autoDiscoveredResourceType);
        _settingsFaultFinder.FixSynchronizationMode(finalResult);

        OptionTasks.DisplayTestReport(
          finalResult,
          _dependencies.SelectedSynchronizationModeRequiresWriteableServerResource,
          _dependencies.SelectedSynchronizationModeDisplayName,
          _dependencies.OutlookFolderType,
          UsedServerAdapterType);
      }
      else if (UsedServerAdapterType == ServerAdapterType.GoogleTaskApi)
      {
        TestResult result = new TestResult(ResourceType.TaskList, CalendarProperties.None, AddressBookProperties.None);

        OptionTasks.DisplayTestReport (result,false,_dependencies.SelectedSynchronizationModeDisplayName, _dependencies.OutlookFolderType, UsedServerAdapterType);
      }
    }
    public static CardDavDataAccess CreateCardDavDataAccess (
        string calenderUrl,
        string username,
        string password,
        TimeSpan timeout,
        ServerAdapterType serverAdapterType)
    {
      var productAndVersion = GetProductAndVersion();

      var cardDavDataAccess = new CardDavDataAccess (
          new Uri (calenderUrl),
          new CardDavClient (
              () => CreateHttpClient (username, password, timeout, serverAdapterType),
              productAndVersion.Item1,
              productAndVersion.Item2));
      return cardDavDataAccess;
    }
    private async Task<bool> TestAddressBook (CardDavDataAccess cardDavDataAccess, StringBuilder errorMessageBuilder)
    {
      bool hasError = false;
      if (!await cardDavDataAccess.IsAddressBookAccessSupported())
      {
        errorMessageBuilder.AppendLine ("- The specified Url does not support addressbook.");
        hasError = true;
      }

      if (!await cardDavDataAccess.IsWriteable())
      {
        errorMessageBuilder.AppendLine ("- The specified Url is a read-only addressbook.");
        hasError = true;
      }

      if (_folderType != OlItemType.olContactItem)
      {
        errorMessageBuilder.AppendLine ("- The outlook folder is not a address book, or there is no folder selected.");
        hasError = true;
      }
      return hasError;
    }