PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. using System.Xml.Serialization;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System;
  5. using System.Security;
  6. using System.Linq;
  7. using winapi;
  8. using System.Collections.ObjectModel;
  9. namespace SeventhGate
  10. {
  11. #region Single Configuration Element
  12. /// <summary>
  13. /// Class represents single configuration element with value
  14. /// </summary>
  15. public class ConfigElement
  16. {
  17. /// <summary>
  18. /// Key
  19. /// </summary>
  20. public string Key
  21. {
  22. get;
  23. set;
  24. }
  25. /// <summary>
  26. /// Value
  27. /// </summary>
  28. public string Value
  29. {
  30. get;
  31. set;
  32. }
  33. }
  34. #endregion
  35. #region Main Configuration class
  36. public static class ConfigStore
  37. {
  38. /// <summary>
  39. /// Represents a list of current config elements
  40. /// Keeps current program configuration
  41. /// </summary>
  42. private static List<ConfigElement> config = new List<ConfigElement>();
  43. /// <summary>
  44. /// Adds new config element
  45. /// </summary>
  46. /// <param name="element"></param>
  47. public static void Add(ConfigElement element)
  48. {
  49. config.Add(element);
  50. }
  51. /// <summary>
  52. /// Removes specified config element
  53. /// </summary>
  54. /// <param name="element"></param>
  55. public static void Remove(ConfigElement element)
  56. {
  57. config.Remove(element);
  58. }
  59. /// <summary>
  60. /// Gets specified config element
  61. /// </summary>
  62. /// <param name="key"></param>
  63. /// <returns></returns>
  64. public static ConfigElement Get(string key)
  65. {
  66. var elems = from e in config
  67. where string.Equals(key, e.Key)
  68. select e;
  69. if(elems.Count() > 0)
  70. return elems.First();
  71. return null;
  72. }
  73. /// <summary>
  74. /// Gets value of specified config elements
  75. /// </summary>
  76. /// <param name="key"></param>
  77. /// <returns></returns>
  78. public static object GetValue(string key)
  79. {
  80. var elems = from e in config
  81. where string.Equals(key, e.Key)
  82. select e;
  83. if(elems.Count() > 0)
  84. return elems.First().Value;
  85. return "null";
  86. }
  87. /// <summary>
  88. /// Sets specified config element
  89. /// </summary>
  90. /// <param name="key"></param>
  91. /// <param name="value"></param>
  92. public static void Set(string key, string value)
  93. {
  94. var e = Get(key);
  95. if(e == null)
  96. {
  97. Add(new ConfigElement()
  98. {
  99. Key = key,
  100. Value = value
  101. });
  102. return;
  103. }
  104. e.Value = value;
  105. }
  106. /// <summary>
  107. /// Loads program configuration from file
  108. /// </summary>
  109. /// <param name="filePath"></param>
  110. /// <returns></returns>
  111. public static bool Load(string filePath)
  112. {
  113. if(File.Exists(filePath) == false)
  114. return false;
  115. FileStream fileStream = null;
  116. XmlSerializer xmlSerializer = null;
  117. List<ConfigElement> collection = null;
  118. try
  119. {
  120. fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  121. xmlSerializer = new XmlSerializer(typeof(List<ConfigElement>));
  122. collection = (List<ConfigElement>)xmlSerializer.Deserialize(fileStream);
  123. fileStream.Close();
  124. xmlSerializer = null;
  125. config.Clear();
  126. config.AddRange(collection);
  127. return CheckLoadedConfigFile();
  128. }
  129. catch(ArgumentNullException ex)
  130. {
  131. Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
  132. }
  133. catch(ArgumentOutOfRangeException ex)
  134. {
  135. Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
  136. }
  137. catch(ArgumentException ex)
  138. {
  139. Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
  140. }
  141. catch(NotSupportedException ex)
  142. {
  143. Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
  144. }
  145. catch(FileNotFoundException ex)
  146. {
  147. Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
  148. }
  149. catch(DirectoryNotFoundException ex)
  150. {
  151. Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
  152. }
  153. catch(PathTooLongException ex)
  154. {
  155. Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
  156. }
  157. catch(IOException ex)
  158. {
  159. Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
  160. }
  161. catch(SecurityException ex)
  162. {
  163. Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
  164. }
  165. catch(UnauthorizedAccessException ex)
  166. {
  167. Logger.LogException("ConfigStore", "Load", "Error reading configuration file", ex);
  168. }
  169. return false;
  170. }
  171. /// <summary>
  172. /// Method checks all config elements whether they can be found
  173. /// </summary>
  174. /// <returns></returns>
  175. private static bool CheckLoadedConfigFile()
  176. {
  177. ConfigElement c = ConfigStore.Get(Constants.ConfigStringNetworkName);
  178. if(c == null)
  179. {
  180. Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringNetworkName + " section in loaded config file.");
  181. return false;
  182. }
  183. c = ConfigStore.Get(Constants.ConfigStringNetworkPass);
  184. if(c == null)
  185. {
  186. Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringNetworkPass + " section in loaded config file.");
  187. return false;
  188. }
  189. c = ConfigStore.Get(Constants.ConfigStringNetworkPassShown);
  190. if(c == null)
  191. {
  192. Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringNetworkPassShown + " section in loaded config file.");
  193. return false;
  194. }
  195. c = ConfigStore.Get(Constants.ConfigStringMaxPeersNum);
  196. if(c == null)
  197. {
  198. Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringMaxPeersNum + " section in loaded config file.");
  199. return false;
  200. }
  201. c = ConfigStore.Get(Constants.ConfigStringDebugWindow);
  202. if(c == null)
  203. {
  204. Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringDebugWindow + " section in loaded config file.");
  205. return false;
  206. }
  207. c = ConfigStore.Get(Constants.ConfigStringCheckUpdatesAtStart);
  208. if(c == null)
  209. {
  210. Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringCheckUpdatesAtStart + " section in loaded config file.");
  211. return false;
  212. }
  213. c = ConfigStore.Get(Constants.ConfigStringAnimateTrayIcon);
  214. if(c == null)
  215. {
  216. Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringAnimateTrayIcon + " section in loaded config file.");
  217. return false;
  218. }
  219. c = ConfigStore.Get(Constants.ConfigStringLastUsedConnection);
  220. if(c == null)
  221. {
  222. Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringLastUsedConnection + " section in loaded config file.");
  223. return false;
  224. }
  225. c = ConfigStore.Get(Constants.ConfigStringStartMinimized);
  226. if(c == null)
  227. {
  228. Logger.LogError("ConfigStore", "CheckLoadedConfigFile", "Error validating config file, no " + Constants.ConfigStringStartMinimized + " section in loaded config file.");
  229. return false;
  230. }
  231. return true;
  232. }
  233. /// <summary>
  234. /// Saves program configuration to file
  235. /// </summary>
  236. /// <param name="filePath"></param>
  237. public static bool Save(string filePath)
  238. {
  239. FileStream fileStream = null;
  240. XmlSerializer xmlSerializer = null;
  241. try
  242. {
  243. fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
  244. xmlSerializer = new XmlSerializer(typeof(List<ConfigElement>));
  245. xmlSerializer.Serialize(fileStream, config);
  246. fileStream.Close();
  247. return true;
  248. }
  249. catch(ArgumentNullException ex)
  250. {
  251. Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
  252. }
  253. catch(ArgumentOutOfRangeException ex)
  254. {
  255. Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
  256. }
  257. catch(ArgumentException ex)
  258. {
  259. Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
  260. }
  261. catch(NotSupportedException ex)
  262. {
  263. Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
  264. }
  265. catch(FileNotFoundException ex)
  266. {
  267. Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
  268. }
  269. catch(DirectoryNotFoundException ex)
  270. {
  271. Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
  272. }
  273. catch(PathTooLongException ex)
  274. {
  275. Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
  276. }
  277. catch(IOException ex)
  278. {
  279. Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
  280. }
  281. catch(SecurityException ex)
  282. {
  283. Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
  284. }
  285. catch(UnauthorizedAccessException ex)
  286. {
  287. Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
  288. }
  289. catch(InvalidOperationException ex)
  290. {
  291. Logger.LogException("ConfigStore", "Save", "Error saving configuration file", ex);
  292. }
  293. return false;
  294. }
  295. /// <summary>
  296. /// Creates empty standard program config if file not found
  297. /// with default configuration
  298. /// </summary>
  299. public static void NewConfig()
  300. {
  301. Set(Constants.ConfigStringNetworkName, "MyNetworkName");
  302. Set(Constants.ConfigStringNetworkPass, "myp1a2s3s4w5o6r6d7");
  303. Set(Constants.ConfigStringNetworkPassShown, bool.TrueString);
  304. Set(Constants.ConfigStringMaxPeersNum, "5");
  305. Set(Constants.ConfigStringDebugWindow, bool.TrueString);
  306. Set(Constants.ConfigStringCheckUpdatesAtStart, bool.TrueString);
  307. Set(Constants.ConfigStringAnimateTrayIcon, bool.TrueString);
  308. Set(Constants.ConfigStringLastUsedConnection, "");
  309. Set(Constants.ConfigStringStartMinimized, bool.FalseString);
  310. Save(Constants.ConfigStringFileName);
  311. }
  312. }
  313. #endregion
  314. }