Files
Wino-Mail/Wino.Mail/Views/ImapSetup/AdvancedImapSetupPage.xaml.cs

283 lines
12 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Collections.Generic;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.WinUI;
2024-04-18 01:44:37 +02:00
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Wino.Core.Domain;
using Wino.Core.Domain.Entities.Shared;
2024-04-18 01:44:37 +02:00
using Wino.Core.Domain.Models.Accounts;
using Wino.Core.Domain.Models.AutoDiscovery;
Full trust Wino Server implementation. (#295) * Separation of messages. Introducing Wino.Messages library. * Wino.Server and Wino.Packaging projects. Enabling full trust for UWP and app service connection manager basics. * Remove debug code. * Enable generating assembly info to deal with unsupported os platform warnings. * Fix server-client connection. * UIMessage communication. Single instancing for server and re-connection mechanism on suspension. * Removed IWinoSynchronizerFactory from UWP project. * Removal of background task service from core. * Delegating changes to UI and triggering new background synchronization. * Fix build error. * Moved core lib messages to Messaging project. * Better client-server communication. Handling of requests in the server. New synchronizer factory in the server. * WAM broker and MSAL token caching for OutlookAuthenticator. Handling account creation for Outlook. * WinoServerResponse basics. * Delegating protocol activation for Gmail authenticator. * Adding margin to searchbox to match action bar width. * Move libraries into lib folder. * Storing base64 encoded mime on draft creation instead of MimeMessage object. Fixes serialization/deserialization issue with S.T.Json * Scrollbar adjustments * WınoExpander for thread expander layout ıssue. * Handling synchronizer state changes. * Double init on background activation. * FIxing packaging issues and new Wino Mail launcher protocol for activation from full thrust process. * Remove debug deserialization. * Remove debug code. * Making sure the server connection is established when the app is launched. * Thrust -> Trust string replacement... * Rename package to Wino Mail * Enable translated values in the server. * Fixed an issue where toast activation can't find the clicked mail after the folder is initialized. * Revert debug code. * Change server background sync to every 3 minute and Inbox only synchronization. * Revert google auth changes. * App preferences page. * Changing tray icon visibility on preference change. * Start the server with invisible tray icon if set to invisible. * Reconnect button on the title bar. * Handling of toast actions. * Enable x86 build for server during packaging. * Get rid of old background tasks and v180 migration. * Terminate client when Exit clicked in server. * Introducing SynchronizationSource to prevent notifying UI after server tick synchronization. * Remove confirmAppClose restricted capability and unused debug code in manifest. * Closing the reconnect info popup when reconnect is clicked. * Custom RetryHandler for OutlookSynchronizer and separating client/server logs. * Running server on Windows startup. * Fix startup exe. * Fix for expander list view item paddings. * Force full sync on app launch instead of Inbox. * Fix draft creation. * Fix an issue with custom folder sync logic. * Reporting back account sync progress from server. * Fix sending drafts and missing notifications for imap. * Changing imap folder sync requirements. * Retain file count is set to 3. * Disabled swipe gestures temporarily due to native crash with SwipeControl * Save all attachments implementation. * Localization for save all attachments button. * Fix logging dates for logs. * Fixing ARM64 build. * Add ARM64 build config to packaging project. * Comment out OutOfProcPDB for ARM64. * Hnadling GONE response for Outlook folder synchronization.
2024-08-05 00:36:26 +02:00
using Wino.Messaging.Client.Mails;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
namespace Wino.Views.ImapSetup;
public sealed partial class AdvancedImapSetupPage : Page
2024-04-18 01:44:37 +02:00
{
public static readonly DependencyProperty UseSameCredentialsForSendingProperty = DependencyProperty.Register(nameof(UseSameCredentialsForSending), typeof(bool), typeof(AdvancedImapSetupPage), new PropertyMetadata(true, OnUseSameCredentialsForSendingChanged));
public static readonly DependencyProperty ValidationErrorsProperty = DependencyProperty.Register(nameof(ValidationErrors), typeof(List<string>), typeof(AdvancedImapSetupPage), new PropertyMetadata(new List<string>()));
2025-02-16 11:54:23 +01:00
public List<ImapAuthenticationMethodModel> AvailableAuthenticationMethods { get; } = new List<ImapAuthenticationMethodModel>()
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
new ImapAuthenticationMethodModel(Core.Domain.Enums.ImapAuthenticationMethod.Auto, Translator.ImapAuthenticationMethod_Auto),
new ImapAuthenticationMethodModel(Core.Domain.Enums.ImapAuthenticationMethod.None, Translator.ImapAuthenticationMethod_None),
new ImapAuthenticationMethodModel(Core.Domain.Enums.ImapAuthenticationMethod.NormalPassword, Translator.ImapAuthenticationMethod_Plain),
new ImapAuthenticationMethodModel(Core.Domain.Enums.ImapAuthenticationMethod.EncryptedPassword, Translator.ImapAuthenticationMethod_EncryptedPassword),
new ImapAuthenticationMethodModel(Core.Domain.Enums.ImapAuthenticationMethod.Ntlm, Translator.ImapAuthenticationMethod_Ntlm),
new ImapAuthenticationMethodModel(Core.Domain.Enums.ImapAuthenticationMethod.CramMd5, Translator.ImapAuthenticationMethod_CramMD5),
new ImapAuthenticationMethodModel(Core.Domain.Enums.ImapAuthenticationMethod.DigestMd5, Translator.ImapAuthenticationMethod_DigestMD5)
};
public List<ImapConnectionSecurityModel> AvailableConnectionSecurities { get; set; } = new List<ImapConnectionSecurityModel>()
{
new ImapConnectionSecurityModel(Core.Domain.Enums.ImapConnectionSecurity.Auto, Translator.ImapConnectionSecurity_Auto),
new ImapConnectionSecurityModel(Core.Domain.Enums.ImapConnectionSecurity.SslTls, Translator.ImapConnectionSecurity_SslTls),
new ImapConnectionSecurityModel(Core.Domain.Enums.ImapConnectionSecurity.StartTls, Translator.ImapConnectionSecurity_StartTls),
new ImapConnectionSecurityModel(Core.Domain.Enums.ImapConnectionSecurity.None, Translator.ImapConnectionSecurity_None)
};
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public bool UseSameCredentialsForSending
{
get { return (bool)GetValue(UseSameCredentialsForSendingProperty); }
set { SetValue(UseSameCredentialsForSendingProperty, value); }
}
2024-04-18 01:44:37 +02:00
public List<string> ValidationErrors
{
get { return (List<string>)GetValue(ValidationErrorsProperty); }
set { SetValue(ValidationErrorsProperty, value); }
}
[GeneratedDependencyProperty]
public partial bool HasValidationErrors { get; set; }
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public AdvancedImapSetupPage()
{
InitializeComponent();
2025-02-16 11:54:23 +01:00
NavigationCacheMode = NavigationCacheMode.Enabled;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private static void OnUseSameCredentialsForSendingChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
if (obj is AdvancedImapSetupPage page)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
page.UpdateOutgoingAuthenticationPanel();
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2025-02-16 11:54:23 +01:00
private void UpdateOutgoingAuthenticationPanel()
{
if (UseSameCredentialsForSending)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
OutgoingUsernameBox.Text = UsernameBox.Text;
OutgoingPasswordBox.Password = PasswordBox.Password;
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:54:23 +01:00
else
{
2025-02-16 11:54:23 +01:00
OutgoingUsernameBox.Text = string.Empty;
OutgoingPasswordBox.Password = string.Empty;
}
}
2025-02-16 11:54:23 +01:00
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
2025-02-16 11:54:23 +01:00
// Don't override settings on back scenarios.
// User is trying to try again the same configuration.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (e.NavigationMode == NavigationMode.Back) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Connection is succesfull but error occurred.
// Imap and Smptp settings exists here at this point.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (e.Parameter is AutoDiscoverySettings preDefinedSettings && preDefinedSettings.UserMinimalSettings != null)
{
// TODO: Auto discovery settings adjustments.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
UsernameBox.Text = preDefinedSettings.UserMinimalSettings.Email;
AddressBox.Text = preDefinedSettings.UserMinimalSettings.Email;
DisplayNameBox.Text = preDefinedSettings.UserMinimalSettings.DisplayName;
PasswordBox.Password = preDefinedSettings.UserMinimalSettings.Password;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var serverInfo = preDefinedSettings.ToServerInformation();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
IncomingServerBox.Text = serverInfo.IncomingServer;
IncomingServerPortBox.Text = serverInfo.IncomingServerPort;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
OutgoingPasswordBox.Password = serverInfo.OutgoingServerPassword;
OutgoingServerPort.Text = serverInfo.OutgoingServerPort;
OutgoingUsernameBox.Text = serverInfo.OutgoingServerUsername;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
UseSameCredentialsForSending = OutgoingUsernameBox.Text == UsernameBox.Text;
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:54:23 +01:00
else if (e.Parameter is AutoDiscoveryMinimalSettings autoDiscoveryMinimalSettings)
{
// Auto discovery failed. Only minimal settings are passed.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
UsernameBox.Text = autoDiscoveryMinimalSettings.Email;
AddressBox.Text = autoDiscoveryMinimalSettings.Email;
DisplayNameBox.Text = autoDiscoveryMinimalSettings.DisplayName;
PasswordBox.Password = autoDiscoveryMinimalSettings.Password;
}
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private void CancelClicked(object sender, RoutedEventArgs e) => WeakReferenceMessenger.Default.Send(new ImapSetupDismissRequested(null));
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
private string GetServerWithoutPort(string server)
{
var splitted = server.Split(':');
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (splitted.Length > 1)
{
return splitted[0];
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:54:23 +01:00
return server;
}
private void SignInClicked(object sender, RoutedEventArgs e)
{
var errors = new List<string>();
// Validate email and display name
if (string.IsNullOrWhiteSpace(AddressBox.Text))
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationEmailRequired);
else if (!EmailValidation.EmailValidator.Validate(AddressBox.Text))
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationEmailInvalid);
if (string.IsNullOrWhiteSpace(DisplayNameBox.Text))
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationDisplayNameRequired);
// Validate incoming server details
if (string.IsNullOrWhiteSpace(IncomingServerBox.Text))
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationIncomingServerRequired);
if (string.IsNullOrWhiteSpace(IncomingServerPortBox.Text))
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationIncomingPortRequired);
else if (!int.TryParse(IncomingServerPortBox.Text, out int inPort) || inPort <= 0 || inPort > 65535)
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationIncomingPortInvalid);
// Validate outgoing server details
if (string.IsNullOrWhiteSpace(OutgoingServerBox.Text))
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationOutgoingServerRequired);
if (string.IsNullOrWhiteSpace(OutgoingServerPort.Text))
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationOutgoingPortRequired);
else if (!int.TryParse(OutgoingServerPort.Text, out int outPort) || outPort <= 0 || outPort > 65535)
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationOutgoingPortInvalid);
// Validate authentication details
if (string.IsNullOrWhiteSpace(UsernameBox.Text))
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationUsernameRequired);
if (string.IsNullOrWhiteSpace(PasswordBox.Password))
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationPasswordRequired);
// Validate outgoing credentials if not using same as incoming
if (!UseSameCredentialsForSending)
{
if (string.IsNullOrWhiteSpace(OutgoingUsernameBox.Text))
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationOutgoingUsernameRequired);
if (string.IsNullOrWhiteSpace(OutgoingPasswordBox.Password))
errors.Add(Translator.IMAPAdvancedSetupDialog_ValidationOutgoingPasswordRequired);
}
// Show validation errors if any
HasValidationErrors = errors.Count > 0;
if (HasValidationErrors)
{
ValidationErrors = errors;
return;
}
2025-02-16 11:54:23 +01:00
var info = new CustomServerInformation()
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
IncomingServer = GetServerWithoutPort(IncomingServerBox.Text),
Id = Guid.NewGuid(),
IncomingServerPassword = PasswordBox.Password,
IncomingServerType = Core.Domain.Enums.CustomIncomingServerType.IMAP4,
IncomingServerUsername = UsernameBox.Text,
IncomingAuthenticationMethod = (IncomingAuthenticationMethod.SelectedItem as ImapAuthenticationMethodModel).ImapAuthenticationMethod,
IncomingServerSocketOption = (IncomingConnectionSecurity.SelectedItem as ImapConnectionSecurityModel).ImapConnectionSecurity,
IncomingServerPort = IncomingServerPortBox.Text,
OutgoingServer = GetServerWithoutPort(OutgoingServerBox.Text),
OutgoingServerPort = OutgoingServerPort.Text,
OutgoingServerPassword = OutgoingPasswordBox.Password,
OutgoingAuthenticationMethod = (OutgoingAuthenticationMethod.SelectedItem as ImapAuthenticationMethodModel).ImapAuthenticationMethod,
OutgoingServerSocketOption = (OutgoingConnectionSecurity.SelectedItem as ImapConnectionSecurityModel).ImapConnectionSecurity,
OutgoingServerUsername = OutgoingUsernameBox.Text,
ProxyServer = ProxyServerBox.Text,
ProxyServerPort = ProxyServerPortBox.Text,
Address = AddressBox.Text,
DisplayName = DisplayNameBox.Text,
MaxConcurrentClients = 5
};
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (UseSameCredentialsForSending)
{
info.OutgoingServerUsername = info.IncomingServerUsername;
info.OutgoingServerPassword = info.IncomingServerPassword;
}
else
{
info.OutgoingServerUsername = OutgoingUsernameBox.Text;
info.OutgoingServerPassword = OutgoingPasswordBox.Password;
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
WeakReferenceMessenger.Default.Send(new ImapSetupNavigationRequested(typeof(TestingImapConnectionPage), info));
}
private void IncomingServerChanged(object sender, TextChangedEventArgs e)
{
if (sender is TextBox senderTextBox)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var splitted = senderTextBox.Text.Split(':');
2025-02-16 11:54:23 +01:00
if (splitted.Length > 1)
{
IncomingServerPortBox.Text = splitted[splitted.Length - 1];
2024-04-18 01:44:37 +02:00
}
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private void OutgoingServerChanged(object sender, TextChangedEventArgs e)
{
if (sender is TextBox senderTextBox)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var splitted = senderTextBox.Text.Split(':');
2025-02-16 11:54:23 +01:00
if (splitted.Length > 1)
{
OutgoingServerPort.Text = splitted[splitted.Length - 1];
2024-04-18 01:44:37 +02:00
}
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private void IncomingUsernameChanged(object sender, TextChangedEventArgs e)
{
if (UseSameCredentialsForSending)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
OutgoingUsernameBox.Text = UsernameBox.Text;
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private void IncomingPasswordChanged(object sender, RoutedEventArgs e)
{
if (UseSameCredentialsForSending)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
OutgoingPasswordBox.Password = PasswordBox.Password;
2024-04-18 01:44:37 +02:00
}
}
private void ValidationsGoBackClicked(object sender, RoutedEventArgs e)
{
ValidationErrors.Clear();
HasValidationErrors = false;
}
2024-04-18 01:44:37 +02:00
}