rainmeter-studio/Plugins/PluginRecycleManager/RecycleManager.cpp

162 lines
3.9 KiB
C++
Raw Normal View History

2010-09-23 08:49:43 +00:00
/*
Copyright (C) 2005 Kimmo Pekkola, 2009 Greg Schoppe
2010-09-23 08:49:43 +00:00
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.
2010-09-23 08:49:43 +00:00
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.
2010-09-23 08:49:43 +00:00
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2010-09-23 08:49:43 +00:00
*/
#include <windows.h>
#include <stdlib.h>
2012-01-08 17:35:29 +00:00
#include <stdio.h>
#include "../../Library/RawString.h"
2011-02-03 18:09:24 +00:00
#include "../../Library/Export.h" // Rainmeter's exported functions
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
// System resources that can be counted
2010-09-23 08:49:43 +00:00
enum MEASURETYPE
{
NUMRECYCLE,
SIZERECYCLE
};
2012-01-08 17:35:29 +00:00
struct MeasureData
2010-09-23 08:49:43 +00:00
{
2012-01-08 17:35:29 +00:00
MEASURETYPE type;
CRawString drives;
2011-03-29 19:21:57 +00:00
2012-01-08 17:35:29 +00:00
MeasureData() : type(NUMRECYCLE) {}
};
2011-03-29 19:21:57 +00:00
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Initialize(void** data)
{
MeasureData* measure = new MeasureData;
*data = measure;
2010-09-23 08:49:43 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
2010-09-23 08:49:43 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2011-03-29 19:21:57 +00:00
2012-01-08 17:35:29 +00:00
LPCWSTR value = RmReadString(rm, L"RecycleType", L"COUNT");
if (_wcsicmp(L"COUNT", value) == 0)
{
measure->type = NUMRECYCLE;
}
else if (_wcsicmp(L"SIZE", value) == 0)
2010-09-23 08:49:43 +00:00
{
2012-01-08 17:35:29 +00:00
measure->type = SIZERECYCLE;
2010-09-23 08:49:43 +00:00
}
2012-01-08 17:35:29 +00:00
else
{
WCHAR buffer[256];
_snwprintf_s(buffer, _TRUNCATE, L"RecycleManager.dll: RecycleType=%s is not valid in [%s]", value, RmGetMeasureName(rm));
RmLog(LOG_ERROR, buffer);
}
value = RmReadString(rm, L"Drives", L"ALL");
measure->drives = (_wcsicmp(value, L"ALL") == 0) ? NULL : value;
2010-09-23 08:49:43 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT double Update(void* data)
2010-09-23 08:49:43 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
double retVal = 0;
2011-03-29 19:21:57 +00:00
SHQUERYRBINFO rbi = {0};
rbi.cbSize = sizeof(SHQUERYRBINFO);
2011-03-29 19:21:57 +00:00
2012-01-08 17:35:29 +00:00
if (measure->drives.empty())
2010-09-23 08:49:43 +00:00
{
if (SHQueryRecycleBin(NULL, &rbi) == S_OK)
2010-09-23 08:49:43 +00:00
{
2012-01-08 17:35:29 +00:00
if (measure->type == SIZERECYCLE)
2010-09-23 08:49:43 +00:00
{
retVal = (double)rbi.i64Size; // size in bytes
2010-09-23 08:49:43 +00:00
}
2012-01-08 17:35:29 +00:00
else if (measure->type == NUMRECYCLE)
2010-09-23 08:49:43 +00:00
{
retVal = (double)rbi.i64NumItems; // number of items in bin
2010-09-23 08:49:43 +00:00
}
}
}
else
2010-09-23 08:49:43 +00:00
{
2012-01-08 17:35:29 +00:00
WCHAR* drives = _wcsdup(measure->drives.c_str());
WCHAR* token = wcstok(drives, L"|");
while (token)
2010-09-23 08:49:43 +00:00
{
2012-01-08 17:35:29 +00:00
if (SHQueryRecycleBin(token, &rbi) == S_OK)
{
2012-01-08 17:35:29 +00:00
if (measure->type == SIZERECYCLE)
{
retVal += (double)rbi.i64Size; // size in bytes
}
2012-01-08 17:35:29 +00:00
else if (measure->type == NUMRECYCLE)
{
retVal += (double)rbi.i64NumItems; // number of items in bin
}
}
2012-01-08 17:35:29 +00:00
token = wcstok(NULL, L"|");
2010-09-23 08:49:43 +00:00
}
2012-01-08 17:35:29 +00:00
free(drives);
2010-09-23 08:49:43 +00:00
}
2011-03-29 19:21:57 +00:00
return retVal;
2010-09-23 08:49:43 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Finalize(void* data)
2010-09-23 08:49:43 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
delete measure;
2010-09-23 08:49:43 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void ExecuteBang(void* data, LPCWSTR args)
2010-09-23 08:49:43 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2010-09-23 08:49:43 +00:00
2012-01-08 17:35:29 +00:00
auto emptyBin = [&](DWORD flags)
{
2012-01-08 17:35:29 +00:00
if (measure->drives.empty())
2010-09-23 08:49:43 +00:00
{
2012-01-08 17:35:29 +00:00
SHEmptyRecycleBin(NULL, NULL, flags);
2010-09-23 08:49:43 +00:00
}
else
2010-09-23 08:49:43 +00:00
{
2012-01-08 17:35:29 +00:00
WCHAR* drives = _wcsdup(measure->drives.c_str());
WCHAR* token = wcstok(drives, L"|");
while (token)
{
2012-01-08 17:35:29 +00:00
SHEmptyRecycleBin(NULL, token, flags);
token = wcstok(NULL, L"|");
}
2012-01-08 17:35:29 +00:00
free(drives);
2010-09-23 08:49:43 +00:00
}
2012-01-08 17:35:29 +00:00
};
if (_wcsicmp(args, L"EmptyBin") == 0)
{
emptyBin(0);
2011-03-29 19:21:57 +00:00
}
2012-01-08 17:35:29 +00:00
else if (_wcsicmp(args, L"EmptyBinSilent") == 0)
2011-03-29 19:21:57 +00:00
{
2012-01-08 17:35:29 +00:00
emptyBin(SHERB_NOCONFIRMATION | SHERB_NOPROGRESSUI | SHERB_NOSOUND);
}
else if (_wcsicmp(args, L"OpenBin") == 0)
{
// Open the Recycle Bin folder
ShellExecute(NULL, L"open", L"explorer.exe", L"/N,::{645FF040-5081-101B-9F08-00AA002F954E}", NULL, SW_SHOW);
2011-03-29 19:21:57 +00:00
}
2010-09-23 08:49:43 +00:00
}