SparkleShare/Sparkles/Configuration.cs

359 lines
11 KiB
C#
Raw Permalink Normal View History

// SparkleShare, a collaboration and sharing tool.
2017-07-23 12:47:54 +00:00
// Copyright (C) 2010 Hylke Bons <hi@planetpeanut.uk>
//
// 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.
//
// 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/>.
using System;
2011-05-25 20:46:46 +00:00
using System.Collections.Generic;
2018-02-18 14:06:14 +00:00
using System.IO;
using System.Xml;
2018-02-18 14:06:14 +00:00
using System.Xml.Linq;
2016-03-31 08:35:26 +00:00
namespace Sparkles {
2016-03-30 23:36:31 +00:00
public class Configuration : XmlDocument {
private static Lazy<Configuration> ConfigLazy = new Lazy<Configuration> (() => {
string app_data_path = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
2018-03-14 16:11:49 +00:00
if (InstallationInfo.OperatingSystem != OS.Windows && InstallationInfo.OperatingSystem != OS.macOS)
app_data_path = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), ".config");
string config_path = Path.Combine (app_data_path, "org.sparkleshare.SparkleShare");
return new Configuration (config_path, "projects.xml");
});
public static Configuration DefaultConfiguration { get { return ConfigLazy.Value; } }
2012-12-06 19:33:39 +00:00
public static bool DebugMode = true;
public readonly string DirectoryPath;
2016-04-08 15:17:08 +00:00
public readonly string FilePath;
public readonly string TmpPath;
public readonly string BinPath;
2018-02-18 14:06:14 +00:00
2016-04-08 15:17:08 +00:00
public readonly string LogFilePath;
2012-04-15 14:58:39 +00:00
public string HomePath {
get {
if (InstallationInfo.OperatingSystem == OS.Windows)
return Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
2016-04-08 15:17:08 +00:00
2016-04-06 09:12:49 +00:00
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");
2016-04-06 09:12:49 +00:00
return Path.Combine (HomePath, "SparkleShare");
2011-07-24 01:00:40 +00:00
}
}
2012-04-15 14:58:39 +00:00
2016-03-30 23:36:31 +00:00
public Configuration (string config_path, string config_file_name)
{
2016-04-08 15:17:08 +00:00
FilePath = Path.Combine (config_path, config_file_name);
DirectoryPath = config_path;
BinPath = Path.Combine (config_path, "bin");
if (!Directory.Exists (BinPath))
Directory.CreateDirectory (BinPath);
2013-07-10 16:07:44 +00:00
string logs_path = Path.Combine (config_path, "logs");
2013-07-10 16:07:44 +00:00
int i = 1;
do {
LogFilePath = Path.Combine (
logs_path, "log_" + DateTime.Now.ToString ("yyyy-MM-dd") + "." + i + ".txt");
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 ("log*.txt")) {
2013-07-10 16:07:44 +00:00
if (file.LastWriteTime < DateTime.Now.AddDays (-7))
file.Delete ();
}
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);
try {
2016-04-08 15:17:08 +00:00
Load (FilePath);
2012-03-17 23:15:35 +00:00
} catch (TypeInitializationException) {
CreateInitialConfig ();
} catch (FileNotFoundException) {
CreateInitialConfig ();
} catch (XmlException) {
2016-04-08 15:17:08 +00:00
var file = new FileInfo (FilePath);
2012-03-17 23:15:35 +00:00
if (file.Length == 0) {
2016-04-08 15:17:08 +00:00
File.Delete (FilePath);
CreateInitialConfig ();
} else {
2018-02-18 14:06:14 +00:00
throw;
}
} finally {
TmpPath = Path.Combine (DirectoryPath, "tmp");
Directory.CreateDirectory (TmpPath);
}
}
2016-04-06 09:12:49 +00:00
void CreateInitialConfig ()
2011-05-26 19:46:49 +00:00
{
2016-04-06 09:12:49 +00:00
string user_name = Environment.UserName;
2011-05-26 19:46:49 +00:00
if (InstallationInfo.OperatingSystem != OS.Windows) {
if (string.IsNullOrEmpty (user_name))
2013-08-30 12:44:05 +00:00
user_name = "Unknown";
else
// On Unix systems the user name may have commas appended
2016-04-06 09:12:49 +00:00
user_name = user_name.TrimEnd (',');
}
2018-02-18 14:06:14 +00:00
XElement xml =
new XElement ("sparkleshare",
new XElement ("user",
new XElement ("name", user_name),
new XElement ("email", "Unknown")
),
new XElement ("notifications", bool.TrueString)
);
LoadXml (xml.ToString ());
2011-05-26 19:46:49 +00:00
}
2016-03-30 23:36:31 +00:00
public User User {
get {
2016-04-06 09:12:49 +00:00
string name = SelectSingleNode ("/sparkleshare/user/name/text()").Value;
string email = SelectSingleNode ("/sparkleshare/user/email/text()").Value;
2012-03-17 23:15:35 +00:00
2016-03-30 23:36:31 +00:00
return new User (name, email);
}
2011-07-24 01:00:40 +00:00
set {
2016-04-06 09:12:49 +00:00
SelectSingleNode ("/sparkleshare/user/name/text()").InnerText = value.Name;
SelectSingleNode ("/sparkleshare/user/email/text()").InnerText = value.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 {
2016-04-06 09:12:49 +00:00
var folders = new List<string> ();
2011-05-25 20:46:46 +00:00
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder"))
folders.Add (node_folder ["name"].InnerText);
folders.Sort ();
2011-05-26 19:46:49 +00:00
return folders;
2011-05-25 20:46:46 +00:00
}
}
public void AddFolder (string name, string identifier, string url, string backend)
{
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");
2012-07-28 13:58:09 +00:00
node_name.InnerText = name;
node_identifier.InnerText = identifier;
2012-07-28 13:58:09 +00:00
node_url.InnerText = url;
node_backend.InnerText = backend;
XmlNode node_folder = CreateNode (XmlNodeType.Element, "folder", null);
2012-07-28 13:58:09 +00:00
node_folder.AppendChild (node_name);
node_folder.AppendChild (node_identifier);
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
2016-04-06 09:12:49 +00:00
public void RenameFolder (string identifier, string new_name)
2012-07-06 09:26:02 +00:00
{
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
2016-04-06 09:12:49 +00:00
node_folder ["name"].InnerText = new_name;
2012-07-06 09:26:02 +00:00
Save ();
}
2016-04-06 09:12:49 +00:00
public string BackendByName (string name)
2011-05-25 20:46:46 +00:00
{
2016-04-06 09:12:49 +00:00
return FolderValueByKey (name, "backend");
2011-05-25 20:46:46 +00:00
}
2016-04-06 09:12:49 +00:00
public string IdentifierByName (string name)
{
2016-04-06 09:12:49 +00:00
return FolderValueByKey (name, "identifier");
}
2016-04-06 09:12:49 +00:00
public string UrlByName (string name)
{
2016-04-06 09:12:49 +00:00
return FolderValueByKey (name, "url");
2011-07-24 01:00:40 +00:00
}
2012-07-06 09:26:02 +00:00
public bool IdentifierExists (string identifier)
{
if (identifier == null)
throw new ArgumentNullException ();
2012-07-06 09:26:02 +00:00
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) {
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)
{
2016-04-06 09:12:49 +00:00
XmlNode folder = FolderByName (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)
{
2016-04-06 09:12:49 +00:00
XmlNode folder = FolderByName (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
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);
}
Save ();
2016-03-30 23:36:31 +00:00
Logger.LogInfo ("Config", "Updated option " + name + ":" + content);
}
2016-04-06 09:12:49 +00:00
XmlNode FolderByName (string name)
2012-12-06 19:33:39 +00:00
{
return SelectSingleNode (string.Format ("/sparkleshare/folder[name=\"{0}\"]", name));
}
2016-04-06 09:12:49 +00:00
string FolderValueByKey (string name, string key)
2012-12-06 19:33:39 +00:00
{
2016-04-06 09:12:49 +00:00
XmlNode folder = FolderByName(name);
2012-12-06 19:33:39 +00:00
if ((folder != null) && (folder [key] != null))
return folder [key].InnerText;
2016-04-06 09:12:49 +00:00
return null;
2012-12-06 19:33:39 +00:00
}
2016-04-06 09:12:49 +00:00
void Save ()
{
2016-04-08 15:17:08 +00:00
Save (FilePath);
Logger.LogInfo ("Config", "Wrote to '" + FilePath + "'");
2012-04-15 14:58:39 +00:00
}
}
2011-05-26 19:46:49 +00:00
}