2011-06-01 23:08:05 +00:00
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
2011-05-24 23:46:55 +00:00
|
|
|
// 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;
|
2011-05-25 00:15:30 +00:00
|
|
|
using System.IO;
|
2011-05-25 20:46:46 +00:00
|
|
|
using System.Collections.Generic;
|
2011-05-24 23:46:55 +00:00
|
|
|
using System.Xml;
|
|
|
|
|
2011-05-26 19:46:49 +00:00
|
|
|
using Mono.Unix;
|
|
|
|
|
2011-05-24 23:46:55 +00:00
|
|
|
namespace SparkleLib {
|
|
|
|
|
|
|
|
public class SparkleConfig : XmlDocument {
|
|
|
|
|
2011-05-25 00:00:27 +00:00
|
|
|
public static SparkleConfig DefaultConfig = new SparkleConfig (
|
2011-05-26 19:46:49 +00:00
|
|
|
SparklePaths.SparkleConfigPath, "config.xml");
|
2011-05-25 00:00:27 +00:00
|
|
|
|
2011-05-24 23:46:55 +00:00
|
|
|
public string Path;
|
|
|
|
|
|
|
|
|
2011-05-26 19:46:49 +00:00
|
|
|
public SparkleConfig (string config_path, string config_file_name)
|
2011-05-24 23:46:55 +00:00
|
|
|
{
|
2011-05-26 19:46:49 +00:00
|
|
|
Path = System.IO.Path.Combine (config_path, config_file_name);
|
|
|
|
|
|
|
|
if (!Directory.Exists (config_path)) {
|
|
|
|
Directory.CreateDirectory (config_path);
|
|
|
|
SparkleHelpers.DebugInfo ("Config", "Created \"" + config_path + "\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
string icons_path = System.IO.Path.Combine (config_path, "icons");
|
|
|
|
if (!Directory.Exists (icons_path)) {
|
|
|
|
Directory.CreateDirectory (icons_path);
|
2011-06-06 19:58:12 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Config", "Created \"" + icons_path + "\"");
|
2011-05-26 19:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!File.Exists (Path))
|
|
|
|
CreateInitialConfig ();
|
2011-05-25 00:15:30 +00:00
|
|
|
|
2011-05-24 23:46:55 +00:00
|
|
|
Load (Path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-26 19:46:49 +00:00
|
|
|
private void CreateInitialConfig ()
|
|
|
|
{
|
2011-06-08 00:29:34 +00:00
|
|
|
string user_name = Environment.UserName;
|
2011-05-26 19:46:49 +00:00
|
|
|
|
2011-06-05 16:22:40 +00:00
|
|
|
if (SparkleBackend.Platform == PlatformID.Unix ||
|
|
|
|
SparkleBackend.Platform == PlatformID.MacOSX) {
|
|
|
|
|
|
|
|
user_name = new UnixUserInfo (UnixEnvironment.UserName).RealName;
|
|
|
|
}
|
|
|
|
|
2011-06-06 19:58:12 +00:00
|
|
|
if (string.IsNullOrEmpty (user_name))
|
2011-06-08 00:29:34 +00:00
|
|
|
user_name = Environment.UserName;
|
2011-06-08 20:39:39 +00:00
|
|
|
else
|
|
|
|
user_name = user_name.TrimEnd (",".ToCharArray());
|
2011-06-06 19:58:12 +00:00
|
|
|
|
2011-05-26 19:46:49 +00:00
|
|
|
TextWriter writer = new StreamWriter (Path);
|
|
|
|
string n = Environment.NewLine;
|
|
|
|
|
|
|
|
writer.Write ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + n +
|
|
|
|
"<sparkleshare>" + n +
|
|
|
|
" <user>" + n +
|
|
|
|
" <name>" + user_name + "</name>" + n +
|
|
|
|
" <email>Unknown</email>" + n +
|
|
|
|
" </user>" + n +
|
|
|
|
"</sparkleshare>");
|
|
|
|
writer.Close ();
|
|
|
|
|
|
|
|
SparkleHelpers.DebugInfo ("Config", "Created \"" + Path + "\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-24 23:46:55 +00:00
|
|
|
public string UserName {
|
|
|
|
get {
|
2011-05-25 18:36:43 +00:00
|
|
|
XmlNode node = SelectSingleNode ("/sparkleshare/user/name/text()");
|
2011-05-24 23:46:55 +00:00
|
|
|
return node.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
2011-05-25 18:36:43 +00:00
|
|
|
XmlNode node = SelectSingleNode ("/sparkleshare/user/name/text()");
|
2011-05-24 23:46:55 +00:00
|
|
|
node.InnerText = value;
|
|
|
|
|
2011-05-25 00:20:37 +00:00
|
|
|
Save ();
|
2011-05-24 23:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string UserEmail {
|
|
|
|
get {
|
2011-05-26 19:46:49 +00:00
|
|
|
XmlNode node = SelectSingleNode ("/sparkleshare/user/email/text()");
|
2011-05-24 23:46:55 +00:00
|
|
|
return node.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
set {
|
2011-05-26 19:46:49 +00:00
|
|
|
XmlNode node = SelectSingleNode ("/sparkleshare/user/email/text()");
|
2011-05-24 23:46:55 +00:00
|
|
|
node.InnerText = value;
|
|
|
|
|
2011-05-25 00:20:37 +00:00
|
|
|
Save ();
|
2011-05-24 23:46:55 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-26 22:41:55 +00:00
|
|
|
public void AddFolder (string name, string url, string backend)
|
2011-05-24 23:46:55 +00:00
|
|
|
{
|
|
|
|
XmlNode node_name = CreateElement ("name");
|
|
|
|
node_name.InnerText = name;
|
|
|
|
|
2011-05-26 22:41:55 +00:00
|
|
|
XmlNode node_url = CreateElement ("url");
|
|
|
|
node_url.InnerText = url;
|
|
|
|
|
2011-05-24 23:46:55 +00:00
|
|
|
XmlNode node_backend = CreateElement ("backend");
|
2011-05-25 20:46:46 +00:00
|
|
|
node_backend.InnerText = backend;
|
2011-05-24 23:46:55 +00:00
|
|
|
|
2011-05-26 22:41:55 +00:00
|
|
|
XmlNode node_folder = CreateNode (XmlNodeType.Element, "folder", null);
|
2011-05-24 23:46:55 +00:00
|
|
|
node_folder.AppendChild (node_name);
|
2011-05-26 22:41:55 +00:00
|
|
|
node_folder.AppendChild (node_url);
|
2011-05-24 23:46:55 +00:00
|
|
|
node_folder.AppendChild (node_backend);
|
|
|
|
|
2011-05-25 18:36:43 +00:00
|
|
|
XmlNode node_root = SelectSingleNode ("/sparkleshare");
|
2011-05-24 23:46:55 +00:00
|
|
|
node_root.AppendChild (node_folder);
|
|
|
|
|
2011-05-25 00:20:37 +00:00
|
|
|
Save ();
|
2011-05-24 23:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-24 23:46:55 +00:00
|
|
|
}
|
2011-05-25 18:38:25 +00:00
|
|
|
|
2011-05-25 00:20:37 +00:00
|
|
|
Save ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-25 20:46:46 +00:00
|
|
|
public bool FolderExists (string name)
|
|
|
|
{
|
|
|
|
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) {
|
|
|
|
if (node_folder ["name"].InnerText.Equals (name))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetBackendForFolder (string name)
|
|
|
|
{
|
|
|
|
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) {
|
|
|
|
if (node_folder ["name"].InnerText.Equals (name))
|
|
|
|
return node_folder ["backend"].InnerText;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-26 22:41:55 +00:00
|
|
|
public string GetUrlForFolder (string name)
|
|
|
|
{
|
|
|
|
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) {
|
|
|
|
if (node_folder ["name"].InnerText.Equals (name))
|
|
|
|
return node_folder ["url"].InnerText;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-30 01:19:58 +00:00
|
|
|
public string GetAnnouncementsForFolder (string name)
|
|
|
|
{
|
|
|
|
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) {
|
|
|
|
if (node_folder ["name"].InnerText.Equals (name) &&
|
|
|
|
node_folder ["announcements"] != null) {
|
|
|
|
|
|
|
|
return node_folder ["announcements"].InnerText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-05-25 00:20:37 +00:00
|
|
|
public void Save ()
|
|
|
|
{
|
|
|
|
if (!File.Exists (Path))
|
|
|
|
throw new ConfigFileNotFoundException (Path + " does not exist");
|
|
|
|
|
2011-05-25 00:22:02 +00:00
|
|
|
Save (Path);
|
2011-05-26 19:46:49 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Config", "Updated \"" + Path + "\"");
|
2011-05-24 23:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-25 00:15:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
public class ConfigFileNotFoundException : Exception {
|
|
|
|
|
2011-05-25 00:22:02 +00:00
|
|
|
public ConfigFileNotFoundException (string message) :
|
|
|
|
base (message) { }
|
2011-05-25 00:15:30 +00:00
|
|
|
}
|
2011-05-26 19:46:49 +00:00
|
|
|
}
|