2005-04-12 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Policy / ApplicationTrustCollection.cs
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.
27 //
28
29 #if NET_2_0
30
31 using System.Collections;
32 using System.Globalization;
33
34 namespace System.Security.Policy {
35
36         public sealed class ApplicationTrustCollection : ICollection, IEnumerable {
37
38                 private ArrayList _list;
39
40                 internal ApplicationTrustCollection ()
41                 {
42                         _list = new ArrayList ();
43                 }
44
45                 // constants
46
47                 public const string ApplicationTrustProperty = "ApplicationTrust";
48                 public const string InstallReferenceIdentifier = "{3f471841-eef2-47d6-89c0-d028f03a4ad5}";
49
50                 // properties
51
52                 public int Count {
53                         get { return _list.Count; }
54                 }
55
56                 public bool IsSynchronized {
57                         get { return false; }   // always false
58                 }
59
60                 public object SyncRoot {
61                         get { return this; }    // self
62                 }
63
64                 public ApplicationTrust this [int index] {
65                         get { return (ApplicationTrust) _list [index]; }
66                 }
67
68                 [MonoTODO]
69                 public ApplicationTrust this [string appFullName] {
70                         get { return (ApplicationTrust) _list [0]; }
71                 }
72
73                 // methods
74
75                 public int Add (ApplicationTrust trust)
76                 {
77                         if (trust == null)
78                                 throw new ArgumentNullException ("trust");
79                         if (trust.ApplicationIdentity == null) {
80                                 throw new ArgumentException (Locale.GetText (
81                                         "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
82                         }
83
84                         return _list.Add (trust);
85                 }
86
87                 public void AddRange (ApplicationTrust[] trusts)
88                 {
89                         if (trusts == null)
90                                 throw new ArgumentNullException ("trusts");
91
92                         foreach (ApplicationTrust t in trusts) {
93                                 if (t.ApplicationIdentity == null) {
94                                         throw new ArgumentException (Locale.GetText (
95                                                 "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
96                                 }
97                                 _list.Add (t);
98                         }
99                 }
100
101                 public void AddRange (ApplicationTrustCollection trusts)
102                 {
103                         if (trusts == null)
104                                 throw new ArgumentNullException ("trusts");
105
106                         foreach (ApplicationTrust t in trusts) {
107                                 if (t.ApplicationIdentity == null) {
108                                         throw new ArgumentException (Locale.GetText (
109                                                 "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
110                                 }
111                                 _list.Add (t);
112                         }
113                 }
114
115                 public void Clear ()
116                 {
117                         _list.Clear ();
118                 }
119
120                 public void CopyTo (ApplicationTrust[] array, int index)
121                 {
122                         _list.CopyTo (array, index);
123                 }
124
125                 void ICollection.CopyTo (Array array, int index)
126                 {
127                         _list.CopyTo (array, index);
128                 }
129
130                 [MonoTODO ("missing MatchExactVersion")]
131                 public ApplicationTrustCollection Find (ApplicationIdentity applicationIdentity, ApplicationVersionMatch versionMatch)
132                 {
133                         ApplicationTrustCollection coll = new ApplicationTrustCollection ();
134                         foreach (ApplicationTrust t in _list) {
135                                 if (t.ApplicationIdentity.Equals (applicationIdentity)) {
136                                         switch (versionMatch) {
137                                         case ApplicationVersionMatch.MatchAllVersions:
138                                                 coll.Add (t);
139                                                 break;
140                                         case ApplicationVersionMatch.MatchExactVersion:
141                                                 // TODO: version is encoded in a fullname ?
142                                                 break;
143                                         }
144                                 }
145                         }
146                         return coll;
147                 }
148
149                 public ApplicationTrustEnumerator GetEnumerator ()
150                 {
151                         return new ApplicationTrustEnumerator (this);
152                 }
153
154                 IEnumerator IEnumerable.GetEnumerator ()
155                 {
156                         return (IEnumerator) new ApplicationTrustEnumerator (this);
157                 }
158
159                 public void Remove (ApplicationTrust trust)
160                 {
161                         if (trust == null)
162                                 throw new ArgumentNullException ("trust");
163                         if (trust.ApplicationIdentity == null) {
164                                 throw new ArgumentException (Locale.GetText (
165                                         "ApplicationTrust.ApplicationIdentity can't be null."), "trust");
166                         }
167
168                         RemoveAllInstances (trust);
169                 }
170
171                 [MonoTODO ("missing MatchExactVersion (relies on Find)")]
172                 public void Remove (ApplicationIdentity applicationIdentity, ApplicationVersionMatch versionMatch)
173                 {
174                         ApplicationTrustCollection coll = Find (applicationIdentity, versionMatch);
175                         foreach (ApplicationTrust t in coll) {
176                                 RemoveAllInstances (t);
177                         }
178                 }
179
180                 public void RemoveRange (ApplicationTrust[] trusts)
181                 {
182                         if (trusts == null)
183                                 throw new ArgumentNullException ("trusts");
184
185                         foreach (ApplicationTrust t in trusts) {
186                                 RemoveAllInstances (t);
187                         }
188                 }
189
190                 public void RemoveRange (ApplicationTrustCollection trusts)
191                 {
192                         if (trusts == null)
193                                 throw new ArgumentNullException ("trusts");
194
195                         foreach (ApplicationTrust t in trusts) {
196                                 RemoveAllInstances (t);
197                         }
198                 }
199
200                 // helpers
201
202                 internal void RemoveAllInstances (ApplicationTrust trust)
203                 {
204                         for (int i=_list.Count - 1; i >= 0; i--) {
205                                 if (trust.Equals (_list [i]))
206                                         _list.RemoveAt (i);
207                         }
208                 }
209         }
210 }
211
212 #endif