/SparkleLib/SparkleConfig.cs
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 17 18using System; 19using System.IO; 20using System.Collections.Generic; 21using System.Xml; 22 23namespace SparkleLib { 24 25 public class SparkleConfig : XmlDocument { 26 27 public static SparkleConfig DefaultConfig; 28 public static bool DebugMode = true; 29 30 public string FullPath; 31 public string TmpPath; 32 public string LogFilePath; 33 34 35 public string HomePath { 36 get { 37 if (SparkleBackend.Platform == PlatformID.Win32NT) 38 return Environment.GetFolderPath (Environment.SpecialFolder.UserProfile); 39 else 40 return Environment.GetFolderPath (Environment.SpecialFolder.Personal); 41 } 42 } 43 44 45 public string FoldersPath { 46 get { 47 if (GetConfigOption ("folders_path") != null) 48 return GetConfigOption ("folders_path"); 49 else 50 return Path.Combine (HomePath, "SparkleShare"); 51 } 52 } 53 54 55 public SparkleConfig (string config_path, string config_file_name) 56 { 57 FullPath = Path.Combine (config_path, config_file_name); 58 string logs_path = Path.Combine (config_path, "logs"); 59 60 int i = 1; 61 do { 62 LogFilePath = Path.Combine ( 63 logs_path, "debug_log_" + DateTime.Now.ToString ("yyyy-MM-dd") + "." + i + ".txt"); 64 65 i++; 66 67 } while (File.Exists (LogFilePath)); 68 69 if (!Directory.Exists (logs_path)) 70 Directory.CreateDirectory (logs_path); 71 72 // Delete logs older than a week 73 foreach (FileInfo file in new DirectoryInfo (logs_path).GetFiles ("debug_log*.txt")) { 74 if (file.LastWriteTime < DateTime.Now.AddDays (-7)) 75 file.Delete (); 76 } 77 78 if (!Directory.Exists (config_path)) 79 Directory.CreateDirectory (config_path); 80 81 try { 82 Load (FullPath); 83 84 } catch (TypeInitializationException) { 85 CreateInitialConfig (); 86 87 } catch (FileNotFoundException) { 88 CreateInitialConfig (); 89 90 } catch (XmlException) { 91 FileInfo file = new FileInfo (FullPath); 92 93 if (file.Length == 0) { 94 File.Delete (FullPath); 95 CreateInitialConfig (); 96 97 } else { 98 throw new XmlException (FullPath + " does not contain a valid config XML structure."); 99 } 100 101 } finally { 102 Load (FullPath); 103 TmpPath = Path.Combine (FoldersPath, ".tmp"); 104 Directory.CreateDirectory (TmpPath); 105 } 106 } 107 108 109 private void CreateInitialConfig () 110 { 111 string user_name = "Unknown"; 112 113 if (SparkleBackend.Platform == PlatformID.Unix || 114 SparkleBackend.Platform == PlatformID.MacOSX) { 115 116 user_name = Environment.UserName; 117 if (string.IsNullOrEmpty (user_name)) 118 user_name = "Unknown"; 119 else 120 user_name = user_name.TrimEnd (",".ToCharArray ()); 121 122 } else { 123 user_name = Environment.UserName; 124 } 125 126 127 string n = Environment.NewLine; 128 File.WriteAllText (FullPath, 129 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + n + 130 "<sparkleshare>" + n + 131 " <user>" + n + 132 " <name>" + user_name + "</name>" + n + 133 " <email>Unknown</email>" + n + 134 " </user>" + n + 135 "</sparkleshare>"); 136 } 137 138 139 public SparkleUser User { 140 get { 141 XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()"); 142 XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()"); 143 string user_name = name_node.Value; 144 string user_email = email_node.Value; 145 146 SparkleUser user = new SparkleUser (user_name, user_email); 147 148 string [] private_key_file_paths = Directory.GetFiles (Path.GetDirectoryName (FullPath), "*.key"); 149 150 if (private_key_file_paths.Length > 0) { 151 user.PrivateKey = File.ReadAllText (private_key_file_paths [0]); 152 user.PrivateKeyFilePath = private_key_file_paths [0]; 153 154 user.PublicKey = File.ReadAllText (private_key_file_paths [0] + ".pub"); 155 user.PublicKeyFilePath = private_key_file_paths [0] + ".pub"; 156 } 157 158 return user; 159 } 160 161 set { 162 SparkleUser user = (SparkleUser) value; 163 164 XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()"); 165 XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()"); 166 name_node.InnerText = user.Name; 167 email_node.InnerText = user.Email; 168 169 Save (); 170 } 171 } 172 173 174 public List<string> Folders { 175 get { 176 List<string> folders = new List<string> (); 177 178 foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) 179 folders.Add (node_folder ["name"].InnerText); 180 181 folders.Sort (); 182 183 return folders; 184 } 185 } 186 187 188 public void AddFolder (string name, string identifier, string url, string backend) 189 { 190 XmlNode node_name = CreateElement ("name"); 191 XmlNode node_identifier = CreateElement ("identifier"); 192 XmlNode node_url = CreateElement ("url"); 193 XmlNode node_backend = CreateElement ("backend"); 194 195 node_name.InnerText = name; 196 node_identifier.InnerText = identifier; 197 node_url.InnerText = url; 198 node_backend.InnerText = backend; 199 200 XmlNode node_folder = CreateNode (XmlNodeType.Element, "folder", null); 201 202 node_folder.AppendChild (node_name); 203 node_folder.AppendChild (node_identifier); 204 node_folder.AppendChild (node_url); 205 node_folder.AppendChild (node_backend); 206 207 XmlNode node_root = SelectSingleNode ("/sparkleshare"); 208 node_root.AppendChild (node_folder); 209 210 Save (); 211 } 212 213 214 public void RemoveFolder (string name) 215 { 216 foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) { 217 if (node_folder ["name"].InnerText.Equals (name)) 218 SelectSingleNode ("/sparkleshare").RemoveChild (node_folder); 219 } 220 221 Save (); 222 } 223 224 225 public void RenameFolder (string identifier, string name) 226 { 227 XmlNode node_folder = SelectSingleNode ( 228 string.Format ("/sparkleshare/folder[identifier=\"{0}\"]", identifier)); 229 230 node_folder ["name"].InnerText = name; 231 Save (); 232 } 233 234 235 public string GetBackendForFolder (string name) 236 { 237 return GetFolderValue (name, "backend"); 238 } 239 240 241 public string GetIdentifierForFolder (string name) 242 { 243 return GetFolderValue (name, "identifier"); 244 } 245 246 247 public string GetUrlForFolder (string name) 248 { 249 return GetFolderValue (name, "url"); 250 } 251 252 253 public bool IdentifierExists (string identifier) 254 { 255 if (identifier == null) 256 throw new ArgumentNullException (); 257 258 foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) { 259 XmlElement folder_id = node_folder ["identifier"]; 260 261 if (folder_id != null && identifier.Equals (folder_id.InnerText)) 262 return true; 263 } 264 265 return false; 266 } 267 268 269 public bool SetFolderOptionalAttribute (string folder_name, string key, string value) 270 { 271 XmlNode folder = GetFolder (folder_name); 272 273 if (folder == null) 274 return false; 275 276 if (folder [key] != null) { 277 folder [key].InnerText = value; 278 279 } else { 280 XmlNode new_node = CreateElement (key); 281 new_node.InnerText = value; 282 folder.AppendChild (new_node); 283 } 284 285 Save (); 286 287 return true; 288 } 289 290 291 public string GetFolderOptionalAttribute (string folder_name, string key) 292 { 293 XmlNode folder = GetFolder (folder_name); 294 295 if (folder != null) { 296 if (folder [key] != null) 297 return folder [key].InnerText; 298 else 299 return null; 300 301 } else { 302 return null; 303 } 304 } 305 306 307 public string GetConfigOption (string name) 308 { 309 XmlNode node = SelectSingleNode ("/sparkleshare/" + name); 310 311 if (node != null) 312 return node.InnerText; 313 else 314 return null; 315 } 316 317 318 public void SetConfigOption (string name, string content) 319 { 320 XmlNode node = SelectSingleNode ("/sparkleshare/" + name); 321 322 if (node != null) { 323 node.InnerText = content; 324 325 } else { 326 node = CreateElement (name); 327 node.InnerText = content; 328 329 XmlNode node_root = SelectSingleNode ("/sparkleshare"); 330 node_root.AppendChild (node); 331 } 332 333 Save (); 334 SparkleLogger.LogInfo ("Config", "Updated option " + name + ":" + content); 335 } 336 337 338 private XmlNode GetFolder (string name) 339 { 340 return SelectSingleNode (string.Format ("/sparkleshare/folder[name=\"{0}\"]", name)); 341 } 342 343 344 private string GetFolderValue (string name, string key) 345 { 346 XmlNode folder = GetFolder(name); 347 348 if ((folder != null) && (folder [key] != null)) 349 return folder [key].InnerText; 350 else 351 return null; 352 } 353 354 355 private void Save () 356 { 357 if (!File.Exists (FullPath)) 358 throw new FileNotFoundException (FullPath + " does not exist"); 359 360 Save (FullPath); 361 SparkleLogger.LogInfo ("Config", "Wrote to '" + FullPath + "'"); 362 } 363 } 364}