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,24 +185,37 @@ 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;
// Now set the value
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>)) {
string[] arrayValues = propertyValue.Split(new Char[] {','});
if (arrayValues != null) {
object list = Activator.CreateInstance(fieldType);
MethodInfo methodInfo = fieldType.GetMethod("Add");
foreach(string arrayValue in arrayValues) {
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<>))) { if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) {
// We are dealing with a generic type that is nullable // We are dealing with a generic type that is nullable
fieldType = Nullable.GetUnderlyingType(fieldType); fieldType = Nullable.GetUnderlyingType(fieldType);
} }
field.SetValue(section,ConvertValueToFieldType(fieldType, propertyValue));
// 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));
} }
} }
} }
@ -207,56 +223,28 @@ namespace GreenshotCore.Configuration {
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];
} else {
// Create instance of this type
section = (IniSection)Activator.CreateInstance(iniSectionType);
// Get the properties for the section
Dictionary<string, string> properties = null;
if (iniProperties.ContainsKey(sectionName)) {
properties = iniProperties[sectionName];
} }
private object ConvertValueToFieldType(Type fieldType, string value) {
// Iterate over the fields and fill them if (value == null && value.Length == 0) {
FieldInfo[] fields = iniSectionType.GetFields(); return null;
foreach(FieldInfo field in fields) {
if (Attribute.IsDefined(field, typeof(IniPropertyAttribute))) {
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)) { if (fieldType == typeof(string)) {
field.SetValue(section, propertyValue); return value;
} else if (fieldType == typeof(bool) || fieldType == typeof(bool?)) { } else if (fieldType == typeof(bool) || fieldType == typeof(bool?)) {
field.SetValue(section, bool.Parse(propertyValue)); return bool.Parse(value);
} else if (fieldType == typeof(int) || fieldType == typeof(int?)) { } else if (fieldType == typeof(int) || fieldType == typeof(int?)) {
field.SetValue(section, int.Parse(propertyValue)); return int.Parse(value);
} else if (fieldType.IsEnum) { } else if (fieldType.IsEnum) {
field.SetValue(section, Enum.Parse(fieldType, propertyValue)); try {
return Enum.Parse(fieldType, value);
} catch (Exception e) {
LOG.Error("Can't parse value: " + value, e);
} }
} }
} return null;
}
return section;
} }
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,14 +349,32 @@ 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);
} }
} }
} }
} }
}
writer.WriteLine(); writer.WriteLine();
} }