forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeasureRecycleManager.cpp
230 lines (196 loc) · 5.6 KB
/
MeasureRecycleManager.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
/* Copyright (C) 2016 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#include "StdAfx.h"
#include "MeasureRecycleManager.h"
#include "ConfigParser.h"
#include "Logger.h"
#include "System.h"
namespace {
bool g_Thread = false;
double g_BinCount = 0.0;
double g_BinSize = 0.0;
int g_UpdateCount = 0;
int g_InstanceCount = 0;
CRITICAL_SECTION g_CriticalSection;
DWORD WINAPI QueryRecycleBinThreadProc(void* pParam)
{
// NOTE: Do not use CRT functions (since thread was created with CreateThread())!
SHQUERYRBINFO rbi = { 0 };
rbi.cbSize = sizeof(SHQUERYRBINFO);
SHQueryRecycleBin(nullptr, &rbi);
g_BinCount = (double)rbi.i64NumItems;
g_BinSize = (double)rbi.i64Size;
g_Thread = false;
return 0UL;
}
bool HasRecycleBinChanged()
{
static DWORD s_LastVolumeCount = 0UL;
static ULONGLONG s_LastWriteTime = 0ULL;
bool changed = false;
// Check if items have been added to recycle bin since last check.
HKEY volumeKey = nullptr;
const WCHAR* subKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\BitBucket\\Volume";
LSTATUS ls = RegOpenKeyEx(HKEY_CURRENT_USER, subKey, 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &volumeKey);
if (ls == ERROR_SUCCESS)
{
DWORD volumeCount = 0UL;
RegQueryInfoKey(volumeKey, nullptr, nullptr, nullptr, &volumeCount, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
if (volumeCount != s_LastVolumeCount)
{
s_LastVolumeCount = volumeCount;
changed = true;
}
WCHAR buffer[64] = { 0 };
DWORD bufferSize = _countof(buffer);
DWORD index = 0UL;
while ((ls = RegEnumKeyEx(volumeKey, index, buffer, &bufferSize, nullptr, nullptr, nullptr, nullptr)) == ERROR_SUCCESS)
{
HKEY volumeSubKey = nullptr;
ls = RegOpenKeyEx(volumeKey, buffer, 0, KEY_QUERY_VALUE, &volumeSubKey);
if (ls == ERROR_SUCCESS)
{
ULONGLONG lastWriteTime = 0ULL;
ls = RegQueryInfoKey(volumeSubKey, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, (FILETIME*)&lastWriteTime);
if (ls == ERROR_SUCCESS)
{
if (lastWriteTime > s_LastWriteTime)
{
s_LastWriteTime = lastWriteTime;
changed = true;
}
}
RegCloseKey(volumeSubKey);
volumeSubKey = nullptr;
}
bufferSize = _countof(buffer);
++index;
}
RegCloseKey(volumeKey);
volumeKey = nullptr;
}
if (!changed)
{
// Check if recycle bin has been emptied.
HKEY iconKey = nullptr;
const WCHAR* subKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CLSID\\{645FF040-5081-101B-9F08-00AA002F954E}\\DefaultIcon";
ls = RegOpenKeyEx(HKEY_CURRENT_USER, subKey, 0, KEY_QUERY_VALUE, &iconKey);
if (ls == ERROR_SUCCESS)
{
ULONGLONG lastWriteTime = 0ULL;
ls = RegQueryInfoKey(iconKey, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, (FILETIME*)&lastWriteTime);
if (ls == ERROR_SUCCESS)
{
if (lastWriteTime > s_LastWriteTime)
{
s_LastWriteTime = lastWriteTime;
changed = true;
}
}
RegCloseKey(iconKey);
iconKey = nullptr;
}
}
return changed;
}
} // namespace
enum class MeasureRecycleManager::Type
{
None,
Count,
Size
};
MeasureRecycleManager::MeasureRecycleManager(Skin* skin, const WCHAR* name) : Measure(skin, name),
m_Type(Type::None)
{
if (g_InstanceCount <= 0)
{
System::InitializeCriticalSection(&g_CriticalSection);
}
++g_InstanceCount;
}
MeasureRecycleManager::~MeasureRecycleManager()
{
--g_InstanceCount;
if (g_InstanceCount <= 0)
{
DeleteCriticalSection(&g_CriticalSection);
}
}
void MeasureRecycleManager::ReadOptions(ConfigParser& parser, const WCHAR* section)
{
Measure::ReadOptions(parser, section);
const WCHAR* type = parser.ReadString(section, L"RecycleType", L"COUNT").c_str();
if (_wcsicmp(L"COUNT", type) == 0)
{
m_Type = Type::Count;
}
else if (_wcsicmp(L"SIZE", type) == 0)
{
m_Type = Type::Size;
}
else
{
m_Type = Type::None;
LogErrorF(this, L"Invalid RecycleType=%s");
}
}
void MeasureRecycleManager::UpdateValue()
{
if (TryEnterCriticalSection(&g_CriticalSection))
{
if (!g_Thread)
{
++g_UpdateCount;
if (g_UpdateCount > g_InstanceCount)
{
if (HasRecycleBinChanged())
{
// Delay next check.
g_UpdateCount = g_InstanceCount * -2;
DWORD id = 0UL;
HANDLE thread = CreateThread(nullptr, 0ULL, QueryRecycleBinThreadProc, nullptr, 0UL, &id);
if (thread)
{
CloseHandle(thread);
g_Thread = true;
}
}
else
{
g_UpdateCount = 0;
}
}
}
LeaveCriticalSection(&g_CriticalSection);
}
switch (m_Type)
{
case Type::Size:
m_Value = g_BinSize;
break;
case Type::Count:
m_Value = g_BinCount;
break;
}
}
void MeasureRecycleManager::Command(const std::wstring& command)
{
const WCHAR* args = command.c_str();
if (_wcsicmp(args, L"EmptyBin") == 0)
{
SHEmptyRecycleBin(nullptr, nullptr, 0);
}
else if (_wcsicmp(args, L"EmptyBinSilent") == 0)
{
SHEmptyRecycleBin(nullptr, nullptr, SHERB_NOCONFIRMATION | SHERB_NOPROGRESSUI | SHERB_NOSOUND);
}
else if (_wcsicmp(args, L"OpenBin") == 0)
{
ShellExecute(nullptr, L"open", L"explorer.exe", L"/N,::{645FF040-5081-101B-9F08-00AA002F954E}", nullptr, SW_SHOW);
}
}