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
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
** CMeasureDiskSpace
|
|
|
|
**
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2011-02-15 16:26:54 +00:00
|
|
|
CMeasureDiskSpace::CMeasureDiskSpace(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name),
|
2011-01-29 00:11:01 +00:00
|
|
|
m_Total(false),
|
|
|
|
m_Label(false),
|
|
|
|
m_IgnoreRemovable(true),
|
|
|
|
m_OldTotalBytes()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ~CMeasureDiskSpace
|
|
|
|
**
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
CMeasureDiskSpace::~CMeasureDiskSpace()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Update
|
|
|
|
**
|
2011-03-29 19:21:57 +00:00
|
|
|
** Updates the current disk free space value.
|
2009-02-10 18:37:48 +00:00
|
|
|
**
|
|
|
|
*/
|
|
|
|
bool CMeasureDiskSpace::Update()
|
|
|
|
{
|
|
|
|
if (!CMeasure::PreUpdate()) return false;
|
|
|
|
|
2009-10-08 16:49:41 +00:00
|
|
|
if (!m_Drive.empty())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-11-28 22:20:27 +00:00
|
|
|
BOOL sizeResult = FALSE;
|
2010-11-28 21:39:46 +00:00
|
|
|
ULARGE_INTEGER i64TotalBytes, i64FreeBytes;
|
|
|
|
|
2009-10-08 16:49:41 +00:00
|
|
|
UINT type = GetDriveType(m_Drive.c_str());
|
2009-10-14 09:18:12 +00:00
|
|
|
if (type != DRIVE_NO_ROOT_DIR)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2009-10-14 09:18:12 +00:00
|
|
|
if (type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
|
|
|
|
{
|
2010-11-28 21:39:46 +00:00
|
|
|
ULARGE_INTEGER i64FreeBytesToCaller;
|
2009-10-14 09:18:12 +00:00
|
|
|
|
|
|
|
UINT oldMode = SetErrorMode(0);
|
|
|
|
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box
|
|
|
|
SetLastError(ERROR_SUCCESS);
|
2010-11-28 21:39:46 +00:00
|
|
|
sizeResult = GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes);
|
2009-10-14 09:18:12 +00:00
|
|
|
SetErrorMode(oldMode); // Reset
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2010-11-28 21:39:46 +00:00
|
|
|
|
|
|
|
if (sizeResult)
|
2009-10-08 16:49:41 +00:00
|
|
|
{
|
2011-05-27 18:39:57 +00:00
|
|
|
m_Value = (double)(__int64)((m_Total) ? i64TotalBytes : i64FreeBytes).QuadPart;
|
2010-11-28 21:39:46 +00:00
|
|
|
|
|
|
|
if (i64TotalBytes.QuadPart != m_OldTotalBytes)
|
|
|
|
{
|
|
|
|
// Total size was changed, so set new max value.
|
2011-05-27 18:39:57 +00:00
|
|
|
m_MaxValue = (double)(__int64)i64TotalBytes.QuadPart;
|
2010-11-28 21:39:46 +00:00
|
|
|
m_OldTotalBytes = i64TotalBytes.QuadPart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Value = 0.0;
|
|
|
|
m_MaxValue = 0.0;
|
2009-10-14 09:18:12 +00:00
|
|
|
m_OldTotalBytes = 0;
|
2010-11-28 21:39:46 +00:00
|
|
|
}
|
|
|
|
|
2010-11-28 22:20:27 +00:00
|
|
|
if (m_Label)
|
2010-11-28 21:39:46 +00:00
|
|
|
{
|
2010-11-28 22:20:27 +00:00
|
|
|
BOOL labelResult = FALSE;
|
|
|
|
WCHAR volumeName[MAX_PATH] = {0};
|
|
|
|
|
|
|
|
if (type != DRIVE_NO_ROOT_DIR)
|
2010-11-28 21:39:46 +00:00
|
|
|
{
|
2010-11-28 22:20:27 +00:00
|
|
|
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(m_Drive.c_str(), volumeName, MAX_PATH, NULL, NULL, NULL, NULL, 0);
|
|
|
|
SetErrorMode(oldMode); // Reset
|
|
|
|
}
|
2010-11-28 21:39:46 +00:00
|
|
|
}
|
2010-11-28 22:20:27 +00:00
|
|
|
|
|
|
|
m_LabelName = (labelResult) ? volumeName : L"";
|
|
|
|
}
|
|
|
|
else if (!m_LabelName.empty())
|
|
|
|
{
|
2011-10-09 08:01:31 +00:00
|
|
|
m_LabelName.clear();
|
2009-10-08 16:49:41 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return PostUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** GetStringValue
|
|
|
|
**
|
|
|
|
** Returns the time as string.
|
|
|
|
**
|
|
|
|
*/
|
2011-01-19 15:31:45 +00:00
|
|
|
const WCHAR* CMeasureDiskSpace::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
if (m_Label)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
return CheckSubstitute(m_LabelName.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMeasure::GetStringValue(autoScale, scale, decimals, percentual);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ReadConfig
|
|
|
|
**
|
|
|
|
** Reads the measure specific configs.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CMeasureDiskSpace::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|
|
|
{
|
2010-11-28 21:39:46 +00:00
|
|
|
double oldMaxValue = m_MaxValue;
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
CMeasure::ReadConfig(parser, section);
|
|
|
|
|
|
|
|
m_Drive = parser.ReadString(section, L"Drive", L"C:\\");
|
2009-10-08 16:49:41 +00:00
|
|
|
if (m_Drive.empty())
|
|
|
|
{
|
2011-09-09 16:31:55 +00:00
|
|
|
Log(LOG_WARNING, L"FreeDiskSpace: Drive= empty");
|
2010-11-28 21:39:46 +00:00
|
|
|
m_Value = 0.0;
|
|
|
|
m_MaxValue = 0.0;
|
|
|
|
m_OldTotalBytes = 0;
|
2011-11-27 12:46:59 +00:00
|
|
|
m_LabelName.clear();
|
2009-10-08 16:49:41 +00:00
|
|
|
}
|
2011-11-05 09:01:06 +00:00
|
|
|
else if (!CSystem::IsPathSeparator(m_Drive[m_Drive.length() - 1])) // E.g. "C:"
|
2009-10-08 16:49:41 +00:00
|
|
|
{
|
|
|
|
m_Drive += L"\\"; // A trailing backslash is required.
|
|
|
|
}
|
|
|
|
|
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));
|
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;
|
|
|
|
ULARGE_INTEGER i64TotalBytes;
|
2009-10-14 09:18:12 +00:00
|
|
|
|
2010-11-28 21:39:46 +00:00
|
|
|
if (!m_Drive.empty())
|
|
|
|
{
|
|
|
|
UINT type = GetDriveType(m_Drive.c_str());
|
|
|
|
if (type != DRIVE_NO_ROOT_DIR &&
|
|
|
|
type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
|
2009-10-08 16:49:41 +00:00
|
|
|
{
|
2010-11-28 21:39:46 +00:00
|
|
|
ULARGE_INTEGER i64FreeBytesToCaller, i64FreeBytes;
|
2009-10-14 09:18:12 +00:00
|
|
|
|
2010-11-28 21:39:46 +00:00
|
|
|
UINT oldMode = SetErrorMode(0);
|
|
|
|
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box
|
|
|
|
result = GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes);
|
|
|
|
SetErrorMode(oldMode); // Reset
|
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-05-27 18:39:57 +00:00
|
|
|
m_MaxValue = (double)(__int64)i64TotalBytes.QuadPart;
|
2010-11-28 21:39:46 +00:00
|
|
|
m_OldTotalBytes = i64TotalBytes.QuadPart;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_MaxValue = 0.0;
|
|
|
|
m_OldTotalBytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_MaxValue = oldMaxValue;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|