-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathState.cs
More file actions
415 lines (356 loc) · 16.1 KB
/
State.cs
File metadata and controls
415 lines (356 loc) · 16.1 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) 2013, 2020 Dijji, and released under Ms-PL. This, with other relevant licenses, can be found in the root of this distribution.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Xml.Serialization;
namespace CustomWindowsProperties
{
internal class State
{
private Options options = null;
public List<PropertyConfig> SystemProperties { get; } = new List<PropertyConfig>();
public List<PropertyConfig> CustomProperties { get; } = new List<PropertyConfig>();
public List<PropertyConfig> SavedProperties { get; } = new List<PropertyConfig>();
public Dictionary<string, PropertyConfig> DictInstalledProperties { get; } = new Dictionary<string, PropertyConfig>();
public Dictionary<string, PropertyConfig> DictSavedProperties { get; } = new Dictionary<string, PropertyConfig>();
public string DataFolder
{
get { return Options.DataFolder; }
set { Options.DataFolder = value; SaveOptions(); }
}
private Options Options
{
get
{
if (options == null)
options = new Options();
return options;
}
}
private string OptionsFileName
{
get
{
return ApplicationDataFolder + Path.DirectorySeparatorChar + "Options.xml";
}
}
private string ApplicationDataFolder
{
get
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"CustomWindowsProperties");
}
}
public void LoadOptions()
{
if (File.Exists(OptionsFileName))
{
try
{
XmlSerializer x = new XmlSerializer(typeof(Options));
using (TextReader reader = new StreamReader(OptionsFileName))
{
options = (Options)x.Deserialize(reader);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error reading saved options");
}
}
}
public void SaveOptions()
{
try
{
if (!Directory.Exists(ApplicationDataFolder))
Directory.CreateDirectory(ApplicationDataFolder);
XmlSerializer x = new XmlSerializer(typeof(Options));
using (TextWriter writer = new StreamWriter(OptionsFileName))
{
x.Serialize(writer, Options);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error saving chosen options");
}
}
public PropertyConfig LoadPropertyConfig(string fullFileName)
{
if (File.Exists(fullFileName))
{
try
{
XmlSerializer x = new XmlSerializer(typeof(PropertyConfig));
using (TextReader reader = new StreamReader(fullFileName))
{
return (PropertyConfig)x.Deserialize(reader);
}
}
catch (Exception ex)
{
MessageBox.Show($"Error '{ex.Message}' loading saved property from '{fullFileName}'",
"Error loading saved property");
}
}
return null;
}
public void SavePropertyConfig(PropertyConfig config)
{
string fileName = DataFolder + Path.DirectorySeparatorChar + config.CanonicalName + ".xml";
try
{
XmlSerializer x = new XmlSerializer(typeof(PropertyConfig));
using (TextWriter writer = new StreamWriter(fileName))
{
x.Serialize(writer, config);
}
}
catch (Exception ex)
{
MessageBox.Show($"Error '{ex.Message}' saving property {config.CanonicalName} to '{fileName}'",
"Error saving property");
}
}
public void DeletePropertyConfig(string canonicalName)
{
string fileName = DataFolder + Path.DirectorySeparatorChar + canonicalName + ".xml";
try
{
if (File.Exists(fileName))
File.Delete(fileName);
}
catch (Exception ex)
{
MessageBox.Show($"Error '{ex.Message}' deleting property {canonicalName} saved in '{fileName}'",
"Error deleting property");
}
}
public void Populate()
{
LoadOptions();
PopulatePropertyList(SystemProperties, PropertySystemNativeMethods.PropDescEnumFilter.PDEF_SYSTEM);
PopulatePropertyList(CustomProperties, PropertySystemNativeMethods.PropDescEnumFilter.PDEF_NONSYSTEM);
LoadSavedProperties();
}
public void AddSavedProperty(PropertyConfig config)
{
SavedProperties.Add(config);
DictSavedProperties[config.CanonicalName] = config;
}
public void RemoveSavedProperty(string canonicalName)
{
var index = SavedProperties.FindIndex(p => p.CanonicalName == canonicalName);
if (index != -1)
SavedProperties.RemoveAt(index);
DictSavedProperties.Remove(canonicalName);
}
public void AddInstalledProperty(PropertyConfig config)
{
CustomProperties.Add(config);
DictInstalledProperties[config.CanonicalName] = config;
}
public void RemoveInstalledProperty(string canonicalName)
{
var index = CustomProperties.FindIndex(p => p.CanonicalName == canonicalName);
if (index != -1)
CustomProperties.RemoveAt(index);
DictInstalledProperties.Remove(canonicalName);
}
// Returns negative numbers for failure, positive for success
public int RegisterCustomProperty(string fullFileName, PropertyConfig pc, out PropertyConfig installedConfig)
{
installedConfig = null;
FileInfo fi = new FileInfo(fullFileName);
if (!fi.Exists)
throw new Exception($"Installed property configuration file {fullFileName} is missing");
// Copy the file into a more protected common area away from the editor
var targetFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"CustomWindowsProperties");
if (!Directory.Exists(targetFolder))
Directory.CreateDirectory(targetFolder);
var targetFileName = $"{targetFolder}{Path.DirectorySeparatorChar}{fi.Name}";
File.Copy(fullFileName, targetFileName, true);
var result = PropertySystemNativeMethods.PSRegisterPropertySchema(targetFileName);
if (result == 0 || result == 0x000401A0) // INPLACE_S_TRUNCATED
{
// Read back what was actually installed
installedConfig = GetInstalledProperty(pc);
if (installedConfig != null)
{
if (result == 0)
return 0;
else
{
//MessageBox.Show("Property configuration was installed by Windows, but not all sections could be used. " +
// "There may be more information in the Application event log.",
// "Partial installation");
return 1;
}
}
else
MessageBox.Show("Property configuration was rejected by Windows. There may be more information in the Application event log.",
"Error installing property");
}
else
MessageBox.Show($"Property registration failed with error code 0x{result:x}", "Error installing property");
return -1;
}
private PropertyConfig GetInstalledProperty(PropertyConfig pc, bool skipBasics = false)
{
try
{
PropertyConfig installed = null;
var key = new PropertyKey(pc.FormatId, (int)pc.PropertyId);
var guidDescription = new Guid(ShellIIDGuid.IPropertyDescription);
if (!skipBasics)
{
var hr = PropertySystemNativeMethods.PSGetPropertyDescription(
ref key, ref guidDescription, out IPropertyDescription propertyDescription);
if (hr >= 0)
{
var shellProperty = new ShellPropertyDescription(propertyDescription);
installed = new PropertyConfig(shellProperty);
shellProperty.Dispose(); // Releases propertyDescription
}
}
else
installed = pc; // Continue populating existing config
if (installed != null)
{
var guidSearch = new Guid(ShellIIDGuid.IPropertyDescriptionSearchInfo);
var hr = PropertySystemNativeMethods.PSGetPropertyDescription(
ref key, ref guidSearch, out IPropertyDescriptionSearchInfo propSearchInfo);
if (hr >= 0)
{
hr = propSearchInfo.GetSearchInfoFlags(out PropertySearchInfoFlags searchOptions);
if (hr >= 0)
{
installed.InInvertedIndex = searchOptions.HasFlag(PropertySearchInfoFlags.InInvertedIndex);
installed.IsColumn = searchOptions.HasFlag(PropertySearchInfoFlags.IsColumn);
installed.IsColumnSparse = searchOptions.HasFlag(PropertySearchInfoFlags.IsColumnSparse);
installed.AlwaysInclude = searchOptions.HasFlag(PropertySearchInfoFlags.AlwaysInclude);
installed.UseForTypeAhead = searchOptions.HasFlag(PropertySearchInfoFlags.UseForTypeAhead);
}
hr = propSearchInfo.GetColumnIndexType(out ColumnIndexType ppType);
if (hr >= 0) installed.ColumnIndexType = ppType;
// Just the canonical name again
//hr = propSearchInfo.GetProjectionString(out IntPtr namePtr);
//if (CoreErrorHelper.Succeeded(hr) && namePtr != IntPtr.Zero)
//{
// string displayName = Marshal.PtrToStringUni(namePtr);
// Marshal.FreeCoTaskMem(namePtr);
//}
hr = propSearchInfo.GetMaxSize(out uint maxSize);
if (hr >= 0) installed.MaxSize = maxSize;
Marshal.ReleaseComObject(propSearchInfo);
}
var guidAlias = new Guid(ShellIIDGuid.IPropertyDescriptionAliasInfo);
hr = PropertySystemNativeMethods.PSGetPropertyDescription(
ref key, ref guidAlias, out IPropertyDescriptionAliasInfo propAliasInfo);
if (hr >= 0)
{
hr = propAliasInfo.GetSortByAlias(guidDescription, out IPropertyDescription alias);
if (hr >= 0 && alias != null)
{
alias.GetCanonicalName(out string canonicalName);
pc.SortByAlias = canonicalName;
// To do Consider adding additional aliases
Marshal.ReleaseComObject(alias);
}
Marshal.ReleaseComObject(propAliasInfo);
}
}
return installed;
}
#pragma warning disable CS0168 // Variable is declared but never used
#pragma warning disable IDE0059 // Unnecessary assignment of a value
catch (Exception ex)
#pragma warning restore IDE0059 // Unnecessary assignment of a value
#pragma warning restore CS0168 // Variable is declared but never used
{
return null;
}
}
public bool UnregisterCustomProperty(string canonicalName)
{
// The file is held in a more protected common area away from the editor
var targetFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"CustomWindowsProperties");
var targetFileName = $"{targetFolder}{Path.DirectorySeparatorChar}{canonicalName}.propdesc";
if (!File.Exists(targetFileName))
throw new Exception($"Installed property configuration file {targetFileName} is missing");
var result = PropertySystemNativeMethods.PSUnregisterPropertySchema(targetFileName);
if (result == 0)
{
File.Delete(targetFileName);
return true;
}
MessageBox.Show($"Property unregistration failed with error code 0x{result:x}", "Error uninstalling property");
return false;
}
private void PopulatePropertyList(List<PropertyConfig> propertyList,
PropertySystemNativeMethods.PropDescEnumFilter filter)
{
propertyList.Clear();
IPropertyDescriptionList propertyDescriptionList = null;
IPropertyDescription propertyDescription = null;
Guid guid = new Guid(ShellIIDGuid.IPropertyDescriptionList);
try
{
var hr = PropertySystemNativeMethods.PSEnumeratePropertyDescriptions(
filter, ref guid, out propertyDescriptionList);
if (hr >= 0)
{
propertyDescriptionList.GetCount(out uint count);
guid = new Guid(ShellIIDGuid.IPropertyDescription);
for (uint i = 0; i < count; i++)
{
propertyDescriptionList.GetAt(i, ref guid, out propertyDescription);
if (propertyDescription != null)
{
var shellProperty = new ShellPropertyDescription(propertyDescription);
var pc = new PropertyConfig(shellProperty);
shellProperty.Dispose(); // Releases propertyDescription
propertyDescription = null;
GetInstalledProperty(pc, true); // Add search and alias info
propertyList.Add(pc);
DictInstalledProperties.Add(pc.CanonicalName, pc);
}
}
}
}
finally
{
if (propertyDescriptionList != null)
{
Marshal.ReleaseComObject(propertyDescriptionList);
}
if (propertyDescription != null)
{
Marshal.ReleaseComObject(propertyDescription);
}
}
}
private void LoadSavedProperties()
{
if (DataFolder == null)
return;
var di = new DirectoryInfo(DataFolder);
foreach (var fi in di.GetFiles().Where(f => f.Extension == ".xml"))
{
var pc = LoadPropertyConfig(fi.FullName);
if (pc != null) // Occurs when property cannot be loaded
{
SavedProperties.Add(pc);
DictSavedProperties.Add(pc.CanonicalName, pc);
}
}
}
}
}