mirror of
https://github.com/greenshot/greenshot
synced 2025-08-21 05:53:27 -07:00
Refactored the ini code to be more "OO", making it possible to reset a value which was needed to implement an auto-repair for certain settings. This was intiated by Bug #3509754, which hat the reason that somehow the Hotkey name was corrupt.
git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1721 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
parent
20d4fb693f
commit
4b3f7ba0a5
10 changed files with 507 additions and 440 deletions
|
@ -20,6 +20,8 @@
|
|||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
|
||||
namespace Greenshot.IniFile {
|
||||
/// <summary>
|
||||
|
@ -27,16 +29,23 @@ namespace Greenshot.IniFile {
|
|||
/// </summary>
|
||||
[Serializable]
|
||||
public abstract class IniSection {
|
||||
protected static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(IniSection));
|
||||
|
||||
[NonSerialized]
|
||||
private IDictionary<string, IniValue> values = new Dictionary<string, IniValue>();
|
||||
|
||||
/// <summary>
|
||||
/// Get the dictionary with all the IniValues
|
||||
/// </summary>
|
||||
public IDictionary<string, IniValue> Values {
|
||||
get {
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flag to specify if values have been changed
|
||||
/// </summary>
|
||||
public bool IsDirty = false;
|
||||
|
||||
/// <summary>
|
||||
|
@ -65,10 +74,86 @@ namespace Greenshot.IniFile {
|
|||
public virtual void AfterLoad() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This will be called before saving the Section, so we can encrypt passwords etc...
|
||||
/// </summary>
|
||||
public virtual void BeforeSave() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This will be called before saving the Section, so we can decrypt passwords etc...
|
||||
/// </summary>
|
||||
public virtual void AfterSave() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill the section with the supplied properties
|
||||
/// </summary>
|
||||
/// <param name="properties"></param>
|
||||
public void Fill(Dictionary<string, string> properties) {
|
||||
Type iniSectionType = this.GetType();
|
||||
|
||||
// Iterate over the members and create IniValueContainers
|
||||
foreach (FieldInfo fieldInfo in iniSectionType.GetFields()) {
|
||||
if (Attribute.IsDefined(fieldInfo, typeof(IniPropertyAttribute))) {
|
||||
if (!Values.ContainsKey(fieldInfo.Name)) {
|
||||
IniPropertyAttribute iniPropertyAttribute = (IniPropertyAttribute)fieldInfo.GetCustomAttributes(typeof(IniPropertyAttribute), false)[0];
|
||||
Values.Add(fieldInfo.Name, new IniValue(this, fieldInfo, iniPropertyAttribute));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (PropertyInfo propertyInfo in iniSectionType.GetProperties()) {
|
||||
if (Attribute.IsDefined(propertyInfo, typeof(IniPropertyAttribute))) {
|
||||
if (!Values.ContainsKey(propertyInfo.Name)) {
|
||||
IniPropertyAttribute iniPropertyAttribute = (IniPropertyAttribute)propertyInfo.GetCustomAttributes(typeof(IniPropertyAttribute), false)[0];
|
||||
Values.Add(propertyInfo.Name, new IniValue(this, propertyInfo, iniPropertyAttribute));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (string fieldName in Values.Keys) {
|
||||
IniValue iniValue = Values[fieldName];
|
||||
try {
|
||||
iniValue.SetValueFromProperties(properties);
|
||||
} catch (Exception ex) {
|
||||
LOG.Error(ex);
|
||||
}
|
||||
}
|
||||
AfterLoad();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write the section to the writer
|
||||
/// </summary>
|
||||
/// <param name="writer"></param>
|
||||
/// <param name="onlyProperties"></param>
|
||||
public void Write(TextWriter writer, bool onlyProperties) {
|
||||
BeforeSave();
|
||||
try {
|
||||
Attribute[] classAttributes = Attribute.GetCustomAttributes(this.GetType());
|
||||
IniSectionAttribute iniSectionAttribute = null;
|
||||
foreach (Attribute attribute in classAttributes) {
|
||||
if (attribute is IniSectionAttribute) {
|
||||
iniSectionAttribute = (IniSectionAttribute)attribute;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (iniSectionAttribute == null) {
|
||||
throw new ArgumentException("Section didn't implement the IniSectionAttribute");
|
||||
}
|
||||
|
||||
if (!onlyProperties) {
|
||||
writer.WriteLine("; {0}", iniSectionAttribute.Description);
|
||||
}
|
||||
writer.WriteLine("[{0}]", iniSectionAttribute.Name);
|
||||
|
||||
foreach (IniValue value in Values.Values) {
|
||||
value.Write(writer, onlyProperties);
|
||||
}
|
||||
} finally {
|
||||
AfterSave();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue