/SparkleShare/SparklePlugin.cs

http://github.com/hbons/SparkleShare · C# · 123 lines · 83 code · 25 blank · 15 comment · 7 complexity · 49a940085c43e4b904f3fd98cde7dd9e MD5 · raw file

  1. // SparkleShare, a collaboration and sharing tool.
  2. // Copyright (C) 2010 Hylke Bons (hylkebons@gmail.com)
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see (http://www.gnu.org/licenses/).
  16. using System;
  17. using System.Xml;
  18. using IO = System.IO;
  19. namespace SparkleShare {
  20. public class SparklePlugin : XmlDocument {
  21. public static string PluginsPath = "";
  22. public static string LocalPluginsPath = IO.Path.Combine (
  23. Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "sparkleshare", "plugins");
  24. new public string Name { get { return GetValue ("info", "name"); } }
  25. public string Description { get { return GetValue ("info", "description"); } }
  26. public string Backend { get { return GetValue ("info", "backend"); } }
  27. public string Fingerprint { get { return GetValue ("info", "fingerprint"); } }
  28. public string AnnouncementsUrl { get { return GetValue ("info", "announcements_url"); } }
  29. public string Address { get { return GetValue ("address", "value"); } }
  30. public string AddressExample { get { return GetValue ("address", "example"); } }
  31. public string Path { get { return GetValue ("path", "value"); } }
  32. public string PathExample { get { return GetValue ("path", "example"); } }
  33. public string ImagePath {
  34. get {
  35. string image_file_name = GetValue ("info", "icon");
  36. string image_path = IO.Path.Combine (this.plugin_directory, image_file_name);
  37. if (IO.File.Exists (image_path))
  38. return image_path;
  39. else
  40. return IO.Path.Combine (PluginsPath, image_file_name);
  41. }
  42. }
  43. public bool PathUsesLowerCase {
  44. get {
  45. string uses_lower_case = GetValue ("path", "uses_lower_case");
  46. if (!string.IsNullOrEmpty (uses_lower_case))
  47. return uses_lower_case.Equals (bool.TrueString);
  48. else
  49. return false;
  50. }
  51. }
  52. private string plugin_directory;
  53. public SparklePlugin (string plugin_path)
  54. {
  55. this.plugin_directory = System.IO.Path.GetDirectoryName (plugin_path);
  56. Load (plugin_path);
  57. }
  58. public static SparklePlugin Create (string name, string description, string address_value,
  59. string address_example, string path_value, string path_example)
  60. {
  61. string plugin_path = System.IO.Path.Combine (LocalPluginsPath, name + ".xml");
  62. if (IO.File.Exists (plugin_path))
  63. return null;
  64. string plugin_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  65. "<sparkleshare>" +
  66. " <plugin>" +
  67. " <info>" +
  68. " <name>" + name + "</name>" +
  69. " <description>" + description + "</description>" +
  70. " <icon>own-server.png</icon>" +
  71. " </info>" +
  72. " <address>" +
  73. " <value>" + address_value + "</value>" +
  74. " <example>" + address_example + "</example>" +
  75. " </address>" +
  76. " <path>" +
  77. " <value>" + path_value + "</value>" +
  78. " <example>" + path_example + "</example>" +
  79. " </path>" +
  80. " </plugin>" +
  81. "</sparkleshare>";
  82. plugin_xml = plugin_xml.Replace ("<value></value>", "<value/>");
  83. plugin_xml = plugin_xml.Replace ("<example></example>", "<example/>");
  84. if (!IO.Directory.Exists (LocalPluginsPath))
  85. IO.Directory.CreateDirectory (LocalPluginsPath);
  86. IO.File.WriteAllText (plugin_path, plugin_xml);
  87. return new SparklePlugin (plugin_path);
  88. }
  89. private string GetValue (string a, string b)
  90. {
  91. XmlNode node = SelectSingleNode ("/sparkleshare/plugin/" + a + "/" + b + "/text()");
  92. if (node != null && !string.IsNullOrEmpty (node.Value))
  93. return node.Value;
  94. else
  95. return null;
  96. }
  97. }
  98. }