2020-05-12 18:43:37 +02:00
|
|
|
|
using System;
|
|
|
|
|
using Windows.ApplicationModel.DataTransfer;
|
2020-05-26 12:30:52 +02:00
|
|
|
|
using Windows.UI.Core;
|
2017-11-07 11:45:02 +01:00
|
|
|
|
using Windows.UI.Xaml;
|
2020-05-12 18:43:37 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-11-07 11:45:02 +01:00
|
|
|
|
using Microsoft.Xaml.Interactivity;
|
2020-05-12 18:43:37 +02:00
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
|
|
|
|
using ModernKeePass.Common;
|
2017-11-07 11:45:02 +01:00
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.Actions
|
|
|
|
|
{
|
|
|
|
|
public class ClipboardAction : DependencyObject, IAction
|
|
|
|
|
{
|
2020-05-26 12:30:52 +02:00
|
|
|
|
private bool _isWindowActivated = true;
|
2020-05-12 18:43:37 +02:00
|
|
|
|
|
2017-11-07 11:45:02 +01:00
|
|
|
|
public string Text
|
|
|
|
|
{
|
|
|
|
|
get { return (string)GetValue(TextProperty); }
|
|
|
|
|
set { SetValue(TextProperty, value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty TextProperty =
|
2020-05-18 14:14:28 +02:00
|
|
|
|
DependencyProperty.Register(nameof(Text), typeof(string), typeof(ClipboardAction), new PropertyMetadata(string.Empty));
|
|
|
|
|
public bool IsProtected
|
|
|
|
|
{
|
|
|
|
|
get { return (bool)GetValue(IsProtectedProperty); }
|
|
|
|
|
set { SetValue(IsProtectedProperty, value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty IsProtectedProperty =
|
|
|
|
|
DependencyProperty.Register(nameof(IsProtected), typeof(bool), typeof(ClipboardAction), new PropertyMetadata(false));
|
2017-11-07 11:45:02 +01:00
|
|
|
|
|
|
|
|
|
public object Execute(object sender, object parameter)
|
|
|
|
|
{
|
2018-09-12 16:49:01 +02:00
|
|
|
|
if (string.IsNullOrEmpty(Text)) return null;
|
2020-05-12 18:43:37 +02:00
|
|
|
|
|
|
|
|
|
var settings = App.Services.GetRequiredService<ISettingsProxy>();
|
2020-05-18 14:14:28 +02:00
|
|
|
|
var cryptography = App.Services.GetRequiredService<ICryptographyClient>();
|
2020-05-12 18:43:37 +02:00
|
|
|
|
|
2020-05-26 12:30:52 +02:00
|
|
|
|
CoreWindow.GetForCurrentThread().Activated += ClipboardAction_Activated;
|
|
|
|
|
|
2020-06-01 23:03:57 +02:00
|
|
|
|
var dispatcher = new DispatcherTimer {Interval = TimeSpan.FromSeconds(settings.GetSetting(Constants.Settings.ClipboardTimeout, 10))};
|
|
|
|
|
dispatcher.Tick += Dispatcher_Tick;
|
2020-05-12 18:43:37 +02:00
|
|
|
|
|
2017-11-07 11:45:02 +01:00
|
|
|
|
var dataPackage = new DataPackage { RequestedOperation = DataPackageOperation.Copy };
|
2020-05-18 14:14:28 +02:00
|
|
|
|
dataPackage.SetText(IsProtected ? cryptography.UnProtect(Text).GetAwaiter().GetResult() : Text);
|
2017-11-07 11:45:02 +01:00
|
|
|
|
Clipboard.SetContent(dataPackage);
|
2020-05-25 19:23:32 +02:00
|
|
|
|
Clipboard.Flush();
|
2020-06-01 23:03:57 +02:00
|
|
|
|
dispatcher.Start();
|
2020-05-12 18:43:37 +02:00
|
|
|
|
|
2017-11-07 11:45:02 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-05-12 18:43:37 +02:00
|
|
|
|
|
2020-05-26 12:30:52 +02:00
|
|
|
|
private void ClipboardAction_Activated(CoreWindow sender, WindowActivatedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
_isWindowActivated = args.WindowActivationState != CoreWindowActivationState.Deactivated;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 18:43:37 +02:00
|
|
|
|
private void Dispatcher_Tick(object sender, object e)
|
|
|
|
|
{
|
2020-05-26 12:30:52 +02:00
|
|
|
|
if (_isWindowActivated) Clipboard.SetContent(null);
|
2020-05-12 18:43:37 +02:00
|
|
|
|
}
|
2017-11-07 11:45:02 +01:00
|
|
|
|
}
|
|
|
|
|
}
|