Enhanced ini framework with enums and lists

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@809 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2010-08-17 21:09:20 +00:00
parent d609525999
commit af6567e8fe
2 changed files with 74 additions and 64 deletions

View file

@ -19,8 +19,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Collections.Generic;
namespace GreenshotCore.Configuration { namespace GreenshotCore.Configuration {
public enum Destinations {Editor=1, FileDefault=2, FileWithDialog=4, Clipboard=8, Printer=16, EMail=32}
/// <summary> /// <summary>
/// Description of CoreConfiguration. /// Description of CoreConfiguration.
/// </summary> /// </summary>
@ -32,5 +34,7 @@ namespace GreenshotCore.Configuration {
public bool RegisterHotkeys; public bool RegisterHotkeys;
[IniProperty("IsFirstLaunch", Description="Is this the first time launch?", DefaultValue="true")] [IniProperty("IsFirstLaunch", Description="Is this the first time launch?", DefaultValue="true")]
public bool IsFirstLaunch; public bool IsFirstLaunch;
[IniProperty("Destinations", Description="Which destinations? Options are: Editor, FileDefault, FileWithDialog, Clipboard, Printer, EMail", DefaultValue="Editor")]
public List<Destinations> OutputDestinations;
} }
} }

View file

@ -22,6 +22,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
namespace GreenshotCore.Configuration { namespace GreenshotCore.Configuration {
@ -104,7 +105,9 @@ namespace GreenshotCore.Configuration {
/// <param name="iniLocation"></param> /// <param name="iniLocation"></param>
private IniConfig() { private IniConfig() {
this.iniLocation = CreateIniLocation(CONFIG_FILE_NAME); this.iniLocation = CreateIniLocation(CONFIG_FILE_NAME);
// Load the defaults
Read(CreateIniLocation(DEFAULTS_CONFIG_FILE_NAME)); Read(CreateIniLocation(DEFAULTS_CONFIG_FILE_NAME));
// Load the normal
Read(CreateIniLocation(iniLocation)); Read(CreateIniLocation(iniLocation));
} }
@ -120,7 +123,7 @@ namespace GreenshotCore.Configuration {
LOG.Info("Reading ini-properties from file: " + iniLocation); LOG.Info("Reading ini-properties from file: " + iniLocation);
String currentSection = null; String currentSection = null;
foreach (string line in File.ReadAllLines(iniLocation)) { foreach (string line in File.ReadAllLines(iniLocation, Encoding.UTF8)) {
if (line == null) { if (line == null) {
continue; continue;
} }
@ -182,81 +185,66 @@ namespace GreenshotCore.Configuration {
propertyValue = properties[propertyName]; propertyValue = properties[propertyName];
} else { } else {
propertyValue = iniPropertyAttribute.DefaultValue; propertyValue = iniPropertyAttribute.DefaultValue;
LOG.Debug("Using default property for " + propertyName + " : " + propertyValue);
} }
// Get the type, or the underlying type for nullables // Get the type, or the underlying type for nullables
Type fieldType = field.FieldType; Type fieldType = field.FieldType;
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {
// We are dealing with a generic type that is nullable
fieldType = Nullable.GetUnderlyingType(fieldType);
}
// Now set the value // Now set the value
if (fieldType == typeof(string)) { if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>)) {
field.SetValue(section, propertyValue); string[] arrayValues = propertyValue.Split(new Char[] {','});
} else if (fieldType == typeof(bool) || fieldType == typeof(bool?)) { if (arrayValues != null) {
field.SetValue(section, bool.Parse(propertyValue)); object list = Activator.CreateInstance(fieldType);
} else if (fieldType == typeof(int) || fieldType == typeof(int?)) { MethodInfo methodInfo = fieldType.GetMethod("Add");
field.SetValue(section, int.Parse(propertyValue));
} else if (fieldType.IsEnum) { foreach(string arrayValue in arrayValues) {
field.SetValue(section, Enum.Parse(fieldType, propertyValue)); if (arrayValue != null && arrayValue.Length > 0) {
object newValue = ConvertValueToFieldType(fieldType.GetGenericArguments()[0], arrayValue);
LOG.Debug("Adding: " + newValue);
if (newValue != null) {
methodInfo.Invoke(list, new object[] {newValue});
}
}
}
field.SetValue(section, list);
}
} else {
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {
// We are dealing with a generic type that is nullable
fieldType = Nullable.GetUnderlyingType(fieldType);
}
field.SetValue(section,ConvertValueToFieldType(fieldType, propertyValue));
} }
} }
} }
} }
return section; return section;
} }
public IniSection GetSection(Type iniSectionType) { private List<T> CreateList<T>() {
IniSection section = null; List<T> mylist = new List<T>();
string sectionName = getSectionName(iniSectionType); return mylist;
if (sectionMap.ContainsKey(sectionName)) { }
section = sectionMap[sectionName]; private object ConvertValueToFieldType(Type fieldType, string value) {
} else { if (value == null && value.Length == 0) {
// Create instance of this type return null;
section = (IniSection)Activator.CreateInstance(iniSectionType); }
if (fieldType == typeof(string)) {
// Get the properties for the section return value;
Dictionary<string, string> properties = null; } else if (fieldType == typeof(bool) || fieldType == typeof(bool?)) {
if (iniProperties.ContainsKey(sectionName)) { return bool.Parse(value);
properties = iniProperties[sectionName]; } else if (fieldType == typeof(int) || fieldType == typeof(int?)) {
} return int.Parse(value);
} else if (fieldType.IsEnum) {
// Iterate over the fields and fill them try {
FieldInfo[] fields = iniSectionType.GetFields(); return Enum.Parse(fieldType, value);
foreach(FieldInfo field in fields) { } catch (Exception e) {
if (Attribute.IsDefined(field, typeof(IniPropertyAttribute))) { LOG.Error("Can't parse value: " + value, e);
IniPropertyAttribute iniPropertyAttribute = (IniPropertyAttribute)field.GetCustomAttributes(typeof(IniPropertyAttribute), false)[0];
string propertyName = iniPropertyAttribute.Name;
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 {
propertyValue = iniPropertyAttribute.DefaultValue;
}
// Get the type, or the underlying type for nullables
Type fieldType = field.FieldType;
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {
// We are dealing with a generic type that is nullable
fieldType = Nullable.GetUnderlyingType(fieldType);
}
// Now set the value
if (fieldType == typeof(string)) {
field.SetValue(section, propertyValue);
} else if (fieldType == typeof(bool) || fieldType == typeof(bool?)) {
field.SetValue(section, bool.Parse(propertyValue));
} else if (fieldType == typeof(int) || fieldType == typeof(int?)) {
field.SetValue(section, int.Parse(propertyValue));
} else if (fieldType.IsEnum) {
field.SetValue(section, Enum.Parse(fieldType, propertyValue));
}
}
} }
} }
return section; return null;
} }
private string getSectionName(Type iniSectionType) { private string getSectionName(Type iniSectionType) {
@ -346,7 +334,7 @@ namespace GreenshotCore.Configuration {
public void Save() { public void Save() {
LOG.Info("Saving configuration to: " + iniLocation); LOG.Info("Saving configuration to: " + iniLocation);
TextWriter writer = new StreamWriter(iniLocation); TextWriter writer = new StreamWriter(iniLocation, false, Encoding.UTF8);
foreach(IniSection section in sectionMap.Values) { foreach(IniSection section in sectionMap.Values) {
Type classType = section.GetType(); Type classType = section.GetType();
Attribute[] classAttributes = Attribute.GetCustomAttributes(classType); Attribute[] classAttributes = Attribute.GetCustomAttributes(classType);
@ -361,10 +349,28 @@ namespace GreenshotCore.Configuration {
IniPropertyAttribute iniPropertyAttribute = (IniPropertyAttribute)field.GetCustomAttributes(typeof(IniPropertyAttribute), false)[0]; IniPropertyAttribute iniPropertyAttribute = (IniPropertyAttribute)field.GetCustomAttributes(typeof(IniPropertyAttribute), false)[0];
writer.WriteLine("; {0}", iniPropertyAttribute.Description); writer.WriteLine("; {0}", iniPropertyAttribute.Description);
object value = field.GetValue(section); object value = field.GetValue(section);
Type fieldType = field.FieldType;
if (value == null) { if (value == null) {
value = iniPropertyAttribute.DefaultValue; value = iniPropertyAttribute.DefaultValue;
fieldType = typeof(string);
}
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>)) {
writer.Write("{0}=", iniPropertyAttribute.Name);
int listCount = (int)fieldType.GetProperty("Count").GetValue(value, null);
// Loop though generic list
for (int index = 0; index < listCount; index++) {
object item = fieldType.GetMethod("get_Item").Invoke(value, new object[] { index });
// Now you have an instance of the item in the generic list
if (index < listCount -1) {
writer.Write("{0},", item);
} else {
writer.Write("{0}", item);
}
}
writer.WriteLine();
} else {
writer.WriteLine("{0}={1}", iniPropertyAttribute.Name, value);
} }
writer.WriteLine("{0}={1}", iniPropertyAttribute.Name, value);
} }
} }
} }