- [FreeDiskSpace] Added new "Type=" option to get drive type. (e.g. Fixed/Removable/Network) If set to 1, retrieve drive type as number/string. Default is 0.

- Code cleanup.
This commit is contained in:
spx 2011-12-02 10:42:11 +00:00
parent 2d05df47cd
commit 320c2d7c83
3 changed files with 119 additions and 58 deletions

View File

@ -21,6 +21,19 @@
#include "Rainmeter.h" #include "Rainmeter.h"
#include "System.h" #include "System.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
};
/* /*
** CMeasureDiskSpace ** CMeasureDiskSpace
** **
@ -28,6 +41,7 @@
** **
*/ */
CMeasureDiskSpace::CMeasureDiskSpace(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name), CMeasureDiskSpace::CMeasureDiskSpace(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name),
m_Type(false),
m_Total(false), m_Total(false),
m_Label(false), m_Label(false),
m_IgnoreRemovable(true), m_IgnoreRemovable(true),
@ -57,63 +71,101 @@ bool CMeasureDiskSpace::Update()
if (!m_Drive.empty()) if (!m_Drive.empty())
{ {
BOOL sizeResult = FALSE; const WCHAR* drive = m_Drive.c_str();
ULARGE_INTEGER i64TotalBytes, i64FreeBytes; UINT type = GetDriveType(drive);
UINT type = GetDriveType(m_Drive.c_str()); if (m_Type)
if (type != DRIVE_NO_ROOT_DIR)
{ {
if (type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives switch (type)
{ {
ULARGE_INTEGER i64FreeBytesToCaller; case DRIVE_UNKNOWN:
case DRIVE_NO_ROOT_DIR:
UINT oldMode = SetErrorMode(0); m_Value = DRIVETYPE_REMOVED;
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box m_DriveInfo = L"Removed";
SetLastError(ERROR_SUCCESS); break;
sizeResult = GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes); case DRIVE_REMOVABLE:
SetErrorMode(oldMode); // Reset m_Value = DRIVETYPE_REMOVABLE;
} m_DriveInfo = L"Removable";
} break;
case DRIVE_FIXED:
if (sizeResult) m_Value = DRIVETYPE_FIXED;
{ m_DriveInfo = L"Fixed";
m_Value = (double)(__int64)((m_Total) ? i64TotalBytes : i64FreeBytes).QuadPart; break;
case DRIVE_REMOTE:
if (i64TotalBytes.QuadPart != m_OldTotalBytes) m_Value = DRIVETYPE_NETWORK;
{ m_DriveInfo = L"Network";
// Total size was changed, so set new max value. break;
m_MaxValue = (double)(__int64)i64TotalBytes.QuadPart; case DRIVE_CDROM:
m_OldTotalBytes = i64TotalBytes.QuadPart; m_Value = DRIVETYPE_CDROM;
m_DriveInfo = L"CDRom";
break;
case DRIVE_RAMDISK:
m_Value = DRIVETYPE_RAM;
m_DriveInfo = L"Ram";
break;
default:
m_Value = DRIVETYPE_ERROR;
m_DriveInfo = L"Error";
break;
} }
} }
else else
{ {
m_Value = 0.0; BOOL sizeResult = FALSE;
m_MaxValue = 0.0; ULONGLONG i64TotalBytes, i64FreeBytes;
m_OldTotalBytes = 0;
}
if (m_Label)
{
BOOL labelResult = FALSE;
WCHAR volumeName[MAX_PATH] = {0};
if (type != DRIVE_NO_ROOT_DIR) if (type != DRIVE_NO_ROOT_DIR)
{ {
if (!m_IgnoreRemovable || type != DRIVE_REMOVABLE) // Ignore removable drives if (type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
{ {
UINT oldMode = SetErrorMode(0); UINT oldMode = SetErrorMode(0);
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box
labelResult = GetVolumeInformation(m_Drive.c_str(), volumeName, MAX_PATH, NULL, NULL, NULL, NULL, 0); SetLastError(ERROR_SUCCESS);
sizeResult = GetDiskFreeSpaceEx(drive, NULL, (PULARGE_INTEGER)&i64TotalBytes, (PULARGE_INTEGER)&i64FreeBytes);
SetErrorMode(oldMode); // Reset SetErrorMode(oldMode); // Reset
} }
} }
m_LabelName = (labelResult) ? volumeName : L""; if (sizeResult)
} {
else if (!m_LabelName.empty()) m_Value = (double)(__int64)((m_Total) ? i64TotalBytes : i64FreeBytes);
{
m_LabelName.clear(); 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)
{
if (!m_IgnoreRemovable || type != DRIVE_REMOVABLE) // Ignore removable drives
{
UINT oldMode = SetErrorMode(0);
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box
labelResult = GetVolumeInformation(drive, volumeName, MAX_PATH + 1, NULL, NULL, NULL, NULL, 0);
SetErrorMode(oldMode); // Reset
}
}
m_DriveInfo = (labelResult) ? volumeName : L"";
}
else if (!m_DriveInfo.empty())
{
m_DriveInfo.clear();
}
} }
} }
@ -128,9 +180,9 @@ bool CMeasureDiskSpace::Update()
*/ */
const WCHAR* CMeasureDiskSpace::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual) const WCHAR* CMeasureDiskSpace::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
{ {
if (m_Label) if (m_Type || m_Label)
{ {
return CheckSubstitute(m_LabelName.c_str()); return CheckSubstitute(m_DriveInfo.c_str());
} }
return CMeasure::GetStringValue(autoScale, scale, decimals, percentual); return CMeasure::GetStringValue(autoScale, scale, decimals, percentual);
@ -155,13 +207,14 @@ void CMeasureDiskSpace::ReadConfig(CConfigParser& parser, const WCHAR* section)
m_Value = 0.0; m_Value = 0.0;
m_MaxValue = 0.0; m_MaxValue = 0.0;
m_OldTotalBytes = 0; m_OldTotalBytes = 0;
m_LabelName.clear(); m_DriveInfo.clear();
} }
else if (!CSystem::IsPathSeparator(m_Drive[m_Drive.length() - 1])) // E.g. "C:" else if (!CSystem::IsPathSeparator(m_Drive[m_Drive.length() - 1])) // E.g. "C:"
{ {
m_Drive += L"\\"; // A trailing backslash is required. m_Drive += L"\\"; // A trailing backslash is required.
} }
m_Type = (1 == parser.ReadInt(section, L"Type", 0));
m_Total = (1 == parser.ReadInt(section, L"Total", 0)); m_Total = (1 == parser.ReadInt(section, L"Total", 0));
m_Label = (1 == parser.ReadInt(section, L"Label", 0)); m_Label = (1 == parser.ReadInt(section, L"Label", 0));
m_IgnoreRemovable = (1 == parser.ReadInt(section, L"IgnoreRemovable", 1)); m_IgnoreRemovable = (1 == parser.ReadInt(section, L"IgnoreRemovable", 1));
@ -170,27 +223,26 @@ void CMeasureDiskSpace::ReadConfig(CConfigParser& parser, const WCHAR* section)
if (!m_Initialized) if (!m_Initialized)
{ {
BOOL result = FALSE; BOOL result = FALSE;
ULARGE_INTEGER i64TotalBytes; ULONGLONG i64TotalBytes;
if (!m_Drive.empty()) if (!m_Drive.empty())
{ {
UINT type = GetDriveType(m_Drive.c_str()); const WCHAR* drive = m_Drive.c_str();
UINT type = GetDriveType(drive);
if (type != DRIVE_NO_ROOT_DIR && if (type != DRIVE_NO_ROOT_DIR &&
type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
{ {
ULARGE_INTEGER i64FreeBytesToCaller, i64FreeBytes;
UINT oldMode = SetErrorMode(0); UINT oldMode = SetErrorMode(0);
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box
result = GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes); result = GetDiskFreeSpaceEx(drive, NULL, (PULARGE_INTEGER)&i64TotalBytes, NULL);
SetErrorMode(oldMode); // Reset SetErrorMode(oldMode); // Reset
} }
} }
if (result) if (result)
{ {
m_MaxValue = (double)(__int64)i64TotalBytes.QuadPart; m_MaxValue = (double)(__int64)i64TotalBytes;
m_OldTotalBytes = i64TotalBytes.QuadPart; m_OldTotalBytes = i64TotalBytes;
} }
else else
{ {
@ -200,6 +252,14 @@ void CMeasureDiskSpace::ReadConfig(CConfigParser& parser, const WCHAR* section)
} }
else else
{ {
m_MaxValue = oldMaxValue; if (m_Type)
{
m_MaxValue = DRIVETYPE_MAX;
m_OldTotalBytes = 0;
}
else
{
m_MaxValue = oldMaxValue;
}
} }
} }

View File

@ -35,7 +35,8 @@ protected:
private: private:
std::wstring m_Drive; std::wstring m_Drive;
std::wstring m_LabelName; std::wstring m_DriveInfo;
bool m_Type;
bool m_Total; bool m_Total;
bool m_Label; bool m_Label;
bool m_IgnoreRemovable; bool m_IgnoreRemovable;

View File

@ -1263,25 +1263,25 @@ bool CSystem::RemoveFolder(const std::wstring& strFolder)
*/ */
void CSystem::UpdateIniFileMappingList() void CSystem::UpdateIniFileMappingList()
{ {
static ULARGE_INTEGER s_LastWriteTime = {0}; static ULONGLONG s_LastWriteTime = 0;
HKEY hKey; HKEY hKey;
LONG ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\IniFileMapping", 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey); LONG ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\IniFileMapping", 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey);
if (ret == ERROR_SUCCESS) if (ret == ERROR_SUCCESS)
{ {
DWORD numSubKeys; DWORD numSubKeys;
ULARGE_INTEGER ftLastWriteTime; ULONGLONG ftLastWriteTime;
bool changed = false; bool changed = false;
ret = RegQueryInfoKey(hKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, (LPFILETIME)&ftLastWriteTime); ret = RegQueryInfoKey(hKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, (LPFILETIME)&ftLastWriteTime);
if (ret == ERROR_SUCCESS) if (ret == ERROR_SUCCESS)
{ {
//LogWithArgs(LOG_DEBUG, L"IniFileMapping: numSubKeys=%u, ftLastWriteTime=%llu", numSubKeys, ftLastWriteTime.QuadPart); //LogWithArgs(LOG_DEBUG, L"IniFileMapping: numSubKeys=%u, ftLastWriteTime=%llu", numSubKeys, ftLastWriteTime);
if (ftLastWriteTime.QuadPart != s_LastWriteTime.QuadPart || if (ftLastWriteTime != s_LastWriteTime ||
numSubKeys != c_IniFileMappings.size()) numSubKeys != c_IniFileMappings.size())
{ {
s_LastWriteTime.QuadPart = ftLastWriteTime.QuadPart; s_LastWriteTime = ftLastWriteTime;
if (numSubKeys > c_IniFileMappings.capacity()) if (numSubKeys > c_IniFileMappings.capacity())
{ {
c_IniFileMappings.reserve(numSubKeys); c_IniFileMappings.reserve(numSubKeys);
@ -1291,7 +1291,7 @@ void CSystem::UpdateIniFileMappingList()
} }
else else
{ {
s_LastWriteTime.QuadPart = 0; s_LastWriteTime = 0;
changed = true; changed = true;
} }