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-10-18 11:57:19 +08:00
|
|
|
#include "resource.h"
|
|
|
|
|
|
|
|
extern HINSTANCE g_hInst;
|
|
|
|
|
|
|
|
HWND g_hwndParent = 0;
|
|
|
|
|
|
|
|
|
|
|
|
const wchar_t powerRenameRegPath[] = L"Software\\Microsoft\\PowerRename";
|
|
|
|
const wchar_t powerRenameRegEnabledName[] = L"Enabled";
|
|
|
|
|
|
|
|
bool CPowerRenameMenu::IsEnabled()
|
|
|
|
{
|
|
|
|
DWORD type = REG_DWORD;
|
|
|
|
DWORD dwEnabled = 0;
|
|
|
|
DWORD cb = sizeof(dwEnabled);
|
|
|
|
SHGetValue(HKEY_CURRENT_USER, powerRenameRegPath, powerRenameRegEnabledName, &type, &dwEnabled, &cb);
|
|
|
|
return (dwEnabled == 0) ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CPowerRenameMenu::SetEnabled(_In_ bool enabled)
|
|
|
|
{
|
|
|
|
DWORD dwEnabled = enabled ? 1 : 0;
|
|
|
|
return SUCCEEDED(HRESULT_FROM_WIN32(SHSetValueW(HKEY_CURRENT_USER, powerRenameRegPath, powerRenameRegEnabledName, REG_DWORD, &dwEnabled, sizeof(dwEnabled))));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if (!IsEnabled())
|
|
|
|
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
|
|
|
|
if (!IsEnabled())
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
HRESULT hr = E_UNEXPECTED;
|
|
|
|
if (m_spdo)
|
|
|
|
{
|
|
|
|
if ((uFlags & ~CMF_OPTIMIZEFORINVOKE) && (uFlags & ~(CMF_DEFAULTONLY | CMF_VERBSONLY)))
|
|
|
|
{
|
|
|
|
wchar_t menuName[64] = { 0 };
|
|
|
|
LoadString(g_hInst, IDS_POWERRENAME, menuName, ARRAYSIZE(menuName));
|
|
|
|
InsertMenu(hMenu, index, MF_STRING | MF_BYPOSITION, uIDFirst++, menuName);
|
|
|
|
hr = MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT CPowerRenameMenu::InvokeCommand(_In_ LPCMINVOKECOMMANDINFO pici)
|
|
|
|
{
|
|
|
|
HRESULT hr = E_FAIL;
|
|
|
|
|
2019-10-29 01:14:59 +08:00
|
|
|
if (IsEnabled() &
|
|
|
|
(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-18 11:57:19 +08:00
|
|
|
IStream* pstrm = nullptr;
|
2019-10-29 01:14:59 +08:00
|
|
|
hr = CoMarshalInterThreadInterfaceInStream(__uuidof(m_spdo), m_spdo, &pstrm);
|
|
|
|
if (SUCCEEDED(hr))
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
|
|
|
if (!SHCreateThread(s_PowerRenameUIThreadProc, pstrm, CTF_COINIT | CTF_PROCESS_REF, nullptr))
|
|
|
|
{
|
|
|
|
pstrm->Release(); // if we failed to create the thread, then we must release the stream
|
2019-10-29 01:14:59 +08:00
|
|
|
hr = E_FAIL;
|
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)
|
|
|
|
{
|
|
|
|
IStream* pstrm = static_cast<IStream*>(pData);
|
|
|
|
CComPtr<IDataObject> spdo;
|
2019-10-29 01:14:59 +08:00
|
|
|
HRESULT hr = CoGetInterfaceAndReleaseStream(pstrm, IID_PPV_ARGS(&spdo));
|
|
|
|
if (SUCCEEDED(hr))
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
|
|
|
// Create the smart rename manager
|
|
|
|
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-10-29 01:14:59 +08:00
|
|
|
hr = spsrm->put_smartRenameItemFactory(spsrif);
|
|
|
|
if (SUCCEEDED(hr))
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
|
|
|
// Create the smart rename UI instance and pass the smart rename manager
|
|
|
|
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
|
|
|
|
spsrui->Show();
|
|
|
|
spsrui->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to call shutdown to break circular dependencies
|
|
|
|
spsrm->Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 01:14:59 +08:00
|
|
|
Trace::UIShownRet(hr);
|
|
|
|
|
2019-10-18 11:57:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|