2
// AddinPropertyCollection.cs
5
// Lluis Sanchez Gual <lluis@novell.com>
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections.Generic;
30
namespace Mono.Addins.Description
33
/// A collection of add-in properties
35
public interface AddinPropertyCollection: IEnumerable<AddinProperty>
38
/// Gets the value of a property
41
/// The property value.
43
/// <param name='name'>
44
/// Name of the property.
47
/// If the property is localized, it will return the value for the current language if exists, or the
48
/// default value if it doesn't.
50
string GetPropertyValue (string name);
53
/// Gets the value of a property
56
/// The property value.
58
/// <param name='name'>
59
/// Name of the property.
61
/// <param name='locale'>
62
/// Locale for which the value must be returned.
64
string GetPropertyValue (string name, string locale);
67
/// Sets the value of a property
69
/// <param name='name'>
70
/// Name of the property
72
/// <param name='value'>
75
void SetPropertyValue (string name, string value);
78
/// Sets the value of a property for a specific locale
80
/// <param name='name'>
81
/// Name of the property.
83
/// <param name='value'>
86
/// <param name='locale'>
87
/// Locale of the property to be set.
89
void SetPropertyValue (string name, string value, string locale);
92
/// Removes a property.
94
/// <param name='name'>
95
/// Name of the property.
98
/// This method only removes properties which have no locale set.
100
void RemoveProperty (string name);
103
/// Removes a property with a specified locale
105
/// <param name='name'>
106
/// Name of the property
108
/// <param name='locale'>
109
/// Locale of the property
111
void RemoveProperty (string name, string locale);
114
class AddinPropertyCollectionImpl: List<AddinProperty>, AddinPropertyCollection
116
AddinDescription desc;
118
public AddinPropertyCollectionImpl ()
122
public AddinPropertyCollectionImpl (AddinDescription desc)
127
public AddinPropertyCollectionImpl (AddinPropertyCollection col)
132
public string GetPropertyValue (string name)
134
return GetPropertyValue (name, System.Threading.Thread.CurrentThread.CurrentCulture.ToString ());
137
public string GetPropertyValue (string name, string locale)
139
locale = NormalizeLocale (locale);
140
string lang = GetLocaleLang (locale);
141
AddinProperty sameLangDifCountry = null;
142
AddinProperty sameLang = null;
143
AddinProperty defaultLoc = null;
145
foreach (var p in this) {
146
if (p.Name == name) {
147
if (p.Locale == locale)
148
return ParseString (p.Value);
149
string plang = GetLocaleLang (p.Locale);
150
if (plang == p.Locale && plang == lang) // No country specified
152
else if (plang == lang)
153
sameLangDifCountry = p;
154
else if (p.Locale == null)
158
if (sameLang != null)
159
return ParseString (sameLang.Value);
160
else if (sameLangDifCountry != null)
161
return ParseString (sameLangDifCountry.Value);
162
else if (defaultLoc != null)
163
return ParseString (defaultLoc.Value);
168
string ParseString (string s)
171
return desc.ParseString (s);
176
string NormalizeLocale (string loc)
178
if (string.IsNullOrEmpty (loc))
180
return loc.Replace ('_','-');
183
string GetLocaleLang (string loc)
187
int i = loc.IndexOf ('-');
189
return loc.Substring (0, i);
194
public void SetPropertyValue (string name, string value)
196
SetPropertyValue (name, value, null);
199
public void SetPropertyValue (string name, string value, string locale)
201
if (string.IsNullOrEmpty (name))
202
throw new ArgumentException ("name can't be null or empty");
205
throw new ArgumentNullException ("value");
207
locale = NormalizeLocale (locale);
209
foreach (var p in this) {
210
if (p.Name == name && p.Locale == locale) {
215
AddinProperty prop = new AddinProperty ();
218
prop.Locale = locale;
222
public void RemoveProperty (string name)
224
RemoveProperty (name, null);
227
public void RemoveProperty (string name, string locale)
229
locale = NormalizeLocale (locale);
231
foreach (var p in this) {
232
if (p.Name == name && p.Locale == locale) {
239
internal bool HasProperty (string name)
241
return this.Any (p => p.Name == name);
244
internal string ExtractCoreProperty (string name, bool removeProperty)
246
foreach (var p in this) {
247
if (p.Name == name && p.Locale == null) {