2008-08-20 Sebastien Pouliot <sebastien@ximian.com>
[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, 2008 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 (String.IsNullOrEmpty (xdg_data_home)) {
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
76                 private bool removed = false;
77                 private bool disposed = false;
78
79                 internal IsolatedStorageFile (string basedir)
80                 {
81                 }
82                 
83                 internal void PreCheck ()
84                 {
85                         if (disposed)
86                                 throw new ObjectDisposedException ("Storage was disposed");
87                         if (removed)
88                                 throw new IsolatedStorageException ("Storage was removed");
89                 }
90
91                 public static IsolatedStorageFile GetUserStoreForApplication ()
92                 {
93                         if (appdir == null)
94                                 throw new SecurityException ();
95                         
96                         return new IsolatedStorageFile (appdir);
97                 }
98
99                 public static IsolatedStorageFile GetUserStoreForSite ()
100                 {
101                         if (appdir == null)
102                                 throw new SecurityException ();
103                         
104                         return new IsolatedStorageFile (appdir);
105                 }
106
107                 internal static string Verify (string path)
108                 {
109                         try {
110                                 string full = Path.GetFullPath (Path.Combine (appdir, path));
111                                 if (full.StartsWith (appdir + Path.DirectorySeparatorChar))
112                                         return full;
113                         } catch {
114                                 throw new IsolatedStorageException ();
115                         }
116                         throw new IsolatedStorageException ();
117                 }
118                 
119                 public void CreateDirectory (string dir)
120                 {
121                         Verify (dir);
122                         Directory.CreateDirectory (dir);
123                 }
124
125                 public IsolatedStorageFileStream CreateFile (string path)
126                 {
127                         PreCheck ();
128                         if (path == null)
129                                 throw new ArgumentNullException ("path");
130
131                         return new IsolatedStorageFileStream (path, FileMode.Create, this);
132                 }
133                 
134                 public void DeleteDirectory (string dir)
135                 {
136                         PreCheck ();
137                         Verify (dir);
138                         Directory.Delete (dir);
139                 }
140
141                 public bool DirectoryExists (string path)
142                 {
143                         PreCheck ();
144                         Verify (path);
145                         return Directory.Exists (path);
146                 }
147
148                 public bool FileExists (string path)
149                 {
150                         PreCheck ();
151                         Verify (path);
152                         return File.Exists (path);
153                 }
154
155                 public string [] GetDirectoryNames ()
156                 {
157                         return Directory.GetFiles (appdir);
158                 }
159
160                 public string [] GetDirectoryNames (string searchPattern)
161                 {
162                         if (searchPattern.IndexOf ('/') != -1)
163                                 throw new IsolatedStorageException ();
164                         
165                         return Directory.GetDirectories (appdir, searchPattern);
166                 }
167
168                 public string [] GetFileNames ()
169                 {
170                         return Directory.GetFiles (appdir);
171                 }
172
173                 public string [] GetFileNames (string searchPattern)
174                 {
175                         if (searchPattern.IndexOf ('/') != -1)
176                                 throw new IsolatedStorageException ();
177                         
178                         return Directory.GetFiles (appdir, searchPattern);
179                 }
180
181                 public void DeleteFile (string file)
182                 {
183                         Verify (file);
184                         File.Delete (file);
185                 }
186                 
187                 public void Dispose ()
188                 {
189                         disposed = true;
190                 }
191
192                 public IsolatedStorageFileStream OpenFile (string path, FileMode mode)
193                 {
194                         return OpenFile (path, mode, FileAccess.ReadWrite, FileShare.None);
195                 }
196
197                 public IsolatedStorageFileStream OpenFile (string path, FileMode mode, FileAccess access)
198                 {
199                         return OpenFile (path, mode, access, FileShare.None);
200                 }
201
202                 public IsolatedStorageFileStream OpenFile (string path, FileMode mode, FileAccess access, FileShare share)
203                 {
204                         PreCheck ();
205                         if (path == null)
206                                 throw new ArgumentNullException ("path");
207
208                         return new IsolatedStorageFileStream (path, mode, access, share, this);
209                 }
210
211                 public void Remove ()
212                 {
213                         PreCheck ();
214                         try {
215                                 // TODO - try to clean out everything
216                         }
217                         finally {
218                                 removed = true;
219                         }
220                 }
221
222                 public long AvailableFreeSpace {
223                         get {
224                                 PreCheck ();
225                                 return 1024*1024;
226                         }
227                 }
228
229                 public long Quota {
230                         get {
231                                 PreCheck ();
232                                 return 1024*1024;
233                         }
234                 }
235
236                 public bool IncreaseQuotaTo (long newQuotaSize)
237                 {
238                         PreCheck ();
239                         if (newQuotaSize <= Quota)
240                                 throw new ArgumentException ("newQuotaSize", "Only increase is possible");
241
242                         return true;
243                 }
244         }
245 }
246 #endif