Files
PowerToys/src/modules/previewpane/powerpreview/settings.cpp
Arjun Balgovind 7fd5e18ef4 [File Explorer] Fix enable/disable for File Explorer PowerToy (#6883)
* Commented out enable/disable for File Explorer

* Revert UI changes

* Disable the toggles if PT is not running elevated

* Fixed compilation errors in tests

* Cleaned up preview pane code to separate thumbnail and preview panes as separate classes

* Fixed broken settings format and added elevation check and registry updated required logic. Preview Handler tested manually working, Thumbnail Enable/Disable needs to be fixed

* Updated Thumbnail enable/disable logic and added warning messages

* Update tests for File Explorer

* Fixed RegGetValue failing in Release config

* Renamed new classes

* Split wrappers for disable to work

* Modified enabled flag check to also check if user is on new settings. Fixed casing issue in powerpreview.h that caused a dialog prompt on first launch after install

* Update fontweight and margin

* Fixed release build not working

* Move UseNewSettings usage to powerpreview.cpp to avoid tests breaking. For new settings the enable check is done in constructor and for old settings it is done in enable

* Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw

Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com>

* Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw

Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com>

* Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw

Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com>

* Moved dup code to method

* Use correct versions of general settings for backwards compat test

Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com>
2020-10-09 14:45:30 -07:00

131 lines
4.6 KiB
C++

#include "pch.h"
#include <common.h>
#include "settings.h"
#include "trace.h"
#include <iostream>
#include <atlstr.h>
using namespace std;
namespace PowerPreviewSettings
{
extern "C" IMAGE_DOS_HEADER __ImageBase;
// Base Settings Class Implementation
FileExplorerPreviewSettings::FileExplorerPreviewSettings(bool toggleSettingEnabled, const std::wstring& toggleSettingName, const std::wstring& toggleSettingDescription, LPCWSTR clsid, const std::wstring& registryValueData, RegistryWrapperIface* registryWrapper) :
m_toggleSettingEnabled(toggleSettingEnabled),
m_toggleSettingName(toggleSettingName),
m_toggleSettingDescription(toggleSettingDescription),
m_clsid(clsid),
m_registryValueData(registryValueData),
m_registryWrapper(registryWrapper)
{
}
FileExplorerPreviewSettings::~FileExplorerPreviewSettings()
{
if (this->m_registryWrapper != NULL)
{
delete this->m_registryWrapper;
}
}
bool FileExplorerPreviewSettings::GetToggleSettingState() const
{
return this->m_toggleSettingEnabled;
}
void FileExplorerPreviewSettings::UpdateToggleSettingState(bool state)
{
this->m_toggleSettingEnabled = state;
}
std::wstring FileExplorerPreviewSettings::GetToggleSettingName() const
{
return this->m_toggleSettingName;
}
std::wstring FileExplorerPreviewSettings::GetToggleSettingDescription() const
{
return this->m_toggleSettingDescription;
}
LPCWSTR FileExplorerPreviewSettings::GetCLSID() const
{
return this->m_clsid;
}
std::wstring FileExplorerPreviewSettings::GetRegistryValueData() const
{
return this->m_registryValueData;
}
// Load initial state of the file explorer module. If no inital state present initialize setting with default value.
void FileExplorerPreviewSettings::LoadState(PowerToysSettings::PowerToyValues& settings)
{
auto toggle = settings.get_bool_value(this->GetToggleSettingName());
if (toggle)
{
// If no existing setting found leave the default initialization value.
this->UpdateToggleSettingState(*toggle);
}
}
// Manage change in state of file explorer module settings.
bool FileExplorerPreviewSettings::UpdateState(PowerToysSettings::PowerToyValues& settings, bool enabled, bool isElevated)
{
auto toggle = settings.get_bool_value(this->GetToggleSettingName());
if (toggle)
{
auto lastState = this->GetToggleSettingState();
auto newState = *toggle;
if (lastState != newState)
{
this->UpdateToggleSettingState(newState);
// If global setting is enable. Add or remove the file explorer module otherwise just change the UI and save the updated config.
if (enabled)
{
// Check if the registry state does not match the new state, registry needs to be modified
if (this->CheckRegistryState() != newState)
{
if (isElevated)
{
LONG err;
if (lastState)
{
err = this->Disable();
}
else
{
err = this->Enable();
}
if (err == ERROR_SUCCESS)
{
Trace::PowerPreviewSettingsUpdated(this->GetToggleSettingName().c_str(), lastState, newState, enabled);
}
else
{
Trace::PowerPreviewSettingsUpdateFailed(this->GetToggleSettingName().c_str(), lastState, newState, enabled);
}
}
// If process is not elevated, return false as it is not possible to update the registry
else
{
return false;
}
}
// If it matches the new state, no update to registry is required
}
else
{
Trace::PowerPreviewSettingsUpdated(this->GetToggleSettingName().c_str(), lastState, newState, enabled);
}
}
}
return true;
}
}