/SparkleLib/SparkleConfig.cs

http://github.com/hbons/SparkleShare · C# · 364 lines · 249 code · 99 blank · 16 comment · 43 complexity · 3cb625bc09f32ff56221f5334fa3c83a MD5 · raw file

  1. // SparkleShare, a collaboration and sharing tool.
  2. // Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as
  6. // published by the Free Software Foundation, either version 3 of the
  7. // License, or (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using System.IO;
  18. using System.Collections.Generic;
  19. using System.Xml;
  20. namespace SparkleLib {
  21. public class SparkleConfig : XmlDocument {
  22. public static SparkleConfig DefaultConfig;
  23. public static bool DebugMode = true;
  24. public string FullPath;
  25. public string TmpPath;
  26. public string LogFilePath;
  27. public string HomePath {
  28. get {
  29. if (SparkleBackend.Platform == PlatformID.Win32NT)
  30. return Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
  31. else
  32. return Environment.GetFolderPath (Environment.SpecialFolder.Personal);
  33. }
  34. }
  35. public string FoldersPath {
  36. get {
  37. if (GetConfigOption ("folders_path") != null)
  38. return GetConfigOption ("folders_path");
  39. else
  40. return Path.Combine (HomePath, "SparkleShare");
  41. }
  42. }
  43. public SparkleConfig (string config_path, string config_file_name)
  44. {
  45. FullPath = Path.Combine (config_path, config_file_name);
  46. string logs_path = Path.Combine (config_path, "logs");
  47. int i = 1;
  48. do {
  49. LogFilePath = Path.Combine (
  50. logs_path, "debug_log_" + DateTime.Now.ToString ("yyyy-MM-dd") + "." + i + ".txt");
  51. i++;
  52. } while (File.Exists (LogFilePath));
  53. if (!Directory.Exists (logs_path))
  54. Directory.CreateDirectory (logs_path);
  55. // Delete logs older than a week
  56. foreach (FileInfo file in new DirectoryInfo (logs_path).GetFiles ("debug_log*.txt")) {
  57. if (file.LastWriteTime < DateTime.Now.AddDays (-7))
  58. file.Delete ();
  59. }
  60. if (!Directory.Exists (config_path))
  61. Directory.CreateDirectory (config_path);
  62. try {
  63. Load (FullPath);
  64. } catch (TypeInitializationException) {
  65. CreateInitialConfig ();
  66. } catch (FileNotFoundException) {
  67. CreateInitialConfig ();
  68. } catch (XmlException) {
  69. FileInfo file = new FileInfo (FullPath);
  70. if (file.Length == 0) {
  71. File.Delete (FullPath);
  72. CreateInitialConfig ();
  73. } else {
  74. throw new XmlException (FullPath + " does not contain a valid config XML structure.");
  75. }
  76. } finally {
  77. Load (FullPath);
  78. TmpPath = Path.Combine (FoldersPath, ".tmp");
  79. Directory.CreateDirectory (TmpPath);
  80. }
  81. }
  82. private void CreateInitialConfig ()
  83. {
  84. string user_name = "Unknown";
  85. if (SparkleBackend.Platform == PlatformID.Unix ||
  86. SparkleBackend.Platform == PlatformID.MacOSX) {
  87. user_name = Environment.UserName;
  88. if (string.IsNullOrEmpty (user_name))
  89. user_name = "Unknown";
  90. else
  91. user_name = user_name.TrimEnd (",".ToCharArray ());
  92. } else {
  93. user_name = Environment.UserName;
  94. }
  95. string n = Environment.NewLine;
  96. File.WriteAllText (FullPath,
  97. "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + n +
  98. "<sparkleshare>" + n +
  99. " <user>" + n +
  100. " <name>" + user_name + "</name>" + n +
  101. " <email>Unknown</email>" + n +
  102. " </user>" + n +
  103. "</sparkleshare>");
  104. }
  105. public SparkleUser User {
  106. get {
  107. XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()");
  108. XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()");
  109. string user_name = name_node.Value;
  110. string user_email = email_node.Value;
  111. SparkleUser user = new SparkleUser (user_name, user_email);
  112. string [] private_key_file_paths = Directory.GetFiles (Path.GetDirectoryName (FullPath), "*.key");
  113. if (private_key_file_paths.Length > 0) {
  114. user.PrivateKey = File.ReadAllText (private_key_file_paths [0]);
  115. user.PrivateKeyFilePath = private_key_file_paths [0];
  116. user.PublicKey = File.ReadAllText (private_key_file_paths [0] + ".pub");
  117. user.PublicKeyFilePath = private_key_file_paths [0] + ".pub";
  118. }
  119. return user;
  120. }
  121. set {
  122. SparkleUser user = (SparkleUser) value;
  123. XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()");
  124. XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()");
  125. name_node.InnerText = user.Name;
  126. email_node.InnerText = user.Email;
  127. Save ();
  128. }
  129. }
  130. public List<string> Folders {
  131. get {
  132. List<string> folders = new List<string> ();
  133. foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder"))
  134. folders.Add (node_folder ["name"].InnerText);
  135. folders.Sort ();
  136. return folders;
  137. }
  138. }
  139. public void AddFolder (string name, string identifier, string url, string backend)
  140. {
  141. XmlNode node_name = CreateElement ("name");
  142. XmlNode node_identifier = CreateElement ("identifier");
  143. XmlNode node_url = CreateElement ("url");
  144. XmlNode node_backend = CreateElement ("backend");
  145. node_name.InnerText = name;
  146. node_identifier.InnerText = identifier;
  147. node_url.InnerText = url;
  148. node_backend.InnerText = backend;
  149. XmlNode node_folder = CreateNode (XmlNodeType.Element, "folder", null);
  150. node_folder.AppendChild (node_name);
  151. node_folder.AppendChild (node_identifier);
  152. node_folder.AppendChild (node_url);
  153. node_folder.AppendChild (node_backend);
  154. XmlNode node_root = SelectSingleNode ("/sparkleshare");
  155. node_root.AppendChild (node_folder);
  156. Save ();
  157. }
  158. public void RemoveFolder (string name)
  159. {
  160. foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) {
  161. if (node_folder ["name"].InnerText.Equals (name))
  162. SelectSingleNode ("/sparkleshare").RemoveChild (node_folder);
  163. }
  164. Save ();
  165. }
  166. public void RenameFolder (string identifier, string name)
  167. {
  168. XmlNode node_folder = SelectSingleNode (
  169. string.Format ("/sparkleshare/folder[identifier=\"{0}\"]", identifier));
  170. node_folder ["name"].InnerText = name;
  171. Save ();
  172. }
  173. public string GetBackendForFolder (string name)
  174. {
  175. return GetFolderValue (name, "backend");
  176. }
  177. public string GetIdentifierForFolder (string name)
  178. {
  179. return GetFolderValue (name, "identifier");
  180. }
  181. public string GetUrlForFolder (string name)
  182. {
  183. return GetFolderValue (name, "url");
  184. }
  185. public bool IdentifierExists (string identifier)
  186. {
  187. if (identifier == null)
  188. throw new ArgumentNullException ();
  189. foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) {
  190. XmlElement folder_id = node_folder ["identifier"];
  191. if (folder_id != null && identifier.Equals (folder_id.InnerText))
  192. return true;
  193. }
  194. return false;
  195. }
  196. public bool SetFolderOptionalAttribute (string folder_name, string key, string value)
  197. {
  198. XmlNode folder = GetFolder (folder_name);
  199. if (folder == null)
  200. return false;
  201. if (folder [key] != null) {
  202. folder [key].InnerText = value;
  203. } else {
  204. XmlNode new_node = CreateElement (key);
  205. new_node.InnerText = value;
  206. folder.AppendChild (new_node);
  207. }
  208. Save ();
  209. return true;
  210. }
  211. public string GetFolderOptionalAttribute (string folder_name, string key)
  212. {
  213. XmlNode folder = GetFolder (folder_name);
  214. if (folder != null) {
  215. if (folder [key] != null)
  216. return folder [key].InnerText;
  217. else
  218. return null;
  219. } else {
  220. return null;
  221. }
  222. }
  223. public string GetConfigOption (string name)
  224. {
  225. XmlNode node = SelectSingleNode ("/sparkleshare/" + name);
  226. if (node != null)
  227. return node.InnerText;
  228. else
  229. return null;
  230. }
  231. public void SetConfigOption (string name, string content)
  232. {
  233. XmlNode node = SelectSingleNode ("/sparkleshare/" + name);
  234. if (node != null) {
  235. node.InnerText = content;
  236. } else {
  237. node = CreateElement (name);
  238. node.InnerText = content;
  239. XmlNode node_root = SelectSingleNode ("/sparkleshare");
  240. node_root.AppendChild (node);
  241. }
  242. Save ();
  243. SparkleLogger.LogInfo ("Config", "Updated option " + name + ":" + content);
  244. }
  245. private XmlNode GetFolder (string name)
  246. {
  247. return SelectSingleNode (string.Format ("/sparkleshare/folder[name=\"{0}\"]", name));
  248. }
  249. private string GetFolderValue (string name, string key)
  250. {
  251. XmlNode folder = GetFolder(name);
  252. if ((folder != null) && (folder [key] != null))
  253. return folder [key].InnerText;
  254. else
  255. return null;
  256. }
  257. private void Save ()
  258. {
  259. if (!File.Exists (FullPath))
  260. throw new FileNotFoundException (FullPath + " does not exist");
  261. Save (FullPath);
  262. SparkleLogger.LogInfo ("Config", "Wrote to '" + FullPath + "'");
  263. }
  264. }
  265. }