]> git.mxchange.org Git - simgear.git/commitdiff
Apple directory helpers
authorJames Turner <zakalawe@mac.com>
Tue, 28 Jan 2014 11:16:19 +0000 (11:16 +0000)
committerJames Turner <zakalawe@mac.com>
Tue, 28 Jan 2014 11:16:19 +0000 (11:16 +0000)
- drop use of deprecated FSFindFolder (use NSFileManager)
- support Downloads and Pictures directories

simgear/misc/CMakeLists.txt
simgear/misc/CocoaHelpers.mm [new file with mode: 0644]
simgear/misc/sg_path.cxx

index 89a2f6ade2beed9a2afcae5f2e9e24c9af86783c..16e74bbd6cddecba3953b34496beaa0582d398ab 100644 (file)
@@ -33,6 +33,10 @@ set(SOURCES
     gzcontainerfile.cxx
     )
 
+if (APPLE)
+    list(APPEND SOURCES CocoaHelpers.mm)
+endif()
+
 simgear_component(misc misc "${SOURCES}" "${HEADERS}")
 
 if(ENABLE_TESTS)
diff --git a/simgear/misc/CocoaHelpers.mm b/simgear/misc/CocoaHelpers.mm
new file mode 100644 (file)
index 0000000..028ccfb
--- /dev/null
@@ -0,0 +1,41 @@
+#include <Cocoa/Cocoa.h>
+#include <Foundation/NSAutoreleasePool.h>
+
+#include <simgear/misc/sg_path.hxx>
+
+namespace {
+    
+class CocoaAutoreleasePool
+{
+public:
+    CocoaAutoreleasePool()
+    {
+        pool = [[NSAutoreleasePool alloc] init];
+    }
+
+    ~CocoaAutoreleasePool()
+    {
+        [pool release];
+    }
+
+private:
+    NSAutoreleasePool* pool;
+};
+
+} // of anonyous namespace
+
+SGPath appleSpecialFolder(int dirType, int domainMask, const SGPath& def)
+{
+    CocoaAutoreleasePool ap;
+    NSFileManager* fm = [NSFileManager defaultManager];
+    NSURL* pathUrl = [fm URLForDirectory:dirType
+                                     inDomain:domainMask
+                             appropriateForURL:Nil
+                                       create:YES
+                                         error:nil];
+    if (!pathUrl) {
+        return def;;
+    }
+    
+    return SGPath([[pathUrl path] UTF8String], def.getPermissionChecker());
+}
\ No newline at end of file
index befb41a2e6cad487b0a33207d0464eaa7b1c02b7..3c6e5b6b6693b8ebbc3d3e75bc21c7e3f99126ba 100644 (file)
@@ -81,22 +81,10 @@ static SGPath pathForCSIDL(int csidl, const SGPath& def)
        return def;
 }
 #elif __APPLE__
-#include <CoreServices/CoreServices.h>
 
-//------------------------------------------------------------------------------
-static SGPath appleSpecialFolder(OSType type, const SGPath& def)
-{
-  FSRef ref;
-  OSErr err = FSFindFolder(kUserDomain, kDesktopFolderType, false, &ref);
-  if( err )
-    return def;
-
-  unsigned char path[1024];
-  if( FSRefMakePath(&ref, path, 1024) != noErr )
-    return def;
+// defined in CocoaHelpers.mm
+SGPath appleSpecialFolder(int dirType, int domainMask, const SGPath& def);
 
-  return SGPath((const char*) path, def.getPermissionChecker());
-}
 #else
 static SGPath getXDGDir( const std::string& name,
                          const SGPath& def,
@@ -728,16 +716,16 @@ SGPath SGPath::standardLocation(StandardLocation type, const SGPath& def)
     case PICTURES:
       return pathForCSIDL(CSIDL_MYPICTURES, def);
 #elif __APPLE__
+      // since this is C++, we can't include NSPathUtilities.h to access the enum
+      // values, so hard-coding them here (they are stable, don't worry)
     case DOWNLOADS:
-      if( !def.isNull() )
-        return def;
-      // There is no special downloads folder -> just use the desktop
+      return appleSpecialFolder(15, 1, def);
     case DESKTOP:
-      return appleSpecialFolder(kDesktopFolderType, def);
+      return appleSpecialFolder(12, 1, def);
     case DOCUMENTS:
-      return appleSpecialFolder(kDocumentsFolderType, def);
+      return appleSpecialFolder(9, 1, def);
     case PICTURES:
-      return appleSpecialFolder(kPictureDocumentsFolderType, def);
+      return appleSpecialFolder(19, 1, def);
 #else
     case DESKTOP:
       return getXDGDir("DESKTOP", def, "Desktop");