diff options
author | Lluis Sanchez Gual <lluis@novell.com> | 2011-01-28 19:32:04 +0300 |
---|---|---|
committer | Lluis Sanchez Gual <lluis@novell.com> | 2011-01-28 19:32:04 +0300 |
commit | bfaf1313c0cc8c1e652ba546fe93b08c81311a21 (patch) | |
tree | a9d047b55eb8a371cfc02197169bda5f4de7c0a1 /Mono.Addins/Mono.Addins.Description | |
parent | 584556cf7a021b6e954c2794147bd105e2e39c3c (diff) |
Add support for custom add-in properties.
Diffstat (limited to 'Mono.Addins/Mono.Addins.Description')
3 files changed, 178 insertions, 0 deletions
diff --git a/Mono.Addins/Mono.Addins.Description/AddinDescription.cs b/Mono.Addins/Mono.Addins.Description/AddinDescription.cs index 3d589e9..232e41b 100644 --- a/Mono.Addins/Mono.Addins.Description/AddinDescription.cs +++ b/Mono.Addins/Mono.Addins.Description/AddinDescription.cs @@ -28,6 +28,7 @@ using System; using System.Collections; +using System.Collections.Generic; using System.IO; using System.Xml; using System.Xml.Serialization; @@ -77,6 +78,8 @@ namespace Mono.Addins.Description ExtensionNodeDescription localizer; object[] fileInfo; + AddinPropertyCollectionImpl properties; + internal static BinaryXmlTypeMap typeMap; static AddinDescription () @@ -505,6 +508,17 @@ namespace Mono.Addins.Description } /// <summary> + /// Custom properties specified in the add-in header + /// </summary> + public AddinPropertyCollection Properties { + get { + if (properties == null) + properties = new AddinPropertyCollectionImpl (); + return properties; + } + } + + /// <summary> /// Adds an extension point. /// </summary> /// <returns> @@ -760,6 +774,28 @@ namespace Mono.Addins.Description if (extensionPoints != null) extensionPoints.SaveXml (elem); + + XmlElement oldHeader = (XmlElement) elem.SelectSingleNode ("Header"); + if (properties == null || properties.Count == 0) { + if (oldHeader != null) + elem.RemoveChild (oldHeader); + } else { + if (oldHeader == null) { + oldHeader = elem.OwnerDocument.CreateElement ("Header"); + if (elem.FirstChild != null) + elem.InsertBefore (elem, elem.FirstChild); + else + elem.AppendChild (oldHeader); + } + oldHeader.RemoveAll (); + foreach (var prop in properties) { + XmlElement propElem = elem.OwnerDocument.CreateElement (prop.Name); + if (!string.IsNullOrEmpty (prop.Locale)) + propElem.SetAttribute ("locale", prop.Locale); + propElem.InnerText = prop.Value ?? string.Empty; + oldHeader.AppendChild (propElem); + } + } } @@ -1023,6 +1059,7 @@ namespace Mono.Addins.Description writer.WriteValue ("FilesInfo", fileInfo); writer.WriteValue ("Localizer", localizer); writer.WriteValue ("flags", (int)flags); + writer.WriteValue ("Properties", properties); } void IBinaryXmlElement.Read (BinaryXmlReader reader) @@ -1051,6 +1088,7 @@ namespace Mono.Addins.Description fileInfo = (object[]) reader.ReadValue ("FilesInfo", null); localizer = (ExtensionNodeDescription) reader.ReadValue ("Localizer"); flags = (AddinFlags) reader.ReadInt32Value ("flags"); + properties = (AddinPropertyCollectionImpl) reader.ReadValue ("Properties", new AddinPropertyCollectionImpl ()); if (mainModule != null) mainModule.SetParent (this); diff --git a/Mono.Addins/Mono.Addins.Description/AddinProperty.cs b/Mono.Addins/Mono.Addins.Description/AddinProperty.cs new file mode 100644 index 0000000..e9b25ab --- /dev/null +++ b/Mono.Addins/Mono.Addins.Description/AddinProperty.cs @@ -0,0 +1,37 @@ +// +// AddinProperty.cs +// +// Author: +// Lluis Sanchez Gual <lluis@novell.com> +// +// Copyright (c) 2011 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; + +namespace Mono.Addins.Description +{ + public class AddinProperty + { + public string Name { get; set; } + public string Value { get; set; } + public string Locale { get; set; } + } +} + diff --git a/Mono.Addins/Mono.Addins.Description/AddinPropertyCollection.cs b/Mono.Addins/Mono.Addins.Description/AddinPropertyCollection.cs new file mode 100644 index 0000000..63f2339 --- /dev/null +++ b/Mono.Addins/Mono.Addins.Description/AddinPropertyCollection.cs @@ -0,0 +1,103 @@ +// +// AddinPropertyCollection.cs +// +// Author: +// Lluis Sanchez Gual <lluis@novell.com> +// +// Copyright (c) 2011 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Collections.Generic; + +namespace Mono.Addins.Description +{ + public interface AddinPropertyCollection: IEnumerable<AddinProperty> + { + string GetPropertyValue (string name); + string GetPropertyValue (string name, string locale); + void SetPropertyValue (string name, string value); + void SetPropertyValue (string name, string value, string locale); + void RemoveProperty (string name); + void RemoveProperty (string name, string locale); + } + + class AddinPropertyCollectionImpl: List<AddinProperty>, AddinPropertyCollection + { + public string GetPropertyValue (string name) + { + return GetPropertyValue (name, null); + } + + public string GetPropertyValue (string name, string locale) + { + AddinProperty best = null; + foreach (var p in this) { + if (p.Name == name) { + if (best == null) + best = p; + else if (string.IsNullOrEmpty (p.Locale)) + best = p; + if (p.Locale == locale) + return p.Value; + } + } + if (best != null) + return best.Value; + else + return string.Empty; + } + + public void SetPropertyValue (string name, string value) + { + SetPropertyValue (name, value, null); + } + + public void SetPropertyValue (string name, string value, string locale) + { + foreach (var p in this) { + if (p.Name == name && p.Locale == locale) { + p.Value = value; + return; + } + } + AddinProperty prop = new AddinProperty (); + prop.Name = name; + prop.Value = value; + prop.Locale = locale; + Add (prop); + } + + public void RemoveProperty (string name) + { + RemoveProperty (name, null); + } + + public void RemoveProperty (string name, string locale) + { + foreach (var p in this) { + if (p.Name == name && p.Locale == locale) { + Remove (p); + return; + } + } + } + } +} + |