2020-04-30 04:02:18 +08:00
|
|
|
#include "pch.h"
|
2019-10-18 11:57:19 +08:00
|
|
|
#include "PowerRenameExt.h"
|
|
|
|
#include <PowerRenameUI.h>
|
|
|
|
#include <PowerRenameItem.h>
|
|
|
|
#include <PowerRenameManager.h>
|
2019-10-29 01:14:59 +08:00
|
|
|
#include <trace.h>
|
2020-01-16 05:57:05 +08:00
|
|
|
#include <common.h>
|
2019-11-12 03:00:42 +08:00
|
|
|
#include <Helpers.h>
|
2020-01-16 15:06:22 +08:00
|
|
|
#include <icon_helpers.h>
|
2019-11-12 03:00:42 +08:00
|
|
|
#include <Settings.h>
|
2019-10-18 11:57:19 +08:00
|
|
|
#include "resource.h"
|
|
|
|
|
|
|
|
extern HINSTANCE g_hInst;
|
|
|
|
|
2019-10-31 21:57:38 +08:00
|
|
|
struct InvokeStruct
|
|
|
|
{
|
|
|
|
HWND hwndParent;
|
|
|
|
IStream* pstrm;
|
|
|
|
};
|
2019-10-18 11:57:19 +08:00
|
|
|
|
|
|
|
CPowerRenameMenu::CPowerRenameMenu()
|
|
|
|
{
|
2019-12-19 17:15:54 +08:00
|
|
|
ModuleAddRef();
|
2020-03-25 18:12:38 +08:00
|
|
|
app_name = GET_RESOURCE_STRING(IDS_POWERRENAME_APP_NAME);
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CPowerRenameMenu::~CPowerRenameMenu()
|
|
|
|
{
|
|
|
|
m_spdo = nullptr;
|
2019-11-14 06:13:14 +08:00
|
|
|
DeleteObject(m_hbmpIcon);
|
2019-12-19 17:15:54 +08:00
|
|
|
ModuleRelease();
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
|
2019-12-20 21:59:20 +08:00
|
|
|
HRESULT CPowerRenameMenu::s_CreateInstance(_In_opt_ IUnknown*, _In_ REFIID riid, _Outptr_ void** ppv)
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
|
|
|
*ppv = nullptr;
|
|
|
|
HRESULT hr = E_OUTOFMEMORY;
|
2019-12-20 21:59:20 +08:00
|
|
|
CPowerRenameMenu* pprm = new CPowerRenameMenu();
|
2019-10-18 11:57:19 +08:00
|
|
|
if (pprm)
|
|
|
|
{
|
|
|
|
hr = pprm->QueryInterface(riid, ppv);
|
|
|
|
pprm->Release();
|
|
|
|
}
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IShellExtInit
|
2019-12-20 21:59:20 +08:00
|
|
|
HRESULT CPowerRenameMenu::Initialize(_In_opt_ PCIDLIST_ABSOLUTE, _In_ IDataObject* pdtobj, HKEY)
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
|
|
|
// Check if we have disabled ourselves
|
2020-04-09 01:12:46 +08:00
|
|
|
if (!CSettingsInstance().GetEnabled())
|
2019-10-18 11:57:19 +08:00
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
// Cache the data object to be used later
|
|
|
|
m_spdo = pdtobj;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IContextMenu
|
|
|
|
HRESULT CPowerRenameMenu::QueryContextMenu(HMENU hMenu, UINT index, UINT uIDFirst, UINT, UINT uFlags)
|
|
|
|
{
|
|
|
|
// Check if we have disabled ourselves
|
2020-04-09 01:12:46 +08:00
|
|
|
if (!CSettingsInstance().GetEnabled())
|
2019-11-12 03:00:42 +08:00
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
// Check if we should only be on the extended context menu
|
2020-04-09 01:12:46 +08:00
|
|
|
if (CSettingsInstance().GetExtendedContextMenuOnly() && (!(uFlags & CMF_EXTENDEDVERBS)))
|
2019-10-18 11:57:19 +08:00
|
|
|
return E_FAIL;
|
|
|
|
|
2020-07-23 04:27:58 +08:00
|
|
|
// Check if at least one of the selected items is actually renamable.
|
|
|
|
if (!DataObjectContainsRenamableItem(m_spdo))
|
|
|
|
return E_FAIL;
|
|
|
|
|
2019-10-18 11:57:19 +08:00
|
|
|
HRESULT hr = E_UNEXPECTED;
|
2019-10-31 21:57:38 +08:00
|
|
|
if (m_spdo && !(uFlags & (CMF_DEFAULTONLY | CMF_VERBSONLY | CMF_OPTIMIZEFORINVOKE)))
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
2019-10-31 21:57:38 +08:00
|
|
|
wchar_t menuName[64] = { 0 };
|
|
|
|
LoadString(g_hInst, IDS_POWERRENAME, menuName, ARRAYSIZE(menuName));
|
2019-11-12 03:00:42 +08:00
|
|
|
|
|
|
|
MENUITEMINFO mii;
|
|
|
|
mii.cbSize = sizeof(MENUITEMINFO);
|
|
|
|
mii.fMask = MIIM_STRING | MIIM_FTYPE | MIIM_ID | MIIM_STATE;
|
|
|
|
mii.wID = uIDFirst++;
|
|
|
|
mii.fType = MFT_STRING;
|
|
|
|
mii.dwTypeData = (PWSTR)menuName;
|
|
|
|
mii.fState = MFS_ENABLED;
|
|
|
|
|
2020-04-09 01:12:46 +08:00
|
|
|
if (CSettingsInstance().GetShowIconOnMenu())
|
2019-11-12 03:00:42 +08:00
|
|
|
{
|
|
|
|
HICON hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_RENAME), IMAGE_ICON, 16, 16, 0);
|
|
|
|
if (hIcon)
|
|
|
|
{
|
|
|
|
mii.fMask |= MIIM_BITMAP;
|
2019-11-14 06:13:14 +08:00
|
|
|
if (m_hbmpIcon == NULL)
|
|
|
|
{
|
|
|
|
m_hbmpIcon = CreateBitmapFromIcon(hIcon);
|
|
|
|
}
|
|
|
|
mii.hbmpItem = m_hbmpIcon;
|
2019-11-12 03:00:42 +08:00
|
|
|
DestroyIcon(hIcon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!InsertMenuItem(hMenu, index, TRUE, &mii))
|
|
|
|
{
|
|
|
|
hr = HRESULT_FROM_WIN32(GetLastError());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 1);
|
|
|
|
}
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT CPowerRenameMenu::InvokeCommand(_In_ LPCMINVOKECOMMANDINFO pici)
|
|
|
|
{
|
|
|
|
HRESULT hr = E_FAIL;
|
|
|
|
|
2020-04-09 01:12:46 +08:00
|
|
|
if (CSettingsInstance().GetEnabled() &&
|
2019-10-29 01:14:59 +08:00
|
|
|
(IS_INTRESOURCE(pici->lpVerb)) &&
|
2019-10-18 11:57:19 +08:00
|
|
|
(LOWORD(pici->lpVerb) == 0))
|
|
|
|
{
|
2019-10-29 01:14:59 +08:00
|
|
|
Trace::Invoked();
|
2019-12-20 21:59:20 +08:00
|
|
|
InvokeStruct* pInvokeData = new (std::nothrow) InvokeStruct;
|
2019-10-31 21:57:38 +08:00
|
|
|
hr = pInvokeData ? S_OK : E_OUTOFMEMORY;
|
2019-10-29 01:14:59 +08:00
|
|
|
if (SUCCEEDED(hr))
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
2019-10-31 21:57:38 +08:00
|
|
|
pInvokeData->hwndParent = pici->hwnd;
|
|
|
|
hr = CoMarshalInterThreadInterfaceInStream(__uuidof(m_spdo), m_spdo, &(pInvokeData->pstrm));
|
|
|
|
if (SUCCEEDED(hr))
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
2019-10-31 21:57:38 +08:00
|
|
|
hr = SHCreateThread(s_PowerRenameUIThreadProc, pInvokeData, CTF_COINIT | CTF_PROCESS_REF, nullptr) ? S_OK : E_FAIL;
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
pInvokeData->pstrm->Release(); // if we failed to create the thread, then we must release the stream
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
delete pInvokeData;
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-29 01:14:59 +08:00
|
|
|
Trace::InvokedRet(hr);
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI CPowerRenameMenu::s_PowerRenameUIThreadProc(_In_ void* pData)
|
|
|
|
{
|
2019-10-31 21:57:38 +08:00
|
|
|
InvokeStruct* pInvokeData = static_cast<InvokeStruct*>(pData);
|
2019-12-19 17:15:54 +08:00
|
|
|
CComPtr<IUnknown> dataSource;
|
|
|
|
HRESULT hr = CoGetInterfaceAndReleaseStream(pInvokeData->pstrm, IID_PPV_ARGS(&dataSource));
|
2019-10-29 01:14:59 +08:00
|
|
|
if (SUCCEEDED(hr))
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
2019-11-12 03:00:42 +08:00
|
|
|
// Create the rename manager
|
2019-10-18 11:57:19 +08:00
|
|
|
CComPtr<IPowerRenameManager> spsrm;
|
2019-10-29 01:14:59 +08:00
|
|
|
hr = CPowerRenameManager::s_CreateInstance(&spsrm);
|
|
|
|
if (SUCCEEDED(hr))
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
|
|
|
// Create the factory for our items
|
|
|
|
CComPtr<IPowerRenameItemFactory> spsrif;
|
2019-10-29 01:14:59 +08:00
|
|
|
hr = CPowerRenameItem::s_CreateInstance(nullptr, IID_PPV_ARGS(&spsrif));
|
|
|
|
if (SUCCEEDED(hr))
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
|
|
|
// Pass the factory to the manager
|
2019-11-12 03:00:42 +08:00
|
|
|
hr = spsrm->put_renameItemFactory(spsrif);
|
2019-10-29 01:14:59 +08:00
|
|
|
if (SUCCEEDED(hr))
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
2019-11-12 03:00:42 +08:00
|
|
|
// Create the rename UI instance and pass the rename manager
|
2019-10-18 11:57:19 +08:00
|
|
|
CComPtr<IPowerRenameUI> spsrui;
|
2019-12-19 17:15:54 +08:00
|
|
|
hr = CPowerRenameUI::s_CreateInstance(spsrm, dataSource, false, &spsrui);
|
|
|
|
|
2019-10-29 01:14:59 +08:00
|
|
|
if (SUCCEEDED(hr))
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
2019-12-19 17:15:54 +08:00
|
|
|
IDataObject* dummy;
|
|
|
|
// If we're running on a local COM server, we need to decrement module refcount, which was previously incremented in CPowerRenameMenu::Invoke.
|
|
|
|
if (SUCCEEDED(dataSource->QueryInterface(IID_IShellItemArray, reinterpret_cast<void**>(&dummy))))
|
|
|
|
{
|
|
|
|
ModuleRelease();
|
|
|
|
}
|
2019-10-18 11:57:19 +08:00
|
|
|
// Call blocks until we are done
|
2019-10-31 21:57:38 +08:00
|
|
|
spsrui->Show(pInvokeData->hwndParent);
|
2019-10-18 11:57:19 +08:00
|
|
|
spsrui->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to call shutdown to break circular dependencies
|
|
|
|
spsrm->Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 21:57:38 +08:00
|
|
|
delete pInvokeData;
|
|
|
|
|
2019-10-29 01:14:59 +08:00
|
|
|
Trace::UIShownRet(hr);
|
|
|
|
|
2019-10-18 11:57:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2019-12-19 17:15:54 +08:00
|
|
|
|
|
|
|
HRESULT __stdcall CPowerRenameMenu::GetTitle(IShellItemArray* /*psiItemArray*/, LPWSTR* ppszName)
|
|
|
|
{
|
2020-01-18 03:06:57 +08:00
|
|
|
return SHStrDup(app_name.c_str(), ppszName);
|
2019-12-19 17:15:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT __stdcall CPowerRenameMenu::GetIcon(IShellItemArray* /*psiItemArray*/, LPWSTR* ppszIcon)
|
|
|
|
{
|
2020-04-09 01:12:46 +08:00
|
|
|
if (!CSettingsInstance().GetShowIconOnMenu())
|
2019-12-19 17:15:54 +08:00
|
|
|
{
|
2019-12-20 21:59:20 +08:00
|
|
|
*ppszIcon = nullptr;
|
|
|
|
return E_NOTIMPL;
|
2019-12-19 17:15:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring iconResourcePath = get_module_filename();
|
|
|
|
iconResourcePath += L",-";
|
|
|
|
iconResourcePath += std::to_wstring(IDI_RENAME);
|
|
|
|
return SHStrDup(iconResourcePath.c_str(), ppszIcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT __stdcall CPowerRenameMenu::GetToolTip(IShellItemArray* /*psiItemArray*/, LPWSTR* ppszInfotip)
|
|
|
|
{
|
|
|
|
*ppszInfotip = nullptr;
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT __stdcall CPowerRenameMenu::GetCanonicalName(GUID* pguidCommandName)
|
|
|
|
{
|
|
|
|
*pguidCommandName = __uuidof(this);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT __stdcall CPowerRenameMenu::GetState(IShellItemArray* psiItemArray, BOOL fOkToBeSlow, EXPCMDSTATE* pCmdState)
|
|
|
|
{
|
2020-04-09 01:12:46 +08:00
|
|
|
*pCmdState = CSettingsInstance().GetEnabled() ? ECS_ENABLED : ECS_HIDDEN;
|
2019-12-19 17:15:54 +08:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//#define DEBUG_TELL_PID
|
|
|
|
|
|
|
|
HRESULT __stdcall CPowerRenameMenu::Invoke(IShellItemArray* psiItemArray, IBindCtx* /*pbc*/)
|
|
|
|
{
|
|
|
|
#if defined(DEBUG_TELL_PID)
|
|
|
|
wchar_t buffer[256];
|
|
|
|
swprintf_s(buffer, L"%d", GetCurrentProcessId());
|
|
|
|
MessageBoxW(nullptr, buffer, L"PID", MB_OK);
|
|
|
|
#endif
|
|
|
|
Trace::Invoked();
|
2019-12-20 21:59:20 +08:00
|
|
|
InvokeStruct* pInvokeData = new (std::nothrow) InvokeStruct;
|
2019-12-19 17:15:54 +08:00
|
|
|
HRESULT hr = pInvokeData ? S_OK : E_OUTOFMEMORY;
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
pInvokeData->hwndParent = nullptr;
|
|
|
|
hr = CoMarshalInterThreadInterfaceInStream(__uuidof(psiItemArray), psiItemArray, &(pInvokeData->pstrm));
|
|
|
|
if (!SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
// Prevent Shutting down before PowerRenameUI is created
|
|
|
|
ModuleAddRef();
|
|
|
|
hr = SHCreateThread(s_PowerRenameUIThreadProc, pInvokeData, CTF_COINIT | CTF_PROCESS_REF, nullptr) ? S_OK : E_FAIL;
|
|
|
|
}
|
|
|
|
Trace::InvokedRet(hr);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT __stdcall CPowerRenameMenu::GetFlags(EXPCMDFLAGS* pFlags)
|
|
|
|
{
|
|
|
|
*pFlags = ECF_DEFAULT;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT __stdcall CPowerRenameMenu::EnumSubCommands(IEnumExplorerCommand** ppEnum)
|
|
|
|
{
|
|
|
|
*ppEnum = nullptr;
|
|
|
|
return E_NOTIMPL;
|
2020-01-18 03:12:23 +08:00
|
|
|
}
|