/SeventhGate/SeventhGate/ConfigStore.cs
C# | 350 lines | 261 code | 36 blank | 53 comment | 24 complexity | fcd18aa7f4fb5508d8789ecb285c0c83 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
- using System.Xml.Serialization;
- using System.Collections.Generic;
- using System.IO;
- using System;
- using System.Security;
- using System.Linq;
- using winapi;
- using System.Collections.ObjectModel;
-
- namespace SeventhGate
- {
- #region Single Configuration Element
-
- /// <summary>
- /// Class represents single configuration element with value
- /// </summary>
- public class ConfigElement
- {
- /// <summary>
- /// Key
- /// </summary>
- public string Key
- {
- get;
- set;
- }
-
- /// <summary>
- /// Value
- /// </summary>
- public string Value
- {
- get;
- set;
- }
- }
-
- #endregion
-
- #region Main Configuration class
-
- public static class ConfigStore
- {
- /// <summary>
- /// Represents a list of current config elements
- /// Keeps current program configuration
- /// </summary>
- private static List<ConfigElement> config = new List<ConfigElement>();
-
- /// <summary>
- /// Adds new config element
- /// </summary>
- /// <param name="element"></param>
- public static void Add(ConfigElement element)
- {
- config.Add(element);
- }
-
- /// <summary>
- /// Removes specified config element
- /// </summary>
- /// <param name="element"></param>
- public static void Remove(ConfigElement element)
- {
- config.Remove(element);
- }
-
- /// <summary>
- /// Gets specified config element
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static ConfigElement Get(string key)
- {
- var elems = from e in config
- where string.Equals(key, e.Key)
- select e;
- if(elems.Count() > 0)
- return elems.First();
- return null;
- }
-
- /// <summary>
- /// Gets value of specified config elements
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public static object GetValue(string key)
- {
- var elems = from e in config
- where string.Equals(key, e.Key)
- select e;
- if(elems.Count() > 0)
- return elems.First().Value;
- return "null";
- }
-
- /// <summary>
- /// Sets specified config element
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- public static void Set(string key, string value)
- {
- var e = Get(key);
- if(e == null)
- {
- Add(new ConfigElement()
- {
- Key = key,
- Value = value
- });
- return;
- }
- e.Value = value;
- }
-
- /// <summary>
- /// Loads program configuration from file
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public static bool Load(string filePath)
- {
- if(File.Exists(filePath) == false)
- return false;
-
- FileStream fileStream = null;
- XmlSerializer xmlSerializer = null;
- List<ConfigElement> collection = null;
-
- try
- {
- fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
- xmlSerializer = new XmlSerializer(typeof(List<ConfigElement>));
- collection = (List<ConfigElement>)xmlSerializer.Deserialize(fileStream);
-
- fileStream.Close();
- xmlSerializer = null;
-
- config.Clear();
- config.AddRange(collection);
-
- return CheckLoadedConfigFile();
- }
- catch(ArgumentNullException ex)
- {
- Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
- }
- catch(ArgumentOutOfRangeException ex)
- {
- Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
- }
- catch(ArgumentException ex)
- {
- Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
- }
- catch(NotSupportedException ex)
- {
- Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
- }
- catch(FileNotFoundException ex)
- {
- Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
- }
- catch(DirectoryNotFoundException ex)
- {
- Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
- }
- catch(PathTooLongException ex)
- {
- Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
- }
- catch(IOException ex)
- {
- Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
- }
- catch(SecurityException ex)
- {
- Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
- }
- catch(UnauthorizedAccessException ex)
- {
- Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
- }
-
- return false;
- }
-
- /// <summary>
- /// Method checks all config elements whether they can be found
- /// </summary>
- /// <returns></returns>
- private static bool CheckLoadedConfigFile()
- {
- ConfigElement c = ConfigStore.Get(Constants.ConfigStringNetworkName);
- if(c == null)
- {
- Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringNetworkName + " section in loaded config file.");
- return false;
- }
-
- c = ConfigStore.Get(Constants.ConfigStringNetworkPass);
- if(c == null)
- {
- Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringNetworkPass + " section in loaded config file.");
- return false;
- }
-
- c = ConfigStore.Get(Constants.ConfigStringNetworkPassShown);
- if(c == null)
- {
- Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringNetworkPassShown + " section in loaded config file.");
- return false;
- }
-
- c = ConfigStore.Get(Constants.ConfigStringMaxPeersNum);
- if(c == null)
- {
- Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringMaxPeersNum + " section in loaded config file.");
- return false;
- }
-
- c = ConfigStore.Get(Constants.ConfigStringDebugWindow);
- if(c == null)
- {
- Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringDebugWindow + " section in loaded config file.");
- return false;
- }
-
- c = ConfigStore.Get(Constants.ConfigStringCheckUpdatesAtStart);
- if(c == null)
- {
- Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringCheckUpdatesAtStart + " section in loaded config file.");
- return false;
- }
-
- c = ConfigStore.Get(Constants.ConfigStringAnimateTrayIcon);
- if(c == null)
- {
- Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringAnimateTrayIcon + " section in loaded config file.");
- return false;
- }
-
- c = ConfigStore.Get(Constants.ConfigStringLastUsedConnection);
- if(c == null)
- {
- Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringLastUsedConnection + " section in loaded config file.");
- return false;
- }
-
- c = ConfigStore.Get(Constants.ConfigStringStartMinimized);
- if(c == null)
- {
- Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringStartMinimized + " section in loaded config file.");
- return false;
- }
-
- return true;
- }
-
- /// <summary>
- /// Saves program configuration to file
- /// </summary>
- /// <param name="filePath"></param>
- public static bool Save(string filePath)
- {
- FileStream fileStream = null;
- XmlSerializer xmlSerializer = null;
-
- try
- {
- fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
- xmlSerializer = new XmlSerializer(typeof(List<ConfigElement>));
-
- xmlSerializer.Serialize(fileStream, config);
- fileStream.Close();
-
- return true;
- }
- catch(ArgumentNullException ex)
- {
- Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
- }
- catch(ArgumentOutOfRangeException ex)
- {
- Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
- }
- catch(ArgumentException ex)
- {
- Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
- }
- catch(NotSupportedException ex)
- {
- Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
- }
- catch(FileNotFoundException ex)
- {
- Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
- }
- catch(DirectoryNotFoundException ex)
- {
- Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
- }
- catch(PathTooLongException ex)
- {
- Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
- }
- catch(IOException ex)
- {
- Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
- }
- catch(SecurityException ex)
- {
- Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
- }
- catch(UnauthorizedAccessException ex)
- {
- Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
- }
- catch(InvalidOperationException ex)
- {
- Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
- }
-
- return false;
- }
-
- /// <summary>
- /// Creates empty standard program config if file not found
- /// with default configuration
- /// </summary>
- public static void NewConfig()
- {
- Set(Constants.ConfigStringNetworkName, "MyNetworkName");
- Set(Constants.ConfigStringNetworkPass, "myp1a2s3s4w5o6r6d7");
- Set(Constants.ConfigStringNetworkPassShown, bool.TrueString);
- Set(Constants.ConfigStringMaxPeersNum, "5");
- Set(Constants.ConfigStringDebugWindow, bool.TrueString);
- Set(Constants.ConfigStringCheckUpdatesAtStart, bool.TrueString);
- Set(Constants.ConfigStringAnimateTrayIcon, bool.TrueString);
- Set(Constants.ConfigStringLastUsedConnection, "");
- Set(Constants.ConfigStringStartMinimized, bool.FalseString);
-
- Save(Constants.ConfigStringFileName);
- }
- }
-
- #endregion
- }