diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.Designer.cs b/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.Designer.cs index 47b47d3..c71bbc1 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.Designer.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.Designer.cs @@ -32,6 +32,7 @@ private void InitializeComponent() BackgroundWorkerDispatcher = new System.ComponentModel.BackgroundWorker(); labelInfo = new Label(); TimerOnlineStatus = new System.Windows.Forms.Timer(components); + TimeProcessAccessPolicyCheck = new System.Windows.Forms.Timer(components); SuspendLayout(); // // BackgroundWorkerDispatcher @@ -55,6 +56,10 @@ private void InitializeComponent() TimerOnlineStatus.Interval = 1000; TimerOnlineStatus.Tick += TimerOnlineStatus_Tick; // + // TimeProcessAccessPolicyCheck + // + TimeProcessAccessPolicyCheck.Tick += TimeProcessAccessPolicyCheck_Tick; + // // MainForm // AutoScaleDimensions = new SizeF(96F, 96F); @@ -64,7 +69,7 @@ private void InitializeComponent() ControlBox = false; Controls.Add(labelInfo); FormBorderStyle = FormBorderStyle.None; - Margin = new Padding(1, 1, 1, 1); + Margin = new Padding(1); MaximizeBox = false; MdiChildrenMinimizedAnchorBottom = false; MinimizeBox = false; @@ -84,5 +89,6 @@ private void InitializeComponent() private System.ComponentModel.BackgroundWorker BackgroundWorkerDispatcher; private Label labelInfo; private System.Windows.Forms.Timer TimerOnlineStatus; + private System.Windows.Forms.Timer TimeProcessAccessPolicyCheck; } } \ No newline at end of file diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.cs b/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.cs index 438e0b9..ea0ea02 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.cs @@ -1,7 +1,11 @@ using DnsClient.Internal; +using Magdys.ScreenPrivacyWatermark.App.Infrastructure.AccessPolicy; using Magdys.ScreenPrivacyWatermark.App.Infrastructure.Core; +using Magdys.ScreenPrivacyWatermark.App.Settings; using Magdys.ScreenPrivacyWatermark.App.Watermark; using System.ComponentModel; +using System.Diagnostics; +using System.Text.RegularExpressions; using static Magdys.ScreenPrivacyWatermark.App.NativeMethods; namespace Magdys.ScreenPrivacyWatermark.App.Forms; @@ -11,19 +15,19 @@ public partial class MainForm : Form, IMainForm private readonly ILogger _logger; private readonly IServiceProvider _serviceProvider; - - + private readonly ProcessAccessPolicyOptions _processAccessPolicyOptions; private readonly WatermarkManager _watermarkManager; private bool _isDisplaySettingsChanged = true; public MainForm(ILogger logger, IServiceProvider serviceProvider, - + ProcessAccessPolicyOptions processAccessPolicyOptions, WatermarkManager watermarkManager) { InitializeComponent(); _logger = logger; _serviceProvider = serviceProvider; + _processAccessPolicyOptions = processAccessPolicyOptions; _watermarkManager = watermarkManager; Microsoft.Win32.SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged; } @@ -54,6 +58,15 @@ private async void MainForm_Load(object sender, EventArgs e) { _logger.LogTrace("Executing {e}.", nameof(MainForm_Load)); + if (_processAccessPolicyOptions.AllowedProcessesList.Length > 0) + { + _logger.LogTrace("Process access policy check enabled."); + + _logger.LogTrace("Wildcard names is {EnableWildcardNames}.", _processAccessPolicyOptions.EnableWildcardNames ? "enabled" : "disabled"); + + TimeProcessAccessPolicyCheck.Enabled = true; + } + BackgroundWorkerDispatcher.RunWorkerAsync(); _logger.LogTrace("Executed {e}.", nameof(MainForm_Load)); @@ -71,14 +84,10 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e) case CloseReason.MdiFormClosing: case CloseReason.UserClosing: case CloseReason.TaskManagerClosing: - case CloseReason.ApplicationExitCall: + //case CloseReason.ApplicationExitCall: e.Cancel = true; _logger.LogDebug("Form closing CANCELLED"); break; - default: - _logger.LogDebug("Form closing CANCELLED"); - e.Cancel = true; - break; } _logger.LogTrace("Executed {e}.", nameof(MainForm_FormClosing)); @@ -149,4 +158,76 @@ private async void TimerOnlineStatus_Tick(object sender, EventArgs e) await ShowWatermarkFormsAsync(); _logger.LogTrace("Executed {e}.", nameof(TimerOnlineStatus_Tick)); } + + private void TimeProcessAccessPolicyCheck_Tick(object sender, EventArgs e) + { + +#if !(DEBUG) + _logger.LogTrace(_loggerExecutingText, nameof(TimeProcessPolicyCheck_Tick), Text); +#endif + if (_processAccessPolicyOptions.AllowedProcessesList.Length == 0) + { + return; + } + + // this feature is an expensive operation, so we need to measure the time it takes to execute + var timestamp = Stopwatch.GetTimestamp(); + + var allowedProcessList = _processAccessPolicyOptions.EnableWildcardNames + ? _processAccessPolicyOptions.AllowedProcessesList.Select(p => p.Replace(".", @"\.").Replace("*", ".*")).ToHashSet(StringComparer.OrdinalIgnoreCase) + : new HashSet(_processAccessPolicyOptions.AllowedProcessesList, StringComparer.OrdinalIgnoreCase); + + + var allProcessesWithWindows = Process + .GetProcesses() + .Where(p => p.MainWindowHandle != IntPtr.Zero) + .ToArray(); + + var filteredProcesses = new List(); + foreach (var processWithWindow in allProcessesWithWindows) + { + if (_processAccessPolicyOptions.EnableWildcardNames) + { + foreach (var allowedProcess in allowedProcessList) + { + + if (Regex.IsMatch(processWithWindow.ProcessName, allowedProcess, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(0.5))) + { + filteredProcesses.Add(processWithWindow); + } + } + } + else + { + if (allowedProcessList.Contains(processWithWindow.ProcessName)) + { + filteredProcesses.Add(processWithWindow); + } + } + } + + foreach (var process in filteredProcesses) + { + var isMinimized = IsIconic(process.MainWindowHandle); + if (!isMinimized) + { + ProcessAccessPolicyState.HideWatermark = false; + return; + } + + } + + ProcessAccessPolicyState.HideWatermark = true; + + + var elapsed = Stopwatch.GetElapsedTime(timestamp); + + _logger.LogDebug("Process access policy check took {elapsed} ms", elapsed.Milliseconds); + + +#if !(DEBUG) + _logger.LogTrace(_loggerExecutedText, nameof(TimeProcessPolicyCheck_Tick), Text); +#endif + + } } diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.resx b/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.resx index abb3525..3305286 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.resx +++ b/src/Magdys.ScreenPrivacyWatermark.App/Forms/MainForm.resx @@ -123,4 +123,7 @@ 229, 17 + + 380, 17 + \ No newline at end of file diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Forms/WatermarkForm.cs b/src/Magdys.ScreenPrivacyWatermark.App/Forms/WatermarkForm.cs index 6d6fa57..df67698 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Forms/WatermarkForm.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Forms/WatermarkForm.cs @@ -1,7 +1,6 @@ using Magdys.ScreenPrivacyWatermark.App.Infrastructure.AccessPolicy; -using Magdys.ScreenPrivacyWatermark.App.Settings; +using Magdys.ScreenPrivacyWatermark.App.Watermark.Options; using Microsoft.Extensions.Options; -using System.Diagnostics; using System.Drawing.Drawing2D; using System.Text; using static Magdys.ScreenPrivacyWatermark.App.NativeMethods; @@ -11,24 +10,18 @@ namespace Magdys.ScreenPrivacyWatermark.App.Forms; public partial class WatermarkForm : Form { private readonly ILogger _logger; - private readonly IOptions _watermarkFormatSetting; - - private readonly ProcessAccessPolicyOptions _processAccessPolicyOptions; - - + private readonly IOptions _watermarkFormatSetting; private bool _isFormClosingSubscribed = true; private bool _isInitialized = false; private WatermarkFormOptions _watermarkFormOptions = null; private const string _loggerExecutingText = "Executing {method} for '{text}'"; - private const string _loggerExecutedText = "Executed {method} for '{text}'"; - public WatermarkForm(ILogger logger, IOptions watermarkFormatSetting, ProcessAccessPolicyOptions _processAccessPolicyOptions) + public WatermarkForm(ILogger logger, IOptions watermarkFormatSetting) { InitializeComponent(); _logger = logger; _watermarkFormatSetting = watermarkFormatSetting; - this._processAccessPolicyOptions = _processAccessPolicyOptions; } protected override CreateParams CreateParams @@ -88,7 +81,7 @@ private void WatermarkForm_FormClosing(object sender, FormClosingEventArgs e) case CloseReason.MdiFormClosing: case CloseReason.UserClosing: case CloseReason.TaskManagerClosing: - case CloseReason.ApplicationExitCall: + //case CloseReason.ApplicationExitCall: e.Cancel = true; break; } @@ -104,13 +97,13 @@ private async void WatermarkForm_Load(object sender, EventArgs e) throw new InvalidOperationException($"Form is not initialized. You need to call {nameof(InitializeForm)} method first."); } - if (_processAccessPolicyOptions.AllowedProcessesList.Length != 0) + // Means the process access policy is enabled. + if (ProcessAccessPolicyState.HideWatermark.HasValue) { + _logger.LogDebug("Process Access Policy is enabled."); TimeProcessPolicyCheck.Enabled = true; } - - _logger.LogTrace(_loggerExecutedText, nameof(WatermarkForm_Load), Text); } @@ -192,121 +185,27 @@ private void DrawWatermarkText(Graphics graphics) _logger.LogTrace(_loggerExecutedText, nameof(DrawWatermarkText), Text); } - //private string GetFormattedWatermarkText(Graphics graphics) - //{ - // _logger.LogTrace(_loggerExecutingText, nameof(GetFormattedWatermarkText), Text); - - // string watermarkText = _watermarkFormOptions._watermarkText; - - // string spacer = new string(' ', _watermarkFormatSetting.Value.UseDynamicsSpacing ? new Random().Next(6, 15) : 10); - - // string watermarkBaseString = $"{watermarkText} {spacer}"; - - // _logger.LogDebug("Watermark text: {_watermarkText}", watermarkBaseString); - - // double width = _watermarkFormOptions.Screen.Bounds.Width; - // double height = _watermarkFormOptions.Screen.Bounds.Height; - // double diagonalSize = Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2)); - - // StringBuilder formatedText = new StringBuilder(); - // for (int i = 0; i < 100; i++) - // { - // formatedText.Append(watermarkBaseString); - // } - - // _logger.LogTrace(_loggerExecutedText, nameof(GetFormattedWatermarkText), Text); - - // return formatedText.ToString(); - //} - - //private void DrawWatermarkTextOld(Graphics graphics, string text) - //{ - // _logger.LogTrace(_loggerExecutingText, nameof(DrawWatermarkText), Text); - - // ArgumentNullException.ThrowIfNull(graphics); - - // if (string.IsNullOrEmpty(text)) - // { - // throw new ArgumentException("Watermark text cannot be NULL or empty.", nameof(text)); - // } - - // graphics.TextRenderingHint = _watermarkFormatSetting.Value.TextRender; - // graphics.SmoothingMode = SmoothingMode.Default; - // graphics.InterpolationMode = InterpolationMode.Default; - - // using var font = new Font(_watermarkFormatSetting.Value.FontName, _watermarkFormatSetting.Value.FontSize, FontStyle.Regular, GraphicsUnit.Point); - // using var brush = new SolidBrush(Color.FromArgb(255, _watermarkFormatSetting.Value.Color)); - // using var stringFormat = new StringFormat(StringFormatFlags.NoClip); - - - // graphics.ResetTransform(); - // if (_watermarkFormatSetting.Value.UseDiagonalLines) - // { - // graphics.RotateTransform(-45); - // } - - // double screenDiagonal = Math.Round(Math.Sqrt(Math.Pow(_watermarkFormOptions.Screen.WorkingArea.Width, 2) + Math.Pow(_watermarkFormOptions.Screen.WorkingArea.Height, 2))); - - // int repeatCounts = _watermarkFormatSetting.Value.LinesCount; - - // int x = _watermarkFormatSetting.Value.UseDiagonalLines ? (int)screenDiagonal : Bounds.Height; - - // int spacer = x / repeatCounts; - - // int firstPoint = _watermarkFormatSetting.Value.UseDynamicsSpacing ? new Random().Next(-2, 2) : 0; - - // for (int i = firstPoint; i < repeatCounts + 2 + firstPoint; i++) - // { - // var point = new Point(-spacer * i, spacer * i); - - // if (_watermarkFormatSetting.Value.OutlineColor == null) - // { - // graphics.DrawString(text, font, brush, point, stringFormat); - // } - // else - // { - // using GraphicsPath graphicPath = new GraphicsPath(); - // using Pen pen = new Pen(_watermarkFormatSetting.Value.OutlineColor.Value, _watermarkFormatSetting.Value.OutlineWidth); - // graphicPath.AddString(text, font.FontFamily, (int)font.Style, font.Size * 1.5f, point, stringFormat); - - // graphics.DrawPath(pen, graphicPath); - // graphics.FillPath(brush, graphicPath); - // } - // } - - // _logger.LogTrace(_loggerExecutedText, nameof(DrawWatermarkText), Text); - //} - private void TimeProcessPolicyCheck_Tick(object sender, EventArgs e) { #if !(DEBUG) _logger.LogTrace(_loggerExecutingText, nameof(TimeProcessPolicyCheck_Tick), Text); #endif - var allowedProcessesList = _processAccessPolicyOptions.AllowedProcessesList.Select(p => p.ToUpperInvariant()).ToArray(); - - if (allowedProcessesList.Length == 0) + if (ProcessAccessPolicyState.HideWatermark == null) { - Opacity = _watermarkFormatSetting.Value.OpacityF; + // this should not happen since the timer should be disabled if the HideWatermark is null return; } - var processes = Process - .GetProcesses() - .Where(p => allowedProcessesList.Contains(p.ProcessName.ToUpperInvariant()) && p.MainWindowHandle != IntPtr.Zero) - .ToArray(); - - foreach (var process in processes) + if (ProcessAccessPolicyState.HideWatermark.Value) { - var isMinimized = IsIconic(process.MainWindowHandle); - if (!isMinimized) - { - Opacity = _watermarkFormatSetting.Value.OpacityF; - return; - } + Opacity = 0f; + } + else + { + Opacity = _watermarkFormatSetting.Value.OpacityF; } - Opacity = 0f; #if !(DEBUG) _logger.LogTrace(_loggerExecutedText, nameof(TimeProcessPolicyCheck_Tick), Text); diff --git a/src/Magdys.ScreenPrivacyWatermark.App/GlobalUsings.cs b/src/Magdys.ScreenPrivacyWatermark.App/GlobalUsings.cs deleted file mode 100644 index 669fa48..0000000 --- a/src/Magdys.ScreenPrivacyWatermark.App/GlobalUsings.cs +++ /dev/null @@ -1,4 +0,0 @@ -global using Microsoft.Extensions.Configuration; -global using Microsoft.Extensions.DependencyInjection; -global using Microsoft.Extensions.Hosting; -global using Microsoft.Extensions.Logging; diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/AccessPolicyHostedService.cs b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/AccessPolicyHostedService.cs index e2c00bc..e60ae86 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/AccessPolicyHostedService.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/AccessPolicyHostedService.cs @@ -41,6 +41,13 @@ private async Task EvaulateAccessPolicies() .OrderBy(p => p.Order) .ToList(); + // if there is no policy enabled, grant access + if (policies.Count == 0) + { + logger.LogDebug("No enabled policies found. Granting access."); + return true; + } + switch (evaluationMode) { case EvaluationMode.Any: diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/MacAddressAccessPolicy.cs b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/MacAddressAccessPolicy.cs index 260f62d..b9d1834 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/MacAddressAccessPolicy.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/MacAddressAccessPolicy.cs @@ -8,7 +8,7 @@ internal class MacAddressAccessPolicy(ILogger logger, Ma public int Order => 002; - public async Task CheckAccessAsync() + public Task CheckAccessAsync() { logger.LogTrace("Executing {method}.", nameof(CheckAccessAsync)); @@ -27,6 +27,6 @@ public async Task CheckAccessAsync() logger.LogDebug("User {hasAccess} access based on Policy {PolicyName}", hasAccess ? "granted" : "denied", nameof(MacAddressAccessPolicy)); logger.LogTrace("Executed {method}.", nameof(CheckAccessAsync)); - return hasAccess; + return Task.FromResult(hasAccess); } } \ No newline at end of file diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/MacAddressAccessPolicyOptions.cs b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/MacAddressAccessPolicyOptions.cs index c7d16fa..70c7da9 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/MacAddressAccessPolicyOptions.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/MacAddressAccessPolicyOptions.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Magdys.ScreenPrivacyWatermark.App.Infrastructure.AccessPolicy; +namespace Magdys.ScreenPrivacyWatermark.App.Infrastructure.AccessPolicy; internal class MacAddressAccessPolicyOptions : IAccessPolicyOptions { diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/ProcessAccessPolicyOptions.cs b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/ProcessAccessPolicyOptions.cs index 1a12931..a9202b1 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/ProcessAccessPolicyOptions.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/ProcessAccessPolicyOptions.cs @@ -1,14 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Magdys.ScreenPrivacyWatermark.App.Infrastructure.AccessPolicy; +namespace Magdys.ScreenPrivacyWatermark.App.Infrastructure.AccessPolicy; public class ProcessAccessPolicyOptions : IAccessPolicyOptions { public string AllowedProcesses { get; set; } public string[] AllowedProcessesList => AllowedProcesses.SplitConfiguration(); + + public bool EnableWildcardNames { get; set; } } diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/ProcessAccessPolicyState.cs b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/ProcessAccessPolicyState.cs new file mode 100644 index 0000000..3b0ea3c --- /dev/null +++ b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/AccessPolicy/ProcessAccessPolicyState.cs @@ -0,0 +1,10 @@ +namespace Magdys.ScreenPrivacyWatermark.App.Infrastructure.AccessPolicy; + +internal static class ProcessAccessPolicyState +{ + /// + /// Gets or sets a value indicating whether the watermark should be hidden. + /// Null means is not configured or not enabled. + /// + public static bool? HideWatermark { get; set; } +} diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/Logging/LoggingExtensions.cs b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/Logging/LoggingExtensions.cs index fe7dd94..b5ebec8 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/Logging/LoggingExtensions.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/Logging/LoggingExtensions.cs @@ -49,7 +49,7 @@ public static LoggingConfiguration GetNLogDefaultConfig(NLog.LogLevel logLevel, var loggingConfiguration = new LoggingConfiguration(); var loggerName = "${callsite:includeNamespace=false}"; - var layout = $"${{longdate:universalTime=true}}|v${{assembly-version:type=File}}|${{machinename}}|${{environment-user}}|${{pad:padding=5:inner=${{processid}}}}|${{pad:padding=5:inner=${{level:uppercase=true}}}}|{loggerName}|${{message:withexception=true}}"; + var layout = $"${{longdate:universalTime=false}}|v${{assembly-version:type=File}}|${{machinename}}|${{environment-user}}|${{pad:padding=5:inner=${{processid}}}}|${{pad:padding=5:inner=${{level:uppercase=true}}}}|{loggerName}|${{message:withexception=true}}"; var logfile = new FileTarget("logfile") { @@ -61,7 +61,6 @@ public static LoggingConfiguration GetNLogDefaultConfig(NLog.LogLevel logLevel, ArchiveNumbering = ArchiveNumberingMode.Date, ArchiveDateFormat = "yyyyMMdd", MaxArchiveFiles = 60, - ConcurrentWrites = true, }; var appInsightsTarget = new ApplicationInsightsTarget() @@ -69,7 +68,6 @@ public static LoggingConfiguration GetNLogDefaultConfig(NLog.LogLevel logLevel, InstrumentationKey = "9eb42fac-0fe0-43e9-98a2-4f802b19bd36", Name = "appInsightsTarget", Layout = layout, - }; var contextProperties = new Dictionary diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/Logging/LoggingOptions.cs b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/Logging/LoggingOptions.cs index 899eac7..1bb3d60 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/Logging/LoggingOptions.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Infrastructure/Logging/LoggingOptions.cs @@ -5,6 +5,4 @@ internal class LoggingOptions public string LogLevelConfigKey { get; set; } = "app:LogLevel"; public NLog.LogLevel FallbackLogLevel { get; set; } = NLog.LogLevel.Info; - - public bool UseShortLoggerName { get; set; } = true; } diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Magdys.ScreenPrivacyWatermark.App.csproj b/src/Magdys.ScreenPrivacyWatermark.App/Magdys.ScreenPrivacyWatermark.App.csproj index 95c7635..2b56ce3 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Magdys.ScreenPrivacyWatermark.App.csproj +++ b/src/Magdys.ScreenPrivacyWatermark.App/Magdys.ScreenPrivacyWatermark.App.csproj @@ -35,6 +35,13 @@ en-US + + + + + + + @@ -46,9 +53,9 @@ - - - + + + diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Program.cs b/src/Magdys.ScreenPrivacyWatermark.App/Program.cs index d29954d..cbfc209 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Program.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Program.cs @@ -43,7 +43,6 @@ private static async Task Main(string[] args) .ConfigureAppConfiguration(args, loggerFactory.CreateLogger("Configuration")) .ConfigureLogging(options => { - options.UseShortLoggerName = false; #if DEBUG options.FallbackLogLevel = NLog.LogLevel.Trace; #endif @@ -63,8 +62,8 @@ private static async Task Main(string[] args) #endif }) .ConfigureSettings() - .ConfigureWatermark(logger) .ConfigureAccessPolicies(logger) + .ConfigureWatermark(logger) .ConfigureWinForms(options => { options.CloseMainFormOnly = false; diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Settings/WatermarkProviderSettings.cs b/src/Magdys.ScreenPrivacyWatermark.App/Settings/WatermarkProviderSettings.cs deleted file mode 100644 index f67bb30..0000000 --- a/src/Magdys.ScreenPrivacyWatermark.App/Settings/WatermarkProviderSettings.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace Magdys.ScreenPrivacyWatermark.App.Settings; - -public class WatermarkProviderSettings -{ - public enum WatermarkProvider - { - Local, - ActiveDirectory, - EntraID - } - - public const string SectionName = "Provider"; - - public WatermarkProvider Name { get; set; } = WatermarkProvider.Local; - - [Required] - public string WatermarkOnlinePattern { get; set; } = "{UserPrincipalName} ({JobTitle}) - {Date}"; - - [Required] - public string WatermarkOfflinePattern { get; set; } = "{UserName}"; - - public bool EnableCache { get; set; } = true; - - public List DataDateCultures { get; set; } = ["en-us", "ar-sa"]; -} - - diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Settings/WatermarkFormatSettings.cs b/src/Magdys.ScreenPrivacyWatermark.App/Watermark/Options/WatermarkFormatOptions.cs similarity index 86% rename from src/Magdys.ScreenPrivacyWatermark.App/Settings/WatermarkFormatSettings.cs rename to src/Magdys.ScreenPrivacyWatermark.App/Watermark/Options/WatermarkFormatOptions.cs index a7f6617..34bafcc 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Settings/WatermarkFormatSettings.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Watermark/Options/WatermarkFormatOptions.cs @@ -1,9 +1,9 @@ using System.ComponentModel.DataAnnotations; using System.Drawing.Text; -namespace Magdys.ScreenPrivacyWatermark.App.Settings; +namespace Magdys.ScreenPrivacyWatermark.App.Watermark.Options; -public class WatermarkFormatSettings +public class WatermarkFormatOptions { public const string SectionName = "Format"; @@ -37,7 +37,7 @@ public TextRenderingHint TextRender #if DEBUG set; // setter only while debugging #endif - } = TextRenderingHint.SystemDefault; + } = TextRenderingHint.SingleBitPerPixel; public bool UseDiagonalLines { diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Watermark/WatermarkOptions.cs b/src/Magdys.ScreenPrivacyWatermark.App/Watermark/Options/WatermarkLayoutOptions.cs similarity index 63% rename from src/Magdys.ScreenPrivacyWatermark.App/Watermark/WatermarkOptions.cs rename to src/Magdys.ScreenPrivacyWatermark.App/Watermark/Options/WatermarkLayoutOptions.cs index 5c9a1f2..0f4b872 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Watermark/WatermarkOptions.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Watermark/Options/WatermarkLayoutOptions.cs @@ -1,8 +1,8 @@ using System.ComponentModel.DataAnnotations; -namespace Magdys.ScreenPrivacyWatermark.App.Watermark; +namespace Magdys.ScreenPrivacyWatermark.App.Watermark.Options; -public class WatermarkOptions +public class WatermarkLayoutOptions { public const string SectionName = "Watermark"; @@ -12,5 +12,5 @@ public class WatermarkOptions [Required] public string DisconnectedPattern { get; set; } = "{UserName}"; - public bool EnableCache { get; set; } = true; + public bool EnableWatermarkTextCache { get; set; } = true; } diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Watermark/WatermarkExtensions.cs b/src/Magdys.ScreenPrivacyWatermark.App/Watermark/WatermarkExtensions.cs index a8b6e5e..9b31948 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Watermark/WatermarkExtensions.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Watermark/WatermarkExtensions.cs @@ -1,4 +1,5 @@ using Magdys.ScreenPrivacyWatermark.App.MSGraph; +using Magdys.ScreenPrivacyWatermark.App.Watermark.Options; using Magdys.ScreenPrivacyWatermark.App.Watermark.Sources; using System.Reflection; @@ -8,8 +9,13 @@ internal static class WatermarkExtensions { public static HostApplicationBuilder ConfigureWatermark(this HostApplicationBuilder hostApplicationBuilder, ILogger? logger = null) { - hostApplicationBuilder.Services.AddOptions() - .BindConfiguration(WatermarkOptions.SectionName) + hostApplicationBuilder.Services.AddOptions() + .BindConfiguration(WatermarkLayoutOptions.SectionName) + .ValidateDataAnnotations() + .ValidateOnStart(); + + hostApplicationBuilder.Services.AddOptions() + .BindConfiguration(WatermarkFormatOptions.SectionName) .ValidateDataAnnotations() .ValidateOnStart(); diff --git a/src/Magdys.ScreenPrivacyWatermark.App/Watermark/WatermarkManager.cs b/src/Magdys.ScreenPrivacyWatermark.App/Watermark/WatermarkManager.cs index 355d01a..458589b 100644 --- a/src/Magdys.ScreenPrivacyWatermark.App/Watermark/WatermarkManager.cs +++ b/src/Magdys.ScreenPrivacyWatermark.App/Watermark/WatermarkManager.cs @@ -1,4 +1,5 @@ -using Magdys.ScreenPrivacyWatermark.App.Watermark.Sources; +using Magdys.ScreenPrivacyWatermark.App.Watermark.Options; +using Magdys.ScreenPrivacyWatermark.App.Watermark.Sources; using Microsoft.Extensions.Options; using Polly; using Polly.Retry; @@ -12,7 +13,7 @@ namespace Magdys.ScreenPrivacyWatermark.App.Watermark; public class WatermarkManager( ILogger logger, IServiceProvider serviceProvider, - IOptions watermarkOptions) + IOptions watermarkOptions) { public Dictionary Data { get; } = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -174,7 +175,7 @@ await resiliencePipeline.ExecuteAsync(async context => watermarkText = watermarkTextBuilder.ToString(); - if (watermarkOptions.Value.EnableCache) + if (watermarkOptions.Value.EnableWatermarkTextCache) { logger.LogDebug("Watermark caching is enabled."); var watermarkTextBytes = Encoding.UTF8.GetBytes(watermarkText); @@ -185,7 +186,7 @@ await resiliencePipeline.ExecuteAsync(async context => } else { - if (watermarkOptions.Value.EnableCache) + if (watermarkOptions.Value.EnableWatermarkTextCache) { logger.LogDebug("Watermark caching is enabled."); diff --git a/src/ScreenPrivacyWatermark.sln b/src/ScreenPrivacyWatermark.sln index 5905596..89b13e2 100644 --- a/src/ScreenPrivacyWatermark.sln +++ b/src/ScreenPrivacyWatermark.sln @@ -50,7 +50,6 @@ Global {2742CE11-4343-4565-8326-23F7E440FFCA}.Release|x86.ActiveCfg = Release|Any CPU {2742CE11-4343-4565-8326-23F7E440FFCA}.Release|x86.Build.0 = Release|Any CPU {63CF4400-41EE-4F71-820D-DE85A641F234}.Debug|Any CPU.ActiveCfg = Debug|x64 - {63CF4400-41EE-4F71-820D-DE85A641F234}.Debug|Any CPU.Build.0 = Debug|x64 {63CF4400-41EE-4F71-820D-DE85A641F234}.Debug|ARM64.ActiveCfg = Debug|ARM64 {63CF4400-41EE-4F71-820D-DE85A641F234}.Debug|ARM64.Build.0 = Debug|ARM64 {63CF4400-41EE-4F71-820D-DE85A641F234}.Debug|x64.ActiveCfg = Debug|x64