SettingsHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 2014-2018 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. using System.Xml;
  11. namespace SMBServer
  12. {
  13. public class SettingsHelper
  14. {
  15. public const string SettingsFileName = "Settings.xml";
  16. public static XmlDocument ReadXmlDocument(string path)
  17. {
  18. var doc = new XmlDocument();
  19. doc.Load(path);
  20. return doc;
  21. }
  22. public static XmlDocument ReadSettingsXML()
  23. {
  24. var executableDirectory = Path.GetDirectoryName(Application.ExecutablePath) + "\\";
  25. var document = ReadXmlDocument(executableDirectory + SettingsFileName);
  26. return document;
  27. }
  28. public static UserCollection ReadUserSettings()
  29. {
  30. var users = new UserCollection();
  31. var document = ReadSettingsXML();
  32. var usersNode = document.SelectSingleNode("Settings/Users");
  33. foreach (XmlNode userNode in usersNode.ChildNodes)
  34. {
  35. var accountName = userNode.Attributes["AccountName"].Value;
  36. var password = userNode.Attributes["Password"].Value;
  37. users.Add(accountName, password);
  38. }
  39. return users;
  40. }
  41. public static List<ShareSettings> ReadSharesSettings()
  42. {
  43. var shares = new List<ShareSettings>();
  44. var document = ReadSettingsXML();
  45. var sharesNode = document.SelectSingleNode("Settings/Shares");
  46. foreach (XmlNode shareNode in sharesNode.ChildNodes)
  47. {
  48. var shareName = shareNode.Attributes["Name"].Value;
  49. var sharePath = shareNode.Attributes["Path"].Value;
  50. var readAccessNode = shareNode.SelectSingleNode("ReadAccess");
  51. var readAccess = ReadAccessList(readAccessNode);
  52. var writeAccessNode = shareNode.SelectSingleNode("WriteAccess");
  53. var writeAccess = ReadAccessList(writeAccessNode);
  54. var share = new ShareSettings(shareName, sharePath, readAccess, writeAccess);
  55. shares.Add(share);
  56. }
  57. return shares;
  58. }
  59. private static List<string> ReadAccessList(XmlNode node)
  60. {
  61. var result = new List<string>();
  62. if (node != null)
  63. {
  64. var accounts = node.Attributes["Accounts"].Value;
  65. if (accounts == "*")
  66. {
  67. result.Add("Users");
  68. }
  69. else
  70. {
  71. var splitted = accounts.Split(',');
  72. result.AddRange(splitted);
  73. }
  74. }
  75. return result;
  76. }
  77. public static Dictionary<string, string> ReadConfigs()
  78. {
  79. var document = ReadSettingsXML();
  80. var configs = document.SelectSingleNode("Settings/Configs");
  81. var dic = new Dictionary<string, string>();
  82. if (null == configs) return dic;
  83. foreach (XmlElement element in configs)
  84. {
  85. var key = element.Attributes["Key"]?.Value;
  86. var value = element.Attributes["Value"]?.Value;
  87. if (false == string.IsNullOrEmpty(key) && false == string.IsNullOrEmpty(value)) dic[key] = value;
  88. }
  89. return dic;
  90. }
  91. }
  92. }