mirror of
https://github.com/greenshot/greenshot
synced 2025-07-30 03:30:02 -07:00
Changed to static, this makes it even easier to use.
git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@815 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
parent
d7501c6026
commit
b68fa2e0ff
1 changed files with 27 additions and 42 deletions
|
@ -71,23 +71,21 @@ namespace Greenshot.Core {
|
||||||
private const string CONFIG_FILE_NAME = "greenshot.ini";
|
private const string CONFIG_FILE_NAME = "greenshot.ini";
|
||||||
private const string DEFAULTS_CONFIG_FILE_NAME = "greenshot-defaults.ini";
|
private const string DEFAULTS_CONFIG_FILE_NAME = "greenshot-defaults.ini";
|
||||||
|
|
||||||
private static IniConfig instance = null;
|
|
||||||
|
|
||||||
private string iniLocation = null;
|
|
||||||
private Dictionary<string, IniSection> sectionMap = new Dictionary<string, IniSection>();
|
|
||||||
private Dictionary<string, Dictionary<string, string >> iniProperties = new Dictionary<string, Dictionary<string, string>>();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// get an instance of IniConfig
|
/// Static code for loading
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
static IniConfig() {
|
||||||
public static IniConfig GetInstance() {
|
iniLocation = CreateIniLocation(CONFIG_FILE_NAME);
|
||||||
if (instance == null) {
|
// Load the defaults
|
||||||
instance = new IniConfig();
|
Read(CreateIniLocation(DEFAULTS_CONFIG_FILE_NAME));
|
||||||
}
|
// Load the normal
|
||||||
return instance;
|
Read(CreateIniLocation(iniLocation));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string iniLocation = null;
|
||||||
|
private static Dictionary<string, IniSection> sectionMap = new Dictionary<string, IniSection>();
|
||||||
|
private static Dictionary<string, Dictionary<string, string >> iniProperties = new Dictionary<string, Dictionary<string, string>>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create the location of the configuration file
|
/// Create the location of the configuration file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -101,23 +99,13 @@ namespace Greenshot.Core {
|
||||||
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),@"Greenshot\" + configFilename);
|
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),@"Greenshot\" + configFilename);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Private Constuctor,
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="iniLocation"></param>
|
|
||||||
private IniConfig() {
|
|
||||||
this.iniLocation = CreateIniLocation(CONFIG_FILE_NAME);
|
|
||||||
// Load the defaults
|
|
||||||
Read(CreateIniLocation(DEFAULTS_CONFIG_FILE_NAME));
|
|
||||||
// Load the normal
|
|
||||||
Read(CreateIniLocation(iniLocation));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Read the ini file into the Dictionary
|
/// Read the ini file into the Dictionary
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="iniLocation">Path & Filename of ini file</param>
|
/// <param name="iniLocation">Path & Filename of ini file</param>
|
||||||
private void Read(string iniLocation) {
|
private static void Read(string iniLocation) {
|
||||||
if (!File.Exists(iniLocation)) {
|
if (!File.Exists(iniLocation)) {
|
||||||
LOG.Info("Can't find file: " + iniLocation);
|
LOG.Info("Can't find file: " + iniLocation);
|
||||||
return;
|
return;
|
||||||
|
@ -156,8 +144,9 @@ namespace Greenshot.Core {
|
||||||
/// A generic method which returns an instance of the supplied type, filled with it's configuration
|
/// A generic method which returns an instance of the supplied type, filled with it's configuration
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Filled instance of IniSection type which was supplied</returns>
|
/// <returns>Filled instance of IniSection type which was supplied</returns>
|
||||||
public T GetSection<T>() where T : IniSection {
|
public static T GetIniSection<T>() where T : IniSection {
|
||||||
T section;
|
T section;
|
||||||
|
|
||||||
Type iniSectionType = typeof(T);
|
Type iniSectionType = typeof(T);
|
||||||
string sectionName = getSectionName(iniSectionType);
|
string sectionName = getSectionName(iniSectionType);
|
||||||
if (sectionMap.ContainsKey(sectionName)) {
|
if (sectionMap.ContainsKey(sectionName)) {
|
||||||
|
@ -226,12 +215,8 @@ namespace Greenshot.Core {
|
||||||
}
|
}
|
||||||
return section;
|
return section;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<T> CreateList<T>() {
|
private static object ConvertValueToFieldType(Type fieldType, string value) {
|
||||||
List<T> mylist = new List<T>();
|
|
||||||
return mylist;
|
|
||||||
}
|
|
||||||
private object ConvertValueToFieldType(Type fieldType, string value) {
|
|
||||||
if (value == null && value.Length == 0) {
|
if (value == null && value.Length == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -251,7 +236,7 @@ namespace Greenshot.Core {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string getSectionName(Type iniSectionType) {
|
private static string getSectionName(Type iniSectionType) {
|
||||||
Attribute[] classAttributes = Attribute.GetCustomAttributes(iniSectionType);
|
Attribute[] classAttributes = Attribute.GetCustomAttributes(iniSectionType);
|
||||||
foreach(Attribute attribute in classAttributes) {
|
foreach(Attribute attribute in classAttributes) {
|
||||||
if (attribute is IniSectionAttribute) {
|
if (attribute is IniSectionAttribute) {
|
||||||
|
@ -262,14 +247,14 @@ namespace Greenshot.Core {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Dictionary<string, string> getProperties(string section) {
|
private static Dictionary<string, string> getProperties(string section) {
|
||||||
if (iniProperties.ContainsKey(section)) {
|
if (iniProperties.ContainsKey(section)) {
|
||||||
return iniProperties[section];
|
return iniProperties[section];
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ChangeProperty(string section, string name, string value) {
|
public static void ChangeProperty(string section, string name, string value) {
|
||||||
if (section != null) {
|
if (section != null) {
|
||||||
Dictionary<string, string> properties = iniProperties[section];
|
Dictionary<string, string> properties = iniProperties[section];
|
||||||
properties[name] = value;
|
properties[name] = value;
|
||||||
|
@ -279,7 +264,7 @@ namespace Greenshot.Core {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetProperty(string section, string name, string value) {
|
public static void SetProperty(string section, string name, string value) {
|
||||||
if (section != null) {
|
if (section != null) {
|
||||||
Dictionary<string, string> properties = null;
|
Dictionary<string, string> properties = null;
|
||||||
if (!iniProperties.ContainsKey(section)) {
|
if (!iniProperties.ContainsKey(section)) {
|
||||||
|
@ -296,7 +281,7 @@ namespace Greenshot.Core {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public bool HasProperty(string section, string name) {
|
public static bool HasProperty(string section, string name) {
|
||||||
Dictionary<string, string> properties = getProperties(section);
|
Dictionary<string, string> properties = getProperties(section);
|
||||||
if (properties != null && properties.ContainsKey(name)) {
|
if (properties != null && properties.ContainsKey(name)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -304,7 +289,7 @@ namespace Greenshot.Core {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetProperty(string section, string name) {
|
public static string GetProperty(string section, string name) {
|
||||||
Dictionary<string, string> properties = getProperties(section);
|
Dictionary<string, string> properties = getProperties(section);
|
||||||
if (properties != null && properties.ContainsKey(name)) {
|
if (properties != null && properties.ContainsKey(name)) {
|
||||||
return properties[name];
|
return properties[name];
|
||||||
|
@ -315,7 +300,7 @@ namespace Greenshot.Core {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Split property with ',' and return the splitted string as a string[]
|
/// Split property with ',' and return the splitted string as a string[]
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string[] GetPropertyAsArray(string section, string key) {
|
public static string[] GetPropertyAsArray(string section, string key) {
|
||||||
Dictionary<string, string> properties = getProperties(section);
|
Dictionary<string, string> properties = getProperties(section);
|
||||||
string value = GetProperty(section, key);
|
string value = GetProperty(section, key);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
|
@ -324,19 +309,19 @@ namespace Greenshot.Core {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool GetBoolProperty(string section, string key) {
|
public static bool GetBoolProperty(string section, string key) {
|
||||||
Dictionary<string, string> properties = getProperties(section);
|
Dictionary<string, string> properties = getProperties(section);
|
||||||
string value = GetProperty(section, key);
|
string value = GetProperty(section, key);
|
||||||
return bool.Parse(value);
|
return bool.Parse(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int GetIntProperty(string section, string key) {
|
public static int GetIntProperty(string section, string key) {
|
||||||
Dictionary<string, string> properties = getProperties(section);
|
Dictionary<string, string> properties = getProperties(section);
|
||||||
string value = GetProperty(section, key);
|
string value = GetProperty(section, key);
|
||||||
return int.Parse(value);
|
return int.Parse(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Save() {
|
public static void Save() {
|
||||||
LOG.Info("Saving configuration to: " + iniLocation);
|
LOG.Info("Saving configuration to: " + iniLocation);
|
||||||
TextWriter writer = new StreamWriter(iniLocation, false, Encoding.UTF8);
|
TextWriter writer = new StreamWriter(iniLocation, false, Encoding.UTF8);
|
||||||
foreach(IniSection section in sectionMap.Values) {
|
foreach(IniSection section in sectionMap.Values) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue