PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17
 18using System;
 19using System.Xml;
 20
 21using IO = System.IO;
 22
 23namespace SparkleShare {
 24
 25    public class SparklePlugin : XmlDocument {
 26
 27        public static string PluginsPath = "";
 28
 29        public static string LocalPluginsPath = IO.Path.Combine (
 30            Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "sparkleshare", "plugins");
 31
 32        new public string Name { get { return GetValue ("info", "name"); } }
 33        public string Description { get { return GetValue ("info", "description"); } }
 34        public string Backend { get { return GetValue ("info", "backend"); } }
 35        public string Fingerprint { get { return GetValue ("info", "fingerprint"); } }
 36        public string AnnouncementsUrl { get { return GetValue ("info", "announcements_url"); } }
 37        public string Address { get { return GetValue ("address", "value"); } }
 38        public string AddressExample { get { return GetValue ("address", "example"); } }
 39        public string Path { get { return GetValue ("path", "value"); } }
 40        public string PathExample { get { return GetValue ("path", "example"); } }
 41
 42        public string ImagePath {
 43            get {
 44                string image_file_name = GetValue ("info", "icon");
 45                string image_path      = IO.Path.Combine (this.plugin_directory, image_file_name);
 46                
 47                if (IO.File.Exists (image_path))
 48                    return image_path;
 49                else
 50                    return IO.Path.Combine (PluginsPath, image_file_name);
 51            }
 52        }
 53        
 54        public bool PathUsesLowerCase {
 55            get {
 56                string uses_lower_case = GetValue ("path", "uses_lower_case");
 57                
 58                if (!string.IsNullOrEmpty (uses_lower_case))
 59                    return uses_lower_case.Equals (bool.TrueString);
 60                else
 61                    return false;
 62            }
 63        }
 64
 65        private string plugin_directory;
 66
 67
 68        public SparklePlugin (string plugin_path)
 69        {
 70            this.plugin_directory = System.IO.Path.GetDirectoryName (plugin_path);
 71            Load (plugin_path);
 72        }
 73
 74
 75        public static SparklePlugin Create (string name, string description, string address_value,
 76            string address_example, string path_value, string path_example)
 77        {
 78            string plugin_path = System.IO.Path.Combine (LocalPluginsPath, name + ".xml");
 79
 80            if (IO.File.Exists (plugin_path))
 81                return null;
 82
 83            string plugin_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
 84                "<sparkleshare>" +
 85                "  <plugin>" +
 86                "    <info>" +
 87                "        <name>" + name + "</name>" +
 88                "        <description>" + description + "</description>" +
 89                "        <icon>own-server.png</icon>" +
 90                "    </info>" +
 91                "    <address>" +
 92                "      <value>" + address_value + "</value>" +
 93                "      <example>" + address_example + "</example>" +
 94                "    </address>" +
 95                "    <path>" +
 96                "      <value>" + path_value + "</value>" +
 97                "      <example>" + path_example + "</example>" +
 98                "    </path>" +
 99                "  </plugin>" +
100                "</sparkleshare>";
101
102            plugin_xml = plugin_xml.Replace ("<value></value>", "<value/>");
103            plugin_xml = plugin_xml.Replace ("<example></example>", "<example/>");
104
105            if (!IO.Directory.Exists (LocalPluginsPath))
106                IO.Directory.CreateDirectory (LocalPluginsPath);
107
108            IO.File.WriteAllText (plugin_path, plugin_xml);
109            return new SparklePlugin (plugin_path);
110        }
111
112
113        private string GetValue (string a, string b)
114        {
115            XmlNode node = SelectSingleNode ("/sparkleshare/plugin/" + a + "/" + b + "/text()");
116
117            if (node != null && !string.IsNullOrEmpty (node.Value))
118                return node.Value;
119            else
120                return null;
121        }
122    }
123}