/
Core.cs
250 lines (207 loc) · 9.44 KB
/
Core.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace RED2
{
/// <summary>
/// RED core class, handles all the "hard work" and communicates with the GUI by using various events.
/// </summary>
public class REDCore
{
private MainWindow redMainWindow = null;
public WorkflowSteps CurrentProcessStep = WorkflowSteps.Idle;
private RuntimeData Data = null;
// Workers
private FindEmptyDirectoryWorker searchEmptyFoldersWorker = null;
private DeletionWorker deletionWorker = null;
// Events
public event EventHandler<ErrorEventArgs> OnError;
public event EventHandler OnCancelled;
public event EventHandler OnAborted;
public event EventHandler<ProgressChangedEventArgs> OnProgressChanged;
public event EventHandler<FoundEmptyDirInfoEventArgs> OnFoundEmptyDirectory;
public event EventHandler<FinishedScanForEmptyDirsEventArgs> OnFinishedScanForEmptyDirs;
public event EventHandler<DeleteProcessUpdateEventArgs> OnDeleteProcessChanged;
public event EventHandler<DeletionErrorEventArgs> OnDeleteError;
public event EventHandler<DeleteProcessFinishedEventArgs> OnDeleteProcessFinished;
public REDCore(MainWindow mainWindow, RuntimeData data)
{
this.redMainWindow = mainWindow;
this.Data = data;
}
/// <summary>
/// Start searching empty folders
/// </summary>
public void SearchingForEmptyDirectories()
{
this.CurrentProcessStep = WorkflowSteps.StartSearchingForEmptyDirs;
// Rest folder list
this.Data.ProtectedFolderList = new Dictionary<string, bool>();
searchEmptyFoldersWorker = new FindEmptyDirectoryWorker();
searchEmptyFoldersWorker.Data = this.Data;
searchEmptyFoldersWorker.ProgressChanged += new ProgressChangedEventHandler(FFWorker_ProgressChanged);
searchEmptyFoldersWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(FFWorker_RunWorkerCompleted);
// Start worker
searchEmptyFoldersWorker.RunWorkerAsync(this.Data.StartFolder);
}
/// <summary>
/// This function gets called on a status update of the
/// find worker
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void FFWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (e.UserState is FoundEmptyDirInfoEventArgs)
{
var info = (FoundEmptyDirInfoEventArgs)e.UserState;
if (info.Type == DirectorySearchStatusTypes.Empty)
this.Data.EmptyFolderList.Add(info.Directory);
else if (info.Type == DirectorySearchStatusTypes.Error && this.Data.HideScanErrors)
return;
if (this.OnFoundEmptyDirectory != null)
this.OnFoundEmptyDirectory(this, info);
}
else if (e.UserState is string)
{
if (this.OnProgressChanged != null)
this.OnProgressChanged(this, new ProgressChangedEventArgs(0, (string)e.UserState));
}
else
{
// TODO: Handle unknown types
}
}
void FFWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.CurrentProcessStep = WorkflowSteps.Idle;
if (e.Error != null)
{
this.searchEmptyFoldersWorker.Dispose(); this.searchEmptyFoldersWorker = null;
this.showErrorMsg(e.Error.Message);
}
else if (e.Cancelled)
{
if (this.searchEmptyFoldersWorker.ErrorInfo != null)
{
// A error occured, process was stopped
this.showErrorMsg(this.searchEmptyFoldersWorker.ErrorInfo.ErrorMessage);
this.searchEmptyFoldersWorker.Dispose(); this.searchEmptyFoldersWorker = null;
if (this.OnAborted != null)
this.OnAborted(this, new EventArgs());
}
else
{
this.searchEmptyFoldersWorker.Dispose(); this.searchEmptyFoldersWorker = null;
if (this.OnCancelled != null)
this.OnCancelled(this, new EventArgs());
}
}
else
{
int FolderCount = this.searchEmptyFoldersWorker.FolderCount;
this.searchEmptyFoldersWorker.Dispose(); this.searchEmptyFoldersWorker = null;
if (this.OnFinishedScanForEmptyDirs != null)
this.OnFinishedScanForEmptyDirs(this, new FinishedScanForEmptyDirsEventArgs(this.Data.EmptyFolderList.Count, FolderCount));
}
}
internal void CancelCurrentProcess()
{
if (this.CurrentProcessStep == WorkflowSteps.StartSearchingForEmptyDirs)
{
if (this.searchEmptyFoldersWorker == null) return;
if ((this.searchEmptyFoldersWorker.IsBusy == true) || (searchEmptyFoldersWorker.CancellationPending == false))
searchEmptyFoldersWorker.CancelAsync();
}
else if (this.CurrentProcessStep == WorkflowSteps.DeleteProcessRunning)
{
if (this.deletionWorker == null) return;
if ((this.deletionWorker.IsBusy == true) || (deletionWorker.CancellationPending == false))
deletionWorker.CancelAsync();
}
}
public void StartDeleteProcess()
{
this.CurrentProcessStep = WorkflowSteps.DeleteProcessRunning;
this.deletionWorker = new DeletionWorker();
this.deletionWorker.Data = this.Data;
this.deletionWorker.ProgressChanged += new ProgressChangedEventHandler(deletionWorker_ProgressChanged);
this.deletionWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(deletionWorker_RunWorkerCompleted);
this.deletionWorker.RunWorkerAsync();
}
void deletionWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
var state = e.UserState as DeleteProcessUpdateEventArgs;
if (this.OnDeleteProcessChanged != null)
this.OnDeleteProcessChanged(this, state);
}
void deletionWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.CurrentProcessStep = WorkflowSteps.Idle;
if (e.Error != null)
{
this.showErrorMsg(e.Error.Message);
this.deletionWorker.Dispose(); this.deletionWorker = null;
}
else if (e.Cancelled)
{
if (this.deletionWorker.ErrorInfo != null)
{
// A error occured, process was stopped
//
// -> Ask user to continue
if (OnDeleteError != null)
OnDeleteError(this, this.deletionWorker.ErrorInfo);
else
throw new Exception("Internal error: event handler is missing.");
}
else
{
// The user cancelled the process
if (this.OnCancelled != null)
this.OnCancelled(this, new EventArgs());
}
}
else
{
// TODO: Use separate class here?
int deletedCount = this.deletionWorker.DeletedCount;
int failedCount = this.deletionWorker.FailedCount;
this.deletionWorker.Dispose(); this.deletionWorker = null;
if (this.OnDeleteProcessFinished != null)
this.OnDeleteProcessFinished(this, new DeleteProcessFinishedEventArgs(deletedCount, failedCount));
}
}
internal void AddProtectedFolder(string path)
{
if (!this.Data.ProtectedFolderList.ContainsKey(path))
this.Data.ProtectedFolderList.Add(path, true);
}
internal void RemoveProtected(string FolderFullName)
{
if (this.Data.ProtectedFolderList.ContainsKey(FolderFullName))
this.Data.ProtectedFolderList.Remove(FolderFullName);
}
public string GetLogMessages()
{
return this.Data.LogMessages.ToString();
}
private void showErrorMsg(string errorMessage)
{
if (this.OnError != null)
this.OnError(this, new ErrorEventArgs(errorMessage));
}
internal void AbortDeletion()
{
this.CurrentProcessStep = WorkflowSteps.Idle;
this.deletionWorker.Dispose(); this.deletionWorker = null;
if (this.OnAborted != null)
this.OnAborted(this, new EventArgs());
}
internal void ContinueDeleteProcess()
{
this.CurrentProcessStep = WorkflowSteps.DeleteProcessRunning;
this.deletionWorker.RunWorkerAsync();
}
}
}