PowerToys/src/settings-ui/Settings.UI/ViewModels/AwakeViewModel.cs
Jaime Bernardo a63288009a
[GPO] Add policies for configuring utilities enabled states (#21411)
* Add GPOWrapper headers and C++/WinRT library

* Check GPO before starting utilities

* Show message on GPO having disabled preview panes.

* Don't generate thumbnails if GPO disabled

* Fix FancyZonesEditor unable to recognize GPOWrapper

* Move settings view models to the settings project

* Use GPO to block enabling utilities in Settings

* Hide context menu entries when gpo disables utilities

* Apply gpo policies when enabling PowerToys on runner

* Add version and metadata to dll

* Add GPOWrapper to the installer

* Fix MSBuild errors on WPF apps by using Projection

* Signing

* Add gpo files and publish them

* Add GPO policies to the bug report tool

* Add some documentation for using GPO

* Mention support to actual lowest supported version of Windows

* Move PowerToys to the root of administrative templates tree

* Save policies on Software\Policies\PowerToys

* Support both machine and user scopes

* Fix documentation to reference computer and user scopes

* Mention incompatibility with outlook in gpo

* Set a better folder structure for gpo assets

* Move PDF Handler warning to the description

* Update doc/gpo/README.md

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

* Add actual minimum version of PowerToys to gpo files

* Fix identation

* Remove GPOWrapper Readme

* Add Active Directory instructions to doc

Co-authored-by: Heiko <61519853+htcfreek@users.noreply.github.com>
2022-10-26 14:02:31 +01:00

184 lines
5.9 KiB
C#

// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.CompilerServices;
using global::PowerToys.GPOWrapper;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class AwakeViewModel : Observable
{
private GeneralSettings GeneralSettingsConfig { get; set; }
private AwakeSettings Settings { get; set; }
private Func<string, int> SendConfigMSG { get; }
public AwakeViewModel(ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<AwakeSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc)
{
// To obtain the general settings configurations of PowerToys Settings.
if (settingsRepository == null)
{
throw new ArgumentNullException(nameof(settingsRepository));
}
GeneralSettingsConfig = settingsRepository.SettingsConfig;
// To obtain the settings configurations of Fancy zones.
if (moduleSettingsRepository == null)
{
throw new ArgumentNullException(nameof(moduleSettingsRepository));
}
Settings = moduleSettingsRepository.SettingsConfig;
_enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredAwakeEnabledValue();
if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
{
// Get the enabled state from GPO.
_enabledStateIsGPOConfigured = true;
_isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
}
else
{
_isEnabled = GeneralSettingsConfig.Enabled.Awake;
}
_keepDisplayOn = Settings.Properties.KeepDisplayOn;
_mode = Settings.Properties.Mode;
_hours = Settings.Properties.Hours;
_minutes = Settings.Properties.Minutes;
// set the callback functions value to hangle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc;
}
public bool IsEnabled
{
get => _isEnabled;
set
{
if (_isEnabled != value)
{
if (_enabledStateIsGPOConfigured)
{
// If it's GPO configured, shouldn't be able to change this state.
return;
}
_isEnabled = value;
GeneralSettingsConfig.Enabled.Awake = value;
OnPropertyChanged(nameof(IsEnabled));
OnPropertyChanged(nameof(IsTimeConfigurationEnabled));
OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig);
SendConfigMSG(outgoing.ToString());
NotifyPropertyChanged();
}
}
}
public bool IsEnabledGpoConfigured
{
get => _enabledStateIsGPOConfigured;
}
public bool IsTimeConfigurationEnabled
{
get => _mode == AwakeMode.TIMED && _isEnabled;
}
public AwakeMode Mode
{
get => _mode;
set
{
if (_mode != value)
{
_mode = value;
OnPropertyChanged(nameof(Mode));
OnPropertyChanged(nameof(IsTimeConfigurationEnabled));
Settings.Properties.Mode = value;
NotifyPropertyChanged();
}
}
}
public bool KeepDisplayOn
{
get => _keepDisplayOn;
set
{
if (_keepDisplayOn != value)
{
_keepDisplayOn = value;
OnPropertyChanged(nameof(KeepDisplayOn));
Settings.Properties.KeepDisplayOn = value;
NotifyPropertyChanged();
}
}
}
public uint Hours
{
get => _hours;
set
{
if (_hours != value)
{
_hours = value;
OnPropertyChanged(nameof(Hours));
Settings.Properties.Hours = value;
NotifyPropertyChanged();
}
}
}
public uint Minutes
{
get => _minutes;
set
{
if (_minutes != value)
{
_minutes = value;
OnPropertyChanged(nameof(Minutes));
Settings.Properties.Minutes = value;
NotifyPropertyChanged();
}
}
}
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
OnPropertyChanged(propertyName);
if (SendConfigMSG != null)
{
SndAwakeSettings outsettings = new SndAwakeSettings(Settings);
SndModuleSettings<SndAwakeSettings> ipcMessage = new SndModuleSettings<SndAwakeSettings>(outsettings);
string targetMessage = ipcMessage.ToJsonString();
SendConfigMSG(targetMessage);
}
}
private GpoRuleConfigured _enabledGpoRuleConfiguration;
private bool _enabledStateIsGPOConfigured;
private bool _isEnabled;
private uint _hours;
private uint _minutes;
private bool _keepDisplayOn;
private AwakeMode _mode;
}
}