Files
Wino-Mail/Wino.Mail/Activation/ProtocolActivationHandler.cs
T

60 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Threading.Tasks;
2024-04-18 01:44:37 +02:00
using CommunityToolkit.Mvvm.Messaging;
using Windows.ApplicationModel.Activation;
using Wino.Core.Domain.Interfaces;
2024-08-10 14:33:02 +02:00
using Wino.Core.Domain.Models.Launch;
using Wino.Messaging.Client.Shell;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
namespace Wino.Activation;
internal class ProtocolActivationHandler : ActivationHandler<ProtocolActivatedEventArgs>
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
private const string MailtoProtocolTag = "mailto:";
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
private readonly INativeAppService _nativeAppService;
private readonly ILaunchProtocolService _launchProtocolService;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public ProtocolActivationHandler(INativeAppService nativeAppService, ILaunchProtocolService launchProtocolService)
{
_nativeAppService = nativeAppService;
_launchProtocolService = launchProtocolService;
}
protected override Task HandleInternalAsync(ProtocolActivatedEventArgs args)
{
// Check URI prefix.
var protocolString = args.Uri.AbsoluteUri;
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
if (protocolString.StartsWith(MailtoProtocolTag))
2025-02-16 11:43:30 +01:00
{
2025-02-16 11:54:23 +01:00
// mailto activation. Try to parse params.
_launchProtocolService.MailToUri = new MailToUri(protocolString);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (_nativeAppService.IsAppRunning())
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
// Just send publish a message. Shell will continue.
WeakReferenceMessenger.Default.Send(new MailtoProtocolMessageRequested());
2024-04-18 01:44:37 +02:00
}
}
2025-02-16 11:54:23 +01:00
return Task.CompletedTask;
}
2025-02-16 11:54:23 +01:00
protected override bool CanHandleInternal(ProtocolActivatedEventArgs args)
{
// Validate the URI scheme.
2025-02-16 11:54:23 +01:00
try
{
var uriGet = args.Uri;
}
2025-02-16 11:54:23 +01:00
catch (UriFormatException)
{
return false;
}
return base.CanHandleInternal(args);
2024-04-18 01:44:37 +02:00
}
}