SparkleShare/SparkleLib/SparkleConfig.cs

377 lines
11 KiB
C#
Raw Normal View History

// SparkleShare, a collaboration and sharing tool.
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.IO;
2011-05-25 20:46:46 +00:00
using System.Collections.Generic;
using System.Security.Principal;
using System.Xml;
namespace SparkleLib {
public class SparkleConfig : XmlDocument {
private static string default_config_path = Path.Combine (
2011-07-24 01:00:40 +00:00
Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData),
2011-08-26 23:11:53 +00:00
"sparkleshare");
2011-07-24 01:00:40 +00:00
public static SparkleConfig DefaultConfig = new SparkleConfig (default_config_path, "config.xml");
public static bool DebugMode = true;
2011-07-24 01:00:40 +00:00
public string FullPath;
2012-01-12 00:21:34 +00:00
public string TmpPath;
public string LogFilePath;
2012-04-15 14:58:39 +00:00
public string HomePath {
get {
if (GetConfigOption ("home_path") != null) {
2012-01-07 08:19:44 +00:00
return GetConfigOption ("home_path");
2012-03-17 23:15:35 +00:00
} else if (SparkleHelpers.IsWindows) {
try {
Environment.SpecialFolder folder =
(Environment.SpecialFolder) Enum.Parse (
typeof(Environment.SpecialFolder), "UserProfile");
2012-03-17 23:15:35 +00:00
return (Environment.GetFolderPath (folder));
} catch {
return Environment.GetFolderPath (Environment.SpecialFolder.Personal);
}
} else
return Environment.GetFolderPath (Environment.SpecialFolder.Personal);
2012-01-07 08:19:44 +00:00
}
}
2012-04-15 14:58:39 +00:00
2011-07-24 01:00:40 +00:00
public string FoldersPath {
get {
if (GetConfigOption ("folders_path") != null)
return GetConfigOption ("folders_path");
else
return Path.Combine (HomePath, "SparkleShare");
}
}
2012-04-15 14:58:39 +00:00
2011-05-26 19:46:49 +00:00
public SparkleConfig (string config_path, string config_file_name)
{
try {
Environment.SpecialFolder folder =
(Environment.SpecialFolder) Enum.Parse(
typeof(Environment.SpecialFolder), "UserProfile");
string old_path = Path.Combine (
Environment.GetFolderPath (Environment.SpecialFolder.Personal), "SparkleShare");
if (Directory.Exists (old_path) &&
2012-03-17 23:15:35 +00:00
Environment.OSVersion.Platform == PlatformID.Win32NT) {
string new_path = Path.Combine (Environment.GetFolderPath (folder), "SparkleShare");
Directory.Move (old_path, new_path);
Console.WriteLine ("Migrated SparkleShare folder to %USERPROFILE%");
2012-03-17 23:15:35 +00:00
}
} catch (Exception e) {
Console.WriteLine ("Failed to migrate: " + e.Message);
// TODO: Remove this block when most people have migrated to the new path
}
FullPath = Path.Combine (config_path, config_file_name);
2012-03-08 14:55:44 +00:00
LogFilePath = Path.Combine (config_path, "debug.log");
if (File.Exists (LogFilePath)) {
try {
File.Delete (LogFilePath);
} catch (Exception) {
// Don't delete the debug.log if 'tail' is reading it
}
}
2011-05-26 19:46:49 +00:00
if (!Directory.Exists (config_path))
2011-05-26 19:46:49 +00:00
Directory.CreateDirectory (config_path);
string icons_path = Path.Combine (config_path, "icons");
if (!Directory.Exists (icons_path))
2011-05-26 19:46:49 +00:00
Directory.CreateDirectory (icons_path);
try {
Load (FullPath);
2012-03-17 23:15:35 +00:00
} catch (TypeInitializationException) {
CreateInitialConfig ();
} catch (FileNotFoundException) {
CreateInitialConfig ();
} catch (XmlException) {
2012-03-17 23:15:35 +00:00
FileInfo file = new FileInfo (FullPath);
2012-03-17 23:15:35 +00:00
if (file.Length == 0) {
File.Delete (FullPath);
CreateInitialConfig ();
} else {
throw new XmlException (FullPath + " does not contain a valid config XML structure.");
}
} finally {
Load (FullPath);
2012-01-12 00:21:34 +00:00
TmpPath = Path.Combine (FoldersPath, ".tmp");
Directory.CreateDirectory (TmpPath);
}
}
2011-05-26 19:46:49 +00:00
private void CreateInitialConfig ()
{
string user_name = "Unknown";
2011-05-26 19:46:49 +00:00
if (SparkleBackend.Platform == PlatformID.Unix ||
SparkleBackend.Platform == PlatformID.MacOSX) {
user_name = Environment.UserName;
if (string.IsNullOrEmpty (user_name))
user_name = "";
else
user_name = user_name.TrimEnd (",".ToCharArray ());
2011-05-26 19:46:49 +00:00
} else {
user_name = Environment.UserName;
}
2011-06-06 19:58:12 +00:00
if (string.IsNullOrEmpty (user_name))
user_name = "Unknown";
2011-06-06 19:58:12 +00:00
2011-11-07 18:35:00 +00:00
string n = Environment.NewLine;
File.WriteAllText (FullPath,
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + n +
"<sparkleshare>" + n +
" <user>" + n +
" <name>" + user_name + "</name>" + n +
" <email>Unknown</email>" + n +
" </user>" + n +
"</sparkleshare>");
2011-05-26 19:46:49 +00:00
}
2011-07-24 01:00:40 +00:00
public SparkleUser User {
get {
2011-07-24 01:00:40 +00:00
XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()");
string name = name_node.Value;
2011-07-24 01:00:40 +00:00
XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()");
string email = email_node.Value;
string pubkey_file_path = Path.Combine (
Path.GetDirectoryName (FullPath),
"sparkleshare." + email + ".key.pub"
);
SparkleUser user = new SparkleUser (name, email);
2012-03-17 23:15:35 +00:00
if (File.Exists (pubkey_file_path))
user.PublicKey = File.ReadAllText (pubkey_file_path);
return user;
}
2011-07-24 01:00:40 +00:00
set {
SparkleUser user = (SparkleUser) value;
2011-07-24 01:00:40 +00:00
XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()");
name_node.InnerText = user.Name;
2011-07-24 01:00:40 +00:00
XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()");
email_node.InnerText = user.Email;
Save ();
}
}
2011-07-02 12:56:42 +00:00
2011-05-26 19:46:49 +00:00
public List<string> Folders {
2011-05-25 20:46:46 +00:00
get {
List<string> folders = new List<string> ();
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder"))
folders.Add (node_folder ["name"].InnerText);
2011-05-26 19:46:49 +00:00
return folders;
2011-05-25 20:46:46 +00:00
}
}
public void AddFolder (string name, string url, string backend)
{
XmlNode node_name = CreateElement ("name");
node_name.InnerText = name;
XmlNode node_url = CreateElement ("url");
node_url.InnerText = url;
XmlNode node_backend = CreateElement ("backend");
2011-05-25 20:46:46 +00:00
node_backend.InnerText = backend;
XmlNode node_folder = CreateNode (XmlNodeType.Element, "folder", null);
node_folder.AppendChild (node_name);
node_folder.AppendChild (node_url);
node_folder.AppendChild (node_backend);
2011-05-25 18:36:43 +00:00
XmlNode node_root = SelectSingleNode ("/sparkleshare");
node_root.AppendChild (node_folder);
Save ();
}
public void RemoveFolder (string name)
{
2011-05-25 18:36:43 +00:00
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) {
2011-05-25 18:38:25 +00:00
if (node_folder ["name"].InnerText.Equals (name))
2011-05-25 18:36:43 +00:00
SelectSingleNode ("/sparkleshare").RemoveChild (node_folder);
}
2011-05-25 18:38:25 +00:00
Save ();
}
2011-05-25 20:46:46 +00:00
public string GetBackendForFolder (string name)
{
return GetFolderValue (name, "backend");
2011-05-25 20:46:46 +00:00
}
public string GetUrlForFolder (string name)
{
return GetFolderValue (name, "url");
2011-07-24 01:00:40 +00:00
}
public bool SetFolderOptionalAttribute (string folder_name, string key, string value)
{
XmlNode folder = GetFolder (folder_name);
2011-07-24 01:00:40 +00:00
if (folder == null)
return false;
if (folder [key] != null) {
folder [key].InnerText = value;
} else {
XmlNode new_node = CreateElement (key);
new_node.InnerText = value;
folder.AppendChild (new_node);
}
Save ();
2011-07-24 01:00:40 +00:00
return true;
}
public string GetFolderOptionalAttribute (string folder_name, string key)
{
XmlNode folder = GetFolder (folder_name);
2011-07-24 01:00:40 +00:00
if (folder != null) {
if (folder [key] != null)
return folder [key].InnerText;
else
return null;
} else {
return null;
}
}
2011-07-24 01:00:40 +00:00
private XmlNode GetFolder (string name)
{
return SelectSingleNode (string.Format ("/sparkleshare/folder[name='{0}']", name));
}
2011-07-24 01:00:40 +00:00
private string GetFolderValue (string name, string key)
{
XmlNode folder = GetFolder(name);
2012-03-17 23:15:35 +00:00
2011-07-24 01:00:40 +00:00
if ((folder != null) && (folder [key] != null)) {
return folder [key].InnerText;
}
return null;
}
2011-07-24 01:00:40 +00:00
2011-05-26 19:46:49 +00:00
public string GetConfigOption (string name)
{
XmlNode node = SelectSingleNode ("/sparkleshare/" + name);
if (node != null)
return node.InnerText;
else
return null;
}
public void SetConfigOption (string name, string content)
{
XmlNode node = SelectSingleNode ("/sparkleshare/" + name);
if (node != null) {
node.InnerText = content;
} else {
node = CreateElement (name);
node.InnerText = content;
XmlNode node_root = SelectSingleNode ("/sparkleshare");
node_root.AppendChild (node);
}
SparkleHelpers.DebugInfo ("Config", "Updated " + name + ":" + content);
Save ();
}
2011-07-24 01:00:40 +00:00
private void Save ()
{
2011-07-24 01:00:40 +00:00
if (!File.Exists (FullPath))
throw new ConfigFileNotFoundException (FullPath + " does not exist");
Save (FullPath);
2011-07-24 01:00:40 +00:00
SparkleHelpers.DebugInfo ("Config", "Updated \"" + FullPath + "\"");
}
}
public class ConfigFileNotFoundException : Exception {
2012-04-15 14:58:39 +00:00
public ConfigFileNotFoundException (string message) : base (message)
{
}
}
2011-05-26 19:46:49 +00:00
}
2011-08-26 23:11:53 +00:00