2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2001 Kimmo Pekkola
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-02-10 18:37:48 +00:00
|
|
|
*/
|
|
|
|
|
2009-10-07 16:45:14 +00:00
|
|
|
#include "StdAfx.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
#include "MeasureDiskSpace.h"
|
|
|
|
#include "Rainmeter.h"
|
2011-11-05 09:01:06 +00:00
|
|
|
#include "System.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-12-02 10:42:11 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
MeasureDiskSpace::MeasureDiskSpace(MeterWindow* meterWindow, const WCHAR* name) : Measure(meterWindow, name),
|
2011-12-02 10:42:11 +00:00
|
|
|
m_Type(false),
|
2011-01-29 00:11:01 +00:00
|
|
|
m_Total(false),
|
|
|
|
m_Label(false),
|
|
|
|
m_IgnoreRemovable(true),
|
2012-07-22 15:23:56 +00:00
|
|
|
m_DiskQuota(true),
|
2011-01-29 00:11:01 +00:00
|
|
|
m_OldTotalBytes()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
MeasureDiskSpace::~MeasureDiskSpace()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-03-29 19:21:57 +00:00
|
|
|
** Updates the current disk free space value.
|
2009-02-10 18:37:48 +00:00
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureDiskSpace::UpdateValue()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2009-10-08 16:49:41 +00:00
|
|
|
if (!m_Drive.empty())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-12-02 10:42:11 +00:00
|
|
|
const WCHAR* drive = m_Drive.c_str();
|
|
|
|
UINT type = GetDriveType(drive);
|
2010-11-28 21:39:46 +00:00
|
|
|
|
2011-12-02 10:42:11 +00:00
|
|
|
if (m_Type)
|
2009-10-08 16:49:41 +00:00
|
|
|
{
|
2011-12-02 10:42:11 +00:00
|
|
|
switch (type)
|
2010-11-28 21:39:46 +00:00
|
|
|
{
|
2011-12-02 10:42:11 +00:00
|
|
|
case DRIVE_UNKNOWN:
|
|
|
|
case DRIVE_NO_ROOT_DIR:
|
|
|
|
m_Value = DRIVETYPE_REMOVED;
|
2012-09-21 10:17:18 +00:00
|
|
|
m_StringValue = L"Removed";
|
2011-12-02 10:42:11 +00:00
|
|
|
break;
|
|
|
|
case DRIVE_REMOVABLE:
|
|
|
|
m_Value = DRIVETYPE_REMOVABLE;
|
2012-09-21 10:17:18 +00:00
|
|
|
m_StringValue = L"Removable";
|
2011-12-02 10:42:11 +00:00
|
|
|
break;
|
|
|
|
case DRIVE_FIXED:
|
|
|
|
m_Value = DRIVETYPE_FIXED;
|
2012-09-21 10:17:18 +00:00
|
|
|
m_StringValue = L"Fixed";
|
2011-12-02 10:42:11 +00:00
|
|
|
break;
|
|
|
|
case DRIVE_REMOTE:
|
|
|
|
m_Value = DRIVETYPE_NETWORK;
|
2012-09-21 10:17:18 +00:00
|
|
|
m_StringValue = L"Network";
|
2011-12-02 10:42:11 +00:00
|
|
|
break;
|
|
|
|
case DRIVE_CDROM:
|
|
|
|
m_Value = DRIVETYPE_CDROM;
|
2012-09-21 10:17:18 +00:00
|
|
|
m_StringValue = L"CDRom";
|
2011-12-02 10:42:11 +00:00
|
|
|
break;
|
|
|
|
case DRIVE_RAMDISK:
|
|
|
|
m_Value = DRIVETYPE_RAM;
|
2012-09-21 10:17:18 +00:00
|
|
|
m_StringValue = L"Ram";
|
2011-12-02 10:42:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
m_Value = DRIVETYPE_ERROR;
|
2012-09-21 10:17:18 +00:00
|
|
|
m_StringValue = L"Error";
|
2011-12-02 10:42:11 +00:00
|
|
|
break;
|
2010-11-28 21:39:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-02 10:42:11 +00:00
|
|
|
BOOL sizeResult = FALSE;
|
|
|
|
ULONGLONG i64TotalBytes, i64FreeBytes;
|
2010-11-28 22:20:27 +00:00
|
|
|
|
2012-08-12 14:46:23 +00:00
|
|
|
if (type != DRIVE_NO_ROOT_DIR &&
|
|
|
|
type != DRIVE_CDROM &&
|
|
|
|
(!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
|
2010-11-28 21:39:46 +00:00
|
|
|
{
|
2012-08-12 14:46:23 +00:00
|
|
|
if (!m_DiskQuota)
|
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
sizeResult = GetDiskFreeSpaceEx(drive, nullptr, (PULARGE_INTEGER)&i64TotalBytes, (PULARGE_INTEGER)&i64FreeBytes);
|
2012-08-12 14:46:23 +00:00
|
|
|
}
|
|
|
|
else
|
2010-11-28 22:20:27 +00:00
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
sizeResult = GetDiskFreeSpaceEx(drive, (PULARGE_INTEGER)&i64FreeBytes, (PULARGE_INTEGER)&i64TotalBytes, nullptr);
|
2010-11-28 22:20:27 +00:00
|
|
|
}
|
2010-11-28 21:39:46 +00:00
|
|
|
}
|
2010-11-28 22:20:27 +00:00
|
|
|
|
2011-12-02 10:42:11 +00:00
|
|
|
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];
|
|
|
|
|
2012-08-12 14:46:23 +00:00
|
|
|
if (type != DRIVE_NO_ROOT_DIR &&
|
|
|
|
(!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore removable drives
|
2011-12-02 10:42:11 +00:00
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
labelResult = GetVolumeInformation(drive, volumeName, MAX_PATH + 1, nullptr, nullptr, nullptr, nullptr, 0);
|
2011-12-02 10:42:11 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 10:17:18 +00:00
|
|
|
m_StringValue = (labelResult) ? volumeName : L"";
|
2011-12-02 10:42:11 +00:00
|
|
|
}
|
2012-09-21 10:17:18 +00:00
|
|
|
else if (!m_StringValue.empty())
|
2011-12-02 10:42:11 +00:00
|
|
|
{
|
2012-09-21 10:17:18 +00:00
|
|
|
m_StringValue.clear();
|
2011-12-02 10:42:11 +00:00
|
|
|
}
|
2009-10-08 16:49:41 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Returns the time as string.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
const WCHAR* MeasureDiskSpace::GetStringValue()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
return (m_Type || m_Label) ? CheckSubstitute(m_StringValue.c_str()) : nullptr;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-06-01 13:06:36 +00:00
|
|
|
** Read the options specified in the ini file.
|
2009-02-10 18:37:48 +00:00
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureDiskSpace::ReadOptions(ConfigParser& parser, const WCHAR* section)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-11-28 21:39:46 +00:00
|
|
|
double oldMaxValue = m_MaxValue;
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
Measure::ReadOptions(parser, section);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
m_Drive = parser.ReadString(section, L"Drive", L"C:\\");
|
2009-10-08 16:49:41 +00:00
|
|
|
if (m_Drive.empty())
|
|
|
|
{
|
2013-05-30 14:19:42 +00:00
|
|
|
LogWarning(L"FreeDiskSpace: Drive= empty");
|
2010-11-28 21:39:46 +00:00
|
|
|
m_Value = 0.0;
|
|
|
|
m_MaxValue = 0.0;
|
|
|
|
m_OldTotalBytes = 0;
|
2012-09-21 10:17:18 +00:00
|
|
|
m_StringValue.clear();
|
2009-10-08 16:49:41 +00:00
|
|
|
}
|
2013-05-31 14:18:52 +00:00
|
|
|
else if (!System::IsPathSeparator(m_Drive[m_Drive.length() - 1])) // E.g. "C:"
|
2009-10-08 16:49:41 +00:00
|
|
|
{
|
2011-12-04 22:18:40 +00:00
|
|
|
m_Drive += L'\\'; // A trailing backslash is required.
|
2009-10-08 16:49:41 +00:00
|
|
|
}
|
|
|
|
|
2011-12-02 10:42:11 +00:00
|
|
|
m_Type = (1 == parser.ReadInt(section, L"Type", 0));
|
2009-02-10 18:37:48 +00:00
|
|
|
m_Total = (1 == parser.ReadInt(section, L"Total", 0));
|
|
|
|
m_Label = (1 == parser.ReadInt(section, L"Label", 0));
|
2009-08-12 16:15:54 +00:00
|
|
|
m_IgnoreRemovable = (1 == parser.ReadInt(section, L"IgnoreRemovable", 1));
|
2012-07-22 15:23:56 +00:00
|
|
|
m_DiskQuota = (1 == parser.ReadInt(section, L"DiskQuota", 1));
|
2012-07-17 17:49:43 +00:00
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
// Set the m_MaxValue
|
2010-11-28 21:39:46 +00:00
|
|
|
if (!m_Initialized)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-11-28 21:39:46 +00:00
|
|
|
BOOL result = FALSE;
|
2011-12-02 10:42:11 +00:00
|
|
|
ULONGLONG i64TotalBytes;
|
2009-10-14 09:18:12 +00:00
|
|
|
|
2010-11-28 21:39:46 +00:00
|
|
|
if (!m_Drive.empty())
|
|
|
|
{
|
2011-12-02 10:42:11 +00:00
|
|
|
const WCHAR* drive = m_Drive.c_str();
|
|
|
|
UINT type = GetDriveType(drive);
|
2010-11-28 21:39:46 +00:00
|
|
|
if (type != DRIVE_NO_ROOT_DIR &&
|
2012-08-12 14:46:23 +00:00
|
|
|
type != DRIVE_CDROM &&
|
|
|
|
(!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
|
2009-10-08 16:49:41 +00:00
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
result = GetDiskFreeSpaceEx(drive, nullptr, (PULARGE_INTEGER)&i64TotalBytes, nullptr);
|
2009-10-08 16:49:41 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2010-11-28 21:39:46 +00:00
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
2011-12-02 10:42:11 +00:00
|
|
|
m_MaxValue = (double)(__int64)i64TotalBytes;
|
|
|
|
m_OldTotalBytes = i64TotalBytes;
|
2010-11-28 21:39:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_MaxValue = 0.0;
|
|
|
|
m_OldTotalBytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-02 10:42:11 +00:00
|
|
|
if (m_Type)
|
|
|
|
{
|
|
|
|
m_MaxValue = DRIVETYPE_MAX;
|
|
|
|
m_OldTotalBytes = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_MaxValue = oldMaxValue;
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|