Add early implementation (will be redone with 2.0 anyways) of IsolatedStorage
[mono.git] / mcs / class / corlib / System.IO.IsolatedStorage / MoonIsolatedStorageFile.cs
1 //
2 // System.IO.IsolatedStorage.MoonIsolatedStorageFile
3 //
4 // Moonlight's implementation for the IsolatedStorageFile
5 // 
6 // Authors
7 //      Miguel de Icaza (miguel@novell.com)
8 //
9 // Copyright (C) 2007 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 #if NET_2_1
31 using System;
32 using System.IO;
33 using System.Security;
34
35 namespace System.IO.IsolatedStorage {
36
37         public sealed class IsolatedStorageFile : IDisposable {
38                 static string appdir;
39                 
40                 static string TryDirectory (string path)
41                 {
42                         try {
43                                 Directory.CreateDirectory (path);
44                                 return path;
45                         } catch {
46                                 return null;
47                         }
48                 }
49                 
50                 static IsolatedStorageFile ()
51                 {
52                         string xdg_data_home = Environment.GetEnvironmentVariable ("XDG_DATA_HOME");
53                         if (xdg_data_home == null || xdg_data_home == String.Empty){
54                                 xdg_data_home = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData);
55                         }
56
57                         string basedir;
58                         basedir = TryDirectory (Path.Combine (xdg_data_home, "moonlight"));
59                         if (basedir == null){
60                                 //
61                                 // Maybe try a few others?
62                                 //
63                                 return;
64                         }
65
66                         // FIXME: Use the actual url from the plugin for this.
67                         appdir = Path.Combine (basedir, "url");
68                         try {
69                                 Directory.CreateDirectory (appdir);
70                         } catch {
71                                 appdir = null;
72                         }
73                 }
74
75                 internal IsolatedStorageFile (string basedir)
76                 {
77                 }
78                 
79                 [SecuritySafeCritical]
80                 public static IsolatedStorageFile GetUserStoreForApplication ()
81                 {
82                         if (appdir == null)
83                                 throw new SecurityException ();
84                         
85                         return new IsolatedStorageFile (appdir);
86                 }
87
88                 internal static string Verify (string path)
89                 {
90                         try {
91                                 string full = Path.GetFullPath (Path.Combine (appdir, path));
92                                 if (full.StartsWith (appdir + Path.DirectorySeparatorChar))
93                                         return full;
94                         } catch {
95                                 throw new IsolatedStorageException ();
96                         }
97                         throw new IsolatedStorageException ();
98                 }
99                 
100                 [SecuritySafeCritical]
101                 public void CreateDirectory (string dir)
102                 {
103                         Verify (dir);
104                         Directory.CreateDirectory (dir);
105                 }    
106                 
107                 [SecuritySafeCritical]
108                 public void DeleteDirectory (string dir)
109                 {
110                         Verify (dir);
111                         Directory.Delete (dir);
112                 }
113
114                 [SecuritySafeCritical]
115                 public string [] GetDirectoryNames (string searchPattern)
116                 {
117                         if (searchPattern.IndexOf ('/') != -1)
118                                 throw new IsolatedStorageException ();
119                         
120                         return Directory.GetDirectories (appdir, searchPattern);
121                 }
122
123                 [SecuritySafeCritical]
124                 public string [] GetFileNames (string searchPattern)
125                 {
126                         if (searchPattern.IndexOf ('/') != -1)
127                                 throw new IsolatedStorageException ();
128                         
129                         return Directory.GetDirectories (appdir, searchPattern);
130                 }
131
132                 [SecuritySafeCritical]
133                 public void DeleteFile (string file)
134                 {
135                         Verify (file);
136                 }
137                 
138                 public void Dispose ()
139                 {
140                 }
141
142                 [SecuritySafeCritical]
143                 public void Close ()
144                 {
145                 }
146
147                 [CLSCompliant(false)]
148                 public ulong CurrentSize {
149                         get {
150                                 return 0;
151                         }
152                 }
153
154                 [CLSCompliant(false)]
155                 public ulong MaximumSize {
156                         get {
157                                 return 1024*1024;
158                         }
159                 }
160         }
161 }
162 #endif