mirror of
https://github.com/cyanfish/naps2.git
synced 2024-08-16 18:50:45 +03:00
Mac: Add email support for the Apple Mail app
This commit is contained in:
parent
e6d5e98c04
commit
4a3c3e7c42
57
NAPS2.Lib.Mac/ImportExport/AppleMailEmailProvider.cs
Normal file
57
NAPS2.Lib.Mac/ImportExport/AppleMailEmailProvider.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using NAPS2.ImportExport.Email;
|
||||
|
||||
namespace NAPS2.ImportExport;
|
||||
|
||||
public class AppleMailEmailProvider : IAppleMailEmailProvider
|
||||
{
|
||||
public Task<bool> SendEmail(EmailMessage emailMessage, ProgressHandler progress = default)
|
||||
{
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
EmailServiceDelegate d = null!;
|
||||
Invoker.Current.Invoke(() =>
|
||||
{
|
||||
var service = NSSharingService.GetSharingService(NSSharingServiceName.ComposeEmail);
|
||||
if (emailMessage.Subject != null)
|
||||
{
|
||||
service.Subject = emailMessage.Subject;
|
||||
}
|
||||
if (emailMessage.Recipients.Any())
|
||||
{
|
||||
service.Recipients = emailMessage.Recipients.Select(x => (NSObject) new NSString(x.Address))
|
||||
.ToArray();
|
||||
}
|
||||
var items = new List<NSObject>();
|
||||
if (emailMessage.BodyText != null)
|
||||
{
|
||||
items.Add(new NSString(emailMessage.BodyText));
|
||||
}
|
||||
foreach (var attachment in emailMessage.Attachments)
|
||||
{
|
||||
items.Add(NSUrl.FromFilename(attachment.FilePath));
|
||||
}
|
||||
d = new EmailServiceDelegate();
|
||||
service.Delegate = d;
|
||||
service.PerformWithItems(items.ToArray());
|
||||
});
|
||||
return await d.Task;
|
||||
});
|
||||
}
|
||||
|
||||
private class EmailServiceDelegate : NSSharingServiceDelegate
|
||||
{
|
||||
private readonly TaskCompletionSource<bool> _tcs = new();
|
||||
|
||||
public override void DidShareItems(NSSharingService sharingService, NSObject[] items)
|
||||
{
|
||||
_tcs.SetResult(true);
|
||||
}
|
||||
|
||||
public override void DidFailToShareItems(NSSharingService sharingService, NSObject[] items, NSError error)
|
||||
{
|
||||
_tcs.SetResult(false);
|
||||
}
|
||||
|
||||
public Task<bool> Task => _tcs.Task;
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ using NAPS2.EtoForms.Mac;
|
||||
using NAPS2.EtoForms.Ui;
|
||||
using NAPS2.Images.Mac;
|
||||
using NAPS2.ImportExport;
|
||||
using NAPS2.ImportExport.Email;
|
||||
using NAPS2.Pdf;
|
||||
using NAPS2.Update;
|
||||
|
||||
@ -17,6 +18,7 @@ public class MacModule : GuiModule
|
||||
|
||||
builder.RegisterType<StubNotificationManager>().As<INotificationManager>().SingleInstance();
|
||||
builder.RegisterType<MacScannedImagePrinter>().As<IScannedImagePrinter>();
|
||||
builder.RegisterType<AppleMailEmailProvider>().As<IAppleMailEmailProvider>();
|
||||
builder.RegisterType<MacDarkModeProvider>().As<IDarkModeProvider>();
|
||||
builder.RegisterType<MacImageContext>().As<ImageContext>();
|
||||
builder.RegisterType<MacImageContext>().AsSelf();
|
||||
|
@ -24,6 +24,8 @@ public class AutofacEmailProviderFactory : IEmailProviderFactory
|
||||
return _container.Resolve<OutlookWebEmailProvider>();
|
||||
case EmailProviderType.Thunderbird:
|
||||
return _container.Resolve<ThunderbirdEmailProvider>();
|
||||
case EmailProviderType.AppleMail:
|
||||
return _container.Resolve<IAppleMailEmailProvider>();
|
||||
default:
|
||||
return _container.Resolve<MapiEmailProvider>();
|
||||
}
|
||||
|
@ -79,6 +79,9 @@ public class EmailSettingsForm : EtoDialogBase
|
||||
case EmailProviderType.Thunderbird:
|
||||
_provider.Text = SettingsResources.EmailProviderType_Thunderbird;
|
||||
break;
|
||||
case EmailProviderType.AppleMail:
|
||||
_provider.Text = SettingsResources.EmailProviderType_AppleMail;
|
||||
break;
|
||||
case EmailProviderType.CustomSmtp:
|
||||
_provider.Text = config.Get(c => c.EmailSetup.SmtpHost) + '\n' + config.Get(c => c.EmailSetup.SmtpUser);
|
||||
break;
|
||||
|
1210
NAPS2.Lib/Icons.Designer.cs
generated
1210
NAPS2.Lib/Icons.Designer.cs
generated
File diff suppressed because it is too large
Load Diff
@ -349,6 +349,9 @@
|
||||
<data name="thunderbird" type="System.Resources.ResXFileRef, System.Windows.Forms">
|
||||
<value>Icons\thunderbird.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</data>
|
||||
<data name="apple_mail" type="System.Resources.ResXFileRef, System.Windows.Forms">
|
||||
<value>Icons\apple_mail.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</data>
|
||||
<data name="email_setting" type="System.Resources.ResXFileRef, System.Windows.Forms">
|
||||
<value>Icons\email_setting.png;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</data>
|
||||
|
BIN
NAPS2.Lib/Icons/apple_mail.png
Normal file
BIN
NAPS2.Lib/Icons/apple_mail.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.2 KiB |
@ -60,6 +60,7 @@ public class EmailProviderController
|
||||
#if NET6_0_OR_GREATER
|
||||
// For Windows we expect Thunderbird to be used through MAPI. For Linux we need to handle it specially.
|
||||
MaybeAddWidget(EmailProviderType.Thunderbird, OperatingSystem.IsLinux());
|
||||
MaybeAddWidget(EmailProviderType.AppleMail, OperatingSystem.IsMacOS());
|
||||
#endif
|
||||
MaybeAddWidget(EmailProviderType.Gmail, _gmailOauthProvider.HasClientCreds);
|
||||
MaybeAddWidget(EmailProviderType.OutlookWeb, _outlookWebOauthProvider.HasClientCreds);
|
||||
@ -89,6 +90,13 @@ public class EmailProviderController
|
||||
// The point is to give a hint to the user that Thunderbird support is present.
|
||||
Enabled = _thunderbirdProvider.IsAvailable
|
||||
},
|
||||
EmailProviderType.AppleMail => new EmailProviderWidget
|
||||
{
|
||||
ProviderType = EmailProviderType.AppleMail,
|
||||
ProviderIcon = Icons.apple_mail.ToEtoImage(),
|
||||
ProviderName = EmailProviderType.AppleMail.Description(),
|
||||
Choose = ChooseAppleMail
|
||||
},
|
||||
EmailProviderType.Gmail => new EmailProviderWidget
|
||||
{
|
||||
ProviderType = EmailProviderType.Gmail,
|
||||
@ -145,6 +153,15 @@ public class EmailProviderController
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool ChooseAppleMail()
|
||||
{
|
||||
var transact = _config.User.BeginTransaction();
|
||||
transact.Remove(c => c.EmailSetup);
|
||||
transact.Set(c => c.EmailSetup.ProviderType, EmailProviderType.AppleMail);
|
||||
transact.Commit();
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool ChooseOauth(OauthProvider provider)
|
||||
{
|
||||
var authForm = _formFactory.Create<AuthorizeForm>();
|
||||
|
@ -12,5 +12,7 @@ public enum EmailProviderType
|
||||
[LocalizedDescription(typeof(SettingsResources), "EmailProviderType_OutlookWeb")]
|
||||
OutlookWeb,
|
||||
[LocalizedDescription(typeof(SettingsResources), "EmailProviderType_Thunderbird")]
|
||||
Thunderbird
|
||||
Thunderbird,
|
||||
[LocalizedDescription(typeof(SettingsResources), "EmailProviderType_AppleMail")]
|
||||
AppleMail
|
||||
}
|
5
NAPS2.Lib/ImportExport/Email/IAppleMailEmailProvider.cs
Normal file
5
NAPS2.Lib/ImportExport/Email/IAppleMailEmailProvider.cs
Normal file
@ -0,0 +1,5 @@
|
||||
namespace NAPS2.ImportExport.Email;
|
||||
|
||||
public interface IAppleMailEmailProvider : IEmailProvider
|
||||
{
|
||||
}
|
@ -93,6 +93,12 @@ namespace NAPS2.Lang.Resources {
|
||||
}
|
||||
}
|
||||
|
||||
internal static string EmailProviderType_AppleMail {
|
||||
get {
|
||||
return ResourceManager.GetString("EmailProviderType_AppleMail", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
internal static string EmailProvider_NotSelected {
|
||||
get {
|
||||
return ResourceManager.GetString("EmailProvider_NotSelected", resourceCulture);
|
||||
|
@ -141,6 +141,9 @@
|
||||
<data name="EmailProviderType_Thunderbird" xml:space="preserve">
|
||||
<value>Thunderbird</value>
|
||||
</data>
|
||||
<data name="EmailProviderType_AppleMail" xml:space="preserve">
|
||||
<value>Apple Mail</value>
|
||||
</data>
|
||||
<data name="EmailProvider_NotSelected" xml:space="preserve">
|
||||
<value>No provider selected.</value>
|
||||
</data>
|
||||
|
Loading…
Reference in New Issue
Block a user