-
Notifications
You must be signed in to change notification settings - Fork 81
/
MessageView.cs
132 lines (112 loc) · 5.33 KB
/
MessageView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) 2016,2019,2020, Dijji, and released under Ms-PL. This can be found in the root of this distribution.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
namespace XstReader
{
// Provides a view of a Message object for the UI
public class MessageView : INotifyPropertyChanged
{
private bool isSelected = false;
public MessageView(Message message)
{
if (message == null)
throw new XstException("MessageView requires a Message object");
Message = message;
}
public Message Message { get; private set; }
public string From { get { return Message.From; } }
public string To { get { return Message.To; } }
public string Cc { get { return Message.Cc; } }
public string FromTo { get { return Message.Folder.Name.StartsWith("Sent") ? To : From; } }
public string Subject { get { return Message.Subject; } }
public DateTime? Received { get { return Message.Received; } }
public DateTime? Submitted { get { return Message.Submitted; } }
public DateTime? Modified { get { return Message.Modified; } } // When any attachment was last modified
public DateTime? Date { get { return Received ?? Submitted; } }
public string DisplayDate { get { return Date != null ? ((DateTime)Date).ToString("g") : "<unknown>"; } }
public string Body { get { return Message.Body; } }
public string BodyHtml { get { return Message.BodyHtml; } }
public byte[] Html { get { return Message.Html; } }
public byte[] RtfCompressed { get { return Message.RtfCompressed; } }
public ObservableCollection<Attachment> Attachments { get; private set; } = new ObservableCollection<Attachment>();
public List<Recipient> Recipients { get { return Message.Recipients; } }
public List<Property> Properties { get { return Message.Properties; } }
public bool MayHaveInlineAttachment { get { return Message.MayHaveInlineAttachment; } }
public bool IsEncryptedOrSigned { get { return Message.IsEncryptedOrSigned; } }
// The following properties are used in XAML bindings to control the UI
public bool HasAttachment { get { return Message.HasAttachment; } }
public bool HasFileAttachment { get { return Message.HasFileAttachment; } }
public bool HasVisibleFileAttachment { get { return Message.HasVisibleFileAttachment; } }
public bool HasEmailAttachment { get { return (Attachments.FirstOrDefault(a => a.IsEmail) != null); } }
public bool ShowText { get { return Message.IsBodyText; } }
public bool ShowHtml { get { return Message.IsBodyHtml; } }
public bool ShowRtf { get { return Message.IsBodyRtf; } }
public bool HasToDisplayList { get { return ToDisplayList.Length > 0; } }
public string ToDisplayList { get { return Message.ToDisplayList; } }
public bool HasCcDisplayList { get { return CcDisplayList.Length > 0; } }
public string CcDisplayList { get { return Message.CcDisplayList; } }
public bool HasBccDisplayList { get { return BccDisplayList.Length > 0; } }
public string BccDisplayList { get { return Message.BccDisplayList; } }
public string FileAttachmentDisplayList { get { return Message.FileAttachmentDisplayList; } }
public string ExportFileName { get { return Message.ExportFileName; } }
public string ExportFileExtension { get { return Message.ExportFileExtension; } }
public bool IsSelected
{
get { return isSelected; }
set
{
if (value != isSelected)
{
isSelected = value;
OnPropertyChanged(nameof(IsSelected));
}
}
}
public void ClearContents()
{
Message.ClearContents();
Attachments.Clear();
}
public void ReadSignedOrEncryptedMessage(XstFile xstFile)
{
Attachment a = Message.Attachments[0];
//get attachment bytes
var ms = new MemoryStream();
xstFile.SaveAttachment(ms, a);
byte[] attachmentBytes = ms.ToArray();
Message.ReadSignedOrEncryptedMessage(attachmentBytes);
}
public void SortAndSaveAttachments(List<Attachment> atts = null)
{
// If no attachments are supplied, sort the list we already have
if (atts == null)
atts = new List<Attachment>(Attachments);
atts.Sort((a, b) =>
{
if (a == null)
return -1;
else if (b == null)
return 1;
else if (a.Hide != b.Hide)
return a.Hide ? 1 : -1;
else
return 0;
});
Attachments.Clear();
foreach (var a in atts)
Attachments.Add(a);
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}