[corlib] Add iOS specific code for System.Environment.
[mono.git] / mcs / class / corlib / System / Environment.iOS.cs
1 // Copyright 2014 Xamarin Inc.
2
3 // note: or older hack to give the Documents (or Library) directories 
4
5 using System.IO;
6 using System.Runtime.InteropServices;
7
8 namespace System {
9
10         public static partial class Environment {
11
12                 static string ns_document;
13                 static string ns_library;
14
15                 [DllImport ("__Internal")]
16                 extern static string xamarin_GetFolderPath (int folder);
17
18                 static string NSDocumentDirectory {
19                         get {
20                                 if (ns_document == null) {
21 #if MONOTOUCH_TV
22                                         // The "normal" NSDocumentDirectory is a read-only directory on tvOS
23                                         // and that breaks a lot of assumptions in the runtime and the BCL
24                                         // to avoid this we relocate the Documents directory under Caches
25                                         ns_document = Path.Combine (NSLibraryDirectory, "Caches", "Documents");
26                                         if (!Directory.Exists (ns_document))
27                                                 Directory.CreateDirectory (ns_document);
28 #else
29                                         ns_document = xamarin_GetFolderPath (/* NSDocumentDirectory */ 9);
30 #endif
31                                 }
32                                 return ns_document;
33                         }
34                 }
35
36                 // Various user-visible documentation, support, and configuration files
37                 static string NSLibraryDirectory {
38                         get {
39                                 if (ns_library == null)
40                                         ns_library = xamarin_GetFolderPath (/* NSLibraryDirectory */ 5);
41                                 return ns_library;
42                         }
43                 }
44
45                 public static string GetFolderPath (SpecialFolder folder, SpecialFolderOption option)
46                 {
47                         return UnixGetFolderPath (folder, option);
48                 }
49
50                 // needed by our BCL, e.g. IsolatedStorageFile.cs
51                 internal static string UnixGetFolderPath (SpecialFolder folder, SpecialFolderOption option)
52                 {
53                         var dir = iOSGetFolderPath (folder);
54                         if ((option == SpecialFolderOption.Create) && !Directory.Exists (dir))
55                                 Directory.CreateDirectory (dir);
56                         return dir;
57                 }
58
59                 internal static string iOSGetFolderPath (SpecialFolder folder)
60                 {
61                         switch (folder) {
62                         case SpecialFolder.MyComputer:
63                         case SpecialFolder.Programs:
64                         case SpecialFolder.SendTo:
65                         case SpecialFolder.StartMenu:
66                         case SpecialFolder.Startup:
67                         case SpecialFolder.Cookies:
68                         case SpecialFolder.History:
69                         case SpecialFolder.Recent:
70                         case SpecialFolder.CommonProgramFiles:
71                         case SpecialFolder.System:
72                         case SpecialFolder.NetworkShortcuts:
73                         case SpecialFolder.CommonStartMenu:
74                         case SpecialFolder.CommonPrograms:
75                         case SpecialFolder.CommonStartup:
76                         case SpecialFolder.CommonDesktopDirectory:
77                         case SpecialFolder.PrinterShortcuts:
78                         case SpecialFolder.Windows:
79                         case SpecialFolder.SystemX86:
80                         case SpecialFolder.ProgramFilesX86:
81                         case SpecialFolder.CommonProgramFilesX86:
82                         case SpecialFolder.CommonDocuments:
83                         case SpecialFolder.CommonAdminTools:
84                         case SpecialFolder.AdminTools:
85                         case SpecialFolder.CommonMusic:
86                         case SpecialFolder.CommonPictures:
87                         case SpecialFolder.CommonVideos:
88                         case SpecialFolder.LocalizedResources:
89                         case SpecialFolder.CommonOemLinks:
90                         case SpecialFolder.CDBurning:
91                                 return String.Empty;
92                         
93                         // personal == ~
94                         case SpecialFolder.Personal:
95                         case SpecialFolder.LocalApplicationData:
96                                 return NSDocumentDirectory;
97
98                         case SpecialFolder.ApplicationData:
99                                 // note: at first glance that looked like a good place to return NSLibraryDirectory 
100                                 // but it would break isolated storage for existing applications
101                                 return Path.Combine (NSDocumentDirectory, ".config");
102
103                         case SpecialFolder.Resources:
104                                 return NSLibraryDirectory; // older (8.2 and previous) would return String.Empty
105
106                         case SpecialFolder.Desktop:
107                         case SpecialFolder.DesktopDirectory:
108                                 return Path.Combine (NSDocumentDirectory, "Desktop");
109
110                         case SpecialFolder.MyMusic:
111                                 return Path.Combine (NSDocumentDirectory, "Music");
112
113                         case SpecialFolder.MyPictures:
114                                 return Path.Combine (NSDocumentDirectory, "Pictures");
115
116                         case SpecialFolder.Templates:
117                                 return Path.Combine (NSDocumentDirectory, "Templates");
118
119                         case SpecialFolder.MyVideos:
120                                 return Path.Combine (NSDocumentDirectory, "Videos");
121
122                         case SpecialFolder.CommonTemplates:
123                                 return "/usr/share/templates";
124
125                         case SpecialFolder.Fonts:
126                                 return Path.Combine (NSDocumentDirectory, ".fonts");
127
128                         case SpecialFolder.Favorites:
129                                 return Path.Combine (NSLibraryDirectory, "Favorites");
130
131                         case SpecialFolder.ProgramFiles:
132                                 return "/Applications";
133
134                         case SpecialFolder.InternetCache:
135                                 return Path.Combine (NSLibraryDirectory, "Caches");
136
137                         case SpecialFolder.UserProfile:
138                                 return internalGetHome ();
139
140                         case SpecialFolder.CommonApplicationData:
141                                 return "/usr/share";
142
143                         default:
144                                 throw new ArgumentException ("Invalid SpecialFolder");
145                         }
146                 }
147         }
148 }