SparkleShare/Sparkles/Preset.cs

125 lines
4.7 KiB
C#
Raw 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
// 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.Xml;
using IO = System.IO;
2016-04-01 08:15:47 +00:00
namespace Sparkles {
2016-03-31 08:35:26 +00:00
public class Preset : XmlDocument {
2016-03-31 08:35:26 +00:00
public static string PresetsPath = "";
2016-03-31 08:35:26 +00:00
public static string LocalPresetsPath = IO.Path.Combine (
Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "org.sparkleshare.SparkleShare", "presets");
2012-12-02 17:36:37 +00:00
new public string Name { get { return GetValue ("info", "name"); } }
2012-07-28 16:13:50 +00:00
public string Description { get { return GetValue ("info", "description"); } }
public string Backend { get { return GetValue ("info", "backend"); } }
public string Fingerprint { get { return GetValue ("info", "fingerprint"); } }
public string AnnouncementsUrl { get { return GetValue ("info", "announcements_url"); } }
public string Address { get { return GetValue ("address", "value"); } }
public string AddressExample { get { return GetValue ("address", "example"); } }
public string Path { get { return GetValue ("path", "value"); } }
public string PathExample { get { return GetValue ("path", "example"); } }
2012-02-08 13:51:07 +00:00
public string ImagePath {
get {
string image_file_name = GetValue ("info", "icon");
2016-03-31 08:35:26 +00:00
string image_path = IO.Path.Combine (preset_directory, image_file_name);
if (IO.File.Exists (image_path))
return image_path;
2016-03-31 08:35:26 +00:00
return IO.Path.Combine (PresetsPath, image_file_name);
2012-02-08 13:51:07 +00:00
}
}
2012-10-20 22:25:36 +00:00
public bool PathUsesLowerCase {
get {
string uses_lower_case = GetValue ("path", "uses_lower_case");
if (!string.IsNullOrEmpty (uses_lower_case))
return uses_lower_case.Equals (bool.TrueString);
else
return false;
}
}
2016-03-31 08:35:26 +00:00
string preset_directory;
2012-08-29 12:06:46 +00:00
2016-03-31 08:35:26 +00:00
public Preset (string preset_path)
2012-02-08 13:51:07 +00:00
{
2016-03-31 08:35:26 +00:00
preset_directory = IO.Path.GetDirectoryName (preset_path);
Load (preset_path);
2012-02-08 13:51:07 +00:00
}
2016-03-31 08:35:26 +00:00
public static Preset Create (string name, string description, string address_value,
string address_example, string path_value, string path_example)
{
2016-03-31 08:35:26 +00:00
string preset_path = IO.Path.Combine (LocalPresetsPath, name + ".xml");
2016-03-31 08:35:26 +00:00
if (IO.File.Exists (preset_path))
return null;
2016-04-06 09:12:49 +00:00
// TODO: Don't write manually
2016-03-31 08:35:26 +00:00
string preset_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<sparkleshare>" +
2016-03-31 08:35:26 +00:00
" <preset>" +
" <info>" +
" <name>" + name + "</name>" +
" <description>" + description + "</description>" +
" <icon>own-server.png</icon>" +
" </info>" +
" <address>" +
" <value>" + address_value + "</value>" +
" <example>" + address_example + "</example>" +
" </address>" +
" <path>" +
" <value>" + path_value + "</value>" +
" <example>" + path_example + "</example>" +
" </path>" +
2016-03-31 08:35:26 +00:00
" </preset>" +
"</sparkleshare>";
2016-03-31 08:35:26 +00:00
preset_xml = preset_xml.Replace ("<value></value>", "<value/>");
preset_xml = preset_xml.Replace ("<example></example>", "<example/>");
2016-03-31 08:35:26 +00:00
if (!IO.Directory.Exists (LocalPresetsPath))
IO.Directory.CreateDirectory (LocalPresetsPath);
2016-03-31 08:35:26 +00:00
IO.File.WriteAllText (preset_path, preset_xml);
return new Preset (preset_path);
}
2012-02-08 13:51:07 +00:00
private string GetValue (string a, string b)
{
2016-03-31 08:35:26 +00:00
XmlNode node = SelectSingleNode ("/sparkleshare/preset/" + a + "/" + b + "/text()");
if (node != null && !string.IsNullOrEmpty (node.Value))
2012-02-08 13:51:07 +00:00
return node.Value;
else
return null;
}
}
2016-03-31 08:35:26 +00:00
}