forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeasureDiskSpace.cpp
226 lines (205 loc) · 5.22 KB
/
MeasureDiskSpace.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
/* Copyright (C) 2001 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 "MeasureDiskSpace.h"
#include "Rainmeter.h"
#include "System.h"
#include "../Common/PathUtil.h"
enum DRIVETYPE
{
DRIVETYPE_ERROR = 0,
DRIVETYPE_REMOVED = 1,
DRIVETYPE_REMOVABLE = 3,
DRIVETYPE_FIXED = 4,
DRIVETYPE_NETWORK = 5,
DRIVETYPE_CDROM = 6,
DRIVETYPE_RAM = 7,
DRIVETYPE_MAX = DRIVETYPE_RAM
};
MeasureDiskSpace::MeasureDiskSpace(Skin* skin, const WCHAR* name) : Measure(skin, name),
m_Type(false),
m_Total(false),
m_Label(false),
m_IgnoreRemovable(true),
m_DiskQuota(true),
m_OldTotalBytes()
{
}
MeasureDiskSpace::~MeasureDiskSpace()
{
}
/*
** Updates the current disk free space value.
**
*/
void MeasureDiskSpace::UpdateValue()
{
if (!m_Drive.empty())
{
const WCHAR* drive = m_Drive.c_str();
UINT type = GetDriveType(drive);
if (m_Type)
{
switch (type)
{
case DRIVE_UNKNOWN:
case DRIVE_NO_ROOT_DIR:
m_Value = DRIVETYPE_REMOVED;
m_StringValue = L"Removed";
break;
case DRIVE_REMOVABLE:
m_Value = DRIVETYPE_REMOVABLE;
m_StringValue = L"Removable";
break;
case DRIVE_FIXED:
m_Value = DRIVETYPE_FIXED;
m_StringValue = L"Fixed";
break;
case DRIVE_REMOTE:
m_Value = DRIVETYPE_NETWORK;
m_StringValue = L"Network";
break;
case DRIVE_CDROM:
m_Value = DRIVETYPE_CDROM;
m_StringValue = L"CDRom";
break;
case DRIVE_RAMDISK:
m_Value = DRIVETYPE_RAM;
m_StringValue = L"Ram";
break;
default:
m_Value = DRIVETYPE_ERROR;
m_StringValue = L"Error";
break;
}
}
else
{
BOOL sizeResult = FALSE;
ULONGLONG i64TotalBytes, i64FreeBytes;
if (type != DRIVE_NO_ROOT_DIR &&
type != DRIVE_CDROM &&
(!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
{
if (!m_DiskQuota)
{
sizeResult = GetDiskFreeSpaceEx(drive, nullptr, (PULARGE_INTEGER)&i64TotalBytes, (PULARGE_INTEGER)&i64FreeBytes);
}
else
{
sizeResult = GetDiskFreeSpaceEx(drive, (PULARGE_INTEGER)&i64FreeBytes, (PULARGE_INTEGER)&i64TotalBytes, nullptr);
}
}
if (sizeResult)
{
m_Value = (double)(__int64)((m_Total) ? i64TotalBytes : i64FreeBytes);
if (i64TotalBytes != m_OldTotalBytes)
{
// Total size was changed, so set new max value.
m_MaxValue = (double)(__int64)i64TotalBytes;
m_OldTotalBytes = i64TotalBytes;
}
}
else
{
m_Value = 0.0;
m_MaxValue = 0.0;
m_OldTotalBytes = 0;
}
if (m_Label)
{
BOOL labelResult = FALSE;
WCHAR volumeName[MAX_PATH + 1];
if (type != DRIVE_NO_ROOT_DIR &&
(!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore removable drives
{
labelResult = GetVolumeInformation(drive, volumeName, MAX_PATH + 1, nullptr, nullptr, nullptr, nullptr, 0);
}
m_StringValue = (labelResult) ? volumeName : L"";
}
else if (!m_StringValue.empty())
{
m_StringValue.clear();
}
}
}
}
/*
** Returns the time as string.
**
*/
const WCHAR* MeasureDiskSpace::GetStringValue()
{
return (m_Type || m_Label) ? CheckSubstitute(m_StringValue.c_str()) : nullptr;
}
/*
** Read the options specified in the ini file.
**
*/
void MeasureDiskSpace::ReadOptions(ConfigParser& parser, const WCHAR* section)
{
double oldMaxValue = m_MaxValue;
Measure::ReadOptions(parser, section);
m_Drive = parser.ReadString(section, L"Drive", L"C:\\");
if (m_Drive.empty())
{
LogWarningF(this, L"FreeDiskSpace: Drive= empty");
m_Value = 0.0;
m_MaxValue = 0.0;
m_OldTotalBytes = 0;
m_StringValue.clear();
}
else
{
// A trailing backslash is required for GetDiskFreeSpaceEx().
PathUtil::AppendBackslashIfMissing(m_Drive);
}
m_Type = parser.ReadBool(section, L"Type", false);
m_Total = parser.ReadBool(section, L"Total", false);
m_Label = parser.ReadBool(section, L"Label", false);
m_IgnoreRemovable = parser.ReadBool(section, L"IgnoreRemovable", true);
m_DiskQuota = parser.ReadBool(section, L"DiskQuota", true);
// Set the m_MaxValue
if (!m_Initialized)
{
BOOL result = FALSE;
ULONGLONG i64TotalBytes;
if (!m_Drive.empty())
{
const WCHAR* drive = m_Drive.c_str();
UINT type = GetDriveType(drive);
if (type != DRIVE_NO_ROOT_DIR &&
type != DRIVE_CDROM &&
(!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
{
result = GetDiskFreeSpaceEx(drive, nullptr, (PULARGE_INTEGER)&i64TotalBytes, nullptr);
}
}
if (result)
{
m_MaxValue = (double)(__int64)i64TotalBytes;
m_OldTotalBytes = i64TotalBytes;
}
else
{
m_MaxValue = 0.0;
m_OldTotalBytes = 0;
}
}
else
{
if (m_Type)
{
m_MaxValue = DRIVETYPE_MAX;
m_OldTotalBytes = 0;
}
else
{
m_MaxValue = oldMaxValue;
}
}
}