/* Copyright (C) 2014-2018 Tal Aloni . 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 ReadSharesSettings() { var shares = new List(); 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 ReadAccessList(XmlNode node) { var result = new List(); 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 ReadConfigs() { var document = ReadSettingsXML(); var configs = document.SelectSingleNode("Settings/Configs"); var dic = new Dictionary(); 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; } } }