Small fixes for loading the configuration correctly, and correcting values when needed.

This commit is contained in:
Robin 2018-06-09 14:07:48 +02:00
commit b530aa9afd
8 changed files with 32 additions and 134 deletions

View file

@ -24,6 +24,7 @@
using Autofac; using Autofac;
using Dapplo.Addons; using Dapplo.Addons;
using Dapplo.CaliburnMicro.Configuration; using Dapplo.CaliburnMicro.Configuration;
using Dapplo.Ini;
using Greenshot.Addon.ExternalCommand.ViewModels; using Greenshot.Addon.ExternalCommand.ViewModels;
using Greenshot.Addons.Components; using Greenshot.Addons.Components;
@ -47,6 +48,9 @@ namespace Greenshot.Addon.ExternalCommand
.RegisterType<ExternalCommandMasterViewModel>() .RegisterType<ExternalCommandMasterViewModel>()
.AsSelf() .AsSelf()
.SingleInstance(); .SingleInstance();
IniConfig.Current.AfterLoad<IExternalCommandConfiguration>(externalCommandConfiguration => externalCommandConfiguration.AfterLoad());
base.Load(builder); base.Load(builder);
} }

View file

@ -55,8 +55,6 @@ namespace Greenshot.Addon.ExternalCommand
_externalCommandConfig = externalCommandConfiguration; _externalCommandConfig = externalCommandConfiguration;
_coreConfiguration = coreConfiguration; _coreConfiguration = coreConfiguration;
_greenshotLanguage = greenshotLanguage; _greenshotLanguage = greenshotLanguage;
externalCommandConfiguration.AfterLoad();
} }
public IEnumerable<Lazy<IDestination, DestinationAttribute>> Provide() public IEnumerable<Lazy<IDestination, DestinationAttribute>> Provide()
@ -109,8 +107,6 @@ namespace Greenshot.Addon.ExternalCommand
{ {
_externalCommandConfig.Delete(command); _externalCommandConfig.Delete(command);
} }
_externalCommandConfig.AfterLoad();
} }
} }
} }

View file

@ -23,8 +23,10 @@
using Autofac; using Autofac;
using Dapplo.Addons; using Dapplo.Addons;
using Dapplo.Ini;
using Greenshot.Addons.Components; using Greenshot.Addons.Components;
using Greenshot.Addons.Controls; using Greenshot.Addons.Controls;
using Greenshot.Addons.Core;
using Greenshot.Addons.ViewModels; using Greenshot.Addons.ViewModels;
namespace Greenshot.Addons namespace Greenshot.Addons
@ -45,6 +47,8 @@ namespace Greenshot.Addons
.RegisterType<PleaseWaitForm>() .RegisterType<PleaseWaitForm>()
.AsSelf(); .AsSelf();
// REgister the after load, so it's called when the configuration is created
IniConfig.Current.AfterLoad<ICoreConfiguration>(coreConfiguration => coreConfiguration.AfterLoad());
base.Load(builder); base.Load(builder);
} }
} }

View file

@ -145,7 +145,7 @@ namespace Greenshot.Addons.Controls
string initialDirectory = null; string initialDirectory = null;
try try
{ {
conf.ValidateAndCorrectOutputFileAsFullpath(); conf.ValidateAndCorrect();
initialDirectory = Path.GetDirectoryName(conf.OutputFileAsFullpath); initialDirectory = Path.GetDirectoryName(conf.OutputFileAsFullpath);
} }
catch catch

View file

@ -25,12 +25,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Windows.Forms;
using Dapplo.Log;
using Greenshot.Addons.Core.Enums; using Greenshot.Addons.Core.Enums;
#endregion #endregion
@ -42,136 +39,34 @@ namespace Greenshot.Addons.Core
/// </summary> /// </summary>
public static class CoreConfigurationExtensions public static class CoreConfigurationExtensions
{ {
private static readonly LogSource Log = new LogSource();
/// <summary> /// <summary>
/// Validate the OutputFilePath, and if this is not correct it will be set to the default /// Validate the values in the ICoreConfiguration, correct them where needed
/// Added for BUG-1992, reset the OutputFilePath / OutputFileAsFullpath if they don't exist (e.g. the configuration is
/// used on a different PC)
/// </summary> /// </summary>
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)) if (!Directory.Exists(coreConfiguration.OutputFilePath))
{ {
coreConfiguration.RestoreToDefault("OutputFilePath"); coreConfiguration.RestoreToDefault(nameof(ICoreConfiguration.OutputFilePath));
} }
} if (string.IsNullOrEmpty(coreConfiguration.OutputFilePath))
/// <summary>
/// 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)
/// </summary>
public static void ValidateAndCorrectOutputFileAsFullpath(this ICoreConfiguration coreConfiguration)
{ {
coreConfiguration.OutputFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
}
var outputFilePath = Path.GetDirectoryName(coreConfiguration.OutputFileAsFullpath); var outputFilePath = Path.GetDirectoryName(coreConfiguration.OutputFileAsFullpath);
if (outputFilePath == null || !File.Exists(coreConfiguration.OutputFileAsFullpath) && !Directory.Exists(outputFilePath)) if (outputFilePath == null || !File.Exists(coreConfiguration.OutputFileAsFullpath) && !Directory.Exists(outputFilePath))
{ {
coreConfiguration.RestoreToDefault("OutputFileAsFullpath"); coreConfiguration.RestoreToDefault(nameof(ICoreConfiguration.OutputFileAsFullpath));
}
}
/// <summary>
/// Specifies what THIS build is
/// </summary>
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;
}
}
/// <summary>
/// Supply values we can't put as defaults
/// </summary>
/// <param name="coreConfiguration">ICoreConfiguration</param>
/// <param name="property">The property to return a default for</param>
/// <returns>object with the default value for the supplied property</returns>
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<string> { "Firefox", "IE", "Chrome" };
case "TitleFixMatcher":
return new Dictionary<string, string> { { "Firefox", " - Mozilla Firefox.*" }, { "IE", " - (Microsoft|Windows) Internet Explorer.*" }, { "Chrome", " - Google Chrome.*" } };
case "TitleFixReplacer":
return new Dictionary<string, string> { { "Firefox", "" }, { "IE", "" }, { "Chrome", "" } };
}
return null;
}
/// <summary>
/// This method will be called before writing the configuration
/// </summary>
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
} }
} }
/// <summary> /// <summary>
/// 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
/// </summary> /// </summary>
/// <param name="coreConfiguration"></param>
public static void AfterLoad(this ICoreConfiguration coreConfiguration) public static void AfterLoad(this ICoreConfiguration coreConfiguration)
{ {
// Comment with releases // Comment with releases
@ -298,6 +193,8 @@ namespace Greenshot.Addons.Core
{ {
coreConfiguration.WebRequestReadWriteTimeout = 100; coreConfiguration.WebRequestReadWriteTimeout = 100;
} }
coreConfiguration.ValidateAndCorrect();
} }
} }
} }

View file

@ -849,6 +849,7 @@ namespace Greenshot.Addons.Core
SaveToStream(surface, stream, outputSettings); SaveToStream(surface, stream, outputSettings);
} }
// TODO: This should not be done here, remove this!!
if (copyPathToClipboard) if (copyPathToClipboard)
{ {
using (var clipboardAccessToken = ClipboardNative.Access()) using (var clipboardAccessToken = ClipboardNative.Access())

View file

@ -132,13 +132,10 @@ namespace Greenshot.Destinations
{ {
string fullPath = null; string fullPath = null;
Log.Info().WriteLine("Creating new filename"); Log.Info().WriteLine("Creating new filename");
var pattern = CoreConfiguration.OutputFileFilenamePattern;
if (string.IsNullOrEmpty(pattern)) CoreConfiguration.ValidateAndCorrect();
{
pattern = "greenshot ${capturetime}"; var filename = FilenameHelper.GetFilenameFromPattern(CoreConfiguration.OutputFileFilenamePattern, CoreConfiguration.OutputFileFormat, captureDetails);
}
var filename = FilenameHelper.GetFilenameFromPattern(pattern, CoreConfiguration.OutputFileFormat, captureDetails);
CoreConfiguration.ValidateAndCorrectOutputFilePath();
var filepath = FilenameHelper.FillVariables(CoreConfiguration.OutputFilePath, false); var filepath = FilenameHelper.FillVariables(CoreConfiguration.OutputFilePath, false);
try try
{ {

View file

@ -275,7 +275,7 @@ namespace Greenshot.Forms
Contextmenu_OpenRecent(this, null); Contextmenu_OpenRecent(this, null);
break; break;
case ClickActions.OPEN_LAST_IN_EDITOR: case ClickActions.OPEN_LAST_IN_EDITOR:
_coreConfiguration.ValidateAndCorrectOutputFileAsFullpath(); _coreConfiguration.ValidateAndCorrect();
if (File.Exists(_coreConfiguration.OutputFileAsFullpath)) if (File.Exists(_coreConfiguration.OutputFileAsFullpath))
{ {
@ -297,8 +297,7 @@ namespace Greenshot.Forms
/// </summary> /// </summary>
private void Contextmenu_OpenRecent(object sender, EventArgs eventArgs) private void Contextmenu_OpenRecent(object sender, EventArgs eventArgs)
{ {
_coreConfiguration.ValidateAndCorrectOutputFilePath(); _coreConfiguration.ValidateAndCorrect();
_coreConfiguration.ValidateAndCorrectOutputFileAsFullpath();
var path = _coreConfiguration.OutputFileAsFullpath; var path = _coreConfiguration.OutputFileAsFullpath;
if (!File.Exists(path)) if (!File.Exists(path))
{ {