forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.cpp
539 lines (474 loc) · 16.2 KB
/
Settings.cpp
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include "pch.h"
#include "Settings.h"
#include "PowerRenameInterfaces.h"
#include "settings_helpers.h"
#include <filesystem>
#include <commctrl.h>
#include <algorithm>
#include <fstream>
namespace
{
const wchar_t c_powerRenameDataFilePath[] = L"\\power-rename-settings.json";
const wchar_t c_powerRenameUIFlagsFilePath[] = L"\\power-rename-ui-flags";
const wchar_t c_searchMRUListFilePath[] = L"\\search-mru.json";
const wchar_t c_replaceMRUListFilePath[] = L"\\replace-mru.json";
const wchar_t c_rootRegPath[] = L"Software\\Microsoft\\PowerRename";
const wchar_t c_mruSearchRegPath[] = L"\\SearchMRU";
const wchar_t c_mruReplaceRegPath[] = L"\\ReplaceMRU";
const wchar_t c_enabled[] = L"Enabled";
const wchar_t c_showIconOnMenu[] = L"ShowIcon";
const wchar_t c_extendedContextMenuOnly[] = L"ExtendedContextMenuOnly";
const wchar_t c_persistState[] = L"PersistState";
const wchar_t c_maxMRUSize[] = L"MaxMRUSize";
const wchar_t c_flags[] = L"Flags";
const wchar_t c_searchText[] = L"SearchText";
const wchar_t c_replaceText[] = L"ReplaceText";
const wchar_t c_mruEnabled[] = L"MRUEnabled";
const wchar_t c_mruList[] = L"MRUList";
const wchar_t c_insertionIdx[] = L"InsertionIdx";
unsigned int GetRegNumber(const std::wstring& valueName, unsigned int defaultValue)
{
DWORD type = REG_DWORD;
DWORD data = 0;
DWORD size = sizeof(DWORD);
if (SHGetValue(HKEY_CURRENT_USER, c_rootRegPath, valueName.c_str(), &type, &data, &size) == ERROR_SUCCESS)
{
return data;
}
return defaultValue;
}
void SetRegNumber(const std::wstring& valueName, unsigned int value)
{
SHSetValue(HKEY_CURRENT_USER, c_rootRegPath, valueName.c_str(), REG_DWORD, &value, sizeof(value));
}
bool GetRegBoolean(const std::wstring& valueName, bool defaultValue)
{
DWORD value = GetRegNumber(valueName.c_str(), defaultValue ? 1 : 0);
return (value == 0) ? false : true;
}
void SetRegBoolean(const std::wstring& valueName, bool value)
{
SetRegNumber(valueName, value ? 1 : 0);
}
std::wstring GetRegString(const std::wstring& valueName,const std::wstring& subPath)
{
wchar_t value[CSettings::MAX_INPUT_STRING_LEN];
value[0] = L'\0';
DWORD type = REG_SZ;
DWORD size = CSettings::MAX_INPUT_STRING_LEN * sizeof(wchar_t);
std::wstring completePath = std::wstring(c_rootRegPath) + subPath;
if (SHGetValue(HKEY_CURRENT_USER, completePath.c_str(), valueName.c_str(), &type, value, &size) == ERROR_SUCCESS)
{
return std::wstring(value);
}
return std::wstring{};
}
bool LastModifiedTime(const std::wstring& filePath, FILETIME* lpFileTime)
{
WIN32_FILE_ATTRIBUTE_DATA attr{};
if (GetFileAttributesExW(filePath.c_str(), GetFileExInfoStandard, &attr))
{
*lpFileTime = attr.ftLastWriteTime;
return true;
}
return false;
}
}
class MRUListHandler
{
public:
MRUListHandler(unsigned int size, const std::wstring& filePath, const std::wstring& regPath) :
pushIdx(0),
nextIdx(1),
size(size),
jsonFilePath(PTSettingsHelper::get_module_save_folder_location(L"PowerRename") + filePath),
registryFilePath(regPath)
{
items.resize(size);
Load();
}
void Push(const std::wstring& data);
bool Next(std::wstring& data);
void Reset();
private:
void Load();
void Save();
void MigrateFromRegistry();
json::JsonArray Serialize();
void ParseJson();
bool Exists(const std::wstring& data);
std::vector<std::wstring> items;
unsigned int pushIdx;
unsigned int nextIdx;
unsigned int size;
const std::wstring jsonFilePath;
const std::wstring registryFilePath;
};
void MRUListHandler::Push(const std::wstring& data)
{
if (Exists(data))
{
// TODO: Already existing item should be put on top of MRU list.
return;
}
items[pushIdx] = data;
pushIdx = (pushIdx + 1) % size;
Save();
}
bool MRUListHandler::Next(std::wstring& data)
{
if (nextIdx == size + 1)
{
Reset();
return false;
}
// Go backwards to consume latest items first.
unsigned int idx = (pushIdx + size - nextIdx) % size;
if (items[idx].empty())
{
Reset();
return false;
}
data = items[idx];
++nextIdx;
return true;
}
void MRUListHandler::Reset()
{
nextIdx = 1;
}
void MRUListHandler::Load()
{
if (!std::filesystem::exists(jsonFilePath))
{
MigrateFromRegistry();
Save();
}
else
{
ParseJson();
}
}
void MRUListHandler::Save()
{
json::JsonObject jsonData;
jsonData.SetNamedValue(c_maxMRUSize, json::value(size));
jsonData.SetNamedValue(c_insertionIdx, json::value(pushIdx));
jsonData.SetNamedValue(c_mruList, Serialize());
json::to_file(jsonFilePath, jsonData);
}
json::JsonArray MRUListHandler::Serialize()
{
json::JsonArray searchMRU{};
std::wstring data{};
for (const std::wstring& item : items)
{
searchMRU.Append(json::value(item));
}
return searchMRU;
}
void MRUListHandler::MigrateFromRegistry()
{
std::wstring searchListKeys = GetRegString(c_mruList, registryFilePath);
std::sort(std::begin(searchListKeys), std::end(searchListKeys));
for (const wchar_t& key : searchListKeys)
{
Push(GetRegString(std::wstring(1, key), registryFilePath));
}
}
void MRUListHandler::ParseJson()
{
auto json = json::from_file(jsonFilePath);
if (json)
{
const json::JsonObject& jsonObject = json.value();
try
{
unsigned int oldSize{ size };
if (json::has(jsonObject, c_maxMRUSize, json::JsonValueType::Number))
{
oldSize = (unsigned int)jsonObject.GetNamedNumber(c_maxMRUSize);
}
unsigned int oldPushIdx{ 0 };
if (json::has(jsonObject, c_insertionIdx, json::JsonValueType::Number))
{
oldPushIdx = (unsigned int)jsonObject.GetNamedNumber(c_insertionIdx);
if (oldPushIdx < 0 || oldPushIdx >= oldSize)
{
oldPushIdx = 0;
}
}
if (json::has(jsonObject, c_mruList, json::JsonValueType::Array))
{
auto jsonArray = jsonObject.GetNamedArray(c_mruList);
if (oldSize == size)
{
for (uint32_t i = 0; i < jsonArray.Size(); ++i)
{
items[i] = std::wstring(jsonArray.GetStringAt(i));
}
pushIdx = oldPushIdx;
}
else
{
std::vector<std::wstring> temp;
for (unsigned int i = 0; i < min(jsonArray.Size(), size); ++i)
{
int idx = (oldPushIdx + oldSize - (i + 1)) % oldSize;
temp.push_back(std::wstring(jsonArray.GetStringAt(idx)));
}
if (size > oldSize)
{
std::reverse(std::begin(temp), std::end(temp));
pushIdx = (unsigned int)temp.size();
temp.resize(size);
}
else
{
temp.resize(size);
std::reverse(std::begin(temp), std::end(temp));
}
items = std::move(temp);
Save();
}
}
}
catch (const winrt::hresult_error&) { }
}
}
bool MRUListHandler::Exists(const std::wstring& data)
{
return std::find(std::begin(items), std::end(items), data) != std::end(items);
}
class CRenameMRU :
public IEnumString,
public IPowerRenameMRU
{
public:
// IUnknown
IFACEMETHODIMP_(ULONG) AddRef();
IFACEMETHODIMP_(ULONG) Release();
IFACEMETHODIMP QueryInterface(_In_ REFIID riid, _Outptr_ void** ppv);
// IEnumString
IFACEMETHODIMP Next(__in ULONG celt, __out_ecount_part(celt, *pceltFetched) LPOLESTR* rgelt, __out_opt ULONG* pceltFetched);
IFACEMETHODIMP Skip(__in ULONG) { return E_NOTIMPL; }
IFACEMETHODIMP Reset();
IFACEMETHODIMP Clone(__deref_out IEnumString** ppenum) { *ppenum = nullptr; return E_NOTIMPL; }
// IPowerRenameMRU
IFACEMETHODIMP AddMRUString(_In_ PCWSTR entry);
static HRESULT CreateInstance(_In_ const std::wstring& filePath, _In_ const std::wstring& regPath, _Outptr_ IUnknown** ppUnk);
private:
CRenameMRU(int size, const std::wstring& filePath, const std::wstring& regPath);
std::unique_ptr<MRUListHandler> mruList;
unsigned int refCount = 0;
};
CRenameMRU::CRenameMRU(int size, const std::wstring& filePath, const std::wstring& regPath) :
refCount(1)
{
mruList = std::make_unique<MRUListHandler>(size, filePath, regPath);
}
HRESULT CRenameMRU::CreateInstance(_In_ const std::wstring& filePath, _In_ const std::wstring& regPath, _Outptr_ IUnknown** ppUnk)
{
*ppUnk = nullptr;
unsigned int maxMRUSize = CSettingsInstance().GetMaxMRUSize();
HRESULT hr = maxMRUSize > 0 ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
CRenameMRU* renameMRU = new CRenameMRU(maxMRUSize, filePath, regPath);
hr = renameMRU ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
renameMRU->QueryInterface(IID_PPV_ARGS(ppUnk));
renameMRU->Release();
}
}
return hr;
}
IFACEMETHODIMP_(ULONG) CRenameMRU::AddRef()
{
return InterlockedIncrement(&refCount);
}
IFACEMETHODIMP_(ULONG) CRenameMRU::Release()
{
unsigned int cnt = InterlockedDecrement(&refCount);
if (cnt == 0)
{
delete this;
}
return cnt;
}
IFACEMETHODIMP CRenameMRU::QueryInterface(_In_ REFIID riid, _Outptr_ void** ppv)
{
static const QITAB qit[] = {
QITABENT(CRenameMRU, IEnumString),
QITABENT(CRenameMRU, IPowerRenameMRU),
{ 0 }
};
return QISearch(this, qit, riid, ppv);
}
IFACEMETHODIMP CRenameMRU::Next(__in ULONG celt, __out_ecount_part(celt, *pceltFetched) LPOLESTR* rgelt, __out_opt ULONG* pceltFetched)
{
if (pceltFetched)
{
*pceltFetched = 0;
}
if (!celt)
{
return S_OK;
}
if (!rgelt)
{
return S_FALSE;
}
HRESULT hr = S_FALSE;
if (std::wstring data{}; mruList->Next(data))
{
hr = SHStrDup(data.c_str(), rgelt);
if (SUCCEEDED(hr) && pceltFetched != nullptr)
{
*pceltFetched = 1;
}
}
return hr;
}
IFACEMETHODIMP CRenameMRU::Reset()
{
mruList->Reset();
return S_OK;
}
IFACEMETHODIMP CRenameMRU::AddMRUString(_In_ PCWSTR entry)
{
mruList->Push(entry);
return S_OK;
}
CSettings::CSettings()
{
std::wstring result = PTSettingsHelper::get_module_save_folder_location(L"PowerRename");
jsonFilePath = result + std::wstring(c_powerRenameDataFilePath);
UIFlagsFilePath = result + std::wstring(c_powerRenameUIFlagsFilePath);
Load();
}
void CSettings::Save()
{
json::JsonObject jsonData;
jsonData.SetNamedValue(c_enabled, json::value(settings.enabled));
jsonData.SetNamedValue(c_showIconOnMenu, json::value(settings.showIconOnMenu));
jsonData.SetNamedValue(c_extendedContextMenuOnly, json::value(settings.extendedContextMenuOnly));
jsonData.SetNamedValue(c_persistState, json::value(settings.persistState));
jsonData.SetNamedValue(c_mruEnabled, json::value(settings.MRUEnabled));
jsonData.SetNamedValue(c_maxMRUSize, json::value(settings.maxMRUSize));
jsonData.SetNamedValue(c_searchText, json::value(settings.searchText));
jsonData.SetNamedValue(c_replaceText, json::value(settings.replaceText));
json::to_file(jsonFilePath, jsonData);
GetSystemTimeAsFileTime(&lastLoadedTime);
}
void CSettings::Load()
{
if (!std::filesystem::exists(jsonFilePath))
{
MigrateFromRegistry();
Save();
WriteFlags();
}
else
{
ParseJson();
ReadFlags();
}
}
void CSettings::Reload()
{
// Load json settings from data file if it is modified in the meantime.
FILETIME lastModifiedTime{};
if (LastModifiedTime(jsonFilePath, &lastModifiedTime) &&
CompareFileTime(&lastModifiedTime, &lastLoadedTime) == 1)
{
Load();
}
}
void CSettings::MigrateFromRegistry()
{
settings.enabled = GetRegBoolean(c_enabled, true);
settings.showIconOnMenu = GetRegBoolean(c_showIconOnMenu, true);
settings.extendedContextMenuOnly = GetRegBoolean(c_extendedContextMenuOnly, false); // Disabled by default.
settings.persistState = GetRegBoolean(c_persistState, true);
settings.MRUEnabled = GetRegBoolean(c_mruEnabled, true);
settings.maxMRUSize = GetRegNumber(c_maxMRUSize, 10);
settings.flags = GetRegNumber(c_flags, 0);
settings.searchText = GetRegString(c_searchText, L"");
settings.replaceText = GetRegString(c_replaceText, L"");
}
void CSettings::ParseJson()
{
auto json = json::from_file(jsonFilePath);
if (json)
{
const json::JsonObject& jsonSettings = json.value();
try
{
if (json::has(jsonSettings, c_enabled, json::JsonValueType::Boolean))
{
settings.enabled = jsonSettings.GetNamedBoolean(c_enabled);
}
if (json::has(jsonSettings, c_showIconOnMenu, json::JsonValueType::Boolean))
{
settings.showIconOnMenu = jsonSettings.GetNamedBoolean(c_showIconOnMenu);
}
if (json::has(jsonSettings, c_extendedContextMenuOnly, json::JsonValueType::Boolean))
{
settings.extendedContextMenuOnly = jsonSettings.GetNamedBoolean(c_extendedContextMenuOnly);
}
if (json::has(jsonSettings, c_persistState, json::JsonValueType::Boolean))
{
settings.persistState = jsonSettings.GetNamedBoolean(c_persistState);
}
if (json::has(jsonSettings, c_mruEnabled, json::JsonValueType::Boolean))
{
settings.MRUEnabled = jsonSettings.GetNamedBoolean(c_mruEnabled);
}
if (json::has(jsonSettings, c_maxMRUSize, json::JsonValueType::Number))
{
settings.maxMRUSize = (unsigned int)jsonSettings.GetNamedNumber(c_maxMRUSize);
}
if (json::has(jsonSettings, c_searchText, json::JsonValueType::String))
{
settings.searchText = jsonSettings.GetNamedString(c_searchText);
}
if (json::has(jsonSettings, c_replaceText, json::JsonValueType::String))
{
settings.replaceText = jsonSettings.GetNamedString(c_replaceText);
}
}
catch (const winrt::hresult_error&) { }
}
GetSystemTimeAsFileTime(&lastLoadedTime);
}
void CSettings::ReadFlags()
{
std::ifstream file(UIFlagsFilePath, std::ios::binary);
if (file.is_open())
{
file >> settings.flags;
}
}
void CSettings::WriteFlags()
{
std::ofstream file(UIFlagsFilePath, std::ios::binary);
if (file.is_open())
{
file << settings.flags;
}
}
CSettings& CSettingsInstance()
{
static CSettings instance;
return instance;
}
HRESULT CRenameMRUSearch_CreateInstance(_Outptr_ IUnknown** ppUnk)
{
return CRenameMRU::CreateInstance(c_searchMRUListFilePath, c_mruSearchRegPath, ppUnk);
}
HRESULT CRenameMRUReplace_CreateInstance(_Outptr_ IUnknown** ppUnk)
{
return CRenameMRU::CreateInstance(c_replaceMRUListFilePath, c_mruReplaceRegPath, ppUnk);
}