Show the email provider prompt on first click and other behavioral polish

This commit is contained in:
Ben Olden-Cooligan 2018-08-27 14:40:25 -04:00
parent b81f7748ce
commit 207da6ecf9
4 changed files with 68 additions and 54 deletions

View File

@ -12,7 +12,8 @@ namespace NAPS2.ImportExport.Email
{
var builder = new BodyBuilder
{
TextBody = string.IsNullOrWhiteSpace(emailMessage.BodyText) ? "Test body" : emailMessage.BodyText
// Ensure there is some content (a newline is fine) to work around the buggy new gmail UI
TextBody = string.IsNullOrWhiteSpace(emailMessage.BodyText) ? "\n" : emailMessage.BodyText
};
foreach (var attachment in emailMessage.Attachments)
{
@ -20,11 +21,10 @@ namespace NAPS2.ImportExport.Email
}
var message = new MimeMessage();
emailMessage.Recipients.Add(new EmailRecipient { Address = "someone@example.com", Name = "Someone", Type = EmailRecipientType.To });
CopyRecips(emailMessage.Recipients, EmailRecipientType.To, message.To);
CopyRecips(emailMessage.Recipients, EmailRecipientType.Cc, message.Cc);
CopyRecips(emailMessage.Recipients, EmailRecipientType.Bcc, message.Bcc);
message.Subject = string.IsNullOrWhiteSpace(emailMessage.Subject) ? "Scan" : emailMessage.Subject;
message.Subject = emailMessage.Subject ?? "";
message.Body = builder.ToMessageBody();
SendMimeMessage(message);

View File

@ -23,6 +23,9 @@ namespace NAPS2.ImportExport.Email.Oauth
var messageId = gmailOauthProvider.UploadDraft(message.ToString());
var userEmail = userConfigManager.Config.EmailSetup?.GmailUser;
// Open the draft in the user's browser
// Note: As of this writing, the direct url is bugged in the new gmail UI, and there is no workaround
// https://issuetracker.google.com/issues/113127519
// At least it directs to the drafts folder
Process.Start($"https://mail.google.com/mail/?authuser={userEmail}#drafts/{messageId}");
}
}

View File

@ -68,13 +68,13 @@ namespace NAPS2.WinForms
});
}
providerWidgets.Add(new EmailProviderWidget
{
ProviderType = EmailProviderType.CustomSmtp,
ProviderIcon = Icons.email_setting,
ProviderName = EmailProviderType.CustomSmtp.Description(),
ClickAction = ChooseCustomSmtp
});
//providerWidgets.Add(new EmailProviderWidget
//{
// ProviderType = EmailProviderType.CustomSmtp,
// ProviderIcon = Icons.email_setting,
// ProviderName = EmailProviderType.CustomSmtp.Description(),
// ClickAction = ChooseCustomSmtp
//});
// Put the configured provider at the top
var defaultWidget = GetDefaultWidget();
@ -110,11 +110,6 @@ namespace NAPS2.WinForms
}
}
private void ChooseCustomSmtp()
{
throw new NotImplementedException();
}
private void ShowWidgets()
{
int heightDiff = Height - panel1.Height;

View File

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAPS2.Config;
using NAPS2.ImportExport;
using NAPS2.ImportExport.Email;
using NAPS2.ImportExport.Images;
@ -28,8 +30,9 @@ namespace NAPS2.WinForms
private readonly OcrManager ocrManager;
private readonly IEmailProviderFactory emailProviderFactory;
private readonly IOperationProgress operationProgress;
private readonly IUserConfigManager userConfigManager;
public WinFormsExportHelper(PdfSettingsContainer pdfSettingsContainer, ImageSettingsContainer imageSettingsContainer, EmailSettingsContainer emailSettingsContainer, DialogHelper dialogHelper, FileNamePlaceholders fileNamePlaceholders, ChangeTracker changeTracker, IOperationFactory operationFactory, IFormFactory formFactory, OcrManager ocrManager, IEmailProviderFactory emailProviderFactory, IOperationProgress operationProgress)
public WinFormsExportHelper(PdfSettingsContainer pdfSettingsContainer, ImageSettingsContainer imageSettingsContainer, EmailSettingsContainer emailSettingsContainer, DialogHelper dialogHelper, FileNamePlaceholders fileNamePlaceholders, ChangeTracker changeTracker, IOperationFactory operationFactory, IFormFactory formFactory, OcrManager ocrManager, IEmailProviderFactory emailProviderFactory, IOperationProgress operationProgress, IUserConfigManager userConfigManager)
{
this.pdfSettingsContainer = pdfSettingsContainer;
this.imageSettingsContainer = imageSettingsContainer;
@ -42,6 +45,7 @@ namespace NAPS2.WinForms
this.ocrManager = ocrManager;
this.emailProviderFactory = emailProviderFactory;
this.operationProgress = operationProgress;
this.userConfigManager = userConfigManager;
}
public async Task<bool> SavePDF(List<ScannedImage> images, ISaveNotify notify)
@ -123,54 +127,66 @@ namespace NAPS2.WinForms
public async Task<bool> EmailPDF(List<ScannedImage> images)
{
if (images.Any())
if (!images.Any())
{
var emailSettings = emailSettingsContainer.EmailSettings;
var invalidChars = new HashSet<char>(Path.GetInvalidFileNameChars());
var attachmentName = new string(emailSettings.AttachmentName.Where(x => !invalidChars.Contains(x)).ToArray());
if (string.IsNullOrEmpty(attachmentName))
{
attachmentName = "Scan.pdf";
}
if (!attachmentName.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase))
{
attachmentName += ".pdf";
}
attachmentName = fileNamePlaceholders.SubstitutePlaceholders(attachmentName, DateTime.Now, false);
return false;
}
var tempFolder = new DirectoryInfo(Path.Combine(Paths.Temp, Path.GetRandomFileName()));
tempFolder.Create();
try
if (userConfigManager.Config.EmailSetup == null)
{
// First run; prompt for a
var form = formFactory.Create<FEmailProvider>();
if (form.ShowDialog() != DialogResult.OK)
{
string targetPath = Path.Combine(tempFolder.FullName, attachmentName);
if (!await ExportPDF(targetPath, images, true))
return false;
}
}
var emailSettings = emailSettingsContainer.EmailSettings;
var invalidChars = new HashSet<char>(Path.GetInvalidFileNameChars());
var attachmentName = new string(emailSettings.AttachmentName.Where(x => !invalidChars.Contains(x)).ToArray());
if (string.IsNullOrEmpty(attachmentName))
{
attachmentName = "Scan.pdf";
}
if (!attachmentName.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase))
{
attachmentName += ".pdf";
}
attachmentName = fileNamePlaceholders.SubstitutePlaceholders(attachmentName, DateTime.Now, false);
var tempFolder = new DirectoryInfo(Path.Combine(Paths.Temp, Path.GetRandomFileName()));
tempFolder.Create();
try
{
string targetPath = Path.Combine(tempFolder.FullName, attachmentName);
if (!await ExportPDF(targetPath, images, true))
{
// Cancel or error
return false;
}
var message = new EmailMessage
{
Attachments =
{
// Cancel or error
return false;
}
var message = new EmailMessage
{
Attachments =
new EmailAttachment
{
new EmailAttachment
{
FilePath = targetPath,
AttachmentName = attachmentName
}
FilePath = targetPath,
AttachmentName = attachmentName
}
};
if (emailProviderFactory.Default.SendEmail(message))
{
changeTracker.HasUnsavedChanges = false;
return true;
}
}
finally
};
if (emailProviderFactory.Default.SendEmail(message))
{
tempFolder.Delete(true);
changeTracker.HasUnsavedChanges = false;
return true;
}
}
finally
{
tempFolder.Delete(true);
}
return false;
}
}