mirror of
https://github.com/greenshot/greenshot
synced 2025-08-20 21:43:24 -07:00
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
This commit is contained in:
parent
ea26f9ca50
commit
bb7f216567
8 changed files with 120 additions and 51 deletions
|
@ -71,9 +71,7 @@ namespace GreenshotOCR {
|
|||
|
||||
// Load configuration
|
||||
config = IniConfig.GetIniSection<OCRConfiguration>();
|
||||
if (config.IsDirty) {
|
||||
IniConfig.Save();
|
||||
}
|
||||
|
||||
this.host.RegisterHotKey(3, 0x2C, new HotKeyHandler(MyHotkeyHandler));
|
||||
|
||||
// Here we can hang ourselves to the main context menu!
|
||||
|
|
|
@ -47,8 +47,9 @@
|
|||
<Reference Include="System.Windows.Forms" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="FixTitle.cs" />
|
||||
<Compile Include="TitleFix.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TitleFixConfiguration.cs" />
|
||||
<None Include="Properties\AssemblyInfo.cs.template" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -29,23 +29,19 @@ using Greenshot.Capturing;
|
|||
using Greenshot.Core;
|
||||
using Greenshot.Plugin;
|
||||
|
||||
namespace FixTitle {
|
||||
namespace TitleFix {
|
||||
/// <summary>
|
||||
/// An example Plugin so developers can see how they can develop their own plugin
|
||||
/// </summary>
|
||||
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() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -61,14 +57,29 @@ namespace FixTitle {
|
|||
this.captureHost = captureHost;
|
||||
this.myAttributes = myAttributes;
|
||||
|
||||
LoadConfig();
|
||||
this.config = IniConfig.GetIniSection<TitleFixConfiguration>();
|
||||
|
||||
if (config != null) {
|
||||
this.host.OnCaptureTaken += new OnCaptureTakenHandler(CaptureTaken);
|
||||
} else {
|
||||
LOG.Warn("Not registering FixTitle plugin due to missing configuration");
|
||||
// check configuration
|
||||
List<string> corruptKeys = new List<string>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
66
Greenshot-TitleFix-Plugin/TitleFixConfiguration.cs
Normal file
66
Greenshot-TitleFix-Plugin/TitleFixConfiguration.cs
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Greenshot.Core;
|
||||
|
||||
namespace TitleFix {
|
||||
/// <summary>
|
||||
/// Description of FlickrConfiguration.
|
||||
/// </summary>
|
||||
[IniSection("TitleFix", Description="Greenshot TitleFix Plugin configuration")]
|
||||
public class TitleFixConfiguration : IniSection {
|
||||
[IniProperty("ActiveFixes", Description="The fixes that are active.")]
|
||||
public List<string> active;
|
||||
|
||||
[IniProperty("Matcher", Description="A regular expressions to match the title with.")]
|
||||
public Dictionary<string, string> matchers;
|
||||
|
||||
[IniProperty("Replacer", Description="The replacements for the matchers.")]
|
||||
public Dictionary<string, string> replacers;
|
||||
|
||||
/// <summary>
|
||||
/// Supply values we can't put as defaults
|
||||
/// </summary>
|
||||
/// <param name="property">The property to return a default for</param>
|
||||
/// <returns>object with the default value for the supplied property</returns>
|
||||
public override object GetDefault(string property) {
|
||||
switch(property) {
|
||||
case "ActiveFixes":
|
||||
List<string> activeDefaults = new List<string>();
|
||||
activeDefaults.Add("Firefox");
|
||||
activeDefaults.Add("IE");
|
||||
return activeDefaults;
|
||||
case "Matcher":
|
||||
Dictionary<string, string> matcherDefaults = new Dictionary<string, string>();
|
||||
matcherDefaults.Add("Firefox", " - Mozilla Firefox.*");
|
||||
matcherDefaults.Add("IE", " - Microsoft Internet Explorer.*");
|
||||
return matcherDefaults;
|
||||
case "Replacer":
|
||||
Dictionary<string, string> replacerDefaults = new Dictionary<string, string>();
|
||||
replacerDefaults.Add("Firefox", "");
|
||||
replacerDefaults.Add("IE", "");
|
||||
return replacerDefaults;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -99,13 +99,17 @@ namespace Greenshot.Core {
|
|||
/// Supply values we can't put as defaults
|
||||
/// </summary>
|
||||
/// <param name="property">The property to return a default for</param>
|
||||
/// <returns>string with the default value for the supplied property</returns>
|
||||
public override string GetDefault(string property) {
|
||||
/// <returns>object with the default value for the supplied property</returns>
|
||||
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<string, bool> testvalues = new Dictionary<string, bool>();
|
||||
testvalues.Add("Robin", true);
|
||||
return testvalues;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -72,8 +72,8 @@ namespace Greenshot.Core {
|
|||
/// Supply values we can't put as defaults
|
||||
/// </summary>
|
||||
/// <param name="property">The property to return a default for</param>
|
||||
/// <returns>string with the default value for the supplied property</returns>
|
||||
public virtual string GetDefault(string property) {
|
||||
/// <returns>object with the default value for the supplied property</returns>
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue