-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMainForm.cs
158 lines (140 loc) · 5.95 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WLMToPst
{
public partial class MainForm : Form
{
public delegate void CatchExceptionDelegate(Exception ex);
private PSTGenerator generator;
private Thread conversionThread;
public MainForm()
{
InitializeComponent();
CheckConvertButtonEnabled();
generator = new PSTGenerator();
}
private void LogException(Exception ex)
{
string simpleErrorMessage = $"FirstChanceException event raised in {AppDomain.CurrentDomain.FriendlyName}: {ex.Message}";
string fullErrorMessage = simpleErrorMessage + "\r\n\r\n" + Utils.LogExceptionString(ex);
Console.WriteLine(fullErrorMessage);
Trace.WriteLine(fullErrorMessage);
Trace.Flush(); // Flush the output.
if (ex is System.Runtime.InteropServices.COMException)
{
MessageBox.Show("Redemption could not be loaded, please install outlook and try again!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
MessageBox.Show(fullErrorMessage, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void btSelectInputFolder_Click(object sender, EventArgs e)
{
using (var wldDirBrowser = new FolderBrowserDialog())
{
wldDirBrowser.SelectedPath = Directory.GetCurrentDirectory();
DialogResult result = wldDirBrowser.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(wldDirBrowser.SelectedPath))
{
tbInputFolder.Text = wldDirBrowser.SelectedPath;
}
CheckConvertButtonEnabled();
}
}
private void btSelectOutputFile_Click(object sender, EventArgs e)
{
OpenFileDialog wldPSTFileBrowser = new OpenFileDialog();
wldPSTFileBrowser.InitialDirectory = Directory.GetCurrentDirectory();
wldPSTFileBrowser.Title = "Save to PST file";
wldPSTFileBrowser.CheckFileExists = false;
wldPSTFileBrowser.CheckPathExists = true;
wldPSTFileBrowser.DefaultExt = "pst";
wldPSTFileBrowser.Filter = "Outlook PST files (*.pst)|*.pst|All files (*.*)|*.*";
wldPSTFileBrowser.FilterIndex = 1;
wldPSTFileBrowser.RestoreDirectory = true;
wldPSTFileBrowser.ReadOnlyChecked = true;
wldPSTFileBrowser.ShowReadOnly = true;
if (wldPSTFileBrowser.ShowDialog() == DialogResult.OK)
{
tbOutputFile.Text = wldPSTFileBrowser.FileName;
}
CheckConvertButtonEnabled();
}
private void bnConvert_Click(object sender, EventArgs e)
{
if (conversionThread != null)
{
conversionThread.Abort();
conversionThread = null;
}
conversionThread = new Thread(BeginConversion) { Name = "MainConversionThread" };
CheckConvertButtonEnabled();
conversionThread.Start();
}
private void BeginConversion()
{
try
{
string outputPstFullPath = Path.Combine(Directory.GetCurrentDirectory(), "testpst_" + new Random(Utils.ToUnixTimeInt(DateTime.Now)).Next(100, 1000) + ".pst");
generator = new PSTGenerator();
generator.UpdateConvertionThreadProgressHandler += UpdateConvertionThreadProgress;
//generator.CreatePSTFromWindowsLiveMailFolder(outputPstFullPath, @"f:\_Backups_\Tiny\Users\Gebruiker\Windows Live Mail\Kpnmail (MH ab2\");
generator.CreatePSTFromWindowsLiveMailFolder(tbOutputFile.Text, tbInputFolder.Text);
if (InvokeRequired)
{
this.Invoke(new Action(() =>
{
MessageBox.Show(this, "Finished!");
conversionThread = null;
CheckConvertButtonEnabled();
}));
}
}
catch (Exception ex)
{
this.Invoke(new CatchExceptionDelegate(this.LogException), ex);
conversionThread = null;
CheckConvertButtonEnabled();
}
}
private void UpdateConvertionThreadProgress(float i, float total)
{
if (InvokeRequired)
{
BeginInvoke(new Action<float, float>(UpdateConvertionThreadProgress), new object[] { i, total });
return;
}
pbMainProgress.Value = Math.Min((int)i, 100);
}
private void tbInputFolder_TextChanged(object sender, EventArgs e)
{
CheckConvertButtonEnabled();
}
private void tbOutputFile_TextChanged(object sender, EventArgs e)
{
CheckConvertButtonEnabled();
}
private void CheckConvertButtonEnabled()
{
bool inputDirValid = false;
bool outputFileValid = false;
try
{
var fileName = Path.GetFileName(tbOutputFile.Text);
var fileExtension = Path.GetExtension(tbOutputFile.Text);
inputDirValid = Directory.Exists(tbInputFolder.Text);
outputFileValid = Directory.Exists(new FileInfo(tbOutputFile.Text).Directory.FullName) && string.IsNullOrEmpty(fileName) == false && string.IsNullOrEmpty(fileExtension) == false;
} catch{}
bnConvert.Enabled = conversionThread == null && inputDirValid && outputFileValid;
}
}
}