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
|
2013-10-11 15:13:46 +00:00
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
// License, or (at your option) any later version.
|
2011-05-24 23:46:55 +00:00
|
|
|
//
|
|
|
|
// 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
|
2013-10-11 15:13:46 +00:00
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-05-24 23:46:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
2012-02-24 17:50:05 +00:00
|
|
|
using System.Xml;
|
2011-05-24 23:46:55 +00:00
|
|
|
|
|
|
|
namespace SparkleLib {
|
|
|
|
|
|
|
|
public class SparkleConfig : XmlDocument {
|
|
|
|
|
2012-07-14 10:45:54 +00:00
|
|
|
public static SparkleConfig DefaultConfig;
|
2012-12-06 19:33:39 +00:00
|
|
|
public static bool DebugMode = true;
|
2012-01-18 13:03:23 +00:00
|
|
|
|
2011-07-24 01:00:40 +00:00
|
|
|
public string FullPath;
|
2012-01-12 00:21:34 +00:00
|
|
|
public string TmpPath;
|
2012-01-18 13:03:23 +00:00
|
|
|
public string LogFilePath;
|
2011-12-25 19:33:39 +00:00
|
|
|
|
2012-04-15 14:58:39 +00:00
|
|
|
|
2011-12-25 19:33:39 +00:00
|
|
|
public string HomePath {
|
|
|
|
get {
|
2012-07-28 13:58:09 +00:00
|
|
|
if (SparkleBackend.Platform == PlatformID.Win32NT)
|
2012-04-18 14:33:16 +00:00
|
|
|
return Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
|
|
|
|
else
|
2012-03-17 23:15:35 +00:00
|
|
|
return Environment.GetFolderPath (Environment.SpecialFolder.Personal);
|
2012-01-07 08:19:44 +00:00
|
|
|
}
|
2011-12-25 19:33:39 +00:00
|
|
|
}
|
2011-12-18 16:10:06 +00:00
|
|
|
|
2012-04-15 14:58:39 +00:00
|
|
|
|
2011-07-24 01:00:40 +00:00
|
|
|
public string FoldersPath {
|
|
|
|
get {
|
2012-09-20 09:33:49 +00:00
|
|
|
if (GetConfigOption ("folders_path") != null)
|
|
|
|
return GetConfigOption ("folders_path");
|
|
|
|
else
|
|
|
|
return Path.Combine (HomePath, "SparkleShare");
|
2011-07-24 01:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-24 23:46:55 +00:00
|
|
|
|
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)
|
2011-05-24 23:46:55 +00:00
|
|
|
{
|
2013-07-10 16:07:44 +00:00
|
|
|
FullPath = Path.Combine (config_path, config_file_name);
|
|
|
|
string logs_path = Path.Combine (config_path, "logs");
|
2012-01-11 23:02:22 +00:00
|
|
|
|
2013-07-10 16:07:44 +00:00
|
|
|
int i = 1;
|
|
|
|
do {
|
|
|
|
LogFilePath = Path.Combine (
|
|
|
|
logs_path, "debug_log_" + DateTime.Now.ToString ("yyyy-MM-dd") + "." + i + ".txt");
|
2012-01-11 23:02:22 +00:00
|
|
|
|
2013-07-10 16:07:44 +00:00
|
|
|
i++;
|
|
|
|
|
|
|
|
} while (File.Exists (LogFilePath));
|
|
|
|
|
|
|
|
if (!Directory.Exists (logs_path))
|
|
|
|
Directory.CreateDirectory (logs_path);
|
|
|
|
|
|
|
|
// Delete logs older than a week
|
|
|
|
foreach (FileInfo file in new DirectoryInfo (logs_path).GetFiles ("debug_log*.txt")) {
|
|
|
|
if (file.LastWriteTime < DateTime.Now.AddDays (-7))
|
|
|
|
file.Delete ();
|
2012-01-11 23:02:22 +00:00
|
|
|
}
|
2011-05-26 19:46:49 +00:00
|
|
|
|
2012-01-18 13:03:23 +00:00
|
|
|
if (!Directory.Exists (config_path))
|
2011-05-26 19:46:49 +00:00
|
|
|
Directory.CreateDirectory (config_path);
|
|
|
|
|
2011-08-26 21:50:50 +00:00
|
|
|
try {
|
|
|
|
Load (FullPath);
|
2012-03-17 23:15:35 +00:00
|
|
|
|
2011-09-20 18:09:43 +00:00
|
|
|
} catch (TypeInitializationException) {
|
|
|
|
CreateInitialConfig ();
|
|
|
|
|
2011-10-29 13:27:53 +00:00
|
|
|
} catch (FileNotFoundException) {
|
|
|
|
CreateInitialConfig ();
|
2011-05-25 00:15:30 +00:00
|
|
|
|
2011-08-26 21:50:50 +00:00
|
|
|
} catch (XmlException) {
|
|
|
|
FileInfo file = new FileInfo (FullPath);
|
2012-03-17 23:15:35 +00:00
|
|
|
|
2011-08-26 21:50:50 +00:00
|
|
|
if (file.Length == 0) {
|
|
|
|
File.Delete (FullPath);
|
2011-10-01 21:15:46 +00:00
|
|
|
CreateInitialConfig ();
|
2011-08-26 21:50:50 +00:00
|
|
|
|
|
|
|
} else {
|
2011-09-22 11:24:33 +00:00
|
|
|
throw new XmlException (FullPath + " does not contain a valid config XML structure.");
|
2011-08-26 21:50:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
Load (FullPath);
|
2012-01-12 00:21:34 +00:00
|
|
|
TmpPath = Path.Combine (FoldersPath, ".tmp");
|
2012-04-12 22:44:31 +00:00
|
|
|
Directory.CreateDirectory (TmpPath);
|
2011-08-26 21:50:50 +00:00
|
|
|
}
|
2011-05-24 23:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-26 19:46:49 +00:00
|
|
|
private void CreateInitialConfig ()
|
|
|
|
{
|
2011-06-09 16:20:15 +00:00
|
|
|
string user_name = "Unknown";
|
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) {
|
|
|
|
|
2011-11-28 13:40:34 +00:00
|
|
|
user_name = Environment.UserName;
|
2011-06-09 17:53:36 +00:00
|
|
|
if (string.IsNullOrEmpty (user_name))
|
2013-08-30 12:44:05 +00:00
|
|
|
user_name = "Unknown";
|
2011-06-09 17:53:36 +00:00
|
|
|
else
|
2011-11-28 13:40:34 +00:00
|
|
|
user_name = user_name.TrimEnd (",".ToCharArray ());
|
2011-05-26 19:46:49 +00:00
|
|
|
|
2011-06-09 16:20:15 +00:00
|
|
|
} else {
|
|
|
|
user_name = Environment.UserName;
|
2011-06-05 16:22:40 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 15:59:43 +00:00
|
|
|
// TODO: Don't do this manually
|
2011-11-07 18:35:00 +00:00
|
|
|
string n = Environment.NewLine;
|
2011-11-07 18:03:19 +00:00
|
|
|
File.WriteAllText (FullPath,
|
|
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + n +
|
|
|
|
"<sparkleshare>" + n +
|
|
|
|
" <user>" + n +
|
|
|
|
" <name>" + user_name + "</name>" + n +
|
2011-11-11 22:01:51 +00:00
|
|
|
" <email>Unknown</email>" + n +
|
|
|
|
" </user>" + n +
|
2016-03-26 15:59:43 +00:00
|
|
|
" <notifications>True</notifications>" + n +
|
2011-11-07 18:03:19 +00:00
|
|
|
"</sparkleshare>");
|
2011-05-26 19:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-24 01:00:40 +00:00
|
|
|
public SparkleUser User {
|
2011-05-24 23:46:55 +00:00
|
|
|
get {
|
2012-10-26 22:26:15 +00:00
|
|
|
XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()");
|
2011-07-24 01:00:40 +00:00
|
|
|
XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()");
|
2012-02-17 20:26:13 +00:00
|
|
|
|
2016-03-26 15:59:43 +00:00
|
|
|
string name = name_node.Value;
|
|
|
|
string email = email_node.Value;
|
2012-03-17 23:15:35 +00:00
|
|
|
|
2016-03-26 15:59:43 +00:00
|
|
|
return new SparkleUser (name, email);
|
2011-05-24 23:46:55 +00:00
|
|
|
}
|
|
|
|
|
2011-07-24 01:00:40 +00:00
|
|
|
set {
|
|
|
|
SparkleUser user = (SparkleUser) value;
|
2011-05-24 23:46:55 +00:00
|
|
|
|
2016-03-26 15:59:43 +00:00
|
|
|
XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()");
|
|
|
|
XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()");
|
|
|
|
|
2012-10-26 22:26:15 +00:00
|
|
|
name_node.InnerText = user.Name;
|
2011-07-24 01:00:40 +00:00
|
|
|
email_node.InnerText = user.Email;
|
2011-05-24 23:46:55 +00:00
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
Save ();
|
2011-05-24 23:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-02 12:56:42 +00:00
|
|
|
|
2011-12-11 19:45:12 +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);
|
|
|
|
|
2012-09-21 17:04:47 +00:00
|
|
|
folders.Sort ();
|
|
|
|
|
2011-05-26 19:46:49 +00:00
|
|
|
return folders;
|
2011-05-25 20:46:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-02 16:05:05 +00:00
|
|
|
|
2012-07-05 11:33:32 +00:00
|
|
|
public void AddFolder (string name, string identifier, string url, string backend)
|
2011-05-24 23:46:55 +00:00
|
|
|
{
|
2012-07-28 13:58:09 +00:00
|
|
|
XmlNode node_name = CreateElement ("name");
|
|
|
|
XmlNode node_identifier = CreateElement ("identifier");
|
|
|
|
XmlNode node_url = CreateElement ("url");
|
|
|
|
XmlNode node_backend = CreateElement ("backend");
|
2011-05-24 23:46:55 +00:00
|
|
|
|
2012-07-28 13:58:09 +00:00
|
|
|
node_name.InnerText = name;
|
2012-07-05 11:33:32 +00:00
|
|
|
node_identifier.InnerText = identifier;
|
2012-07-28 13:58:09 +00:00
|
|
|
node_url.InnerText = url;
|
|
|
|
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);
|
2012-07-28 13:58:09 +00:00
|
|
|
|
2011-05-24 23:46:55 +00:00
|
|
|
node_folder.AppendChild (node_name);
|
2012-07-05 11:33:32 +00:00
|
|
|
node_folder.AppendChild (node_identifier);
|
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);
|
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
Save ();
|
2011-06-28 18:36:47 +00:00
|
|
|
}
|
2011-05-24 23:46:55 +00:00
|
|
|
|
2011-07-02 16:05:05 +00:00
|
|
|
|
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
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
Save ();
|
2011-05-25 00:20:37 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 20:46:46 +00:00
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
public void RenameFolder (string identifier, string name)
|
|
|
|
{
|
2012-07-28 13:58:09 +00:00
|
|
|
XmlNode node_folder = SelectSingleNode (
|
|
|
|
string.Format ("/sparkleshare/folder[identifier=\"{0}\"]", identifier));
|
2012-07-06 09:26:02 +00:00
|
|
|
|
2012-07-28 13:58:09 +00:00
|
|
|
node_folder ["name"].InnerText = name;
|
2012-07-06 09:26:02 +00:00
|
|
|
Save ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-25 20:46:46 +00:00
|
|
|
public string GetBackendForFolder (string name)
|
|
|
|
{
|
2012-02-24 17:50:05 +00:00
|
|
|
return GetFolderValue (name, "backend");
|
2011-05-25 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
2011-07-02 16:05:05 +00:00
|
|
|
|
2012-07-05 11:33:32 +00:00
|
|
|
public string GetIdentifierForFolder (string name)
|
|
|
|
{
|
|
|
|
return GetFolderValue (name, "identifier");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-26 22:41:55 +00:00
|
|
|
public string GetUrlForFolder (string name)
|
|
|
|
{
|
2012-02-24 17:50:05 +00:00
|
|
|
return GetFolderValue (name, "url");
|
2011-07-24 01:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
public bool IdentifierExists (string identifier)
|
|
|
|
{
|
2012-07-11 20:31:45 +00:00
|
|
|
if (identifier == null)
|
|
|
|
throw new ArgumentNullException ();
|
|
|
|
|
2012-07-06 09:26:02 +00:00
|
|
|
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) {
|
2012-07-10 20:13:18 +00:00
|
|
|
XmlElement folder_id = node_folder ["identifier"];
|
|
|
|
|
|
|
|
if (folder_id != null && identifier.Equals (folder_id.InnerText))
|
2012-07-06 09:26:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-24 01:00:40 +00:00
|
|
|
public bool SetFolderOptionalAttribute (string folder_name, string key, string value)
|
|
|
|
{
|
2012-02-24 17:50:05 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
Save ();
|
2012-04-15 17:03:36 +00:00
|
|
|
|
2011-07-24 01:00:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetFolderOptionalAttribute (string folder_name, string key)
|
|
|
|
{
|
2012-02-24 17:50:05 +00:00
|
|
|
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-05-26 22:41:55 +00:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
Save ();
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo ("Config", "Updated option " + name + ":" + content);
|
2011-05-24 23:46:55 +00:00
|
|
|
}
|
2011-07-02 16:05:05 +00:00
|
|
|
|
|
|
|
|
2012-12-06 19:33:39 +00:00
|
|
|
private XmlNode GetFolder (string name)
|
|
|
|
{
|
|
|
|
return SelectSingleNode (string.Format ("/sparkleshare/folder[name=\"{0}\"]", name));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string GetFolderValue (string name, string key)
|
|
|
|
{
|
|
|
|
XmlNode folder = GetFolder(name);
|
|
|
|
|
|
|
|
if ((folder != null) && (folder [key] != null))
|
|
|
|
return folder [key].InnerText;
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-24 01:00:40 +00:00
|
|
|
private void Save ()
|
2011-07-02 16:05:05 +00:00
|
|
|
{
|
2011-07-24 01:00:40 +00:00
|
|
|
if (!File.Exists (FullPath))
|
2012-07-17 13:09:57 +00:00
|
|
|
throw new FileNotFoundException (FullPath + " does not exist");
|
2011-07-02 16:05:05 +00:00
|
|
|
|
2012-02-24 17:50:05 +00:00
|
|
|
Save (FullPath);
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo ("Config", "Wrote to '" + FullPath + "'");
|
2012-04-15 14:58:39 +00:00
|
|
|
}
|
2011-05-25 00:15:30 +00:00
|
|
|
}
|
2011-05-26 19:46:49 +00:00
|
|
|
}
|