2024-04-18 01:44:37 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2025-11-23 20:56:57 +01:00
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2024-04-18 01:44:37 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
|
|
|
using MimeKit;
|
2025-11-23 20:56:57 +01:00
|
|
|
using MimeKit.Cryptography;
|
2024-04-18 01:44:37 +02:00
|
|
|
using Wino.Core.Domain;
|
2024-11-10 23:28:25 +01:00
|
|
|
using Wino.Core.Domain.Entities.Mail;
|
|
|
|
|
using Wino.Core.Domain.Entities.Shared;
|
2024-04-18 01:44:37 +02:00
|
|
|
using Wino.Core.Domain.Enums;
|
|
|
|
|
using Wino.Core.Domain.Exceptions;
|
2026-02-22 17:55:57 +01:00
|
|
|
using Wino.Core.Domain.Extensions;
|
2024-04-18 01:44:37 +02:00
|
|
|
using Wino.Core.Domain.Interfaces;
|
|
|
|
|
using Wino.Core.Domain.Models.MailItem;
|
2026-03-08 13:21:42 +01:00
|
|
|
using Wino.Core.Domain.Models;
|
2024-04-18 01:44:37 +02:00
|
|
|
using Wino.Core.Domain.Models.Navigation;
|
|
|
|
|
using Wino.Core.Extensions;
|
2025-10-18 11:45:10 +02:00
|
|
|
using Wino.Core.Services;
|
2024-04-18 01:44:37 +02:00
|
|
|
using Wino.Mail.ViewModels.Data;
|
2026-02-07 13:10:57 +01:00
|
|
|
using Wino.Mail.ViewModels.Messages;
|
2024-08-05 00:36:26 +02:00
|
|
|
using Wino.Messaging.Client.Mails;
|
2026-02-22 17:55:57 +01:00
|
|
|
using Wino.Messaging.UI;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
namespace Wino.Mail.ViewModels;
|
|
|
|
|
|
2026-02-07 13:10:57 +01:00
|
|
|
public partial class ComposePageViewModel : MailBaseViewModel,
|
2026-02-25 01:41:48 +01:00
|
|
|
IRecipient<ReaderItemRefreshRequestedEvent>,
|
2026-02-22 17:55:57 +01:00
|
|
|
IRecipient<SynchronizationActionsAdded>,
|
|
|
|
|
IRecipient<SynchronizationActionsCompleted>,
|
|
|
|
|
IRecipient<AccountSynchronizerStateChanged>
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2026-02-23 01:02:59 +01:00
|
|
|
private static readonly TimeSpan LocalDraftRetryGracePeriod = TimeSpan.FromSeconds(15);
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public Func<Task<string>> GetHTMLBodyFunction;
|
|
|
|
|
|
2026-03-08 13:21:42 +01:00
|
|
|
public override async Task KeyboardShortcutHook(KeyboardShortcutTriggerDetails args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Handled || args.Mode != WinoApplicationMode.Mail)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (args.Action == KeyboardShortcutAction.Send)
|
|
|
|
|
{
|
|
|
|
|
await SendAsync();
|
|
|
|
|
args.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
// When we send the message or discard it, we need to block the mime update
|
|
|
|
|
// Update is triggered when we leave the page.
|
|
|
|
|
private bool isUpdatingMimeBlocked = false;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-02-21 10:53:39 +01:00
|
|
|
private bool canSendMail => ComposingAccount != null && !IsLocalDraft && CurrentMimeMessage != null && !IsDraftBusy;
|
2026-02-22 17:55:57 +01:00
|
|
|
private bool canSendLocalDraftToServer => ComposingAccount != null && IsLocalDraft && CurrentMimeMessage != null && !IsDraftBusy && !IsRetryingSendToServer;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[NotifyCanExecuteChangedFor(nameof(DiscardCommand))]
|
|
|
|
|
[NotifyCanExecuteChangedFor(nameof(SendCommand))]
|
2026-02-22 17:55:57 +01:00
|
|
|
[NotifyCanExecuteChangedFor(nameof(SendToServerCommand))]
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
|
|
|
|
private MimeMessage currentMimeMessage = null;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private readonly BodyBuilder bodyBuilder = new BodyBuilder();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public bool IsLocalDraft => CurrentMailDraftItem?.MailCopy?.IsLocalDraft ?? true;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
#region Properties
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
|
|
|
|
[NotifyPropertyChangedFor(nameof(IsLocalDraft))]
|
2026-02-22 17:55:57 +01:00
|
|
|
[NotifyPropertyChangedFor(nameof(ShouldShowSendToServerButton))]
|
|
|
|
|
[NotifyPropertyChangedFor(nameof(ShouldShowSendButton))]
|
2025-02-16 11:54:23 +01:00
|
|
|
[NotifyCanExecuteChangedFor(nameof(DiscardCommand))]
|
|
|
|
|
[NotifyCanExecuteChangedFor(nameof(SendCommand))]
|
2026-02-22 17:55:57 +01:00
|
|
|
[NotifyCanExecuteChangedFor(nameof(SendToServerCommand))]
|
2026-02-21 10:53:39 +01:00
|
|
|
public partial MailItemViewModel CurrentMailDraftItem { get; set; }
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
2026-02-22 17:55:57 +01:00
|
|
|
[NotifyPropertyChangedFor(nameof(ShouldShowSendToServerButton))]
|
2026-02-21 10:53:39 +01:00
|
|
|
[NotifyCanExecuteChangedFor(nameof(DiscardCommand))]
|
|
|
|
|
[NotifyCanExecuteChangedFor(nameof(SendCommand))]
|
2026-02-22 17:55:57 +01:00
|
|
|
[NotifyCanExecuteChangedFor(nameof(SendToServerCommand))]
|
2026-02-21 10:53:39 +01:00
|
|
|
public partial bool IsDraftBusy { get; set; }
|
|
|
|
|
|
2026-02-22 17:55:57 +01:00
|
|
|
[ObservableProperty]
|
|
|
|
|
[NotifyCanExecuteChangedFor(nameof(SendToServerCommand))]
|
|
|
|
|
public partial bool IsRetryingSendToServer { get; set; }
|
|
|
|
|
|
2026-02-21 10:53:39 +01:00
|
|
|
[ObservableProperty]
|
|
|
|
|
public partial bool IsImportanceSelected { get; set; }
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
2026-02-21 10:53:39 +01:00
|
|
|
public partial MessageImportance SelectedMessageImportance { get; set; }
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
2026-02-21 10:53:39 +01:00
|
|
|
public partial bool IsCCBCCVisible { get; set; }
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
2026-02-21 10:53:39 +01:00
|
|
|
public partial string Subject { get; set; }
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
|
|
|
|
[NotifyCanExecuteChangedFor(nameof(DiscardCommand))]
|
|
|
|
|
[NotifyCanExecuteChangedFor(nameof(SendCommand))]
|
2026-02-22 17:55:57 +01:00
|
|
|
[NotifyCanExecuteChangedFor(nameof(SendToServerCommand))]
|
2026-02-21 10:53:39 +01:00
|
|
|
public partial MailAccount ComposingAccount { get; set; }
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
2025-11-23 20:56:57 +01:00
|
|
|
public partial List<MailAccountAlias> AvailableAliases { get; set; }
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
2025-11-23 20:56:57 +01:00
|
|
|
public partial MailAccountAlias SelectedAlias { get; set; }
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
2025-11-23 20:56:57 +01:00
|
|
|
public partial bool IsDraggingOverComposerGrid { get; set; }
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
public partial bool IsDraggingOverFilesDropZone { get; set; }
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
2025-11-23 20:56:57 +01:00
|
|
|
public partial bool IsDraggingOverImagesDropZone { get; set; }
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
public partial bool IsSmimeSignatureEnabled { get; set; }
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
public partial bool IsSmimeEncryptionEnabled { get; set; }
|
2024-05-22 02:10:14 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
2025-11-23 20:56:57 +01:00
|
|
|
public partial X509Certificate2 SelectedSigningCertificate { get; set; }
|
|
|
|
|
|
|
|
|
|
public ObservableCollection<X509Certificate2> AvailableCertificates = [];
|
|
|
|
|
|
|
|
|
|
public bool AreCertificatesAvailable => AvailableCertificates.Count > 0;
|
2024-07-07 01:42:12 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public ObservableCollection<MailAttachmentViewModel> IncludedAttachments { get; set; } = [];
|
|
|
|
|
public ObservableCollection<MailAccount> Accounts { get; set; } = [];
|
|
|
|
|
public ObservableCollection<AccountContact> ToItems { get; set; } = [];
|
|
|
|
|
public ObservableCollection<AccountContact> CCItems { get; set; } = [];
|
|
|
|
|
public ObservableCollection<AccountContact> BCCItems { get; set; } = [];
|
2026-02-22 17:55:57 +01:00
|
|
|
public bool ShouldShowSendToServerButton => IsLocalDraft && !IsDraftBusy;
|
|
|
|
|
public bool ShouldShowSendButton => !IsLocalDraft;
|
2024-05-22 02:10:14 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
#endregion
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public INativeAppService NativeAppService { get; }
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private readonly IMailDialogService _dialogService;
|
|
|
|
|
private readonly IMailService _mailService;
|
|
|
|
|
private readonly IMimeFileService _mimeFileService;
|
|
|
|
|
private readonly IFileService _fileService;
|
|
|
|
|
private readonly IFolderService _folderService;
|
|
|
|
|
private readonly IAccountService _accountService;
|
|
|
|
|
private readonly IWinoRequestDelegator _worker;
|
|
|
|
|
public readonly IFontService FontService;
|
|
|
|
|
public readonly IPreferencesService PreferencesService;
|
|
|
|
|
public readonly IContactService ContactService;
|
2025-11-23 20:56:57 +01:00
|
|
|
public readonly ISmimeCertificateService _smimeCertificateService;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public ComposePageViewModel(IMailDialogService dialogService,
|
|
|
|
|
IMailService mailService,
|
|
|
|
|
IMimeFileService mimeFileService,
|
|
|
|
|
IFileService fileService,
|
|
|
|
|
INativeAppService nativeAppService,
|
|
|
|
|
IFolderService folderService,
|
|
|
|
|
IAccountService accountService,
|
|
|
|
|
IWinoRequestDelegator worker,
|
|
|
|
|
IContactService contactService,
|
|
|
|
|
IFontService fontService,
|
2025-11-23 20:56:57 +01:00
|
|
|
IPreferencesService preferencesService,
|
|
|
|
|
ISmimeCertificateService smimeCertificateService)
|
2025-02-16 11:54:23 +01:00
|
|
|
{
|
|
|
|
|
NativeAppService = nativeAppService;
|
|
|
|
|
ContactService = contactService;
|
|
|
|
|
FontService = fontService;
|
|
|
|
|
PreferencesService = preferencesService;
|
|
|
|
|
|
|
|
|
|
_folderService = folderService;
|
|
|
|
|
_dialogService = dialogService;
|
|
|
|
|
_mailService = mailService;
|
|
|
|
|
_mimeFileService = mimeFileService;
|
|
|
|
|
_fileService = fileService;
|
|
|
|
|
_accountService = accountService;
|
|
|
|
|
_worker = worker;
|
2025-11-23 20:56:57 +01:00
|
|
|
_smimeCertificateService = smimeCertificateService;
|
|
|
|
|
|
|
|
|
|
foreach (var cert in _smimeCertificateService.GetCertificates(emailAddress: SelectedAlias?.AliasAddress))
|
|
|
|
|
{
|
|
|
|
|
if (cert != null)
|
|
|
|
|
{
|
|
|
|
|
AvailableCertificates.Add(cert);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
partial void OnSelectedAliasChanged(MailAccountAlias value)
|
|
|
|
|
{
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
|
|
|
|
IsSmimeSignatureEnabled = value.SelectedSigningCertificateThumbprint != null;
|
|
|
|
|
IsSmimeEncryptionEnabled = value.IsSmimeEncryptionEnabled;
|
|
|
|
|
|
|
|
|
|
AvailableCertificates.Clear();
|
|
|
|
|
var certs = _smimeCertificateService.GetCertificates(emailAddress: SelectedAlias.AliasAddress);
|
|
|
|
|
foreach (var cert in certs)
|
|
|
|
|
{
|
|
|
|
|
AvailableCertificates.Add(cert);
|
|
|
|
|
}
|
|
|
|
|
SelectedSigningCertificate = AvailableCertificates
|
|
|
|
|
.Where(c => c.Thumbprint == SelectedAlias.SelectedSigningCertificateThumbprint).FirstOrDefault() ?? AvailableCertificates.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
partial void OnSelectedSigningCertificateChanged(X509Certificate2 value)
|
|
|
|
|
{
|
|
|
|
|
IsSmimeSignatureEnabled = value != null;
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[RelayCommand]
|
|
|
|
|
private async Task OpenAttachmentAsync(MailAttachmentViewModel attachmentViewModel)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(attachmentViewModel.FilePath)) return;
|
|
|
|
|
|
|
|
|
|
try
|
2024-11-27 19:49:10 +01:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
await NativeAppService.LaunchFileAsync(attachmentViewModel.FilePath);
|
2024-11-27 19:49:10 +01:00
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
catch
|
2025-02-16 11:35:43 +01:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
_dialogService.InfoBarMessage(Translator.Info_FailedToOpenFileTitle, Translator.Info_FailedToOpenFileMessage, InfoBarMessageType.Error);
|
2024-11-27 19:49:10 +01:00
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2025-02-16 11:43:30 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[RelayCommand]
|
|
|
|
|
private async Task SaveAttachmentAsync(MailAttachmentViewModel attachmentViewModel)
|
|
|
|
|
{
|
|
|
|
|
if (attachmentViewModel.Content == null) return;
|
|
|
|
|
var pickedFilePath = await _dialogService.PickFilePathAsync(attachmentViewModel.FileName);
|
|
|
|
|
if (string.IsNullOrWhiteSpace(pickedFilePath)) return;
|
2025-02-16 11:43:30 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _fileService.CopyFileAsync(attachmentViewModel.FilePath, pickedFilePath);
|
2025-02-16 11:35:43 +01:00
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
catch
|
2025-02-16 11:43:30 +01:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
_dialogService.InfoBarMessage(Translator.Info_FailedToOpenFileTitle, Translator.Info_FailedToOpenFileMessage, InfoBarMessageType.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
private async Task AttachFilesAsync()
|
|
|
|
|
{
|
|
|
|
|
var pickedFiles = await _dialogService.PickFilesAsync("*");
|
2024-11-27 19:49:10 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (pickedFiles?.Count == 0) return;
|
2024-11-27 19:49:10 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
foreach (var file in pickedFiles)
|
|
|
|
|
{
|
|
|
|
|
var attachmentViewModel = new MailAttachmentViewModel(file);
|
|
|
|
|
IncludedAttachments.Add(attachmentViewModel);
|
2024-11-27 19:49:10 +01:00
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2024-11-27 19:49:10 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[RelayCommand]
|
|
|
|
|
private void RemoveAttachment(MailAttachmentViewModel attachmentViewModel)
|
|
|
|
|
=> IncludedAttachments.Remove(attachmentViewModel);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[RelayCommand(CanExecute = nameof(canSendMail))]
|
|
|
|
|
private async Task SendAsync()
|
|
|
|
|
{
|
|
|
|
|
// TODO: More detailed mail validations.
|
|
|
|
|
|
|
|
|
|
if (!ToItems.Any())
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
await _dialogService.ShowMessageAsync(Translator.DialogMessage_ComposerMissingRecipientMessage,
|
|
|
|
|
Translator.DialogMessage_ComposerValidationFailedTitle,
|
|
|
|
|
WinoCustomMessageDialogIcon.Warning);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (string.IsNullOrEmpty(Subject))
|
|
|
|
|
{
|
|
|
|
|
var isConfirmed = await _dialogService.ShowConfirmationDialogAsync(Translator.DialogMessage_EmptySubjectConfirmationMessage, Translator.DialogMessage_EmptySubjectConfirmation, Translator.Buttons_Yes);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (!isConfirmed) return;
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (SelectedAlias == null)
|
|
|
|
|
{
|
|
|
|
|
_dialogService.InfoBarMessage(Translator.DialogMessage_AliasNotSelectedTitle, Translator.DialogMessage_AliasNotSelectedMessage, InfoBarMessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
// Save mime changes before sending.
|
|
|
|
|
await UpdateMimeChangesAsync().ConfigureAwait(false);
|
2024-08-17 22:55:58 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
isUpdatingMimeBlocked = true;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-10-03 15:46:38 +02:00
|
|
|
var assignedAccount = CurrentMailDraftItem.MailCopy.AssignedAccount;
|
2025-02-16 11:54:23 +01:00
|
|
|
var sentFolder = await _folderService.GetSpecialFolderByAccountIdAsync(assignedAccount.Id, SpecialFolderType.Sent);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-11-23 20:56:57 +01:00
|
|
|
|
|
|
|
|
// Load alias certs
|
|
|
|
|
var certs = _smimeCertificateService.GetCertificates(emailAddress: SelectedAlias.AliasAddress);
|
|
|
|
|
|
|
|
|
|
if (IsSmimeSignatureEnabled)
|
|
|
|
|
{
|
|
|
|
|
var signingCertificate = !string.IsNullOrEmpty(SelectedAlias.SelectedSigningCertificateThumbprint)
|
|
|
|
|
? certs.FirstOrDefault(c => c?.Thumbprint == SelectedAlias.SelectedSigningCertificateThumbprint)
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
var signer = new CmsSigner(signingCertificate) { DigestAlgorithm = DigestAlgorithm.Sha1 };
|
|
|
|
|
|
|
|
|
|
if (IsSmimeEncryptionEnabled)
|
|
|
|
|
{
|
|
|
|
|
var recipients = new CmsRecipientCollection();
|
|
|
|
|
var cmsRecipients = CurrentMimeMessage.To.Mailboxes
|
|
|
|
|
.Select(mailbox => new CmsRecipient(
|
|
|
|
|
_smimeCertificateService.GetCertificates(emailAddress: mailbox.Address).FirstOrDefault() ?? _smimeCertificateService.GetCertificates(StoreName.AddressBook, emailAddress: mailbox.Address).FirstOrDefault()
|
|
|
|
|
));
|
|
|
|
|
foreach (var recipient in cmsRecipients)
|
|
|
|
|
{
|
|
|
|
|
recipients.Add(recipient);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CurrentMimeMessage.Body = ApplicationPkcs7Mime.SignAndEncrypt(signer, recipients, CurrentMimeMessage.Body);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// CurrentMimeMessage.Body = MultipartSigned.Create(signer, CurrentMimeMessage.Body);
|
|
|
|
|
CurrentMimeMessage.Body = ApplicationPkcs7Mime.Sign(signer, CurrentMimeMessage.Body);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (IsSmimeEncryptionEnabled)
|
|
|
|
|
{
|
|
|
|
|
// var encryptionCertificate = !string.IsNullOrEmpty(SelectedAlias.SelectedEncryptionCertificateThumbprint)
|
|
|
|
|
// ? certs.FirstOrDefault(c => c?.Thumbprint == SelectedAlias.SelectedEncryptionCertificateThumbprint)
|
|
|
|
|
// : null;
|
|
|
|
|
// Encrypt the message if encryption certificate is selected.
|
|
|
|
|
CurrentMimeMessage.Body = ApplicationPkcs7Mime.Encrypt(CurrentMimeMessage.To.Mailboxes, CurrentMimeMessage.Body);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
using MemoryStream memoryStream = new();
|
|
|
|
|
CurrentMimeMessage.WriteTo(FormatOptions.Default, memoryStream);
|
2026-02-23 01:51:44 +01:00
|
|
|
var base64EncodedMessage = Convert.ToBase64String(memoryStream.ToArray());
|
2025-02-16 11:54:23 +01:00
|
|
|
var draftSendPreparationRequest = new SendDraftPreparationRequest(CurrentMailDraftItem.MailCopy,
|
|
|
|
|
SelectedAlias,
|
|
|
|
|
sentFolder,
|
2025-10-03 15:46:38 +02:00
|
|
|
CurrentMailDraftItem.MailCopy.AssignedFolder,
|
|
|
|
|
CurrentMailDraftItem.MailCopy.AssignedAccount.Preferences,
|
2025-02-16 11:54:23 +01:00
|
|
|
base64EncodedMessage);
|
2024-08-05 00:36:26 +02:00
|
|
|
|
2026-02-21 16:14:55 +01:00
|
|
|
await ExecuteUIThread(() =>
|
|
|
|
|
{
|
|
|
|
|
IsDraftBusy = true;
|
|
|
|
|
});
|
2026-02-21 10:53:39 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
await _worker.ExecuteAsync(draftSendPreparationRequest);
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-02-22 17:55:57 +01:00
|
|
|
[RelayCommand(CanExecute = nameof(canSendLocalDraftToServer))]
|
|
|
|
|
private async Task SendToServerAsync()
|
|
|
|
|
{
|
|
|
|
|
if (CurrentMailDraftItem?.MailCopy == null || ComposingAccount == null || CurrentMimeMessage == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await ExecuteUIThread(() =>
|
|
|
|
|
{
|
|
|
|
|
IsRetryingSendToServer = true;
|
|
|
|
|
IsDraftBusy = true;
|
|
|
|
|
NotifyComposeActionStateChanged();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await UpdateMimeChangesAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var localDraftCopy = CurrentMailDraftItem.MailCopy;
|
2026-02-23 01:51:44 +01:00
|
|
|
var (retryReason, referenceMailCopy) = await ResolveRetryDraftContextAsync().ConfigureAwait(false);
|
2026-02-22 17:55:57 +01:00
|
|
|
var draftPreparationRequest = new DraftPreparationRequest(
|
|
|
|
|
localDraftCopy.AssignedAccount ?? ComposingAccount,
|
|
|
|
|
localDraftCopy,
|
|
|
|
|
CurrentMimeMessage.GetBase64MimeMessage(),
|
2026-02-23 01:51:44 +01:00
|
|
|
retryReason,
|
|
|
|
|
referenceMailCopy);
|
2026-02-22 17:55:57 +01:00
|
|
|
|
|
|
|
|
await _worker.ExecuteAsync(draftPreparationRequest).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_dialogService.InfoBarMessage(Translator.Info_RequestCreationFailedTitle, ex.Message, InfoBarMessageType.Error);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
await ExecuteUIThread(() =>
|
|
|
|
|
{
|
|
|
|
|
IsRetryingSendToServer = false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await UpdatePendingOperationStateAsync().ConfigureAwait(false);
|
2026-02-23 01:51:44 +01:00
|
|
|
|
|
|
|
|
await ExecuteUIThread(() =>
|
|
|
|
|
{
|
|
|
|
|
NotifyComposeActionStateChanged();
|
|
|
|
|
});
|
2026-02-22 17:55:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public async Task UpdateMimeChangesAsync()
|
|
|
|
|
{
|
|
|
|
|
if (isUpdatingMimeBlocked || CurrentMimeMessage == null || ComposingAccount == null || CurrentMailDraftItem == null) return;
|
2024-08-18 22:45:23 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
// Save recipients.
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
SaveAddressInfo(ToItems, CurrentMimeMessage.To);
|
|
|
|
|
SaveAddressInfo(CCItems, CurrentMimeMessage.Cc);
|
|
|
|
|
SaveAddressInfo(BCCItems, CurrentMimeMessage.Bcc);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
SaveImportance();
|
|
|
|
|
SaveSubject();
|
|
|
|
|
SaveFromAddress();
|
|
|
|
|
SaveReplyToAddress();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
await SaveAttachmentsAsync();
|
|
|
|
|
await SaveBodyAsync();
|
|
|
|
|
await UpdateMailCopyAsync();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
// Save mime file.
|
|
|
|
|
await _mimeFileService.SaveMimeMessageAsync(CurrentMailDraftItem.MailCopy.FileId, CurrentMimeMessage, ComposingAccount.Id).ConfigureAwait(false);
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private async Task UpdateMailCopyAsync()
|
|
|
|
|
{
|
|
|
|
|
CurrentMailDraftItem.Subject = CurrentMimeMessage.Subject;
|
|
|
|
|
CurrentMailDraftItem.PreviewText = CurrentMimeMessage.TextBody;
|
|
|
|
|
CurrentMailDraftItem.FromAddress = SelectedAlias.AliasAddress;
|
|
|
|
|
CurrentMailDraftItem.HasAttachments = CurrentMimeMessage.Attachments.Any();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
// Update database.
|
|
|
|
|
await _mailService.UpdateMailAsync(CurrentMailDraftItem.MailCopy);
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private async Task SaveAttachmentsAsync()
|
|
|
|
|
{
|
|
|
|
|
bodyBuilder.Attachments.Clear();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
foreach (var path in IncludedAttachments)
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
if (path.Content == null) continue;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
await bodyBuilder.Attachments.AddAsync(path.FileName, new MemoryStream(path.Content));
|
2025-02-16 11:43:30 +01:00
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2025-02-16 11:35:43 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private void SaveImportance()
|
|
|
|
|
{
|
|
|
|
|
CurrentMimeMessage.Importance = IsImportanceSelected ? SelectedMessageImportance : MessageImportance.Normal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveSubject()
|
|
|
|
|
{
|
|
|
|
|
if (Subject != null)
|
2024-08-17 22:55:58 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
CurrentMimeMessage.Subject = Subject;
|
2024-08-17 22:55:58 +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 async Task SaveBodyAsync()
|
|
|
|
|
{
|
|
|
|
|
if (GetHTMLBodyFunction != null)
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
bodyBuilder.SetHtmlBody(await GetHTMLBodyFunction());
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
CurrentMimeMessage.Body = bodyBuilder.ToMessageBody();
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[RelayCommand(CanExecute = nameof(canSendMail))]
|
|
|
|
|
private async Task DiscardAsync()
|
|
|
|
|
{
|
|
|
|
|
if (ComposingAccount == null)
|
|
|
|
|
{
|
|
|
|
|
_dialogService.InfoBarMessage(Translator.Info_MessageCorruptedTitle, Translator.Info_MessageCorruptedMessage, InfoBarMessageType.Error);
|
|
|
|
|
return;
|
2025-02-16 11:43:30 +01:00
|
|
|
}
|
2025-02-16 11:35:43 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
var confirmation = await _dialogService.ShowConfirmationDialogAsync(Translator.DialogMessage_DiscardDraftConfirmationMessage,
|
|
|
|
|
Translator.DialogMessage_DiscardDraftConfirmationTitle,
|
|
|
|
|
Translator.Buttons_Yes);
|
|
|
|
|
|
|
|
|
|
if (confirmation)
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
isUpdatingMimeBlocked = true;
|
|
|
|
|
|
|
|
|
|
// Don't send delete request for local drafts. Just delete the record and mime locally.
|
|
|
|
|
if (CurrentMailDraftItem.MailCopy.IsLocalDraft)
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
await _mailService.DeleteMailAsync(ComposingAccount.Id, CurrentMailDraftItem.Id);
|
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
|
|
|
var deletePackage = new MailOperationPreperationRequest(MailOperation.HardDelete, CurrentMailDraftItem.MailCopy, ignoreHardDeleteProtection: true);
|
|
|
|
|
await _worker.ExecuteAsync(deletePackage).ConfigureAwait(false);
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-02-22 17:55:57 +01:00
|
|
|
//public override void OnNavigatedFrom(NavigationMode mode, object parameters)
|
|
|
|
|
//{
|
|
|
|
|
// base.OnNavigatedFrom(mode, parameters);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-02-22 17:55:57 +01:00
|
|
|
// /// Do not put any code here.
|
|
|
|
|
// /// Make sure to use Page's OnNavigatedTo instead.
|
|
|
|
|
//}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public override async void OnNavigatedTo(NavigationMode mode, object parameters)
|
|
|
|
|
{
|
|
|
|
|
base.OnNavigatedTo(mode, parameters);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (parameters != null && parameters is MailItemViewModel mailItem)
|
|
|
|
|
{
|
|
|
|
|
CurrentMailDraftItem = mailItem;
|
2025-02-16 11:43:30 +01:00
|
|
|
|
2026-02-21 10:53:39 +01:00
|
|
|
await UpdatePendingOperationStateAsync();
|
2025-02-16 11:54:23 +01:00
|
|
|
await TryPrepareComposeAsync(true);
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-02-25 01:41:48 +01:00
|
|
|
public async void Receive(ReaderItemRefreshRequestedEvent message)
|
2026-02-07 13:10:57 +01:00
|
|
|
{
|
2026-02-25 01:41:48 +01:00
|
|
|
if (message.MailItemViewModel == null || !message.MailItemViewModel.IsDraft) return;
|
|
|
|
|
|
2026-02-07 13:10:57 +01:00
|
|
|
// Save current draft before switching.
|
|
|
|
|
await UpdateMimeChangesAsync();
|
|
|
|
|
|
|
|
|
|
// Reset state for the new draft.
|
|
|
|
|
isUpdatingMimeBlocked = false;
|
|
|
|
|
ComposingAccount = null;
|
|
|
|
|
IncludedAttachments.Clear();
|
|
|
|
|
|
|
|
|
|
// Set the new draft item and prepare it.
|
|
|
|
|
CurrentMailDraftItem = message.MailItemViewModel;
|
2026-02-21 10:53:39 +01:00
|
|
|
await UpdatePendingOperationStateAsync();
|
2026-02-07 13:10:57 +01:00
|
|
|
await TryPrepareComposeAsync(true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:55:57 +01:00
|
|
|
public async void Receive(SynchronizationActionsAdded message)
|
|
|
|
|
{
|
|
|
|
|
if (!ShouldTrackDraftSynchronizationState(message.AccountId))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
await UpdatePendingOperationStateAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async void Receive(SynchronizationActionsCompleted message)
|
|
|
|
|
{
|
|
|
|
|
if (!ShouldTrackDraftSynchronizationState(message.AccountId))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
await UpdatePendingOperationStateAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async void Receive(AccountSynchronizerStateChanged message)
|
|
|
|
|
{
|
|
|
|
|
if (message.NewState != AccountSynchronizerState.Idle || !ShouldTrackDraftSynchronizationState(message.AccountId))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
await UpdatePendingOperationStateAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-07 13:10:57 +01:00
|
|
|
protected override void RegisterRecipients()
|
|
|
|
|
{
|
|
|
|
|
base.RegisterRecipients();
|
|
|
|
|
|
2026-02-25 01:41:48 +01:00
|
|
|
Messenger.Register<ReaderItemRefreshRequestedEvent>(this);
|
2026-02-22 17:55:57 +01:00
|
|
|
Messenger.Register<SynchronizationActionsAdded>(this);
|
|
|
|
|
Messenger.Register<SynchronizationActionsCompleted>(this);
|
|
|
|
|
Messenger.Register<AccountSynchronizerStateChanged>(this);
|
2026-02-07 13:10:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void UnregisterRecipients()
|
|
|
|
|
{
|
|
|
|
|
base.UnregisterRecipients();
|
|
|
|
|
|
2026-02-25 01:41:48 +01:00
|
|
|
Messenger.Unregister<ReaderItemRefreshRequestedEvent>(this);
|
2026-02-22 17:55:57 +01:00
|
|
|
Messenger.Unregister<SynchronizationActionsAdded>(this);
|
|
|
|
|
Messenger.Unregister<SynchronizationActionsCompleted>(this);
|
|
|
|
|
Messenger.Unregister<AccountSynchronizerStateChanged>(this);
|
2026-02-07 13:10:57 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private async Task<bool> InitializeComposerAccountAsync()
|
|
|
|
|
{
|
|
|
|
|
if (CurrentMailDraftItem == null) return false;
|
2024-08-17 22:55:58 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (ComposingAccount != null) return true;
|
2024-08-17 22:55:58 +02:00
|
|
|
|
2025-10-03 15:46:38 +02:00
|
|
|
var composingAccount = await _accountService.GetAccountAsync(CurrentMailDraftItem.MailCopy.AssignedAccount.Id).ConfigureAwait(false);
|
2025-02-16 11:54:23 +01:00
|
|
|
if (composingAccount == null) return false;
|
2024-08-17 22:55:58 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
var aliases = await _accountService.GetAccountAliasesAsync(composingAccount.Id).ConfigureAwait(false);
|
2024-08-17 22:55:58 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (aliases == null || !aliases.Any()) return false;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
// MailAccountAlias primaryAlias = aliases.Find(a => a.IsPrimary) ?? aliases.First();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
// Auto-select the correct alias from the message itself.
|
|
|
|
|
// If can't, fallback to primary alias.
|
2024-08-17 22:55:58 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
MailAccountAlias primaryAlias = null;
|
2024-08-17 22:55:58 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (!string.IsNullOrEmpty(CurrentMailDraftItem.FromAddress))
|
|
|
|
|
{
|
|
|
|
|
primaryAlias = aliases.Find(a => a.AliasAddress == CurrentMailDraftItem.FromAddress);
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-02-06 21:46:30 +01:00
|
|
|
primaryAlias ??= await _accountService.GetPrimaryAccountAliasAsync(composingAccount.Id).ConfigureAwait(false);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
await ExecuteUIThread(() =>
|
|
|
|
|
{
|
|
|
|
|
ComposingAccount = composingAccount;
|
|
|
|
|
AvailableAliases = aliases;
|
|
|
|
|
SelectedAlias = primaryAlias;
|
|
|
|
|
});
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-02-21 10:53:39 +01:00
|
|
|
private async Task UpdatePendingOperationStateAsync()
|
|
|
|
|
{
|
2026-02-22 17:55:57 +01:00
|
|
|
var hasPendingOperation = false;
|
2026-02-23 01:02:59 +01:00
|
|
|
var keepBusyForInitialGracePeriod = false;
|
2026-02-21 10:53:39 +01:00
|
|
|
|
|
|
|
|
if (CurrentMailDraftItem?.MailCopy == null || !CurrentMailDraftItem.MailCopy.IsDraft)
|
2026-02-22 17:55:57 +01:00
|
|
|
{
|
|
|
|
|
await ExecuteUIThread(() =>
|
|
|
|
|
{
|
|
|
|
|
IsDraftBusy = false;
|
|
|
|
|
NotifyComposeActionStateChanged();
|
|
|
|
|
});
|
2026-02-21 10:53:39 +01:00
|
|
|
return;
|
2026-02-22 17:55:57 +01:00
|
|
|
}
|
2026-02-21 10:53:39 +01:00
|
|
|
|
|
|
|
|
var accountId = CurrentMailDraftItem.MailCopy.AssignedAccount?.Id ?? Guid.Empty;
|
|
|
|
|
|
2026-02-22 17:55:57 +01:00
|
|
|
if (accountId != Guid.Empty)
|
|
|
|
|
{
|
|
|
|
|
var synchronizer = await SynchronizationManager.Instance.GetSynchronizerAsync(accountId).ConfigureAwait(false);
|
|
|
|
|
hasPendingOperation = synchronizer?.HasPendingOperation(CurrentMailDraftItem.MailCopy.UniqueId) ?? false;
|
|
|
|
|
}
|
2026-02-21 10:53:39 +01:00
|
|
|
|
2026-02-23 01:02:59 +01:00
|
|
|
// Newly created local drafts can have a short period where request queue is empty
|
|
|
|
|
// while folder synchronization/mapping is still in progress.
|
|
|
|
|
// Keep progress visible during this grace period to prevent "Send to server" flicker.
|
|
|
|
|
if (!hasPendingOperation && CurrentMailDraftItem.MailCopy.IsLocalDraft)
|
|
|
|
|
{
|
|
|
|
|
keepBusyForInitialGracePeriod = IsWithinLocalDraftRetryGracePeriod(CurrentMailDraftItem.MailCopy);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 17:55:57 +01:00
|
|
|
await ExecuteUIThread(() =>
|
|
|
|
|
{
|
2026-02-23 01:02:59 +01:00
|
|
|
IsDraftBusy = hasPendingOperation || keepBusyForInitialGracePeriod;
|
2026-02-22 17:55:57 +01:00
|
|
|
NotifyComposeActionStateChanged();
|
|
|
|
|
});
|
2026-02-21 10:53:39 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private async Task TryPrepareComposeAsync(bool downloadIfNeeded)
|
|
|
|
|
{
|
|
|
|
|
if (CurrentMailDraftItem == null) return;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
bool isComposerInitialized = await InitializeComposerAccountAsync();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (!isComposerInitialized) return;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-02-06 21:46:30 +01:00
|
|
|
retry:
|
2024-08-17 20:19:01 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
// Replying existing message.
|
|
|
|
|
MimeMessageInformation mimeMessageInformation = null;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
mimeMessageInformation = await _mimeFileService.GetMimeMessageInformationAsync(CurrentMailDraftItem.MailCopy.FileId, ComposingAccount.Id).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
if (downloadIfNeeded)
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
downloadIfNeeded = false;
|
2024-08-17 20:19:01 +02:00
|
|
|
|
2025-10-04 23:10:07 +02:00
|
|
|
// Download missing MIME message using SynchronizationManager
|
|
|
|
|
await SynchronizationManager.Instance.DownloadMimeMessageAsync(
|
2025-10-18 11:45:10 +02:00
|
|
|
CurrentMailDraftItem.MailCopy,
|
|
|
|
|
CurrentMailDraftItem.MailCopy.AssignedAccount.Id);
|
2024-08-17 20:19:01 +02:00
|
|
|
|
2025-10-04 23:10:07 +02:00
|
|
|
goto retry;
|
2024-06-26 20:00:10 +02:00
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
else
|
2024-11-10 23:28:25 +01:00
|
|
|
_dialogService.InfoBarMessage(Translator.Info_ComposerMissingMIMETitle, Translator.Info_ComposerMissingMIMEMessage, InfoBarMessageType.Error);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (IOException)
|
|
|
|
|
{
|
|
|
|
|
_dialogService.InfoBarMessage(Translator.Busy, Translator.Exception_MailProcessing, InfoBarMessageType.Warning);
|
|
|
|
|
}
|
|
|
|
|
catch (ComposerMimeNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
_dialogService.InfoBarMessage(Translator.Info_ComposerMissingMIMETitle, Translator.Info_ComposerMissingMIMEMessage, InfoBarMessageType.Error);
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (mimeMessageInformation == null)
|
|
|
|
|
return;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
var replyingMime = mimeMessageInformation.MimeMessage;
|
|
|
|
|
var mimeFilePath = mimeMessageInformation.Path;
|
2024-08-18 22:45:23 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
var renderModel = _mimeFileService.GetMailRenderModel(replyingMime, mimeFilePath);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
await ExecuteUIThread(async () =>
|
|
|
|
|
{
|
|
|
|
|
// Extract information
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
CurrentMimeMessage = replyingMime;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
ToItems.Clear();
|
|
|
|
|
CCItems.Clear();
|
|
|
|
|
BCCItems.Clear();
|
2024-08-10 14:33:02 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
await LoadAddressInfoAsync(replyingMime.To, ToItems);
|
|
|
|
|
await LoadAddressInfoAsync(replyingMime.Cc, CCItems);
|
|
|
|
|
await LoadAddressInfoAsync(replyingMime.Bcc, BCCItems);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
LoadAttachments();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (replyingMime.Cc.Any() || replyingMime.Bcc.Any())
|
|
|
|
|
IsCCBCCVisible = true;
|
2024-08-18 22:45:23 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
Subject = replyingMime.Subject;
|
2025-02-16 11:35:43 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
Messenger.Send(new CreateNewComposeMailRequested(renderModel));
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-02-16 11:43:30 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private void LoadAttachments()
|
|
|
|
|
{
|
|
|
|
|
if (CurrentMimeMessage == null) return;
|
|
|
|
|
|
2026-02-07 13:10:57 +01:00
|
|
|
IncludedAttachments.Clear();
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
foreach (var attachment in CurrentMimeMessage.Attachments)
|
|
|
|
|
{
|
|
|
|
|
if (attachment.IsAttachment && attachment is MimePart attachmentPart)
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
IncludedAttachments.Add(new MailAttachmentViewModel(attachmentPart));
|
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 async Task LoadAddressInfoAsync(InternetAddressList list, ObservableCollection<AccountContact> collection)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in list)
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
if (item is MailboxAddress mailboxAddress)
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
var foundContact = await ContactService.GetAddressInformationByAddressAsync(mailboxAddress.Address).ConfigureAwait(false)
|
|
|
|
|
?? new AccountContact() { Name = mailboxAddress.Name, Address = mailboxAddress.Address };
|
2024-08-23 02:06:58 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
await ExecuteUIThread(() => { collection.Add(foundContact); });
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
else if (item is GroupAddress groupAddress)
|
|
|
|
|
await LoadAddressInfoAsync(groupAddress.Members, collection);
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2024-08-17 22:55:58 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private void SaveFromAddress()
|
|
|
|
|
{
|
|
|
|
|
if (SelectedAlias == null) return;
|
2024-09-12 01:14:40 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
CurrentMimeMessage.From.Clear();
|
2024-09-12 01:14:40 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
// Try to get the sender name from the alias. If not, fallback to account sender name.
|
|
|
|
|
var senderName = SelectedAlias.AliasSenderName ?? ComposingAccount.SenderName;
|
2024-08-17 22:55:58 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
CurrentMimeMessage.From.Add(new MailboxAddress(senderName, SelectedAlias.AliasAddress));
|
|
|
|
|
}
|
2024-08-17 22:55:58 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private void SaveReplyToAddress()
|
|
|
|
|
{
|
|
|
|
|
if (SelectedAlias == null) return;
|
2025-02-16 11:43:30 +01:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (!string.IsNullOrEmpty(SelectedAlias.ReplyToAddress))
|
|
|
|
|
{
|
|
|
|
|
if (!CurrentMimeMessage.ReplyTo.Any(a => a is MailboxAddress mailboxAddress && mailboxAddress.Address == SelectedAlias.ReplyToAddress))
|
2024-08-17 22:55:58 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
CurrentMimeMessage.ReplyTo.Clear();
|
|
|
|
|
CurrentMimeMessage.ReplyTo.Add(new MailboxAddress(SelectedAlias.ReplyToAddress, SelectedAlias.ReplyToAddress));
|
2024-08-17 22:55:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2024-08-17 22:55:58 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private void SaveAddressInfo(IEnumerable<AccountContact> addresses, InternetAddressList list)
|
|
|
|
|
{
|
|
|
|
|
list.Clear();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
foreach (var item in addresses)
|
|
|
|
|
list.Add(new MailboxAddress(item.Name, item.Address));
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-02-23 01:51:44 +01:00
|
|
|
private async Task<(DraftCreationReason reason, MailCopy referenceMailCopy)> ResolveRetryDraftContextAsync()
|
|
|
|
|
{
|
|
|
|
|
if (CurrentMimeMessage == null || CurrentMailDraftItem?.MailCopy?.AssignedAccount == null)
|
|
|
|
|
return (DraftCreationReason.Empty, null);
|
|
|
|
|
|
|
|
|
|
var inReplyTo = CurrentMimeMessage.InReplyTo;
|
|
|
|
|
if (string.IsNullOrWhiteSpace(inReplyTo) && CurrentMimeMessage.Headers.Contains(HeaderId.InReplyTo))
|
|
|
|
|
inReplyTo = CurrentMimeMessage.Headers[HeaderId.InReplyTo];
|
|
|
|
|
|
|
|
|
|
inReplyTo = MailHeaderExtensions.StripAngleBrackets(inReplyTo);
|
|
|
|
|
if (string.IsNullOrWhiteSpace(inReplyTo))
|
|
|
|
|
return (DraftCreationReason.Empty, null);
|
|
|
|
|
|
|
|
|
|
var accountId = CurrentMailDraftItem.MailCopy.AssignedAccount.Id;
|
|
|
|
|
var referenceMailCopy = await _mailService.GetMailCopyByMessageIdAsync(accountId, inReplyTo).ConfigureAwait(false);
|
|
|
|
|
if (referenceMailCopy == null)
|
|
|
|
|
return (DraftCreationReason.Empty, null);
|
|
|
|
|
|
|
|
|
|
// We cannot perfectly reconstruct original intent (Reply vs ReplyAll) from persisted data.
|
|
|
|
|
// Infer ReplyAll when multiple recipients exist on the local MIME.
|
|
|
|
|
var totalRecipients = CurrentMimeMessage.To.Mailboxes.Count() + CurrentMimeMessage.Cc.Mailboxes.Count();
|
|
|
|
|
var reason = totalRecipients > 1 ? DraftCreationReason.ReplyAll : DraftCreationReason.Reply;
|
|
|
|
|
|
|
|
|
|
return (reason, referenceMailCopy);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public async Task<AccountContact> GetAddressInformationAsync(string tokenText, ObservableCollection<AccountContact> collection)
|
|
|
|
|
{
|
|
|
|
|
// Get model from the service. This will make sure the name is properly included if there is any record.
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
var info = await ContactService.GetAddressInformationByAddressAsync(tokenText)
|
|
|
|
|
?? new AccountContact() { Name = tokenText, Address = tokenText };
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
// Don't add if there is already that address in the collection.
|
|
|
|
|
if (collection.Any(a => a.Address == info.Address))
|
|
|
|
|
return null;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
return info;
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public void NotifyAddressExists()
|
|
|
|
|
{
|
|
|
|
|
_dialogService.InfoBarMessage(Translator.Info_ContactExistsTitle, Translator.Info_ContactExistsMessage, InfoBarMessageType.Warning);
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public void NotifyInvalidEmail(string address)
|
|
|
|
|
{
|
|
|
|
|
_dialogService.InfoBarMessage(Translator.Info_InvalidAddressTitle, string.Format(Translator.Info_InvalidAddressMessage, address), InfoBarMessageType.Warning);
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-03-01 12:07:15 +01:00
|
|
|
protected override async void OnMailUpdated(MailCopy updatedMail, MailUpdateSource source, MailCopyChangeFlags changedProperties)
|
2025-02-16 11:54:23 +01:00
|
|
|
{
|
2026-03-01 12:07:15 +01:00
|
|
|
base.OnMailUpdated(updatedMail, source, changedProperties);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (CurrentMailDraftItem == null) return;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-10-03 15:46:38 +02:00
|
|
|
if (updatedMail.UniqueId == CurrentMailDraftItem.MailCopy.UniqueId)
|
2025-02-16 11:54:23 +01:00
|
|
|
{
|
2026-02-21 10:53:39 +01:00
|
|
|
await ExecuteUIThread(async () =>
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2026-03-01 12:07:15 +01:00
|
|
|
CurrentMailDraftItem.UpdateFrom(updatedMail, changedProperties);
|
2026-02-21 10:53:39 +01:00
|
|
|
await UpdatePendingOperationStateAsync();
|
2026-02-22 17:55:57 +01:00
|
|
|
NotifyComposeActionStateChanged();
|
2025-02-16 11:54:23 +01:00
|
|
|
});
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-22 17:55:57 +01:00
|
|
|
|
|
|
|
|
private void NotifyComposeActionStateChanged()
|
|
|
|
|
{
|
|
|
|
|
OnPropertyChanged(nameof(IsLocalDraft));
|
|
|
|
|
OnPropertyChanged(nameof(ShouldShowSendToServerButton));
|
|
|
|
|
OnPropertyChanged(nameof(ShouldShowSendButton));
|
|
|
|
|
|
|
|
|
|
DiscardCommand.NotifyCanExecuteChanged();
|
|
|
|
|
SendCommand.NotifyCanExecuteChanged();
|
|
|
|
|
SendToServerCommand.NotifyCanExecuteChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ShouldTrackDraftSynchronizationState(Guid accountId)
|
|
|
|
|
{
|
|
|
|
|
if (accountId == Guid.Empty)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var currentDraftAccountId = CurrentMailDraftItem?.MailCopy?.AssignedAccount?.Id
|
|
|
|
|
?? ComposingAccount?.Id
|
|
|
|
|
?? Guid.Empty;
|
|
|
|
|
|
|
|
|
|
return currentDraftAccountId != Guid.Empty && currentDraftAccountId == accountId;
|
|
|
|
|
}
|
2026-02-23 01:02:59 +01:00
|
|
|
|
|
|
|
|
private bool IsWithinLocalDraftRetryGracePeriod(MailCopy localDraft)
|
|
|
|
|
{
|
|
|
|
|
if (localDraft == null || localDraft.CreationDate == default)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var elapsed = DateTime.UtcNow - localDraft.CreationDate;
|
|
|
|
|
|
|
|
|
|
// Clock skew safety.
|
|
|
|
|
if (elapsed < TimeSpan.Zero)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return elapsed < LocalDraftRetryGracePeriod;
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|