From b530aa9afd615c3a23e5f9ddad928b6a0e7965c9 Mon Sep 17 00:00:00 2001 From: Robin Date: Sat, 9 Jun 2018 14:07:48 +0200 Subject: [PATCH] Small fixes for loading the configuration correctly, and correcting values when needed. --- .../ExternalCommandAddonModule.cs | 4 + .../ExternalCommandDestinationProvider.cs | 4 - src/Greenshot.Addons/AddonsModule.cs | 4 + .../Controls/SaveImageFileDialog.cs | 2 +- .../Core/CoreConfigurationExtensions.cs | 135 +++--------------- src/Greenshot.Addons/Core/ImageOutput.cs | 1 + src/Greenshot/Destinations/FileDestination.cs | 11 +- src/Greenshot/Forms/MainForm.cs | 5 +- 8 files changed, 32 insertions(+), 134 deletions(-) diff --git a/src/Greenshot.Addon.ExternalCommand/ExternalCommandAddonModule.cs b/src/Greenshot.Addon.ExternalCommand/ExternalCommandAddonModule.cs index b96e83833..8fbea7253 100644 --- a/src/Greenshot.Addon.ExternalCommand/ExternalCommandAddonModule.cs +++ b/src/Greenshot.Addon.ExternalCommand/ExternalCommandAddonModule.cs @@ -24,6 +24,7 @@ using Autofac; using Dapplo.Addons; using Dapplo.CaliburnMicro.Configuration; +using Dapplo.Ini; using Greenshot.Addon.ExternalCommand.ViewModels; using Greenshot.Addons.Components; @@ -47,6 +48,9 @@ namespace Greenshot.Addon.ExternalCommand .RegisterType() .AsSelf() .SingleInstance(); + + IniConfig.Current.AfterLoad(externalCommandConfiguration => externalCommandConfiguration.AfterLoad()); + base.Load(builder); } diff --git a/src/Greenshot.Addon.ExternalCommand/ExternalCommandDestinationProvider.cs b/src/Greenshot.Addon.ExternalCommand/ExternalCommandDestinationProvider.cs index 88d58b8ec..07bae7067 100644 --- a/src/Greenshot.Addon.ExternalCommand/ExternalCommandDestinationProvider.cs +++ b/src/Greenshot.Addon.ExternalCommand/ExternalCommandDestinationProvider.cs @@ -55,8 +55,6 @@ namespace Greenshot.Addon.ExternalCommand _externalCommandConfig = externalCommandConfiguration; _coreConfiguration = coreConfiguration; _greenshotLanguage = greenshotLanguage; - - externalCommandConfiguration.AfterLoad(); } public IEnumerable> Provide() @@ -109,8 +107,6 @@ namespace Greenshot.Addon.ExternalCommand { _externalCommandConfig.Delete(command); } - - _externalCommandConfig.AfterLoad(); } } } \ No newline at end of file diff --git a/src/Greenshot.Addons/AddonsModule.cs b/src/Greenshot.Addons/AddonsModule.cs index ce65be0ed..fbbf485d6 100644 --- a/src/Greenshot.Addons/AddonsModule.cs +++ b/src/Greenshot.Addons/AddonsModule.cs @@ -23,8 +23,10 @@ using Autofac; using Dapplo.Addons; +using Dapplo.Ini; using Greenshot.Addons.Components; using Greenshot.Addons.Controls; +using Greenshot.Addons.Core; using Greenshot.Addons.ViewModels; namespace Greenshot.Addons @@ -45,6 +47,8 @@ namespace Greenshot.Addons .RegisterType() .AsSelf(); + // REgister the after load, so it's called when the configuration is created + IniConfig.Current.AfterLoad(coreConfiguration => coreConfiguration.AfterLoad()); base.Load(builder); } } diff --git a/src/Greenshot.Addons/Controls/SaveImageFileDialog.cs b/src/Greenshot.Addons/Controls/SaveImageFileDialog.cs index 21c2ea44f..f70436825 100644 --- a/src/Greenshot.Addons/Controls/SaveImageFileDialog.cs +++ b/src/Greenshot.Addons/Controls/SaveImageFileDialog.cs @@ -145,7 +145,7 @@ namespace Greenshot.Addons.Controls string initialDirectory = null; try { - conf.ValidateAndCorrectOutputFileAsFullpath(); + conf.ValidateAndCorrect(); initialDirectory = Path.GetDirectoryName(conf.OutputFileAsFullpath); } catch diff --git a/src/Greenshot.Addons/Core/CoreConfigurationExtensions.cs b/src/Greenshot.Addons/Core/CoreConfigurationExtensions.cs index d9bbec239..763c8c1f1 100644 --- a/src/Greenshot.Addons/Core/CoreConfigurationExtensions.cs +++ b/src/Greenshot.Addons/Core/CoreConfigurationExtensions.cs @@ -25,12 +25,9 @@ using System; using System.Collections.Generic; -using System.Drawing; using System.IO; using System.Linq; using System.Reflection; -using System.Windows.Forms; -using Dapplo.Log; using Greenshot.Addons.Core.Enums; #endregion @@ -42,136 +39,34 @@ namespace Greenshot.Addons.Core /// public static class CoreConfigurationExtensions { - private static readonly LogSource Log = new LogSource(); - /// - /// Validate the OutputFilePath, and if this is not correct it will be set to the default - /// Added for BUG-1992, reset the OutputFilePath / OutputFileAsFullpath if they don't exist (e.g. the configuration is - /// used on a different PC) + /// Validate the values in the ICoreConfiguration, correct them where needed /// - public static void ValidateAndCorrectOutputFilePath(this ICoreConfiguration coreConfiguration) + public static void ValidateAndCorrect(this ICoreConfiguration coreConfiguration) { + if (string.IsNullOrEmpty(coreConfiguration.OutputFileFilenamePattern)) + { + coreConfiguration.RestoreToDefault(nameof(ICoreConfiguration.OutputFileFilenamePattern)); + } if (!Directory.Exists(coreConfiguration.OutputFilePath)) { - coreConfiguration.RestoreToDefault("OutputFilePath"); + coreConfiguration.RestoreToDefault(nameof(ICoreConfiguration.OutputFilePath)); + } + if (string.IsNullOrEmpty(coreConfiguration.OutputFilePath)) + { + coreConfiguration.OutputFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); } - } - - /// - /// Validate the OutputFileAsFullpath, and if this is not correct it will be set to the default - /// Added for BUG-1992, reset the OutputFilePath / OutputFileAsFullpath if they don't exist (e.g. the configuration is - /// used on a different PC) - /// - public static void ValidateAndCorrectOutputFileAsFullpath(this ICoreConfiguration coreConfiguration) - { var outputFilePath = Path.GetDirectoryName(coreConfiguration.OutputFileAsFullpath); if (outputFilePath == null || !File.Exists(coreConfiguration.OutputFileAsFullpath) && !Directory.Exists(outputFilePath)) { - coreConfiguration.RestoreToDefault("OutputFileAsFullpath"); + coreConfiguration.RestoreToDefault(nameof(ICoreConfiguration.OutputFileAsFullpath)); } } /// - /// Specifies what THIS build is - /// - public static BuildStates BuildState - { - get - { - var informationalVersion = Application.ProductVersion; - if (informationalVersion == null) - { - return BuildStates.RELEASE; - } - - if (informationalVersion.ToLowerInvariant().Contains("-rc")) - { - return BuildStates.RELEASE_CANDIDATE; - } - if (informationalVersion.ToLowerInvariant().Contains("-alpha")) - { - return BuildStates.ALPHA; - } - if (informationalVersion.ToLowerInvariant().Contains("-beta")) - { - return BuildStates.BETA; - } - return BuildStates.RELEASE; - } - } - - /// - /// Supply values we can't put as defaults - /// - /// ICoreConfiguration - /// The property to return a default for - /// object with the default value for the supplied property - public static object GetDefault(this ICoreConfiguration coreConfiguration, string property) - { - switch (property) - { - case "OutputFileAsFullpath": - if (coreConfiguration.IsPortable) - { - return Path.Combine(Application.StartupPath, @"..\..\Documents\Pictures\Greenshots\dummy.png"); - } - return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "dummy.png"); - case "OutputFilePath": - if (!coreConfiguration.IsPortable) - { - return Environment.GetFolderPath(Environment.SpecialFolder.Desktop); - } - - var pafOutputFilePath = Path.Combine(Application.StartupPath, @"..\..\Documents\Pictures\Greenshots"); - if (!Directory.Exists(pafOutputFilePath)) - { - try - { - Directory.CreateDirectory(pafOutputFilePath); - return pafOutputFilePath; - } - catch (Exception ex) - { - Log.Warn().WriteLine(ex); - // Problem creating directory, fallback to Desktop - } - } - else - { - return pafOutputFilePath; - } - return Environment.GetFolderPath(Environment.SpecialFolder.Desktop); - case "DWMBackgroundColor": - return Color.Transparent; - case "ActiveTitleFixes": - return new List { "Firefox", "IE", "Chrome" }; - case "TitleFixMatcher": - return new Dictionary { { "Firefox", " - Mozilla Firefox.*" }, { "IE", " - (Microsoft|Windows) Internet Explorer.*" }, { "Chrome", " - Google Chrome.*" } }; - case "TitleFixReplacer": - return new Dictionary { { "Firefox", "" }, { "IE", "" }, { "Chrome", "" } }; - } - return null; - } - - /// - /// This method will be called before writing the configuration - /// - public static void BeforeSave(this ICoreConfiguration coreConfiguration) - { - try - { - // Store version, this can be used later to fix settings after an update - coreConfiguration.LastSaveWithVersion = Assembly.GetEntryAssembly().GetName().Version.ToString(); - } - catch - { - // ignored - } - } - - /// - /// This method will be called after reading the configuration, so eventually some corrections can be made + /// This method will be called after reading the configuration, so eventually some corrections can be made /// + /// public static void AfterLoad(this ICoreConfiguration coreConfiguration) { // Comment with releases @@ -298,6 +193,8 @@ namespace Greenshot.Addons.Core { coreConfiguration.WebRequestReadWriteTimeout = 100; } + + coreConfiguration.ValidateAndCorrect(); } } } \ No newline at end of file diff --git a/src/Greenshot.Addons/Core/ImageOutput.cs b/src/Greenshot.Addons/Core/ImageOutput.cs index 1b6783cfd..39b22a8ab 100644 --- a/src/Greenshot.Addons/Core/ImageOutput.cs +++ b/src/Greenshot.Addons/Core/ImageOutput.cs @@ -849,6 +849,7 @@ namespace Greenshot.Addons.Core SaveToStream(surface, stream, outputSettings); } + // TODO: This should not be done here, remove this!! if (copyPathToClipboard) { using (var clipboardAccessToken = ClipboardNative.Access()) diff --git a/src/Greenshot/Destinations/FileDestination.cs b/src/Greenshot/Destinations/FileDestination.cs index abe0ad45f..46ae4465b 100644 --- a/src/Greenshot/Destinations/FileDestination.cs +++ b/src/Greenshot/Destinations/FileDestination.cs @@ -132,13 +132,10 @@ namespace Greenshot.Destinations { string fullPath = null; Log.Info().WriteLine("Creating new filename"); - var pattern = CoreConfiguration.OutputFileFilenamePattern; - if (string.IsNullOrEmpty(pattern)) - { - pattern = "greenshot ${capturetime}"; - } - var filename = FilenameHelper.GetFilenameFromPattern(pattern, CoreConfiguration.OutputFileFormat, captureDetails); - CoreConfiguration.ValidateAndCorrectOutputFilePath(); + + CoreConfiguration.ValidateAndCorrect(); + + var filename = FilenameHelper.GetFilenameFromPattern(CoreConfiguration.OutputFileFilenamePattern, CoreConfiguration.OutputFileFormat, captureDetails); var filepath = FilenameHelper.FillVariables(CoreConfiguration.OutputFilePath, false); try { diff --git a/src/Greenshot/Forms/MainForm.cs b/src/Greenshot/Forms/MainForm.cs index cdb6c3d01..fc4c09287 100644 --- a/src/Greenshot/Forms/MainForm.cs +++ b/src/Greenshot/Forms/MainForm.cs @@ -275,7 +275,7 @@ namespace Greenshot.Forms Contextmenu_OpenRecent(this, null); break; case ClickActions.OPEN_LAST_IN_EDITOR: - _coreConfiguration.ValidateAndCorrectOutputFileAsFullpath(); + _coreConfiguration.ValidateAndCorrect(); if (File.Exists(_coreConfiguration.OutputFileAsFullpath)) { @@ -297,8 +297,7 @@ namespace Greenshot.Forms /// private void Contextmenu_OpenRecent(object sender, EventArgs eventArgs) { - _coreConfiguration.ValidateAndCorrectOutputFilePath(); - _coreConfiguration.ValidateAndCorrectOutputFileAsFullpath(); + _coreConfiguration.ValidateAndCorrect(); var path = _coreConfiguration.OutputFileAsFullpath; if (!File.Exists(path)) {