Files
Wino-Mail/Wino.Mail/Views/ImapSetup/TestingImapConnectionPage.xaml.cs
T

133 lines
5.0 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
2024-09-14 21:51:43 +02:00
using Wino.Core.Domain;
2024-11-10 23:28:25 +01:00
using Wino.Core.Domain.Entities.Shared;
using Wino.Core.Domain.Exceptions;
2024-06-07 23:58:51 +02:00
using Wino.Core.Domain.Interfaces;
2024-04-18 01:44:37 +02:00
using Wino.Core.Domain.Models.AutoDiscovery;
2024-09-14 21:51:43 +02:00
using Wino.Core.Domain.Models.Connectivity;
using Wino.Messaging.Client.Mails;
2024-09-14 21:51:43 +02:00
using Wino.Messaging.Server;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
namespace Wino.Views.ImapSetup;
public sealed partial class TestingImapConnectionPage : Page
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
private IWinoServerConnectionManager _winoServerConnectionManager = App.Current.Services.GetService<IWinoServerConnectionManager>();
private AutoDiscoverySettings autoDiscoverySettings;
private CustomServerInformation serverInformationToTest;
public TestingImapConnectionPage()
{
InitializeComponent();
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
base.OnNavigatedTo(e);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// We can only go back to this page from failed connection page.
// We must go back once again in that case to actual setup dialog.
if (e.NavigationMode == NavigationMode.Back)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
WeakReferenceMessenger.Default.Send(new ImapSetupBackNavigationRequested());
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
else
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
// Test connection
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Discovery settings are passed.
// Create server information out of the discovery settings.
if (e.Parameter is AutoDiscoverySettings parameterAutoDiscoverySettings)
{
2025-02-16 11:54:23 +01:00
autoDiscoverySettings = parameterAutoDiscoverySettings;
serverInformationToTest = autoDiscoverySettings.ToServerInformation();
}
2025-02-16 11:54:23 +01:00
else if (e.Parameter is CustomServerInformation customServerInformation)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
// Only server information is passed.
serverInformationToTest = customServerInformation;
2025-02-16 11:43:30 +01:00
}
2025-02-16 11:54:23 +01:00
// Make sure that certificate dialog must be present in case of SSL handshake fails.
await PerformTestAsync(allowSSLHandshake: false);
2025-02-16 11:43:30 +01:00
}
2025-02-16 11:54:23 +01:00
}
2024-09-14 21:51:43 +02:00
2025-02-16 11:54:23 +01:00
private async Task PerformTestAsync(bool allowSSLHandshake)
{
CertificateDialog.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
TestingConnectionPanel.Visibility = Windows.UI.Xaml.Visibility.Visible;
2024-09-14 21:51:43 +02:00
2025-02-16 11:54:23 +01:00
await Task.Delay(1000);
2024-09-14 21:51:43 +02:00
2025-02-16 11:54:23 +01:00
var testResultResponse = await _winoServerConnectionManager
.GetResponseAsync<ImapConnectivityTestResults, ImapConnectivityTestRequested>(new ImapConnectivityTestRequested(serverInformationToTest, allowSSLHandshake));
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
if (!testResultResponse.IsSuccess)
{
// Wino Server is connection is failed.
ReturnWithError(testResultResponse.Message);
}
else
{
var testResultData = testResultResponse.Data;
if (testResultData.IsSuccess)
2024-09-14 21:51:43 +02:00
{
2025-02-16 11:54:23 +01:00
// All success. Finish setup with validated server information.
ReturnWithSuccess();
2024-09-14 21:51:43 +02:00
}
else
{
2025-02-16 11:54:23 +01:00
// Check if certificate UI is required.
2024-09-14 21:51:43 +02:00
2025-02-16 11:54:23 +01:00
if (testResultData.IsCertificateUIRequired)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
// Certificate UI is required. Show certificate dialog.
CertIssuer.Text = testResultData.CertificateIssuer;
CertValidFrom.Text = testResultData.CertificateValidFromDateString;
CertValidTo.Text = testResultData.CertificateExpirationDateString;
TestingConnectionPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
CertificateDialog.Visibility = Windows.UI.Xaml.Visibility.Visible;
2024-04-18 01:44:37 +02:00
}
2024-09-14 21:51:43 +02:00
else
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
// Connection test failed. Show error dialog.
2024-09-14 21:51:43 +02:00
2025-02-16 11:54:23 +01:00
var protocolLog = testResultData.FailureProtocolLog;
2024-09-14 21:51:43 +02:00
2025-02-16 11:54:23 +01:00
ReturnWithError(testResultData.FailedReason, protocolLog);
2024-04-18 01:44:37 +02:00
}
}
}
2025-02-16 11:54:23 +01:00
}
2024-09-14 21:51:43 +02:00
2025-02-16 11:54:23 +01:00
private void ReturnWithError(string error, string protocolLog = "")
{
var failurePackage = new ImapConnectionFailedPackage(error, protocolLog, autoDiscoverySettings);
WeakReferenceMessenger.Default.Send(new ImapSetupBackNavigationRequested(typeof(ImapConnectionFailedPage), failurePackage));
}
2024-09-14 21:51:43 +02:00
2025-02-16 11:54:23 +01:00
private void ReturnWithSuccess()
=> WeakReferenceMessenger.Default.Send(new ImapSetupDismissRequested(serverInformationToTest));
2024-09-14 21:51:43 +02:00
2025-02-16 11:54:23 +01:00
private void DenyClicked(object sender, Windows.UI.Xaml.RoutedEventArgs e)
=> ReturnWithError(Translator.IMAPSetupDialog_CertificateDenied, string.Empty);
2024-09-14 21:51:43 +02:00
2025-02-16 11:54:23 +01:00
private async void AllowClicked(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
// Run the test again, but this time allow SSL handshake.
// Any authentication error will be shown to the user after this test.
2024-09-14 21:51:43 +02:00
2025-02-16 11:54:23 +01:00
await PerformTestAsync(allowSSLHandshake: true);
2024-04-18 01:44:37 +02:00
}
}