[corlib] Add iOS specific code for System.Environment.
authorRolf Bjarne Kvinge <rolf@xamarin.com>
Tue, 15 Mar 2016 11:41:50 +0000 (12:41 +0100)
committerRodrigo Kumpera <kumpera@gmail.com>
Mon, 28 Mar 2016 22:51:33 +0000 (15:51 -0700)
List of original contributors:

Sebastien Pouliot <sebastien@xamarin.com>
Rolf Bjarne Kvinge <rolf@xamarin.com>

mcs/class/corlib/System/Environment.MonoTouch.opt.cs [deleted file]
mcs/class/corlib/System/Environment.iOS.cs [new file with mode: 0644]
mcs/class/corlib/monotouch_corlib.dll.sources
mcs/class/corlib/monotouch_opt_corlib.dll.sources
mcs/class/corlib/monotouch_runtime_corlib.dll.sources
mcs/class/corlib/monotouch_tv_corlib.dll.sources
mcs/class/corlib/monotouch_watch_corlib.dll.sources

diff --git a/mcs/class/corlib/System/Environment.MonoTouch.opt.cs b/mcs/class/corlib/System/Environment.MonoTouch.opt.cs
deleted file mode 100644 (file)
index d45de30..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-#if MONOTOUCH
-
-// this file is a shim to enable compiling monotouch profiles without mono-extensions
-namespace System
-{
-       public static partial class Environment
-       {
-               public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option)
-               {
-                       throw new NotSupportedException ();
-               }
-
-               internal static string UnixGetFolderPath (SpecialFolder folder, SpecialFolderOption option)
-               {
-                       throw new NotSupportedException ();
-               }
-       }
-}
-
-#endif
diff --git a/mcs/class/corlib/System/Environment.iOS.cs b/mcs/class/corlib/System/Environment.iOS.cs
new file mode 100644 (file)
index 0000000..04ad76d
--- /dev/null
@@ -0,0 +1,148 @@
+// Copyright 2014 Xamarin Inc.
+
+// note: or older hack to give the Documents (or Library) directories 
+
+using System.IO;
+using System.Runtime.InteropServices;
+
+namespace System {
+
+       public static partial class Environment {
+
+               static string ns_document;
+               static string ns_library;
+
+               [DllImport ("__Internal")]
+               extern static string xamarin_GetFolderPath (int folder);
+
+               static string NSDocumentDirectory {
+                       get {
+                               if (ns_document == null) {
+#if MONOTOUCH_TV
+                                       // The "normal" NSDocumentDirectory is a read-only directory on tvOS
+                                       // and that breaks a lot of assumptions in the runtime and the BCL
+                                       // to avoid this we relocate the Documents directory under Caches
+                                       ns_document = Path.Combine (NSLibraryDirectory, "Caches", "Documents");
+                                       if (!Directory.Exists (ns_document))
+                                               Directory.CreateDirectory (ns_document);
+#else
+                                       ns_document = xamarin_GetFolderPath (/* NSDocumentDirectory */ 9);
+#endif
+                               }
+                               return ns_document;
+                       }
+               }
+
+               // Various user-visible documentation, support, and configuration files
+               static string NSLibraryDirectory {
+                       get {
+                               if (ns_library == null)
+                                       ns_library = xamarin_GetFolderPath (/* NSLibraryDirectory */ 5);
+                               return ns_library;
+                       }
+               }
+
+               public static string GetFolderPath (SpecialFolder folder, SpecialFolderOption option)
+               {
+                       return UnixGetFolderPath (folder, option);
+               }
+
+               // needed by our BCL, e.g. IsolatedStorageFile.cs
+               internal static string UnixGetFolderPath (SpecialFolder folder, SpecialFolderOption option)
+               {
+                       var dir = iOSGetFolderPath (folder);
+                       if ((option == SpecialFolderOption.Create) && !Directory.Exists (dir))
+                               Directory.CreateDirectory (dir);
+                       return dir;
+               }
+
+               internal static string iOSGetFolderPath (SpecialFolder folder)
+               {
+                       switch (folder) {
+                       case SpecialFolder.MyComputer:
+                       case SpecialFolder.Programs:
+                       case SpecialFolder.SendTo:
+                       case SpecialFolder.StartMenu:
+                       case SpecialFolder.Startup:
+                       case SpecialFolder.Cookies:
+                       case SpecialFolder.History:
+                       case SpecialFolder.Recent:
+                       case SpecialFolder.CommonProgramFiles:
+                       case SpecialFolder.System:
+                       case SpecialFolder.NetworkShortcuts:
+                       case SpecialFolder.CommonStartMenu:
+                       case SpecialFolder.CommonPrograms:
+                       case SpecialFolder.CommonStartup:
+                       case SpecialFolder.CommonDesktopDirectory:
+                       case SpecialFolder.PrinterShortcuts:
+                       case SpecialFolder.Windows:
+                       case SpecialFolder.SystemX86:
+                       case SpecialFolder.ProgramFilesX86:
+                       case SpecialFolder.CommonProgramFilesX86:
+                       case SpecialFolder.CommonDocuments:
+                       case SpecialFolder.CommonAdminTools:
+                       case SpecialFolder.AdminTools:
+                       case SpecialFolder.CommonMusic:
+                       case SpecialFolder.CommonPictures:
+                       case SpecialFolder.CommonVideos:
+                       case SpecialFolder.LocalizedResources:
+                       case SpecialFolder.CommonOemLinks:
+                       case SpecialFolder.CDBurning:
+                               return String.Empty;
+                       
+                       // personal == ~
+                       case SpecialFolder.Personal:
+                       case SpecialFolder.LocalApplicationData:
+                               return NSDocumentDirectory;
+
+                       case SpecialFolder.ApplicationData:
+                               // note: at first glance that looked like a good place to return NSLibraryDirectory 
+                               // but it would break isolated storage for existing applications
+                               return Path.Combine (NSDocumentDirectory, ".config");
+
+                       case SpecialFolder.Resources:
+                               return NSLibraryDirectory; // older (8.2 and previous) would return String.Empty
+
+                       case SpecialFolder.Desktop:
+                       case SpecialFolder.DesktopDirectory:
+                               return Path.Combine (NSDocumentDirectory, "Desktop");
+
+                       case SpecialFolder.MyMusic:
+                               return Path.Combine (NSDocumentDirectory, "Music");
+
+                       case SpecialFolder.MyPictures:
+                               return Path.Combine (NSDocumentDirectory, "Pictures");
+
+                       case SpecialFolder.Templates:
+                               return Path.Combine (NSDocumentDirectory, "Templates");
+
+                       case SpecialFolder.MyVideos:
+                               return Path.Combine (NSDocumentDirectory, "Videos");
+
+                       case SpecialFolder.CommonTemplates:
+                               return "/usr/share/templates";
+
+                       case SpecialFolder.Fonts:
+                               return Path.Combine (NSDocumentDirectory, ".fonts");
+
+                       case SpecialFolder.Favorites:
+                               return Path.Combine (NSLibraryDirectory, "Favorites");
+
+                       case SpecialFolder.ProgramFiles:
+                               return "/Applications";
+
+                       case SpecialFolder.InternetCache:
+                               return Path.Combine (NSLibraryDirectory, "Caches");
+
+                       case SpecialFolder.UserProfile:
+                               return internalGetHome ();
+
+                       case SpecialFolder.CommonApplicationData:
+                               return "/usr/share";
+
+                       default:
+                               throw new ArgumentException ("Invalid SpecialFolder");
+                       }
+               }
+       }
+}
\ No newline at end of file
index 55b9d548f268ed10350a13e3debc791bb4c3c233..356dd7710bfc34b6eb178afdbb97648433378741 100644 (file)
@@ -18,3 +18,4 @@ CommonCrypto/SecRandom.cs
 CommonCrypto/RC4CommonCrypto.cs
 CommonCrypto/MD2Managed.g.cs
 CommonCrypto/MD4Managed.g.cs
+System/Environment.iOS.cs
index be231efa3a565123acdbe40d7f0cafd9e6b3cb5d..f4c6bf5322473a02b62fcddf7cddcb5b0b8fa3c9 100644 (file)
@@ -1,4 +1,3 @@
-System/Environment.MonoTouch.opt.cs
 System/Guid.MonoTouch.opt.cs
 System.Text/EncodingHelper.MonoTouch.opt.cs
 System.Security.Cryptography.X509Certificates/X509Helper.MonoTouch.opt.cs
index 55b9d548f268ed10350a13e3debc791bb4c3c233..356dd7710bfc34b6eb178afdbb97648433378741 100644 (file)
@@ -18,3 +18,4 @@ CommonCrypto/SecRandom.cs
 CommonCrypto/RC4CommonCrypto.cs
 CommonCrypto/MD2Managed.g.cs
 CommonCrypto/MD4Managed.g.cs
+System/Environment.iOS.cs
index 55b9d548f268ed10350a13e3debc791bb4c3c233..356dd7710bfc34b6eb178afdbb97648433378741 100644 (file)
@@ -18,3 +18,4 @@ CommonCrypto/SecRandom.cs
 CommonCrypto/RC4CommonCrypto.cs
 CommonCrypto/MD2Managed.g.cs
 CommonCrypto/MD4Managed.g.cs
+System/Environment.iOS.cs
index 55b9d548f268ed10350a13e3debc791bb4c3c233..356dd7710bfc34b6eb178afdbb97648433378741 100644 (file)
@@ -18,3 +18,4 @@ CommonCrypto/SecRandom.cs
 CommonCrypto/RC4CommonCrypto.cs
 CommonCrypto/MD2Managed.g.cs
 CommonCrypto/MD4Managed.g.cs
+System/Environment.iOS.cs