2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Security.Permissions / IsolatedStorageFilePermission.cs
1 //
2 // System.Security.Permissions.IsolatedStorageFilePermission.cs
3 //
4 // Author
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2003 Motus Technologies. http://www.motus.com
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Runtime.InteropServices;
31
32 namespace System.Security.Permissions {
33
34         [Serializable]
35         public sealed class IsolatedStorageFilePermission : IsolatedStoragePermission, IBuiltInPermission {
36
37                 private const int version = 1;
38
39                 // Constructors
40
41                 [MonoTODO ("usage/quota calculated from evidences/policy")]
42                 public IsolatedStorageFilePermission (PermissionState state) : base (state)
43                 {
44                         if (!IsUnrestricted ()) {
45                                 // TODO
46                         }
47                 }
48
49                 // Properties
50
51                 // Methods
52
53                 public override IPermission Copy () 
54                 {
55                         IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
56                         p.m_userQuota = m_userQuota;
57                         p.m_machineQuota = m_machineQuota;
58                         p.m_expirationDays = m_expirationDays;
59                         p.m_permanentData = m_permanentData;
60                         p.m_allowed = m_allowed;
61                         return p;
62                 }
63
64                 public override IPermission Intersect (IPermission target) 
65                 {
66                         IsolatedStorageFilePermission isfp = Cast (target);
67                         if (isfp == null)
68                                 return null;
69                         if (IsEmpty () && isfp.IsEmpty ())
70                                 return null;
71
72                         IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
73                         p.m_userQuota = (m_userQuota < isfp.m_userQuota) ? m_userQuota : isfp.m_userQuota;
74                         p.m_machineQuota = (m_machineQuota < isfp.m_machineQuota) ? m_machineQuota : isfp.m_machineQuota;
75                         p.m_expirationDays = (m_expirationDays < isfp.m_expirationDays) ? m_expirationDays : isfp.m_expirationDays;
76                         p.m_permanentData = (m_permanentData && isfp.m_permanentData);
77                         // UsageAllowed == Unrestricted is a special case handled by the property
78                         p.UsageAllowed = (m_allowed < isfp.m_allowed) ? m_allowed : isfp.m_allowed;
79                         return p;
80                 }
81
82                 public override bool IsSubsetOf (IPermission target) 
83                 {
84                         IsolatedStorageFilePermission isfp = Cast (target);
85                         if (isfp == null)
86                                 return IsEmpty ();
87                         if (isfp.IsUnrestricted ())
88                                 return true;
89
90                         if (m_userQuota > isfp.m_userQuota)
91                                 return false;
92                         if (m_machineQuota > isfp.m_machineQuota)
93                                 return false;
94                         if (m_expirationDays > isfp.m_expirationDays)
95                                 return false;
96                         if (m_permanentData != isfp.m_permanentData)
97                                 return false;
98                         if (m_allowed > isfp.m_allowed)
99                                 return false;
100                         return true;
101                 }
102
103                 public override IPermission Union (IPermission target)
104                 {
105                         IsolatedStorageFilePermission isfp = Cast (target);
106                         if (isfp == null)
107                                 return Copy ();
108
109                         IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
110                         p.m_userQuota = (m_userQuota > isfp.m_userQuota) ? m_userQuota : isfp.m_userQuota;
111                         p.m_machineQuota = (m_machineQuota > isfp.m_machineQuota) ? m_machineQuota : isfp.m_machineQuota;
112                         p.m_expirationDays = (m_expirationDays > isfp.m_expirationDays) ? m_expirationDays : isfp.m_expirationDays;
113                         p.m_permanentData = (m_permanentData || isfp.m_permanentData);
114                         // UsageAllowed == Unrestricted is a special case handled by the property
115                         p.UsageAllowed = (m_allowed > isfp.m_allowed) ? m_allowed : isfp.m_allowed;
116                         return p;
117                 }
118
119 #if NET_2_0
120                 [MonoTODO]
121                 [ComVisible (false)]
122                 public override SecurityElement ToXml ()
123                 {
124                         SecurityElement se = base.ToXml ();
125                         // TODO - something must have been added ???
126                         return se;
127                 }
128 #endif
129
130                 // IBuiltInPermission
131                 int IBuiltInPermission.GetTokenIndex ()
132                 {
133                         return (int) BuiltInToken.IsolatedStorageFile;
134                 }
135
136                 // helpers
137
138                 private IsolatedStorageFilePermission Cast (IPermission target)
139                 {
140                         if (target == null)
141                                 return null;
142
143                         IsolatedStorageFilePermission isfp = (target as IsolatedStorageFilePermission);
144                         if (isfp == null) {
145                                 ThrowInvalidPermission (target, typeof (IsolatedStorageFilePermission));
146                         }
147
148                         return isfp;
149                 }
150         }
151 }