2019-10-18 11:57:19 +08:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "PowerRenameExt.h"
|
|
|
|
#include <interface/powertoy_module_interface.h>
|
2019-11-12 03:00:42 +08:00
|
|
|
#include <settings.h>
|
2019-10-29 01:14:59 +08:00
|
|
|
#include <trace.h>
|
2019-10-18 11:57:19 +08:00
|
|
|
#include <common/settings_objects.h>
|
2020-01-15 07:00:05 +08:00
|
|
|
#include <common/common.h>
|
|
|
|
#include "resource.h"
|
2019-12-19 17:15:54 +08:00
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
std::atomic<DWORD> g_dwModuleRefCount = 0;
|
2019-10-18 11:57:19 +08:00
|
|
|
HINSTANCE g_hInst = 0;
|
|
|
|
|
|
|
|
extern "C" IMAGE_DOS_HEADER __ImageBase;
|
|
|
|
|
2019-11-12 03:00:42 +08:00
|
|
|
class CPowerRenameClassFactory : public IClassFactory
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
|
|
|
public:
|
2019-11-12 03:00:42 +08:00
|
|
|
CPowerRenameClassFactory(_In_ REFCLSID clsid) :
|
2019-10-18 11:57:19 +08:00
|
|
|
m_refCount(1),
|
|
|
|
m_clsid(clsid)
|
|
|
|
{
|
2019-12-19 17:15:54 +08:00
|
|
|
ModuleAddRef();
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IUnknown methods
|
|
|
|
IFACEMETHODIMP QueryInterface(_In_ REFIID riid, _COM_Outptr_ void** ppv)
|
|
|
|
{
|
2019-12-20 21:59:20 +08:00
|
|
|
static const QITAB qit[] = {
|
2019-11-12 03:00:42 +08:00
|
|
|
QITABENT(CPowerRenameClassFactory, IClassFactory),
|
2019-10-18 11:57:19 +08:00
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
return QISearch(this, qit, riid, ppv);
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:59:20 +08:00
|
|
|
IFACEMETHODIMP_(ULONG)
|
|
|
|
AddRef()
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
2019-12-19 17:15:54 +08:00
|
|
|
return ++m_refCount;
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
|
2019-12-20 21:59:20 +08:00
|
|
|
IFACEMETHODIMP_(ULONG)
|
|
|
|
Release()
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
2019-12-19 17:15:54 +08:00
|
|
|
LONG refCount = --m_refCount;
|
2019-10-18 11:57:19 +08:00
|
|
|
if (refCount == 0)
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
return refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IClassFactory methods
|
|
|
|
IFACEMETHODIMP CreateInstance(_In_opt_ IUnknown* punkOuter, _In_ REFIID riid, _Outptr_ void** ppv)
|
|
|
|
{
|
|
|
|
*ppv = NULL;
|
|
|
|
HRESULT hr;
|
|
|
|
if (punkOuter)
|
|
|
|
{
|
|
|
|
hr = CLASS_E_NOAGGREGATION;
|
|
|
|
}
|
2020-03-08 08:25:12 +08:00
|
|
|
else if (m_clsid == CLSID_PowerRenameMenu)
|
|
|
|
{
|
|
|
|
hr = CPowerRenameMenu::s_CreateInstance(punkOuter, riid, ppv);
|
|
|
|
}
|
2019-10-18 11:57:19 +08:00
|
|
|
else
|
|
|
|
{
|
2020-03-08 08:25:12 +08:00
|
|
|
hr = CLASS_E_CLASSNOTAVAILABLE;
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP LockServer(BOOL bLock)
|
|
|
|
{
|
|
|
|
if (bLock)
|
|
|
|
{
|
2019-12-19 17:15:54 +08:00
|
|
|
ModuleAddRef();
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-19 17:15:54 +08:00
|
|
|
ModuleRelease();
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-11-12 03:00:42 +08:00
|
|
|
~CPowerRenameClassFactory()
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
2019-12-19 17:15:54 +08:00
|
|
|
ModuleRelease();
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 17:15:54 +08:00
|
|
|
std::atomic<long> m_refCount;
|
2019-10-18 11:57:19 +08:00
|
|
|
CLSID m_clsid;
|
|
|
|
};
|
|
|
|
|
|
|
|
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void*)
|
|
|
|
{
|
|
|
|
switch (dwReason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
g_hInst = hInstance;
|
2019-10-29 01:14:59 +08:00
|
|
|
Trace::RegisterProvider();
|
2019-10-18 11:57:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DLL_PROCESS_DETACH:
|
2019-10-31 21:57:38 +08:00
|
|
|
Trace::UnregisterProvider();
|
2019-10-18 11:57:19 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Checks if there are any external references to this module
|
|
|
|
//
|
|
|
|
STDAPI DllCanUnloadNow(void)
|
|
|
|
{
|
|
|
|
return (g_dwModuleRefCount == 0) ? S_OK : S_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// DLL export for creating COM objects
|
|
|
|
//
|
2019-12-20 21:59:20 +08:00
|
|
|
STDAPI DllGetClassObject(_In_ REFCLSID clsid, _In_ REFIID riid, _Outptr_ void** ppv)
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
|
|
|
*ppv = NULL;
|
2019-12-20 21:59:20 +08:00
|
|
|
CPowerRenameClassFactory* pClassFactory = new CPowerRenameClassFactory(clsid);
|
2020-03-08 08:25:12 +08:00
|
|
|
HRESULT hr = pClassFactory->QueryInterface(riid, ppv);
|
|
|
|
pClassFactory->Release();
|
2019-10-18 11:57:19 +08:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
STDAPI DllRegisterServer()
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
STDAPI DllUnregisterServer()
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2019-12-19 17:15:54 +08:00
|
|
|
void ModuleAddRef()
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
|
|
|
g_dwModuleRefCount++;
|
|
|
|
}
|
|
|
|
|
2019-12-19 17:15:54 +08:00
|
|
|
void ModuleRelease()
|
2019-10-18 11:57:19 +08:00
|
|
|
{
|
|
|
|
g_dwModuleRefCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
class PowerRenameModule : public PowertoyModuleIface
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
// Enabled by default
|
|
|
|
bool m_enabled = true;
|
2020-01-18 03:06:57 +08:00
|
|
|
std::wstring app_name;
|
2019-10-18 11:57:19 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Return the display name of the powertoy, this will be cached
|
|
|
|
virtual PCWSTR get_name() override
|
|
|
|
{
|
2020-01-18 03:06:57 +08:00
|
|
|
return app_name.c_str();
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable the powertoy
|
|
|
|
virtual void enable()
|
|
|
|
{
|
|
|
|
m_enabled = true;
|
|
|
|
save_settings();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable the powertoy
|
|
|
|
virtual void disable()
|
|
|
|
{
|
|
|
|
m_enabled = false;
|
|
|
|
save_settings();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns if the powertoy is enabled
|
|
|
|
virtual bool is_enabled() override
|
|
|
|
{
|
|
|
|
return m_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return array of the names of all events that this powertoy listens for, with
|
|
|
|
// nullptr as the last element of the array. Nullptr can also be retured for empty list.
|
|
|
|
virtual PCWSTR* get_events() override
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return JSON with the configuration options.
|
|
|
|
// These are the settings shown on the settings page along with their current values.
|
|
|
|
virtual bool get_config(_Out_ PWSTR buffer, _Out_ int* buffer_size) override
|
|
|
|
{
|
|
|
|
HINSTANCE hinstance = reinterpret_cast<HINSTANCE>(&__ImageBase);
|
|
|
|
|
|
|
|
// Create a Settings object.
|
|
|
|
PowerToysSettings::Settings settings(hinstance, get_name());
|
2020-01-15 07:17:08 +08:00
|
|
|
settings.set_description(GET_RESOURCE_STRING(IDS_SETTINGS_DESCRIPTION));
|
2020-01-21 03:52:46 +08:00
|
|
|
settings.set_icon_key(L"pt-power-rename");
|
2019-10-18 11:57:19 +08:00
|
|
|
|
|
|
|
// Link to the GitHub PowerRename sub-page
|
2020-01-15 07:17:08 +08:00
|
|
|
settings.set_overview_link(GET_RESOURCE_STRING(IDS_OVERVIEW_LINK));
|
2019-10-18 11:57:19 +08:00
|
|
|
|
2019-11-12 12:58:39 +08:00
|
|
|
settings.add_bool_toogle(
|
2020-01-21 03:52:46 +08:00
|
|
|
L"bool_persist_input",
|
2020-01-15 07:17:08 +08:00
|
|
|
GET_RESOURCE_STRING(IDS_RESTORE_SEARCH),
|
2019-12-20 21:59:20 +08:00
|
|
|
CSettings::GetPersistState());
|
2019-11-12 12:58:39 +08:00
|
|
|
|
|
|
|
settings.add_bool_toogle(
|
2020-01-21 03:52:46 +08:00
|
|
|
L"bool_mru_enabled",
|
2020-01-15 07:17:08 +08:00
|
|
|
GET_RESOURCE_STRING(IDS_ENABLE_AUTO),
|
2019-12-20 21:59:20 +08:00
|
|
|
CSettings::GetMRUEnabled());
|
2019-11-12 12:58:39 +08:00
|
|
|
|
|
|
|
settings.add_int_spinner(
|
2020-01-21 03:52:46 +08:00
|
|
|
L"int_max_mru_size",
|
2020-01-15 07:17:08 +08:00
|
|
|
GET_RESOURCE_STRING(IDS_MAX_ITEMS),
|
2019-11-12 12:58:39 +08:00
|
|
|
CSettings::GetMaxMRUSize(),
|
|
|
|
0,
|
|
|
|
20,
|
2019-12-20 21:59:20 +08:00
|
|
|
1);
|
2019-11-12 12:58:39 +08:00
|
|
|
|
|
|
|
settings.add_bool_toogle(
|
2020-01-21 03:52:46 +08:00
|
|
|
L"bool_show_icon_on_menu",
|
2020-01-15 07:17:08 +08:00
|
|
|
GET_RESOURCE_STRING(IDS_ICON_CONTEXT_MENU),
|
2019-12-20 21:59:20 +08:00
|
|
|
CSettings::GetShowIconOnMenu());
|
2019-11-12 12:58:39 +08:00
|
|
|
|
|
|
|
settings.add_bool_toogle(
|
2020-01-21 03:52:46 +08:00
|
|
|
L"bool_show_extended_menu",
|
2020-01-15 07:17:08 +08:00
|
|
|
GET_RESOURCE_STRING(IDS_EXTENDED_MENU_INFO),
|
2019-12-20 21:59:20 +08:00
|
|
|
CSettings::GetExtendedContextMenuOnly());
|
2019-11-12 12:58:39 +08:00
|
|
|
|
2019-10-18 11:57:19 +08:00
|
|
|
return settings.serialize_to_buffer(buffer, buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Passes JSON with the configuration settings for the powertoy.
|
|
|
|
// This is called when the user hits Save on the settings page.
|
|
|
|
virtual void set_config(PCWSTR config) override
|
|
|
|
{
|
2019-12-20 21:59:20 +08:00
|
|
|
try
|
|
|
|
{
|
2019-11-12 12:58:39 +08:00
|
|
|
// Parse the input JSON string.
|
|
|
|
PowerToysSettings::PowerToyValues values =
|
|
|
|
PowerToysSettings::PowerToyValues::from_json_string(config);
|
|
|
|
|
2020-01-21 03:52:46 +08:00
|
|
|
CSettings::SetPersistState(values.get_bool_value(L"bool_persist_input").value());
|
|
|
|
CSettings::SetMRUEnabled(values.get_bool_value(L"bool_mru_enabled").value());
|
|
|
|
CSettings::SetMaxMRUSize(values.get_int_value(L"int_max_mru_size").value());
|
|
|
|
CSettings::SetShowIconOnMenu(values.get_bool_value(L"bool_show_icon_on_menu").value());
|
|
|
|
CSettings::SetExtendedContextMenuOnly(values.get_bool_value(L"bool_show_extended_menu").value());
|
2020-02-13 17:44:03 +08:00
|
|
|
|
|
|
|
Trace::SettingsChanged();
|
2019-11-12 12:58:39 +08:00
|
|
|
}
|
2019-12-20 21:59:20 +08:00
|
|
|
catch (std::exception)
|
|
|
|
{
|
2019-11-12 12:58:39 +08:00
|
|
|
// Improper JSON.
|
|
|
|
}
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Signal from the Settings editor to call a custom action.
|
|
|
|
// This can be used to spawn more complex editors.
|
|
|
|
virtual void call_custom_action(const wchar_t* action) override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle incoming event, data is event-specific
|
|
|
|
virtual intptr_t signal_event(const wchar_t* name, intptr_t data) override
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:59:20 +08:00
|
|
|
virtual void register_system_menu_helper(PowertoySystemMenuIface* helper) override {}
|
|
|
|
virtual void signal_system_menu_action(const wchar_t* name) override {}
|
2019-11-12 18:48:14 +08:00
|
|
|
|
2019-10-18 11:57:19 +08:00
|
|
|
// Destroy the powertoy and free memory
|
|
|
|
virtual void destroy() override
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_settings()
|
|
|
|
{
|
2019-11-12 03:00:42 +08:00
|
|
|
m_enabled = CSettings::GetEnabled();
|
2019-10-29 01:14:59 +08:00
|
|
|
Trace::EnablePowerRename(m_enabled);
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void save_settings()
|
|
|
|
{
|
2019-11-12 03:00:42 +08:00
|
|
|
CSettings::SetEnabled(m_enabled);
|
2019-10-29 01:14:59 +08:00
|
|
|
Trace::EnablePowerRename(m_enabled);
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PowerRenameModule()
|
|
|
|
{
|
|
|
|
init_settings();
|
2020-01-18 06:23:07 +08:00
|
|
|
app_name = GET_RESOURCE_STRING(IDS_POWERRENAME);
|
2019-10-18 11:57:19 +08:00
|
|
|
}
|
2020-01-15 07:00:05 +08:00
|
|
|
|
2020-01-18 03:06:57 +08:00
|
|
|
~PowerRenameModule(){};
|
2019-10-18 11:57:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()
|
|
|
|
{
|
|
|
|
return new PowerRenameModule();
|
2020-01-18 03:12:23 +08:00
|
|
|
}
|