2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Security.Policy / ApplicationTrustCollection.cs
blob3afbad5c676784bfdb23dece300f208ac15f16fd
1 //
2 // System.Security.Policy.ApplicationTrustCollection class
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Globalization;
32 using System.Runtime.InteropServices;
34 namespace System.Security.Policy {
36 [ComVisible (true)]
37 public sealed class ApplicationTrustCollection : ICollection, IEnumerable {
39 private ArrayList _list;
41 internal ApplicationTrustCollection ()
43 _list = new ArrayList ();
46 // constants (from beta1 - still useful ?)
48 // public const string ApplicationTrustProperty = "ApplicationTrust";
49 // public const string InstallReferenceIdentifier = "{3f471841-eef2-47d6-89c0-d028f03a4ad5}";
51 // properties
53 public int Count {
54 get { return _list.Count; }
57 public bool IsSynchronized {
58 get { return false; } // always false
61 public object SyncRoot {
62 get { return this; } // self
65 public ApplicationTrust this [int index] {
66 get { return (ApplicationTrust) _list [index]; }
69 public ApplicationTrust this [string appFullName] {
70 get {
71 for (int i=0; i < _list.Count; i++) {
72 ApplicationTrust at = (_list [i] as ApplicationTrust);
73 if (at.ApplicationIdentity.FullName == appFullName)
74 return at;
76 return null;
80 // methods
82 public int Add (ApplicationTrust trust)
84 if (trust == null)
85 throw new ArgumentNullException ("trust");
86 if (trust.ApplicationIdentity == null) {
87 throw new ArgumentException (Locale.GetText (
88 "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
91 return _list.Add (trust);
94 public void AddRange (ApplicationTrust[] trusts)
96 if (trusts == null)
97 throw new ArgumentNullException ("trusts");
99 foreach (ApplicationTrust t in trusts) {
100 if (t.ApplicationIdentity == null) {
101 throw new ArgumentException (Locale.GetText (
102 "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
104 _list.Add (t);
108 public void AddRange (ApplicationTrustCollection trusts)
110 if (trusts == null)
111 throw new ArgumentNullException ("trusts");
113 foreach (ApplicationTrust t in trusts) {
114 if (t.ApplicationIdentity == null) {
115 throw new ArgumentException (Locale.GetText (
116 "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
118 _list.Add (t);
122 public void Clear ()
124 _list.Clear ();
127 public void CopyTo (ApplicationTrust[] array, int index)
129 _list.CopyTo (array, index);
132 void ICollection.CopyTo (Array array, int index)
134 _list.CopyTo (array, index);
137 public ApplicationTrustCollection Find (ApplicationIdentity applicationIdentity, ApplicationVersionMatch versionMatch)
139 if (applicationIdentity == null)
140 throw new ArgumentNullException ("applicationIdentity");
142 string fullname = applicationIdentity.FullName;
144 switch (versionMatch) {
145 case ApplicationVersionMatch.MatchAllVersions:
146 int pos = fullname.IndexOf (", Version=");
147 if (pos >= 0)
148 fullname = fullname.Substring (0, pos);
149 break;
150 case ApplicationVersionMatch.MatchExactVersion:
151 break;
152 default:
153 throw new ArgumentException ("versionMatch");
156 ApplicationTrustCollection coll = new ApplicationTrustCollection ();
157 foreach (ApplicationTrust t in _list) {
158 if (t.ApplicationIdentity.FullName.StartsWith (fullname)) {
159 coll.Add (t);
163 return coll;
166 public ApplicationTrustEnumerator GetEnumerator ()
168 return new ApplicationTrustEnumerator (this);
171 IEnumerator IEnumerable.GetEnumerator ()
173 return (IEnumerator) new ApplicationTrustEnumerator (this);
176 public void Remove (ApplicationTrust trust)
178 if (trust == null)
179 throw new ArgumentNullException ("trust");
180 if (trust.ApplicationIdentity == null) {
181 throw new ArgumentException (Locale.GetText (
182 "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
185 RemoveAllInstances (trust);
188 public void Remove (ApplicationIdentity applicationIdentity, ApplicationVersionMatch versionMatch)
190 ApplicationTrustCollection coll = Find (applicationIdentity, versionMatch);
191 foreach (ApplicationTrust t in coll) {
192 RemoveAllInstances (t);
196 public void RemoveRange (ApplicationTrust[] trusts)
198 if (trusts == null)
199 throw new ArgumentNullException ("trusts");
201 foreach (ApplicationTrust t in trusts) {
202 RemoveAllInstances (t);
206 public void RemoveRange (ApplicationTrustCollection trusts)
208 if (trusts == null)
209 throw new ArgumentNullException ("trusts");
211 foreach (ApplicationTrust t in trusts) {
212 RemoveAllInstances (t);
216 // helpers
218 internal void RemoveAllInstances (ApplicationTrust trust)
220 for (int i=_list.Count - 1; i >= 0; i--) {
221 if (trust.Equals (_list [i]))
222 _list.RemoveAt (i);