forked from Dijji/XstReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
311 lines (279 loc) · 11.8 KB
/
MainWindow.xaml.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) 2016, Dijji, and released under Ms-PL. This can be found in the root of this distribution.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
namespace XstReader
{
/// <summary>
/// XstReader is a viewer for xst (.ost and .pst) files
/// This file contains the interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private View view = new View();
private XstFile xstFile = null;
private GridViewColumnHeader listViewSortCol = null;
private SortAdorner listViewSortAdorner = null;
public MainWindow()
{
InitializeComponent();
this.DataContext = view;
}
private void btnOpen_Click(object sender, RoutedEventArgs e)
{
// Ask for a .ost or .pst file to open
System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
dialog.Filter = "xst files (*.ost;*.pst)|*.ost;*.pst|All files (*.*)|*.*";
dialog.FilterIndex = 1;
dialog.InitialDirectory = Properties.Settings.Default.LastFolder;
if (dialog.InitialDirectory == "")
dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
dialog.RestoreDirectory = true;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.LastFolder = System.IO.Path.GetDirectoryName(dialog.FileName);
Properties.Settings.Default.Save();
view.Clear();
txtStatus.Text = "Loading...";
Mouse.OverrideCursor = Cursors.Wait;
// Load on a background thread so we can keep the UI in sync
Task.Factory.StartNew(() =>
{
try
{
xstFile = new XstFile(view, dialog.FileName);
xstFile.ReadFolderTree();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error reading xst file");
}
})
// When loading completes, update the UI using the UI thread
.ContinueWith((task) =>
{
Application.Current.Dispatcher.Invoke(new Action(() =>
{
txtStatus.Text = "";
Mouse.OverrideCursor = null;
Title = "Xst Reader - " + System.IO.Path.GetFileName(dialog.FileName);
}));
});
}
}
private void treeFolders_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
Folder f = (Folder)e.NewValue;
view.SelectedFolder = f;
if (f != null)
{
f.Messages.Clear();
txtStatus.Text = "Reading messages...";
Mouse.OverrideCursor = Cursors.Wait;
// Read messages on a background thread so we can keep the UI in sync
Task.Factory.StartNew(() =>
{
try
{
xstFile.ReadMessages(f);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error reading messages");
}
})
// When loading completes, update the UI using the UI thread
.ContinueWith((task) =>
{
Application.Current.Dispatcher.Invoke(new Action(() =>
{
txtStatus.Text = "";
Mouse.OverrideCursor = null;
}));
});
// If there is no sort in effect, sort by date in descending order
if (listViewSortCol == null)
{
string tag = "Date";
listViewSortCol = ((GridView)listMessages.View).Columns.Select(c => (GridViewColumnHeader)c.Header).Where(h => h.Tag.ToString() == tag).First();
listViewSortAdorner = new SortAdorner(listViewSortCol, ListSortDirection.Descending);
AdornerLayer.GetAdornerLayer(listViewSortCol).Add(listViewSortAdorner);
listMessages.Items.SortDescriptions.Add(new SortDescription(tag, ListSortDirection.Descending));
}
}
}
private void listMessages_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Message m = (Message)listMessages.SelectedItem;
if (m != null)
{
try
{
xstFile.ReadMessageDetails(m);
ShowMessage(m);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error reading message details");
}
}
view.SetMessage(m);
}
private void listMessagesColumnHeader_Click(object sender, RoutedEventArgs e)
{
// Sort the messages by the clicked on column
GridViewColumnHeader column = (sender as GridViewColumnHeader);
string sortBy = column.Tag.ToString();
if (listViewSortCol != null)
{
AdornerLayer.GetAdornerLayer(listViewSortCol).Remove(listViewSortAdorner);
listMessages.Items.SortDescriptions.Clear();
}
ListSortDirection newDir = ListSortDirection.Ascending;
if (listViewSortCol == column && listViewSortAdorner.Direction == newDir)
newDir = ListSortDirection.Descending;
listViewSortCol = column;
listViewSortAdorner = new SortAdorner(listViewSortCol, newDir);
AdornerLayer.GetAdornerLayer(listViewSortCol).Add(listViewSortAdorner);
listMessages.Items.SortDescriptions.Add(new SortDescription(sortBy, newDir));
}
private void listRecipients_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
view.SelectedRecipientChanged((Recipient)listRecipients.SelectedItem);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error reading recipient");
}
}
private void listAttachments_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
view.SelectedAttachmentsChanged(listAttachments.SelectedItems.Cast<Attachment>());
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error reading attachment");
}
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
SaveAttachments(listAttachments.SelectedItems.Cast<Attachment>().Where(a => a.IsFile));
}
private void btnSaveAll_Click(object sender, RoutedEventArgs e)
{
SaveAttachments(listAttachments.Items.Cast<Attachment>().Where(a => a.IsFile));
}
private void btnOpenEmail_Click(object sender, RoutedEventArgs e)
{
Message m = xstFile.OpenAttachedMessage(listAttachments.Items.Cast<Attachment>().First(a => a.IsEmail));
ShowMessage(m);
view.PushMessage(m);
}
private void btnCloseEmail_Click(object sender, RoutedEventArgs e)
{
view.PopMessage();
ShowMessage(view.CurrentMessage);
}
private void rbContent_Click(object sender, RoutedEventArgs e)
{
view.ShowContent = true;
}
private void rbProperties_Click(object sender, RoutedEventArgs e)
{
view.ShowContent = false;
}
private void ShowMessage(Message m)
{
try
{
if (m != null)
{
// Can't bind HTML content, so push it into the control, if the message is HTML
if (m.ShowHtml)
{
string embedded = null;
if (m.MayHaveInlineAttachment)
embedded = m.EmbedAttachments(xstFile); // Returns null if this is not appropriate
if (embedded != null)
{
wbMessage.NavigateToString(embedded);
m.SortAndSaveAttachments(); // Re-sort attachments in case any new in-line rendering discovered
}
else if (m.BodyHtml != null)
{
wbMessage.NavigateToString(m.BodyHtml);
}
else if (m.Html != null)
{
var ms = new System.IO.MemoryStream(m.Html);
wbMessage.NavigateToStream(ms);
}
else if (m.Body != null)
{
wbMessage.NavigateToString(m.Body);
}
}
// Can't bind RTF content, so push it into the control, if the message is RTF
else if (m.ShowRtf)
{
var decomp = new RtfDecompressor();
using (System.IO.MemoryStream ms = decomp.Decompress(m.RtfCompressed, true))
{
ms.Position = 0;
rtfMessage.SelectAll();
rtfMessage.Selection.Load(ms, DataFormats.Rtf);
}
}
}
else
{
// Clear the HTML, in case we were showing that before
wbMessage.Navigate("about:blank");
// Clear the RTF, in case we were showing that before
rtfMessage.Document.Blocks.Clear();
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error reading message body");
}
}
private void SaveAttachments(IEnumerable<Attachment> attachments)
{
// Find out where to save the attachments
System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
dialog.Description = "Choose folder for saving attachments";
dialog.RootFolder = Environment.SpecialFolder.MyComputer;
dialog.SelectedPath = Properties.Settings.Default.LastAttachmentFolder;
if (dialog.SelectedPath == "")
dialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
dialog.ShowNewFolderButton = true;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.LastAttachmentFolder = dialog.SelectedPath;
Properties.Settings.Default.Save();
try
{
foreach (var a in attachments)
{
xstFile.SaveAttachment(dialog.SelectedPath, a);
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error saving attachments");
}
}
}
}
}