2007-11-30 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System.IO.IsolatedStorage / IsolatedStorage.cs
1 //
2 // System.IO.IsolatedStorage.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Globalization;
32 using System.Reflection;
33 using System.Runtime.InteropServices;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Security.Policy;
37
38 namespace System.IO.IsolatedStorage {
39
40 #if NET_2_0
41         [ComVisible (true)]
42 #endif
43         public abstract class IsolatedStorage : MarshalByRefObject {
44
45                 // Constructor
46                 protected IsolatedStorage ()
47                         : base ()
48                 {
49                 }
50
51                 internal IsolatedStorageScope storage_scope;
52                 internal object _assemblyIdentity;
53                 internal object _domainIdentity;
54                 internal object _applicationIdentity;
55
56                 // Properties
57
58 #if NET_2_0
59                 [MonoTODO ("requires manifest support")]
60                 [ComVisible (false)]
61                 public object ApplicationIdentity {
62                         [SecurityPermission (SecurityAction.Demand, ControlPolicy=true)]
63                         get {
64                                 if ((storage_scope & IsolatedStorageScope.Application) == 0) {
65                                         throw new InvalidOperationException (Locale.GetText ("Invalid Isolation Scope.")); 
66                                 }
67                                 if (_applicationIdentity == null)
68                                         throw new InvalidOperationException (Locale.GetText ("Identity unavailable.")); 
69
70                                 throw new NotImplementedException (Locale.GetText ("CAS related")); 
71                         }
72                 }
73 #endif
74
75                 public object AssemblyIdentity {
76                         [SecurityPermission (SecurityAction.Demand, ControlPolicy=true)]
77                         get {
78 #if NET_2_0
79                                 if ((storage_scope & IsolatedStorageScope.Assembly) == 0) {
80                                         throw new InvalidOperationException (Locale.GetText ("Invalid Isolation Scope.")); 
81                                 }
82                                 if (_assemblyIdentity == null)
83                                         throw new InvalidOperationException (Locale.GetText ("Identity unavailable.")); 
84 #endif
85                                 return _assemblyIdentity;
86                         }
87                 }
88
89                 [CLSCompliant (false)]
90                 public virtual ulong CurrentSize {
91                         get {
92                                 throw new InvalidOperationException (
93                                         Locale.GetText ("IsolatedStorage does not have a preset CurrentSize."));
94                         }
95                 }
96
97                 public object DomainIdentity {
98                         [SecurityPermission (SecurityAction.Demand, ControlPolicy=true)]
99                         get {
100                                 if ((storage_scope & IsolatedStorageScope.Domain) == 0) {
101                                         throw new InvalidOperationException (Locale.GetText ("Invalid Isolation Scope.")); 
102                                 }
103                                 if (_domainIdentity == null)
104                                         throw new InvalidOperationException (Locale.GetText ("Identity unavailable.")); 
105                                 return _domainIdentity;
106                         }
107                 }
108
109                 [CLSCompliant (false)]
110                 public virtual ulong MaximumSize {
111                         get {
112                                 throw new InvalidOperationException (
113                                         Locale.GetText ("IsolatedStorage does not have a preset MaximumSize."));
114                         }
115                 }
116
117                 public IsolatedStorageScope Scope {
118                         get { return storage_scope; }
119                 }
120
121                 protected virtual char SeparatorExternal {
122                         get { return System.IO.Path.DirectorySeparatorChar; }
123                 }
124
125                 protected virtual char SeparatorInternal {
126                         get { return '.'; }
127                 }
128
129                 // Methods
130                 protected abstract IsolatedStoragePermission GetPermission (PermissionSet ps);
131
132                 protected void InitStore (IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType)
133                 {
134                         // I know it's useless - but it's tested as such...
135                         switch (scope) {
136                         case (IsolatedStorageScope.Assembly | IsolatedStorageScope.User):
137                         case (IsolatedStorageScope.Assembly | IsolatedStorageScope.User | IsolatedStorageScope.Domain):
138                                 throw new NotImplementedException (scope.ToString ());
139                         default:
140                                 // invalid (incomplete) scope
141                                 throw new ArgumentException (scope.ToString ());
142                         }
143                 }
144 #if NET_2_0
145                 [MonoTODO ("requires manifest support")]
146                 protected void InitStore (IsolatedStorageScope scope, Type appEvidenceType)
147                 {
148                         if (AppDomain.CurrentDomain.ApplicationIdentity == null)
149                                 throw new IsolatedStorageException (Locale.GetText ("No ApplicationIdentity available for AppDomain."));
150
151                         if (appEvidenceType == null) {
152                                 // TODO - Choose evidence
153                         }
154
155                         // no exception here because this can work without CAS
156                         storage_scope = scope;
157                 }
158 #endif
159                 public abstract void Remove ();
160         }
161 }