Skip to content

Commit 697a60a

Browse files
committed
Get drive volume names directly
1 parent 94f5a70 commit 697a60a

File tree

2 files changed

+46
-87
lines changed

2 files changed

+46
-87
lines changed

src/Explorer/ExplorerDialog.cpp

Lines changed: 46 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2828
#include <filesystem>
2929
#include <list>
3030
#include <string>
31+
#include <optional>
3132

3233
#include "Explorer.h"
3334
#include "ExplorerResource.h"
@@ -75,23 +76,6 @@ DWORD WINAPI UpdateThread(LPVOID lpParam)
7576
return 0;
7677
}
7778

78-
79-
DWORD WINAPI GetVolumeInformationTimeoutThread(LPVOID lpParam)
80-
{
81-
DWORD serialNr = 0;
82-
DWORD space = 0;
83-
DWORD flags = 0;
84-
GetVolumeInfo* volInfo = (GetVolumeInfo*)lpParam;
85-
86-
if (volInfo->maxSize < MAX_PATH+1) {
87-
*volInfo->pIsValidDrive = GetVolumeInformation(volInfo->pszDrivePathName,
88-
volInfo->pszVolumeName, volInfo->maxSize, &serialNr, &space, &flags, nullptr, 0);
89-
}
90-
91-
::SetEvent(g_hEvent[EID_GET_VOLINFO]);
92-
return 0;
93-
}
94-
9579
ToolBarButtonUnit toolBarIcons[] = {
9680
{IDM_EX_FAVORITES, IDI_SEPARATOR_ICON, IDI_SEPARATOR_ICON, IDI_SEPARATOR_ICON, IDB_TB_FAVES, 0},
9781
{0, IDI_SEPARATOR_ICON, IDI_SEPARATOR_ICON, IDI_SEPARATOR_ICON, IDI_SEPARATOR_ICON, 0},
@@ -130,6 +114,20 @@ LPCTSTR GetNameStrFromCmd(UINT resourceId)
130114
return nullptr;
131115
}
132116

117+
std::optional<std::wstring> GetVolumeName(LPCWSTR drivePath)
118+
{
119+
DWORD volumeNameSize = MAX_PATH;
120+
std::wstring volumeName(volumeNameSize, L'\0');
121+
122+
if (::GetVolumeInformation(drivePath, &volumeName[0], volumeNameSize, nullptr, nullptr, nullptr, nullptr, 0)) {
123+
volumeName.resize(wcslen(volumeName.c_str()));
124+
return volumeName;
125+
}
126+
else {
127+
return std::nullopt;
128+
}
129+
}
130+
133131
struct FileSystemEntry {
134132
std::wstring name;
135133
DWORD attributes;
@@ -1438,70 +1436,61 @@ void ExplorerDialog::onPaste()
14381436
*/
14391437
void ExplorerDialog::UpdateRoots()
14401438
{
1441-
DWORD driveList = ::GetLogicalDrives();
1442-
BOOL isValidDrive = FALSE;
1443-
BOOL haveChildren = FALSE;
1439+
const DWORD driveList = ::GetLogicalDrives();
14441440

1445-
HTREEITEM hCurrentItem = TreeView_GetNextItem(_hTreeCtrl, TVI_ROOT, TVGN_CHILD);
1441+
HTREEITEM hCurrentItem = TreeView_GetNextItem(_hTreeCtrl, TVI_ROOT, TVGN_CHILD);
14461442

1447-
WCHAR drivePathName[] = L" :\\";
1448-
WCHAR TEMP[MAX_PATH] = {0};
1449-
WCHAR volumeName[MAX_PATH];
1443+
WCHAR drivePathName[] = L" :\\";
14501444

14511445
for (INT i = 0; i < 26; i++) {
1452-
drivePathName[0] = 'A' + i;
1446+
const WCHAR driveLetter = L'A' + i;
1447+
drivePathName[0] = driveLetter;
14531448

14541449
if (0x01 & (driveList >> i)) {
1455-
/* create volume name */
1456-
isValidDrive = ExploreVolumeInformation(drivePathName, TEMP, MAX_PATH);
1457-
_stprintf(volumeName, L"%c:", 'A' + i);
1450+
auto volumeInfo = GetVolumeName(drivePathName);
1451+
std::wstring volumeName = std::format(L"{}:", driveLetter);
1452+
bool haveChildren = false;
14581453

1459-
if (isValidDrive == TRUE) {
1460-
_stprintf(volumeName, L"%c: [%s]", 'A' + i, TEMP);
1461-
1462-
/* have children */
1454+
if (volumeInfo) {
1455+
volumeName = std::format(L"{}: [{}]", driveLetter, *volumeInfo);
14631456
haveChildren = HaveChildren(drivePathName);
14641457
}
14651458

14661459
if (hCurrentItem != nullptr) {
1467-
int iIconNormal = 0;
1468-
int iIconSelected = 0;
1469-
int iIconOverlayed = 0;
1470-
1471-
/* get current volume name in list and test if name is changed */
1472-
GetItemText(hCurrentItem, TEMP, MAX_PATH);
1473-
1474-
if (_tcscmp(volumeName, TEMP) == 0) {
1475-
/* if names are equal, go to next item in tree */
1460+
auto currentItemName = GetItemText(hCurrentItem);
1461+
if (volumeName == currentItemName) {
1462+
// if names are equal, go to next item in tree
1463+
int iIconNormal = 0;
1464+
int iIconSelected = 0;
1465+
int iIconOverlayed = 0;
14761466
ExtractIcons(drivePathName, nullptr, DEVT_DRIVE, &iIconNormal, &iIconSelected, &iIconOverlayed);
14771467
UpdateItem(hCurrentItem, volumeName, iIconNormal, iIconSelected, iIconOverlayed, 0, haveChildren);
14781468
hCurrentItem = TreeView_GetNextItem(_hTreeCtrl, hCurrentItem, TVGN_NEXT);
14791469
}
1480-
else if (volumeName[0] == TEMP[0]) {
1481-
/* if names are not the same but the drive letter are equal, rename item */
1482-
1483-
/* get icons */
1470+
else if (!currentItemName.empty() && (driveLetter == currentItemName.front())) {
1471+
// if names are not the same but the drive letter are equal, rename item
1472+
int iIconNormal = 0;
1473+
int iIconSelected = 0;
1474+
int iIconOverlayed = 0;
14841475
ExtractIcons(drivePathName, nullptr, DEVT_DRIVE, &iIconNormal, &iIconSelected, &iIconOverlayed);
14851476
UpdateItem(hCurrentItem, volumeName, iIconNormal, iIconSelected, iIconOverlayed, 0, haveChildren);
14861477
DeleteChildren(hCurrentItem);
14871478
hCurrentItem = TreeView_GetNextItem(_hTreeCtrl, hCurrentItem, TVGN_NEXT);
14881479
}
14891480
else {
1490-
/* insert the device when new and not present before */
1481+
// insert the device when new and not present before
14911482
HTREEITEM hItem = TreeView_GetNextItem(_hTreeCtrl, hCurrentItem, TVGN_PREVIOUS);
1492-
InsertChildFolder(volumeName, TVI_ROOT, hItem, isValidDrive);
1483+
InsertChildFolder(volumeName, TVI_ROOT, hItem, volumeInfo.has_value());
14931484
}
14941485
}
14951486
else {
1496-
InsertChildFolder(volumeName, TVI_ROOT, TVI_LAST, isValidDrive);
1487+
InsertChildFolder(volumeName, TVI_ROOT, TVI_LAST, volumeInfo.has_value());
14971488
}
1498-
}
1499-
else {
1489+
} else {
1490+
// get current volume name in list and test if name is changed
15001491
if (hCurrentItem != nullptr) {
1501-
/* get current volume name in list and test if name is changed */
1502-
GetItemText(hCurrentItem, TEMP, MAX_PATH);
1503-
1504-
if (drivePathName[0] == TEMP[0]) {
1492+
auto currentItemName = GetItemText(hCurrentItem);
1493+
if (!currentItemName.empty() && (driveLetter == currentItemName[0])) {
15051494
HTREEITEM pPrevItem = hCurrentItem;
15061495
hCurrentItem = TreeView_GetNextItem(_hTreeCtrl, hCurrentItem, TVGN_NEXT);
15071496
TreeView_DeleteItem(_hTreeCtrl, pPrevItem);
@@ -1788,39 +1777,12 @@ std::wstring ExplorerDialog::GetPath(HTREEITEM currentItem) const
17881777
void ExplorerDialog::NotifyNewFile()
17891778
{
17901779
if (isCreated()) {
1791-
WCHAR TEMP[MAX_PATH]{};
1792-
::SendMessage(_hParent, NPPM_GETCURRENTDIRECTORY, 0, (LPARAM)TEMP);
1793-
_ToolBar.enable(IDM_EX_GO_TO_FOLDER, (wcslen(TEMP) != 0));
1780+
WCHAR currentDirectory[MAX_PATH]{};
1781+
::SendMessage(_hParent, NPPM_GETCURRENTDIRECTORY, 0, (LPARAM)currentDirectory);
1782+
_ToolBar.enable(IDM_EX_GO_TO_FOLDER, (wcslen(currentDirectory) != 0));
17941783
}
17951784
}
17961785

1797-
1798-
BOOL ExplorerDialog::ExploreVolumeInformation(LPCTSTR pszDrivePathName, LPTSTR pszVolumeName, UINT maxSize)
1799-
{
1800-
BOOL isValidDrive = FALSE;
1801-
GetVolumeInfo volInfo {
1802-
.pszDrivePathName = pszDrivePathName,
1803-
.pszVolumeName = pszVolumeName,
1804-
.maxSize = maxSize,
1805-
.pIsValidDrive = &isValidDrive,
1806-
};
1807-
1808-
DWORD dwThreadId = 0;
1809-
_hExploreVolumeThread = ::CreateThread(nullptr, 0, GetVolumeInformationTimeoutThread, &volInfo, 0, &dwThreadId);
1810-
1811-
if (_hExploreVolumeThread) {
1812-
if (WAIT_OBJECT_0 != ::WaitForSingleObject(g_hEvent[EID_GET_VOLINFO], _pExProp->uTimeout)) {
1813-
::TerminateThread(_hExploreVolumeThread, 0);
1814-
isValidDrive = FALSE;
1815-
}
1816-
1817-
::CloseHandle(_hExploreVolumeThread);
1818-
_hExploreVolumeThread = nullptr;
1819-
}
1820-
1821-
return isValidDrive;
1822-
}
1823-
18241786
void ExplorerDialog::UpdateLayout()
18251787
{
18261788
RECT rc = { 0 };

src/Explorer/ExplorerDialog.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ enum EventID {
4242
EID_EXPAND_ITEM,
4343
EID_THREAD_END,
4444
EID_MAX_THREAD,
45-
EID_GET_VOLINFO,
4645
EID_MAX,
4746
};
4847

@@ -125,8 +124,6 @@ class ExplorerDialog : public DockingDlgInterface, public TreeHelper, public CID
125124
HTREEITEM InsertChildFolder(const std::wstring& childFolderName, HTREEITEM parentItem, HTREEITEM insertAfter = TVI_LAST, BOOL bChildrenTest = TRUE);
126125
void FetchChildren(HTREEITEM parentItem);
127126
std::wstring GetPath(HTREEITEM currentItem) const;
128-
129-
BOOL ExploreVolumeInformation(LPCTSTR pszDrivePathName, LPTSTR pszVolumeName, UINT maxSize);
130127
void UpdateLayout();
131128
private:
132129
/* Handles */

0 commit comments

Comments
 (0)