123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /* Copyright (C) 2014-2018 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
- *
- * You can redistribute this program and/or modify it under the terms of
- * the GNU Lesser Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- */
- using System.Collections.Generic;
- using System.IO;
- using System.Windows.Forms;
- using System.Xml;
- namespace SMBServer
- {
- public class SettingsHelper
- {
- public const string SettingsFileName = "Settings.xml";
- public static XmlDocument ReadXmlDocument(string path)
- {
- var doc = new XmlDocument();
- doc.Load(path);
- return doc;
- }
- public static XmlDocument ReadSettingsXML()
- {
- var executableDirectory = Path.GetDirectoryName(Application.ExecutablePath) + "\\";
- var document = ReadXmlDocument(executableDirectory + SettingsFileName);
- return document;
- }
- public static UserCollection ReadUserSettings()
- {
- var users = new UserCollection();
- var document = ReadSettingsXML();
- var usersNode = document.SelectSingleNode("Settings/Users");
- foreach (XmlNode userNode in usersNode.ChildNodes)
- {
- var accountName = userNode.Attributes["AccountName"].Value;
- var password = userNode.Attributes["Password"].Value;
- users.Add(accountName, password);
- }
- return users;
- }
- public static List<ShareSettings> ReadSharesSettings()
- {
- var shares = new List<ShareSettings>();
- var document = ReadSettingsXML();
- var sharesNode = document.SelectSingleNode("Settings/Shares");
- foreach (XmlNode shareNode in sharesNode.ChildNodes)
- {
- var shareName = shareNode.Attributes["Name"].Value;
- var sharePath = shareNode.Attributes["Path"].Value;
- var readAccessNode = shareNode.SelectSingleNode("ReadAccess");
- var readAccess = ReadAccessList(readAccessNode);
- var writeAccessNode = shareNode.SelectSingleNode("WriteAccess");
- var writeAccess = ReadAccessList(writeAccessNode);
- var share = new ShareSettings(shareName, sharePath, readAccess, writeAccess);
- shares.Add(share);
- }
- return shares;
- }
- private static List<string> ReadAccessList(XmlNode node)
- {
- var result = new List<string>();
- if (node != null)
- {
- var accounts = node.Attributes["Accounts"].Value;
- if (accounts == "*")
- {
- result.Add("Users");
- }
- else
- {
- var splitted = accounts.Split(',');
- result.AddRange(splitted);
- }
- }
- return result;
- }
- public static Dictionary<string, string> ReadConfigs()
- {
- var document = ReadSettingsXML();
- var configs = document.SelectSingleNode("Settings/Configs");
-
- var dic = new Dictionary<string, string>();
- if (null == configs) return dic;
- foreach (XmlElement element in configs)
- {
- var key = element.Attributes["Key"]?.Value;
- var value = element.Attributes["Value"]?.Value;
- if (false == string.IsNullOrEmpty(key) && false == string.IsNullOrEmpty(value)) dic[key] = value;
- }
- return dic;
- }
- }
- }
|