From bb7f21656778a4d97969bb81b549d4656ea0f2ba Mon Sep 17 00:00:00 2001 From: RKrom Date: Tue, 24 Aug 2010 13:55:39 +0000 Subject: [PATCH] More refactoring for the new configuration framework git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@858 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- Greenshot-OCR-Plugin/OCRPlugin.cs | 4 +- .../Greenshot-TitleFix-Plugin.csproj | 3 +- .../Properties/AssemblyInfo.cs.template | 2 +- .../{FixTitle.cs => TitleFix.cs} | 66 ++++++++----------- .../TitleFixConfiguration.cs | 66 +++++++++++++++++++ Greenshot/Forms/MainForm.cs | 3 + .../Configuration/CoreConfiguration.cs | 8 ++- GreenshotCore/Core/ConfigHelper.cs | 19 ++++-- 8 files changed, 120 insertions(+), 51 deletions(-) rename Greenshot-TitleFix-Plugin/{FixTitle.cs => TitleFix.cs} (65%) create mode 100644 Greenshot-TitleFix-Plugin/TitleFixConfiguration.cs diff --git a/Greenshot-OCR-Plugin/OCRPlugin.cs b/Greenshot-OCR-Plugin/OCRPlugin.cs index 2c1c5a10b..fac3b002c 100644 --- a/Greenshot-OCR-Plugin/OCRPlugin.cs +++ b/Greenshot-OCR-Plugin/OCRPlugin.cs @@ -71,9 +71,7 @@ namespace GreenshotOCR { // Load configuration config = IniConfig.GetIniSection(); - if (config.IsDirty) { - IniConfig.Save(); - } + this.host.RegisterHotKey(3, 0x2C, new HotKeyHandler(MyHotkeyHandler)); // Here we can hang ourselves to the main context menu! diff --git a/Greenshot-TitleFix-Plugin/Greenshot-TitleFix-Plugin.csproj b/Greenshot-TitleFix-Plugin/Greenshot-TitleFix-Plugin.csproj index 7625fe9e2..cac9b094d 100644 --- a/Greenshot-TitleFix-Plugin/Greenshot-TitleFix-Plugin.csproj +++ b/Greenshot-TitleFix-Plugin/Greenshot-TitleFix-Plugin.csproj @@ -47,8 +47,9 @@ - + + diff --git a/Greenshot-TitleFix-Plugin/Properties/AssemblyInfo.cs.template b/Greenshot-TitleFix-Plugin/Properties/AssemblyInfo.cs.template index ea4281eef..3f8c3ad8f 100644 --- a/Greenshot-TitleFix-Plugin/Properties/AssemblyInfo.cs.template +++ b/Greenshot-TitleFix-Plugin/Properties/AssemblyInfo.cs.template @@ -39,7 +39,7 @@ using Greenshot.Plugin; [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // The PluginAttribute describes the "entryType" and if the plugin is configurable -[assembly: PluginAttribute("FixTitle.FixTitle", false)] +[assembly: PluginAttribute("TitleFix.TitleFix", false)] // This sets the default COM visibility of types in the assembly to invisible. // If you need to expose a type to COM, use [ComVisible(true)] on that type. diff --git a/Greenshot-TitleFix-Plugin/FixTitle.cs b/Greenshot-TitleFix-Plugin/TitleFix.cs similarity index 65% rename from Greenshot-TitleFix-Plugin/FixTitle.cs rename to Greenshot-TitleFix-Plugin/TitleFix.cs index 21a8651db..3a5cc58b6 100644 --- a/Greenshot-TitleFix-Plugin/FixTitle.cs +++ b/Greenshot-TitleFix-Plugin/TitleFix.cs @@ -29,23 +29,19 @@ using Greenshot.Capturing; using Greenshot.Core; using Greenshot.Plugin; -namespace FixTitle { +namespace TitleFix { /// /// An example Plugin so developers can see how they can develop their own plugin /// - public class FixTitle : IGreenshotPlugin { - private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(FixTitle)); - private const string CONFIG_FILE_NAME = "titlefix.properties"; - private const string ACTIVE_PROPERTY = "active"; - private const string REGEXP_PROPERTY = ".regexp"; - private const string REPLACE_PROPERTY = ".replace"; + public class TitleFix : IGreenshotPlugin { + private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(TitleFix)); private IGreenshotPluginHost host; private ICaptureHost captureHost = null; private PluginAttribute myAttributes; - private Properties config = null; + private TitleFixConfiguration config = null; - public FixTitle() { + public TitleFix() { } /// @@ -61,14 +57,29 @@ namespace FixTitle { this.captureHost = captureHost; this.myAttributes = myAttributes; - LoadConfig(); + this.config = IniConfig.GetIniSection(); + + // check configuration + List corruptKeys = new List(); + foreach(string key in config.active) { + if (!config.matchers.ContainsKey(key) || !config.matchers.ContainsKey(key)) { + LOG.Warn("Key " + key + " not found, configuration is broken! Disabling this key!"); + corruptKeys.Add(key); + } + } - if (config != null) { - this.host.OnCaptureTaken += new OnCaptureTakenHandler(CaptureTaken); - } else { - LOG.Warn("Not registering FixTitle plugin due to missing configuration"); + // Fix configuration if needed + if(corruptKeys.Count > 0) { + foreach(string corruptKey in corruptKeys) { + // Removing any reference to the key + config.active.Remove(corruptKey); + config.matchers.Remove(corruptKey); + config.replacers.Remove(corruptKey); + } + config.IsDirty = true; } + this.host.OnCaptureTaken += new OnCaptureTakenHandler(CaptureTaken); } public virtual void Shutdown() { @@ -91,9 +102,9 @@ namespace FixTitle { LOG.Debug("Title before: " + title); if (title != null && title.Length > 0) { title = title.Trim(); - foreach(string titleIdentifier in config.GetPropertyAsArray(ACTIVE_PROPERTY)) { - string regexpString = config.GetProperty(titleIdentifier + REGEXP_PROPERTY); - string replaceString = config.GetProperty(titleIdentifier + REPLACE_PROPERTY); + foreach(string titleIdentifier in config.active) { + string regexpString = config.matchers[titleIdentifier]; + string replaceString = config.replacers[titleIdentifier]; if (regexpString != null && regexpString.Length > 0) { Regex regex = new Regex(regexpString); title = regex.Replace(title, replaceString); @@ -103,26 +114,5 @@ namespace FixTitle { LOG.Debug("Title after: " + title); eventArgs.Capture.CaptureDetails.Title = title; } - - private void LoadConfig() { - string configfilename = Path.Combine(host.ConfigurationPath, CONFIG_FILE_NAME); - if (File.Exists(configfilename)) { - config = Properties.read(configfilename); - } else { - config = new Properties(); - } - // Check if we have a configuration - if (!config.ContainsKey(ACTIVE_PROPERTY)) { - // Create default with FireFox - config.AddProperty("Firefox"+REGEXP_PROPERTY, " - Mozilla Firefox.*"); - config.AddProperty("Firefox"+REPLACE_PROPERTY, ""); - // and IE - config.AddProperty("IE"+REGEXP_PROPERTY, " - Microsoft Internet Explorer.*"); - config.AddProperty("IE"+REPLACE_PROPERTY, ""); - // Activate both - config.AddProperty(ACTIVE_PROPERTY, "IE,Firefox"); - config.write(configfilename); - } - } } } \ No newline at end of file diff --git a/Greenshot-TitleFix-Plugin/TitleFixConfiguration.cs b/Greenshot-TitleFix-Plugin/TitleFixConfiguration.cs new file mode 100644 index 000000000..4a64bb107 --- /dev/null +++ b/Greenshot-TitleFix-Plugin/TitleFixConfiguration.cs @@ -0,0 +1,66 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2010 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: http://getgreenshot.org/ + * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +using System; +using System.Collections.Generic; +using Greenshot.Core; + +namespace TitleFix { + /// + /// Description of FlickrConfiguration. + /// + [IniSection("TitleFix", Description="Greenshot TitleFix Plugin configuration")] + public class TitleFixConfiguration : IniSection { + [IniProperty("ActiveFixes", Description="The fixes that are active.")] + public List active; + + [IniProperty("Matcher", Description="A regular expressions to match the title with.")] + public Dictionary matchers; + + [IniProperty("Replacer", Description="The replacements for the matchers.")] + public Dictionary replacers; + + /// + /// Supply values we can't put as defaults + /// + /// The property to return a default for + /// object with the default value for the supplied property + public override object GetDefault(string property) { + switch(property) { + case "ActiveFixes": + List activeDefaults = new List(); + activeDefaults.Add("Firefox"); + activeDefaults.Add("IE"); + return activeDefaults; + case "Matcher": + Dictionary matcherDefaults = new Dictionary(); + matcherDefaults.Add("Firefox", " - Mozilla Firefox.*"); + matcherDefaults.Add("IE", " - Microsoft Internet Explorer.*"); + return matcherDefaults; + case "Replacer": + Dictionary replacerDefaults = new Dictionary(); + replacerDefaults.Add("Firefox", ""); + replacerDefaults.Add("IE", ""); + return replacerDefaults; + } + return null; + } + } +} diff --git a/Greenshot/Forms/MainForm.cs b/Greenshot/Forms/MainForm.cs index ae7eb3a34..286f03457 100644 --- a/Greenshot/Forms/MainForm.cs +++ b/Greenshot/Forms/MainForm.cs @@ -327,6 +327,9 @@ namespace Greenshot { // Load all the plugins PluginHelper.instance.LoadPlugins(this.contextMenu, captureForm); + // Making sure changes are written + IniConfig.Save(); + PluginHelper.instance.OnImageOutput += new OnImageOutputHandler(ImageWritten); SoundHelper.Initialize(); diff --git a/GreenshotCore/Configuration/CoreConfiguration.cs b/GreenshotCore/Configuration/CoreConfiguration.cs index a09f4f899..dbdaeccff 100644 --- a/GreenshotCore/Configuration/CoreConfiguration.cs +++ b/GreenshotCore/Configuration/CoreConfiguration.cs @@ -99,13 +99,17 @@ namespace Greenshot.Core { /// Supply values we can't put as defaults /// /// The property to return a default for - /// string with the default value for the supplied property - public override string GetDefault(string property) { + /// object with the default value for the supplied property + public override object GetDefault(string property) { switch(property) { case "OutputFileAsFullpath": return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"dummy.png"); case "OutputFilePath": return Environment.GetFolderPath(Environment.SpecialFolder.Desktop); + case "Test": + Dictionary testvalues = new Dictionary(); + testvalues.Add("Robin", true); + return testvalues; } return null; } diff --git a/GreenshotCore/Core/ConfigHelper.cs b/GreenshotCore/Core/ConfigHelper.cs index d9ee95285..a7d6916a6 100644 --- a/GreenshotCore/Core/ConfigHelper.cs +++ b/GreenshotCore/Core/ConfigHelper.cs @@ -72,8 +72,8 @@ namespace Greenshot.Core { /// Supply values we can't put as defaults /// /// The property to return a default for - /// string with the default value for the supplied property - public virtual string GetDefault(string property) { + /// object with the default value for the supplied property + public virtual object GetDefault(string property) { return null; } } @@ -184,19 +184,26 @@ namespace Greenshot.Core { IniPropertyAttribute iniPropertyAttribute = (IniPropertyAttribute)field.GetCustomAttributes(typeof(IniPropertyAttribute), false)[0]; string propertyName = iniPropertyAttribute.Name; string propertyDefaultValue = iniPropertyAttribute.DefaultValue; - if (propertyDefaultValue == null) { - propertyDefaultValue = section.GetDefault(propertyName); - } string propertyValue = null; // Get the value from the ini file, if there is none take the default if (properties != null && properties.ContainsKey(propertyName)) { propertyValue = properties[propertyName]; - } else { + } else if (propertyDefaultValue != null) { // Mark as dirty, we didn't use properties from the file (even defaults from the default file are allowed) section.IsDirty = true; propertyValue = propertyDefaultValue; LOG.Debug("Using default: " + propertyName + "=" + propertyValue); + } else if (propertyValue == null) { + // Use GetDefault to fill the field if none is set + object defaultValue = section.GetDefault(propertyName); + if (defaultValue != null) { + field.SetValue(section, defaultValue); + continue; + } else { + LOG.Debug("No default value, setting to null"); + continue; + } } // Get the type, or the underlying type for nullables