2012-03-17 12:27:20 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2010 Elestel
|
|
|
|
|
|
|
|
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2010-11-15 14:38:12 +00:00
|
|
|
#include "FolderInfo.h"
|
|
|
|
#include <windows.h>
|
|
|
|
#include <list>
|
2012-03-17 12:27:20 +00:00
|
|
|
#include "../API/RainmeterAPI.h"
|
|
|
|
|
|
|
|
#define UPDATE_TIME_MIN_MS 10000
|
|
|
|
|
2012-04-10 15:20:22 +00:00
|
|
|
CFolderInfo::CFolderInfo(void* ownerSkin) :
|
2012-03-17 12:27:20 +00:00
|
|
|
m_InstanceCount(1),
|
2012-04-10 15:20:22 +00:00
|
|
|
m_Skin(ownerSkin),
|
2012-03-17 12:27:20 +00:00
|
|
|
m_IncludeSubFolders(false),
|
|
|
|
m_IncludeHiddenFiles(false),
|
|
|
|
m_IncludeSystemFiles(false),
|
|
|
|
m_Size(),
|
|
|
|
m_FileCount(),
|
|
|
|
m_FolderCount(),
|
|
|
|
m_RegExpFilter(),
|
|
|
|
m_LastUpdateTime()
|
2010-11-15 14:38:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
CFolderInfo::~CFolderInfo()
|
2012-02-04 08:25:11 +00:00
|
|
|
{
|
|
|
|
FreePcre();
|
|
|
|
}
|
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
void CFolderInfo::AddInstance()
|
2010-11-15 14:38:12 +00:00
|
|
|
{
|
2012-03-17 12:27:20 +00:00
|
|
|
++m_InstanceCount;
|
2010-11-15 14:38:12 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
void CFolderInfo::RemoveInstance()
|
2012-02-04 08:25:11 +00:00
|
|
|
{
|
2012-03-17 12:27:20 +00:00
|
|
|
--m_InstanceCount;
|
|
|
|
if (m_InstanceCount == 0)
|
|
|
|
{
|
|
|
|
delete this;
|
2012-02-04 08:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
void CFolderInfo::Clear()
|
2010-11-15 14:38:12 +00:00
|
|
|
{
|
2012-03-17 12:27:20 +00:00
|
|
|
m_Size = 0;
|
|
|
|
m_FileCount = 0;
|
|
|
|
m_FolderCount = 0;
|
|
|
|
}
|
2010-12-06 07:43:30 +00:00
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
void CFolderInfo::FreePcre()
|
|
|
|
{
|
|
|
|
if (m_RegExpFilter)
|
|
|
|
{
|
|
|
|
pcre_free(m_RegExpFilter);
|
2013-05-31 14:28:39 +00:00
|
|
|
m_RegExpFilter = nullptr;
|
2010-11-15 14:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
void CFolderInfo::Update()
|
2010-11-15 14:38:12 +00:00
|
|
|
{
|
2012-03-17 12:27:20 +00:00
|
|
|
DWORD now = GetTickCount();
|
|
|
|
if (now - m_LastUpdateTime > UPDATE_TIME_MIN_MS)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
if (!m_Path.empty())
|
|
|
|
{
|
|
|
|
CalculateSize();
|
|
|
|
}
|
2010-11-15 14:38:12 +00:00
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
m_LastUpdateTime = now;
|
2010-11-15 14:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
void CFolderInfo::CalculateSize()
|
2010-11-15 14:38:12 +00:00
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
std::list<RawString> folderQueue;
|
2012-03-17 12:27:20 +00:00
|
|
|
folderQueue.push_back(m_Path.c_str());
|
2010-11-15 14:38:12 +00:00
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
WCHAR searchPattern[MAX_PATH + 10];
|
|
|
|
WCHAR buffer[MAX_PATH];
|
2010-11-15 14:38:12 +00:00
|
|
|
char utf8Buf[MAX_PATH * 3];
|
|
|
|
WIN32_FIND_DATA findData;
|
|
|
|
HANDLE findHandle;
|
2012-03-17 12:27:20 +00:00
|
|
|
while (!folderQueue.empty())
|
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
const RawString& ref = folderQueue.front();
|
2010-11-15 14:38:12 +00:00
|
|
|
wsprintf(searchPattern, L"%s%s", ref.c_str(), L"\\*.*");
|
2012-03-17 12:27:20 +00:00
|
|
|
|
|
|
|
findHandle = FindFirstFile(searchPattern, &findData);
|
|
|
|
if (INVALID_HANDLE_VALUE == findHandle)
|
|
|
|
{
|
2010-11-15 14:38:12 +00:00
|
|
|
folderQueue.pop_front();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
do
|
|
|
|
{
|
2010-11-15 14:38:12 +00:00
|
|
|
// special case for "." and ".."
|
2011-07-11 05:35:15 +00:00
|
|
|
if (wcscmp(findData.cFileName, L".") == 0 ||
|
2012-03-17 12:27:20 +00:00
|
|
|
wcscmp(findData.cFileName, L"..") == 0)
|
|
|
|
{
|
2010-11-15 14:38:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isFolder = (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0;
|
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
if (!m_IncludeHiddenFiles && (findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
|
|
|
|
{
|
2010-11-15 14:38:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-03-17 12:27:20 +00:00
|
|
|
else if (!m_IncludeSystemFiles && (findData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))
|
|
|
|
{
|
2010-11-15 14:38:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-03-17 12:27:20 +00:00
|
|
|
else if (!isFolder && m_RegExpFilter)
|
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
int utf8BufLen = WideCharToMultiByte(CP_UTF8, 0, findData.cFileName, wcslen(findData.cFileName) + 1, utf8Buf, MAX_PATH * 3, nullptr, nullptr);
|
|
|
|
if (0 != pcre_exec(m_RegExpFilter, nullptr, utf8Buf, utf8BufLen, 0, 0, nullptr, 0))
|
2012-03-17 12:27:20 +00:00
|
|
|
{
|
2010-11-15 14:38:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
if (isFolder)
|
|
|
|
{
|
|
|
|
m_FolderCount++;
|
|
|
|
if (m_IncludeSubFolders)
|
|
|
|
{
|
2010-11-15 14:38:12 +00:00
|
|
|
wsprintf(buffer, L"%s\\%s", ref.c_str(), findData.cFileName);
|
|
|
|
folderQueue.push_back(buffer);
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 12:27:20 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_FileCount++;
|
|
|
|
m_Size += ((UINT64)findData.nFileSizeHigh << 32) + findData.nFileSizeLow;
|
2010-11-15 14:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 12:27:20 +00:00
|
|
|
while (FindNextFile(findHandle, &findData));
|
2010-11-15 14:38:12 +00:00
|
|
|
FindClose(findHandle);
|
|
|
|
|
|
|
|
folderQueue.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 15:20:22 +00:00
|
|
|
void CFolderInfo::SetPath(LPCWSTR path)
|
|
|
|
{
|
|
|
|
if (wcscmp(m_Path.c_str(), path) != 0)
|
|
|
|
{
|
|
|
|
m_Path = path;
|
|
|
|
|
|
|
|
// Force update next time
|
|
|
|
m_LastUpdateTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 12:27:20 +00:00
|
|
|
void CFolderInfo::SetRegExpFilter(LPCWSTR filter)
|
2010-11-15 14:38:12 +00:00
|
|
|
{
|
2012-02-04 08:25:11 +00:00
|
|
|
FreePcre();
|
2010-11-15 14:38:12 +00:00
|
|
|
|
2012-04-10 15:20:22 +00:00
|
|
|
if (*filter)
|
|
|
|
{
|
|
|
|
int filterLen = wcslen(filter) + 1;
|
2013-05-31 14:28:39 +00:00
|
|
|
int bufLen = WideCharToMultiByte(CP_UTF8, 0, filter, filterLen, nullptr, 0, nullptr, nullptr);
|
2010-11-15 14:38:12 +00:00
|
|
|
|
2012-04-10 15:20:22 +00:00
|
|
|
char* buf = new char[bufLen];
|
2013-05-31 14:28:39 +00:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, filter, filterLen, buf, bufLen, nullptr, nullptr);
|
2010-11-15 14:38:12 +00:00
|
|
|
|
2012-04-10 15:20:22 +00:00
|
|
|
const char* error;
|
|
|
|
int erroffset;
|
2013-05-31 14:28:39 +00:00
|
|
|
m_RegExpFilter = pcre_compile(buf, PCRE_UTF8, &error, &erroffset, nullptr);
|
2012-02-04 08:25:11 +00:00
|
|
|
|
2012-04-10 15:20:22 +00:00
|
|
|
delete [] buf;
|
|
|
|
}
|
2010-11-15 14:38:12 +00:00
|
|
|
}
|