Skip to content

Commit

Permalink
Handle saving multiple selected attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dijji committed Dec 5, 2019
1 parent 4aed159 commit 5778bab
Showing 1 changed file with 44 additions and 37 deletions.
81 changes: 44 additions & 37 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private void exportEmail_Executed(object sender, ExecutedRoutedEventArgs e)
try
{
view.CurrentMessage.ExportToFile(fullFileName, xstFile);
SaveAllAttachmentsToAssociatedFolder(fullFileName, view.CurrentMessage);
SaveVisibleAttachmentsToAssociatedFolder(fullFileName, view.CurrentMessage);
}
catch (System.Exception ex)
{
Expand Down Expand Up @@ -289,7 +289,7 @@ private void ExportEmails(IEnumerable<Message>messages)
var fullFileName = String.Format(@"{0}\{1}.{2}",
folderName, fileName, m.ExportFileExtension);
m.ExportToFile(fullFileName, xstFile);
SaveAllAttachmentsToAssociatedFolder(fullFileName, m);
SaveVisibleAttachmentsToAssociatedFolder(fullFileName, m);
good++;
}
catch (System.Exception ex)
Expand Down Expand Up @@ -351,7 +351,25 @@ private void ExportEmailProperties(IEnumerable<Message> messages)
}
}

private void SaveAllAttachmentsToAssociatedFolder(string fullFileName, Message m)
private void SaveAttachments(IEnumerable<Attachment> attachments)
{
string folderName = GetAttachmentsSaveFolderName();

if (folderName != null)
{
try
{
SaveAttachmentsToFolder(folderName, view.CurrentMessage.Date, attachments);
}
catch (System.Exception ex)
{
MessageBox.Show(String.Format("Error '{0}' saving attachments to '{1}'",
ex.Message, view.CurrentMessage.Subject), "Error saving attachments");
}
}
}

private void SaveVisibleAttachmentsToAssociatedFolder(string fullFileName, Message m)
{
if (m.HasVisibleFileAttachment)
{
Expand All @@ -363,15 +381,15 @@ private void SaveAllAttachmentsToAssociatedFolder(string fullFileName, Message m
if (m.Date != null)
Directory.SetCreationTime(targetFolder, (DateTime)m.Date);
}
SaveAllAttachmentsToFolder(targetFolder, m);
SaveAttachmentsToFolder(targetFolder, m.Date, m.Attachments.Where(a => a.IsFile && !a.Hide));
}
}

private void SaveAllAttachmentsToFolder(string fullFolderName, Message m)
private void SaveAttachmentsToFolder(string fullFolderName, DateTime? creationTime, IEnumerable<Attachment> attachments)
{
foreach (var a in m.Attachments.Where(a => a.IsFile && !a.Hide))
foreach (var a in attachments)
{
xstFile.SaveAttachmentToFolder(fullFolderName, m.Date, a);
xstFile.SaveAttachmentToFolder(fullFolderName, creationTime, a);
}
}

Expand All @@ -392,27 +410,9 @@ private void exportEmailProperties_Executed(object sender, ExecutedRoutedEventAr

private void btnSaveAllAttachments_Click(object sender, RoutedEventArgs e)
{
string folderName = GetAttachmentsSaveFolderName();

if (folderName != null)
{
try
{
SaveAllAttachmentsToFolder(folderName, view.CurrentMessage);
}
catch (System.Exception ex)
{
MessageBox.Show(String.Format("Error '{0}' saving attachments to '{1}'",
ex.Message, view.CurrentMessage.Subject), "Error saving attachments");
}
}
SaveAttachments(view.CurrentMessage.Attachments);
}

//private void btnOpenEmail_Click(object sender, RoutedEventArgs e)
//{
// OpenEmailAttachment(listAttachments.Items.Cast<Attachment>().First(a => a.IsEmail));
//}


private void btnCloseEmail_Click(object sender, RoutedEventArgs e)
{
view.PopMessage();
Expand Down Expand Up @@ -837,18 +837,25 @@ private void openAttachmentWith_Executed(object sender, ExecutedRoutedEventArgs

private void saveAttachmentAs_Executed(object sender, ExecutedRoutedEventArgs e)
{
var a = listAttachments.SelectedItem as Attachment;
var fullFileName = GetSaveAttachmentFileName(a.LongFileName);

if (fullFileName != null)
if (listAttachments.SelectedItems.Count > 1)
{
try
{
xstFile.SaveAttachment(fullFileName, view.CurrentMessage.Date, a);
}
catch (System.Exception ex)
SaveAttachments(listAttachments.SelectedItems.Cast<Attachment>());
}
else
{
var a = listAttachments.SelectedItem as Attachment;
var fullFileName = GetSaveAttachmentFileName(a.LongFileName);

if (fullFileName != null)
{
MessageBox.Show(ex.Message, "Error saving attachment");
try
{
xstFile.SaveAttachment(fullFileName, view.CurrentMessage.Date, a);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error saving attachment");
}
}
}
e.Handled = true;
Expand Down

1 comment on commit 5778bab

@flywire
Copy link
Contributor

@flywire flywire commented on 5778bab Dec 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested OK

Please sign in to comment.