-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFilterShell.cs
More file actions
352 lines (287 loc) · 12.6 KB
/
Copy pathFilterShell.cs
File metadata and controls
352 lines (287 loc) · 12.6 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using IFilterShellView.Exceptions;
using IFilterShellView.Filter;
using IFilterShellView.Native;
using IFilterShellView.Parser;
using IFilterShellView.Shell.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
namespace IFilterShellView.Program
{
public class FilterShell
{
public delegate void DelegateOnUIBefore();
public delegate void DelegateOnUIAfter(Exception RuntimeException, bool WasQueryExecuted);
public delegate void DelegateOnUIProgress(List<CPidlData> x);
private readonly BackgroundWorker workerObject;
public DelegateOnUIBefore ptrOnUiBefore { get; set; }
public DelegateOnUIAfter ptrOnUiAfter { get; set; }
public DelegateOnUIProgress ptrOnUiProgress { get; set; }
private DelegateOnUIAfter ptrUnscopedOnUiAfter;
public FilterShell()
{
workerObject = new BackgroundWorker();
workerObject.DoWork += new DoWorkEventHandler(Worker_CallbackMain);
workerObject.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_CallbackDone);
workerObject.ProgressChanged += new ProgressChangedEventHandler(Worker_CallbackProgress);
workerObject.WorkerReportsProgress = true;
workerObject.WorkerSupportsCancellation = true;
}
#region Worker registered events
private void Worker_CallbackMain(object sender, DoWorkEventArgs e)
{
ProcessingUnit(sender as BackgroundWorker);
}
private void Worker_CallbackProgress(object sender, ProgressChangedEventArgs e)
{
if (e.UserState != null)
{
ptrOnUiProgress((List<CPidlData>)e.UserState);
}
}
private void Worker_CallbackDone(object sender, RunWorkerCompletedEventArgs e)
{
Exception RuntimeException = e.Error;
ptrOnUiAfter(RuntimeException, true);
ptrUnscopedOnUiAfter?.Invoke(RuntimeException, true);
Context.Instance.FlagRunInBackgroundWorker = false;
}
#endregion Worker registered events
#region The actual processing unit - handles both filtering and exception handling
private void ProcessingUnit(BackgroundWorker worker)
{
List<CPidlData> LocalSelectionBuffer = new List<CPidlData>();
Func<CPidlData, bool> ILLanguageFunct = null;
IntPtr PidlPtrObj = IntPtr.Zero;
Exception LastException = null;
bool IsOnWorkerThread = worker != null;
bool FlagFlushBuffer = true;
int NumberOfIntervals = Math.Min(10, Context.Instance.PidlCount);
int NumberOfItemsPerInterval = Math.Min(Context.Instance.PidlCount / NumberOfIntervals, NumberOfIntervals);
int ReportPecentage = 0;
int TempCounter = 0;
int FailedAttempts = 0;
int MaxFailedAttempts = 15;
// Callback that handles report management - dispatches reports according to the caller's context.
void ReportAction()
{
List<CPidlData> NewList = LocalSelectionBuffer.GetRange(0, LocalSelectionBuffer.Count);
if (IsOnWorkerThread)
{
worker.ReportProgress(ReportPecentage, (object)NewList);
}
else
{
ptrOnUiProgress(NewList);
}
LocalSelectionBuffer.Clear();
}
// Reset filter count
Context.Instance.FilterCount = 0;
// Disable redrawing for faster selection
Context.Instance.pIFolderView2.SetRedraw(false);
// No point in unselecting all items if I don't want to select any
if (Properties.Settings.Default.AutoSelectFiltered)
{
Context.Instance.pIShellView.SelectItem(IntPtr.Zero, SVSI.SVSI_DESELECTOTHERS);
}
// After deselecting all items check if the filter is empty
if (Context.Instance.FilterText.Length == 0)
{
goto LabelRestoreState;
}
// Compile the predicate chain if needed
if (Context.Instance.FlagExtendedFilterMod)
{
XPressParser XLanguageParser = new XPressParser(Context.Instance.FilterTextWithoutCommandModifier);
ILLanguageFunct = XLanguageParser.Compile();
if (ILLanguageFunct == null)
{
throw new UserException("Parser couldn't compile the given command");
}
}
try
{
// Iterate through all the items inside the shell and start matching
for (int PidlIndex = 0; PidlIndex < Context.Instance.PidlCount; PidlIndex++, TempCounter++)
{
bool FilterMatched = false;
// If I want to return only a sepcific number of elements
if (Properties.Settings.Default.MaxNumberFilterUpTo > 0 &&
Properties.Settings.Default.MaxNumberFilterUpTo <= Context.Instance.FilterCount)
{
break;
}
Context.Instance.pIFolderView2.Item(PidlIndex, out PidlPtrObj);
ReportPecentage = (int)((float)PidlIndex / Context.Instance.PidlCount * 100.0f);
if (PidlPtrObj == IntPtr.Zero)
{
if (FailedAttempts > MaxFailedAttempts)
{
throw new UserException("Too many failures while interacting with the shell view");
}
FailedAttempts++;
continue;
}
else
{
FailedAttempts = 0;
}
NativeWin32.HResult hr = NativeWin32.SHGetDataFromIDListW(
Context.Instance.pIShellFolder,
PidlPtrObj,
NativeWin32.SHGDFIL_FINDDATA,
out NativeWin32.WIN32_FIND_DATA Pidl_Win32_FindData,
NativeWin32.WIN32_FIND_DATA_SIZE);
CPidlData PidlData = new CPidlData();
if (hr == NativeWin32.HResult.Ok)
{
PidlData.PidlName = Pidl_Win32_FindData.cFileName;
PidlData.FileAttributes = Pidl_Win32_FindData.dwFileAttributes;
PidlData.FileSize = ((ulong)Pidl_Win32_FindData.nFileSizeHigh << 32) | Pidl_Win32_FindData.nFileSizeLow;
PidlData.CreationTime = FileTimeExtension.ToDateTime(Pidl_Win32_FindData.ftCreationTime);
PidlData.LastAccessTime = FileTimeExtension.ToDateTime(Pidl_Win32_FindData.ftLastAccessTime);
PidlData.LastWriteTime = FileTimeExtension.ToDateTime(Pidl_Win32_FindData.ftLastWriteTime);
PidlData.AttributesSet = true;
}
else
{
Context.Instance.pIShellFolder.GetDisplayNameOf
(
PidlPtrObj,
SHGNO.INFOLDER | SHGNO.FORPARSING,
Context.Instance.MarshalPIDLNativeDataHolder.STRRET
);
NativeWin32.StrRetToBuf
(
Context.Instance.MarshalPIDLNativeDataHolder.STRRET,
PidlPtrObj,
Context.Instance.MarshalPIDLNativeDataHolder.BUFFER,
Context.Instance.MarshalPIDLNativeDataHolder.MAX_PATH
);
PidlData.PidlName = Context.Instance.MarshalPIDLNativeDataHolder.BUFFER.ToString();
PidlData.AttributesSet = false;
}
if (Context.Instance.FlagExtendedFilterMod)
{
// Then all the matching will be done according to an expression tree
FilterMatched = ILLanguageFunct(PidlData);
}
else
{
// Then all the matching will be done according to settings enabled by the user
FilterMatched = FilterBasedOnSettings(PidlData, Context.Instance.FilterText);
}
// If there was a match then this pidl must be selected
if (FilterMatched)
{
LocalSelectionBuffer.Add(PidlData);
if (Properties.Settings.Default.AutoSelectFiltered)
{
Context.Instance.pIShellView.SelectItem(PidlPtrObj, SVSI.SVSI_SELECT);
}
Context.Instance.FilterCount++;
}
Marshal.FreeCoTaskMem(PidlPtrObj);
PidlPtrObj = IntPtr.Zero;
if (TempCounter >= NumberOfItemsPerInterval)
{
ReportAction();
TempCounter = 0;
}
if (IsOnWorkerThread && worker.CancellationPending)
{
FlagFlushBuffer = false;
break;
}
}
}
catch (Exception ExceptionMessage)
{
if (PidlPtrObj != IntPtr.Zero)
Marshal.FreeCoTaskMem(PidlPtrObj);
LastException = ExceptionMessage;
}
LabelRestoreState:
// Flush the last reports inside the buffer
if (FlagFlushBuffer && LocalSelectionBuffer.Count != 0)
{
ReportAction();
}
Context.Instance.pIFolderView2.SetRedraw(true);
if (LastException != null) throw LastException;
}
private bool FilterBasedOnSettings(CPidlData PidlData, string input)
{
uint SettingsPlacementId = Properties.Settings.Default.SettingsPlacementId;
uint SettingsCaseId = Properties.Settings.Default.SettingsCaseId;
string PidlName = PidlData.PidlName;
// Case insensitive
if (SettingsCaseId == 2)
{
PidlName = PidlData.PidlName.ToLower();
input = input.ToLower();
}
// Find applicable actions
var action = FitlerActions.SettingsActionMap[(FilterSettingsFlags)SettingsPlacementId];
return action(PidlName, input);
}
#endregion
public void StartSync(
DelegateOnUIBefore ptrScopedOnUiBefore,
DelegateOnUIAfter ptrScopedOnUiAfter
)
{
if (!workerObject.IsBusy)
{
ptrOnUiBefore();
ptrScopedOnUiBefore?.Invoke();
bool wasQueryExecuted = false;
Exception RuntimeException = null;
try
{
if (Context.Instance.FlagRunInBackgroundWorker)
{
return;
}
ProcessingUnit(null);
wasQueryExecuted = true;
}
catch (Exception ExceptionMessage)
{
RuntimeException = ExceptionMessage;
}
finally
{
ptrOnUiAfter(RuntimeException, wasQueryExecuted);
ptrScopedOnUiAfter?.Invoke(RuntimeException, wasQueryExecuted);
}
}
}
public void StartAsync(
DelegateOnUIBefore ptrScopedOnUiBefore,
DelegateOnUIAfter ptrScopedOnUiAfter
)
{
if (!workerObject.IsBusy)
{
ptrOnUiBefore();
ptrScopedOnUiBefore?.Invoke();
this.ptrUnscopedOnUiAfter = ptrScopedOnUiAfter;
workerObject.RunWorkerAsync();
}
}
public bool StopAsync()
{
if (workerObject.IsBusy)
{
workerObject.CancelAsync();
return true;
}
return false;
}
public bool IsBusy => workerObject.IsBusy;
}
}