Skip to content

Commit

Permalink
Add progress dialog with cancel support during enumeration of items a…
Browse files Browse the repository at this point in the history
…t startup.
  • Loading branch information
chrdavis committed Nov 8, 2020
1 parent 8982254 commit bb5ad05
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
25 changes: 21 additions & 4 deletions SmartRenameLib/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ HBITMAP CreateBitmapFromIcon(_In_ HICON hIcon, _In_opt_ UINT width, _In_opt_ UIN
return hBitmapResult;
}

HRESULT _ParseEnumItems(_In_ IEnumShellItems* pesi, _In_ ISmartRenameManager* psrm, _In_ int depth = 0)
HRESULT _ParseEnumItems(_In_ IEnumShellItems* pesi, _In_ ISmartRenameManager* psrm, _In_opt_ IProgressDialog* ppd, _In_ int depth = 0)
{
HRESULT hr = E_INVALIDARG;

Expand All @@ -104,6 +104,13 @@ HRESULT _ParseEnumItems(_In_ IEnumShellItems* pesi, _In_ ISmartRenameManager* ps
CComPtr<IShellItem> spsi;
while ((S_OK == pesi->Next(1, &spsi, &celtFetched)) && (SUCCEEDED(hr)))
{
if (ppd && ppd->HasUserCancelled())
{
// Cancelled by user
hr = E_ABORT;
break;
}

CComPtr<ISmartRenameItemFactory> spsrif;
hr = psrm->get_renameItemFactory(&spsrif);
if (SUCCEEDED(hr))
Expand All @@ -114,6 +121,16 @@ HRESULT _ParseEnumItems(_In_ IEnumShellItems* pesi, _In_ ISmartRenameManager* ps
{
spNewItem->put_depth(depth);
hr = psrm->AddItem(spNewItem);
if (SUCCEEDED(hr) && ppd)
{
// Update the progress dialog
PWSTR pathDisplay = nullptr;
if (SUCCEEDED(spNewItem->get_path(&pathDisplay)))
{
ppd->SetLine(2, pathDisplay, TRUE, nullptr);
CoTaskMemFree(pathDisplay);
}
}
}

if (SUCCEEDED(hr))
Expand All @@ -127,7 +144,7 @@ HRESULT _ParseEnumItems(_In_ IEnumShellItems* pesi, _In_ ISmartRenameManager* ps
if (SUCCEEDED(hr))
{
// Parse the folder contents recursively
hr = _ParseEnumItems(spesiNext, psrm, depth + 1);
hr = _ParseEnumItems(spesiNext, psrm, ppd, depth + 1);
}
}
}
Expand All @@ -141,7 +158,7 @@ HRESULT _ParseEnumItems(_In_ IEnumShellItems* pesi, _In_ ISmartRenameManager* ps
}

// Iterate through the data object and add paths to the rotation manager
HRESULT EnumerateDataObject(_In_ IDataObject* pdo, _In_ ISmartRenameManager* psrm)
HRESULT EnumerateDataObject(_In_ IDataObject* pdo, _In_ ISmartRenameManager* psrm, _In_opt_ IProgressDialog* ppd)
{
CComPtr<IShellItemArray> spsia;
HRESULT hr = SHCreateShellItemArrayFromDataObject(pdo, IID_PPV_ARGS(&spsia));
Expand All @@ -151,7 +168,7 @@ HRESULT EnumerateDataObject(_In_ IDataObject* pdo, _In_ ISmartRenameManager* psr
hr = spsia->EnumItems(&spesi);
if (SUCCEEDED(hr))
{
hr = _ParseEnumItems(spesi, psrm);
hr = _ParseEnumItems(spesi, psrm, ppd);
}
}

Expand Down
2 changes: 1 addition & 1 deletion SmartRenameLib/Helpers.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

HRESULT EnumerateDataObject(_In_ IDataObject* pdo, _In_ ISmartRenameManager* psrm);
HRESULT EnumerateDataObject(_In_ IDataObject* pdo, _In_ ISmartRenameManager* psrm, _In_opt_ IProgressDialog* ppd);
HRESULT GetIconIndexFromPath(_In_ PCWSTR path, _Out_ int* index);
HBITMAP CreateBitmapFromIcon(_In_ HICON hIcon, _In_opt_ UINT width = 0, _In_opt_ UINT height = 0);
HWND CreateMsgWindow(_In_ HINSTANCE hInst, _In_ WNDPROC pfnWndProc, _In_ void* p);
Expand Down
1 change: 1 addition & 0 deletions SmartRenameLib/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
#include <shobjidl.h>
#include <shellapi.h>
#include <shlwapi.h>
#include <ShlObj_core.h>

#include "SmartRenameInterfaces.h"
26 changes: 25 additions & 1 deletion SmartRenameUI/SmartRenameUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,32 @@ void CSmartRenameUI::_EnumerateItems(_In_ IDataObject* pdtobj)
// Enumerate the data object and popuplate the manager
if (m_spsrm)
{
// Add a progress dialog in case enumeration of items takes a long time
// This also allows the user to cancel enumeration.
CComPtr<IProgressDialog> sppd;
if (SUCCEEDED(CoCreateInstance(CLSID_ProgressDialog, NULL, CLSCTX_INPROC, IID_PPV_ARGS(&sppd))))
{
wchar_t buff[100] = { 0 };
LoadString(g_hInst, IDS_LOADING, buff, ARRAYSIZE(buff));
sppd->SetLine(1, buff, FALSE, NULL);
LoadString(g_hInst, IDS_APP_TITLE, buff, ARRAYSIZE(buff));
sppd->SetTitle(buff);
sppd->StartProgressDialog(m_hwnd, NULL, PROGDLG_MARQUEEPROGRESS, NULL);
}

m_disableCountUpdate = true;
EnumerateDataObject(pdtobj, m_spsrm);

if (E_ABORT == EnumerateDataObject(pdtobj, m_spsrm, sppd))
{
// User cancelled during enumeration. Close the dialog.
_OnCloseDlg();
}

if (sppd)
{
sppd->StopProgressDialog();
}

m_disableCountUpdate = false;

UINT itemCount = 0;
Expand Down
1 change: 1 addition & 0 deletions SmartRenameUI/SmartRenameUI.rc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ BEGIN
IDS_ORIGINAL "Original"
IDS_LISTVIEW_EMPTY "All items have been filtered out.\nPlease select from the options above to show items."
IDS_ENTIREITEMNAME "Item Name and Extension"
IDS_LOADING "Loading..."
IDC_SMARTRENAME "SMARTRENAME"
IDS_COUNTSLABELFMT "Items Selected: %u | Renaming: %u"
END
Expand Down
Binary file modified SmartRenameUI/resource.h
Binary file not shown.

0 comments on commit bb5ad05

Please sign in to comment.