2024-05-09 10:32:03 -04:00
// 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 ;
2024-08-22 16:17:12 +02:00
using System.Collections.Generic ;
using System.Collections.ObjectModel ;
using System.Collections.Specialized ;
using System.ComponentModel ;
2024-05-09 10:32:03 -04:00
using System.Globalization ;
2024-08-22 16:17:12 +02:00
using System.Linq ;
using System.Reflection ;
2024-05-09 10:32:03 -04:00
using System.Text.Json ;
2024-08-22 16:17:12 +02:00
using System.Text.Json.Serialization ;
2024-05-09 10:32:03 -04:00
using System.Timers ;
2024-10-17 05:14:57 -04:00
2024-05-09 10:32:03 -04:00
using global : : PowerToys . GPOWrapper ;
using Microsoft.PowerToys.Settings.UI.Library ;
using Microsoft.PowerToys.Settings.UI.Library.Helpers ;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces ;
using Microsoft.Win32 ;
using Windows.Security.Credentials ;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class AdvancedPasteViewModel : Observable , IDisposable
{
2024-08-22 16:17:12 +02:00
private static readonly HashSet < string > WarnHotkeys = [ "Ctrl + V" , "Ctrl + Shift + V" ] ;
2024-05-09 10:32:03 -04:00
private bool disposedValue ;
// Delay saving of settings in order to avoid calling save multiple times and hitting file in use exception. If there is no other request to save settings in given interval, we proceed to save it, otherwise we schedule saving it after this interval
private const int SaveSettingsDelayInMs = 500 ;
private GeneralSettings GeneralSettingsConfig { get ; set ; }
private readonly ISettingsUtils _settingsUtils ;
2024-11-13 12:36:45 -05:00
private readonly System . Threading . Lock _delayedActionLock = new System . Threading . Lock ( ) ;
2024-05-09 10:32:03 -04:00
private readonly AdvancedPasteSettings _advancedPasteSettings ;
2024-10-18 15:34:09 +02:00
private readonly AdvancedPasteAdditionalActions _additionalActions ;
2024-08-22 16:17:12 +02:00
private readonly ObservableCollection < AdvancedPasteCustomAction > _customActions ;
2024-05-09 10:32:03 -04:00
private Timer _delayedTimer ;
private GpoRuleConfigured _enabledGpoRuleConfiguration ;
private bool _enabledStateIsGPOConfigured ;
2024-05-26 13:22:50 +02:00
private GpoRuleConfigured _onlineAIModelsGpoRuleConfiguration ;
private bool _onlineAIModelsDisallowedByGPO ;
2024-05-09 10:32:03 -04:00
private bool _isEnabled ;
private Func < string , int > SendConfigMSG { get ; }
public AdvancedPasteViewModel (
ISettingsUtils settingsUtils ,
ISettingsRepository < GeneralSettings > settingsRepository ,
ISettingsRepository < AdvancedPasteSettings > advancedPasteSettingsRepository ,
Func < string , int > ipcMSGCallBackFunc )
{
// To obtain the general settings configurations of PowerToys Settings.
ArgumentNullException . ThrowIfNull ( settingsRepository ) ;
GeneralSettingsConfig = settingsRepository . SettingsConfig ;
// To obtain the settings configurations of Fancy zones.
ArgumentNullException . ThrowIfNull ( settingsRepository ) ;
_settingsUtils = settingsUtils ? ? throw new ArgumentNullException ( nameof ( settingsUtils ) ) ;
ArgumentNullException . ThrowIfNull ( advancedPasteSettingsRepository ) ;
_advancedPasteSettings = advancedPasteSettingsRepository . SettingsConfig ;
2024-10-18 15:34:09 +02:00
_additionalActions = _advancedPasteSettings . Properties . AdditionalActions ;
2024-08-22 16:17:12 +02:00
_customActions = _advancedPasteSettings . Properties . CustomActions . Value ;
2024-05-09 10:32:03 -04:00
InitializeEnabledValue ( ) ;
// set the callback functions value to handle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc ;
_delayedTimer = new Timer ( ) ;
_delayedTimer . Interval = SaveSettingsDelayInMs ;
_delayedTimer . Elapsed + = DelayedTimer_Tick ;
_delayedTimer . AutoReset = false ;
2024-08-22 16:17:12 +02:00
2024-10-18 15:34:09 +02:00
foreach ( var action in _additionalActions . AllActions )
{
action . PropertyChanged + = OnAdditionalActionPropertyChanged ;
}
2024-08-22 16:17:12 +02:00
foreach ( var customAction in _customActions )
{
customAction . PropertyChanged + = OnCustomActionPropertyChanged ;
}
_customActions . CollectionChanged + = OnCustomActionsCollectionChanged ;
UpdateCustomActionsCanMoveUpDown ( ) ;
2024-05-09 10:32:03 -04:00
}
private void InitializeEnabledValue ( )
{
_enabledGpoRuleConfiguration = GPOWrapper . GetConfiguredAdvancedPasteEnabledValue ( ) ;
if ( _enabledGpoRuleConfiguration = = GpoRuleConfigured . Disabled | | _enabledGpoRuleConfiguration = = GpoRuleConfigured . Enabled )
{
// Get the enabled state from GPO.
_enabledStateIsGPOConfigured = true ;
_isEnabled = _enabledGpoRuleConfiguration = = GpoRuleConfigured . Enabled ;
}
else
{
_isEnabled = GeneralSettingsConfig . Enabled . AdvancedPaste ;
}
2024-05-26 13:22:50 +02:00
_onlineAIModelsGpoRuleConfiguration = GPOWrapper . GetAllowedAdvancedPasteOnlineAIModelsValue ( ) ;
if ( _onlineAIModelsGpoRuleConfiguration = = GpoRuleConfigured . Disabled )
{
_onlineAIModelsDisallowedByGPO = true ;
// disable AI if it was enabled
DisableAI ( ) ;
}
2024-05-09 10:32:03 -04:00
}
public bool IsEnabled
{
get = > _isEnabled ;
set
{
if ( _enabledStateIsGPOConfigured )
{
// If it's GPO configured, shouldn't be able to change this state.
return ;
}
if ( _isEnabled ! = value )
{
_isEnabled = value ;
OnPropertyChanged ( nameof ( IsEnabled ) ) ;
2024-06-08 22:56:01 +02:00
OnPropertyChanged ( nameof ( ShowOnlineAIModelsGpoConfiguredInfoBar ) ) ;
OnPropertyChanged ( nameof ( ShowClipboardHistoryIsGpoConfiguredInfoBar ) ) ;
2024-05-09 10:32:03 -04:00
// Set the status of AdvancedPaste in the general settings
GeneralSettingsConfig . Enabled . AdvancedPaste = value ;
var outgoing = new OutGoingGeneralSettings ( GeneralSettingsConfig ) ;
SendConfigMSG ( outgoing . ToString ( ) ) ;
}
}
}
2024-08-22 16:17:12 +02:00
public ObservableCollection < AdvancedPasteCustomAction > CustomActions = > _customActions ;
2024-10-18 15:34:09 +02:00
public AdvancedPasteAdditionalActions AdditionalActions = > _additionalActions ;
2024-05-09 10:32:03 -04:00
private bool OpenAIKeyExists ( )
{
PasswordVault vault = new PasswordVault ( ) ;
PasswordCredential cred = null ;
try
{
cred = vault . Retrieve ( "https://platform.openai.com/api-keys" , "PowerToys_AdvancedPaste_OpenAIKey" ) ;
}
catch ( Exception )
{
return false ;
}
return cred is not null ;
}
2024-05-26 13:22:50 +02:00
public bool IsOpenAIEnabled = > OpenAIKeyExists ( ) & & ! IsOnlineAIModelsDisallowedByGPO ;
2024-05-09 10:32:03 -04:00
public bool IsEnabledGpoConfigured
{
get = > _enabledStateIsGPOConfigured ;
}
2024-05-26 13:22:50 +02:00
public bool IsOnlineAIModelsDisallowedByGPO
{
get = > _onlineAIModelsDisallowedByGPO | | _enabledGpoRuleConfiguration = = GpoRuleConfigured . Disabled ;
}
public bool ShowOnlineAIModelsGpoConfiguredInfoBar
{
2024-06-08 22:56:01 +02:00
get = > _onlineAIModelsDisallowedByGPO & & _isEnabled ;
2024-05-26 13:22:50 +02:00
}
2024-05-09 10:32:03 -04:00
private bool IsClipboardHistoryEnabled ( )
{
string registryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Clipboard\" ;
try
{
int enableClipboardHistory = ( int ) Registry . GetValue ( registryKey , "EnableClipboardHistory" , false ) ;
return enableClipboardHistory ! = 0 ;
}
catch ( Exception )
{
return false ;
}
}
2024-05-24 00:19:01 +01:00
private bool IsClipboardHistoryDisabledByGPO ( )
{
string registryKey = @"HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\System\" ;
try
{
object allowClipboardHistory = Registry . GetValue ( registryKey , "AllowClipboardHistory" , null ) ;
if ( allowClipboardHistory ! = null )
{
return ( int ) allowClipboardHistory = = 0 ;
}
else
{
return false ;
}
}
catch ( Exception )
{
return false ;
}
}
2024-05-09 10:32:03 -04:00
private void SetClipboardHistoryEnabled ( bool value )
{
string registryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Clipboard\" ;
try
{
Registry . SetValue ( registryKey , "EnableClipboardHistory" , value ? 1 : 0 ) ;
}
catch ( Exception )
{
}
}
public bool ClipboardHistoryEnabled
{
get = > IsClipboardHistoryEnabled ( ) ;
set
{
if ( IsClipboardHistoryEnabled ( ) ! = value )
{
SetClipboardHistoryEnabled ( value ) ;
}
}
}
2024-05-24 00:19:01 +01:00
public bool ClipboardHistoryDisabledByGPO
{
get = > IsClipboardHistoryDisabledByGPO ( ) ;
}
2024-06-08 22:56:01 +02:00
public bool ShowClipboardHistoryIsGpoConfiguredInfoBar
{
get = > IsClipboardHistoryDisabledByGPO ( ) & & _isEnabled ;
}
2024-05-09 10:32:03 -04:00
public HotkeySettings AdvancedPasteUIShortcut
{
get = > _advancedPasteSettings . Properties . AdvancedPasteUIShortcut ;
set
{
if ( _advancedPasteSettings . Properties . AdvancedPasteUIShortcut ! = value )
{
_advancedPasteSettings . Properties . AdvancedPasteUIShortcut = value ? ? AdvancedPasteProperties . DefaultAdvancedPasteUIShortcut ;
OnPropertyChanged ( nameof ( AdvancedPasteUIShortcut ) ) ;
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
2024-08-22 16:17:12 +02:00
SaveAndNotifySettings ( ) ;
2024-05-09 10:32:03 -04:00
}
}
}
public HotkeySettings PasteAsPlainTextShortcut
{
get = > _advancedPasteSettings . Properties . PasteAsPlainTextShortcut ;
set
{
if ( _advancedPasteSettings . Properties . PasteAsPlainTextShortcut ! = value )
{
_advancedPasteSettings . Properties . PasteAsPlainTextShortcut = value ? ? AdvancedPasteProperties . DefaultPasteAsPlainTextShortcut ;
OnPropertyChanged ( nameof ( PasteAsPlainTextShortcut ) ) ;
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
2024-08-22 16:17:12 +02:00
SaveAndNotifySettings ( ) ;
2024-05-09 10:32:03 -04:00
}
}
}
public HotkeySettings PasteAsMarkdownShortcut
{
get = > _advancedPasteSettings . Properties . PasteAsMarkdownShortcut ;
set
{
if ( _advancedPasteSettings . Properties . PasteAsMarkdownShortcut ! = value )
{
_advancedPasteSettings . Properties . PasteAsMarkdownShortcut = value ? ? new HotkeySettings ( ) ;
OnPropertyChanged ( nameof ( PasteAsMarkdownShortcut ) ) ;
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
2024-08-22 16:17:12 +02:00
SaveAndNotifySettings ( ) ;
2024-05-09 10:32:03 -04:00
}
}
}
public HotkeySettings PasteAsJsonShortcut
{
get = > _advancedPasteSettings . Properties . PasteAsJsonShortcut ;
set
{
if ( _advancedPasteSettings . Properties . PasteAsJsonShortcut ! = value )
{
_advancedPasteSettings . Properties . PasteAsJsonShortcut = value ? ? new HotkeySettings ( ) ;
OnPropertyChanged ( nameof ( PasteAsJsonShortcut ) ) ;
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
2024-08-22 16:17:12 +02:00
SaveAndNotifySettings ( ) ;
2024-05-09 10:32:03 -04:00
}
}
}
public bool ShowCustomPreview
{
get = > _advancedPasteSettings . Properties . ShowCustomPreview ;
set
{
if ( value ! = _advancedPasteSettings . Properties . ShowCustomPreview )
{
_advancedPasteSettings . Properties . ShowCustomPreview = value ;
NotifySettingsChanged ( ) ;
}
}
}
2024-06-24 16:03:46 +02:00
public bool CloseAfterLosingFocus
{
get = > _advancedPasteSettings . Properties . CloseAfterLosingFocus ;
set
{
if ( value ! = _advancedPasteSettings . Properties . CloseAfterLosingFocus )
{
_advancedPasteSettings . Properties . CloseAfterLosingFocus = value ;
NotifySettingsChanged ( ) ;
}
}
}
2024-10-18 15:34:09 +02:00
public bool IsConflictingCopyShortcut = >
_customActions . Select ( customAction = > customAction . Shortcut )
. Concat ( [ PasteAsPlainTextShortcut , AdvancedPasteUIShortcut , PasteAsMarkdownShortcut , PasteAsJsonShortcut ] )
. Any ( hotkey = > WarnHotkeys . Contains ( hotkey . ToString ( ) ) ) ;
public bool IsAdditionalActionConflictingCopyShortcut = >
_additionalActions . AllActions
. OfType < AdvancedPasteAdditionalAction > ( )
. Select ( additionalAction = > additionalAction . Shortcut )
. Any ( hotkey = > WarnHotkeys . Contains ( hotkey . ToString ( ) ) ) ;
2024-05-09 10:32:03 -04:00
private void DelayedTimer_Tick ( object sender , EventArgs e )
{
lock ( _delayedActionLock )
{
_delayedTimer . Stop ( ) ;
NotifySettingsChanged ( ) ;
}
}
private void NotifySettingsChanged ( )
{
// Using InvariantCulture as this is an IPC message
SendConfigMSG (
2024-06-24 16:03:46 +02:00
string . Format (
CultureInfo . InvariantCulture ,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}" ,
AdvancedPasteSettings . ModuleName ,
JsonSerializer . Serialize ( _advancedPasteSettings ) ) ) ;
2024-05-09 10:32:03 -04:00
}
public void RefreshEnabledState ( )
{
InitializeEnabledValue ( ) ;
OnPropertyChanged ( nameof ( IsEnabled ) ) ;
2024-06-08 22:56:01 +02:00
OnPropertyChanged ( nameof ( ShowOnlineAIModelsGpoConfiguredInfoBar ) ) ;
OnPropertyChanged ( nameof ( ShowClipboardHistoryIsGpoConfiguredInfoBar ) ) ;
2024-05-09 10:32:03 -04:00
}
protected virtual void Dispose ( bool disposing )
{
if ( ! disposedValue )
{
if ( disposing )
{
_delayedTimer . Dispose ( ) ;
}
disposedValue = true ;
}
}
public void Dispose ( )
{
Dispose ( disposing : true ) ;
GC . SuppressFinalize ( this ) ;
}
internal void DisableAI ( )
{
2024-05-26 13:22:50 +02:00
try
{
PasswordVault vault = new PasswordVault ( ) ;
PasswordCredential cred = vault . Retrieve ( "https://platform.openai.com/api-keys" , "PowerToys_AdvancedPaste_OpenAIKey" ) ;
vault . Remove ( cred ) ;
OnPropertyChanged ( nameof ( IsOpenAIEnabled ) ) ;
2024-09-24 19:16:20 +02:00
NotifySettingsChanged ( ) ;
2024-05-26 13:22:50 +02:00
}
catch ( Exception )
{
}
2024-05-09 10:32:03 -04:00
}
internal void EnableAI ( string password )
{
2024-05-26 13:22:50 +02:00
try
{
PasswordVault vault = new PasswordVault ( ) ;
PasswordCredential cred = new PasswordCredential ( "https://platform.openai.com/api-keys" , "PowerToys_AdvancedPaste_OpenAIKey" , password ) ;
vault . Add ( cred ) ;
OnPropertyChanged ( nameof ( IsOpenAIEnabled ) ) ;
2024-09-24 19:16:20 +02:00
NotifySettingsChanged ( ) ;
2024-05-26 13:22:50 +02:00
}
catch ( Exception )
{
}
2024-05-09 10:32:03 -04:00
}
2024-08-22 16:17:12 +02:00
internal AdvancedPasteCustomAction GetNewCustomAction ( string namePrefix )
{
ArgumentException . ThrowIfNullOrEmpty ( namePrefix ) ;
var maxUsedPrefix = _customActions . Select ( customAction = > customAction . Name )
. Where ( name = > name . StartsWith ( namePrefix , StringComparison . InvariantCulture ) )
. Select ( name = > int . TryParse ( name . AsSpan ( namePrefix . Length ) , out int number ) ? number : 0 )
. DefaultIfEmpty ( 0 )
. Max ( ) ;
var maxUsedId = _customActions . Select ( customAction = > customAction . Id )
. DefaultIfEmpty ( - 1 )
. Max ( ) ;
return new ( )
{
Id = maxUsedId + 1 ,
Name = $"{namePrefix} {maxUsedPrefix + 1}" ,
IsShown = true ,
} ;
}
internal void AddCustomAction ( AdvancedPasteCustomAction customAction )
{
if ( _customActions . Any ( existingCustomAction = > existingCustomAction . Id = = customAction . Id ) )
{
throw new ArgumentException ( "Duplicate custom action" , nameof ( customAction ) ) ;
}
_customActions . Add ( customAction ) ;
}
internal void DeleteCustomAction ( AdvancedPasteCustomAction customAction ) = > _customActions . Remove ( customAction ) ;
private void SaveCustomActions ( ) = > SaveAndNotifySettings ( ) ;
private void SaveAndNotifySettings ( )
{
_settingsUtils . SaveSettings ( _advancedPasteSettings . ToJsonString ( ) , AdvancedPasteSettings . ModuleName ) ;
NotifySettingsChanged ( ) ;
}
2024-10-18 15:34:09 +02:00
private void OnAdditionalActionPropertyChanged ( object sender , PropertyChangedEventArgs e )
{
SaveAndNotifySettings ( ) ;
if ( e . PropertyName = = nameof ( AdvancedPasteAdditionalAction . Shortcut ) )
{
OnPropertyChanged ( nameof ( IsAdditionalActionConflictingCopyShortcut ) ) ;
}
}
2024-08-22 16:17:12 +02:00
private void OnCustomActionPropertyChanged ( object sender , PropertyChangedEventArgs e )
{
if ( typeof ( AdvancedPasteCustomAction ) . GetProperty ( e . PropertyName ) . GetCustomAttribute < JsonIgnoreAttribute > ( ) = = null )
{
SaveCustomActions ( ) ;
}
if ( e . PropertyName = = nameof ( AdvancedPasteCustomAction . Shortcut ) )
{
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
}
}
private void OnCustomActionsCollectionChanged ( object sender , NotifyCollectionChangedEventArgs e )
{
void AddRange ( System . Collections . IList items )
{
foreach ( AdvancedPasteCustomAction item in items )
{
item . PropertyChanged + = OnCustomActionPropertyChanged ;
}
}
void RemoveRange ( System . Collections . IList items )
{
foreach ( AdvancedPasteCustomAction item in items )
{
item . PropertyChanged - = OnCustomActionPropertyChanged ;
}
}
switch ( e . Action )
{
case NotifyCollectionChangedAction . Add :
AddRange ( e . NewItems ) ;
break ;
case NotifyCollectionChangedAction . Remove :
RemoveRange ( e . OldItems ) ;
break ;
case NotifyCollectionChangedAction . Replace :
AddRange ( e . NewItems ) ;
RemoveRange ( e . OldItems ) ;
break ;
case NotifyCollectionChangedAction . Move :
break ;
default :
throw new ArgumentException ( $"Unsupported {nameof(e.Action)} {e.Action}" , nameof ( e ) ) ;
}
OnPropertyChanged ( nameof ( IsConflictingCopyShortcut ) ) ;
UpdateCustomActionsCanMoveUpDown ( ) ;
SaveCustomActions ( ) ;
}
private void UpdateCustomActionsCanMoveUpDown ( )
{
for ( int index = 0 ; index < _customActions . Count ; index + + )
{
var customAction = _customActions [ index ] ;
customAction . CanMoveUp = index ! = 0 ;
customAction . CanMoveDown = index ! = _customActions . Count - 1 ;
}
}
2024-05-09 10:32:03 -04:00
}
}