2019-10-18 11:57:19 +08:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "PowerRenameExt.h"
|
|
|
|
#include <PowerRenameUI.h>
|
|
|
|
#include <PowerRenameItem.h>
|
|
|
|
#include <PowerRenameManager.h>
|
2019-10-29 01:14:59 +08:00
|
|
|
#include <trace.h>
|
2019-11-12 03:00:42 +08:00
|
|
|
#include <Helpers.h>
|
|
|
|
#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()
|
|
|
|
{
|
|
|
|
DllAddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
CPowerRenameMenu::~CPowerRenameMenu()
|
|
|
|
{
|
|
|
|
m_spdo = nullptr;
|
|
|
|
DllRelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT CPowerRenameMenu::s_CreateInstance(_In_opt_ IUnknown*, _In_ REFIID riid, _Outptr_ void **ppv)
|
|
|
|
{
|
|
|
|
*ppv = nullptr;
|
|
|
|
HRESULT hr = E_OUTOFMEMORY;
|
|
|
|
CPowerRenameMenu *pprm = new CPowerRenameMenu();
|
|
|
|
if (pprm)
|
|
|
|
{
|
|
|
|
hr = pprm->QueryInterface(riid, ppv);
|
|
|
|
pprm->Release();
|
|
|
|
}
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IShellExtInit
|
|
|
|
HRESULT CPowerRenameMenu::Initialize(_In_opt_ PCIDLIST_ABSOLUTE, _In_ IDataObject *pdtobj, HKEY)
|
|
|
|
{
|
|
|
|
// Check if we have disabled ourselves
|
2019-11-12 03:00:42 +08:00
|
|
|
if (!CSettings::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
|
2019-11-12 03:00:42 +08:00
|
|
|
if (!CSettings::GetEnabled())
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
// Check if we should only be on the extended context menu
|
|
|
|
if (CSettings::GetExtendedContextMenuOnly() && (!(uFlags & CMF_EXTENDEDVERBS)))
|
2019-10-18 11:57:19 +08:00
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (CSettings::GetShowIconOnMenu())
|
|
|
|
{
|
|
|
|
HICON hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_RENAME), IMAGE_ICON, 16, 16, 0);
|
|
|
|
if (hIcon)
|
|
|
|
{
|
|
|
|
mii.fMask |= MIIM_BITMAP;
|
|
|
|
mii.hbmpItem = CreateBitmapFromIcon(hIcon);
|
|
|
|
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;
|
|
|
|
|
2019-11-12 03:00:42 +08:00
|
|
|
if (CSettings::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-10-31 21:57:38 +08:00
|
|
|
InvokeStruct* pInvokeData = new InvokeStruct;
|
|
|
|
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-10-18 11:57:19 +08:00
|
|
|
CComPtr<IDataObject> spdo;
|
2019-10-31 21:57:38 +08:00
|
|
|
HRESULT hr = CoGetInterfaceAndReleaseStream(pInvokeData->pstrm, IID_PPV_ARGS(&spdo));
|
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-10-29 01:14:59 +08:00
|
|
|
hr = CPowerRenameUI::s_CreateInstance(spsrm, spdo, false, &spsrui);
|
|
|
|
if (SUCCEEDED(hr))
|
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;
|
|
|
|
}
|