Cocoa: user and system base dirs retrieval implementation
authorDamien Plisson <damien.plisson@yahoo.fr>
Tue, 16 Feb 2010 08:36:33 +0000 (08:36 +0000)
committerDamien Plisson <damien.plisson@yahoo.fr>
Tue, 16 Feb 2010 08:36:33 +0000 (08:36 +0000)
intern/ghost/intern/GHOST_SystemCocoa.h
intern/ghost/intern/GHOST_SystemCocoa.mm

index c533e5510658c2ae026fd72d40d82490228ace50..2f1a94fc8eb9271925c5c38f6ec4cb99dff66588 100644 (file)
@@ -218,14 +218,14 @@ public:
         * "unpack and run" path, then look for properly installed path, not including versioning.
         * @return Unsigned char string pointing to system dir (eg /usr/share/blender/).
         */
-       virtual GHOST_TUns8* getSystemDir() const = 0;
+       virtual GHOST_TUns8* getSystemDir() const;
 
        /**
         * Determine the base dir in which user configuration is stored, not including versioning.
         * If needed, it will create the base directory.
         * @return Unsigned char string pointing to user dir (eg ~/.blender/).
         */
-       virtual GHOST_TUns8* getUserDir() const = 0;
+       virtual GHOST_TUns8* getUserDir() const;
 
        /**
      * Handles a window event. Called by GHOST_WindowCocoa window delegate
index 45438ec27c3dc74c68fc32897436f961a31b60af..0a5445b4811aa0572de5ef36fa069184eee9eb1f 100644 (file)
@@ -1737,12 +1737,60 @@ void GHOST_SystemCocoa::putClipboard(GHOST_TInt8 *buffer, bool selection) const
        [pool drain];
 }
 
+#pragma mark Base directories retrieval
+
 GHOST_TUns8* GHOST_SystemCocoa::getSystemDir() const
 {
-
+       static GHOST_TUns8 tempPath[512] = "";
+       NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+       NSFileManager *fileManager;
+       NSString *basePath;
+       NSArray *paths;
+       
+       paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSLocalDomainMask, YES);
+       
+       if ([paths count] > 0)
+               basePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Blender"];
+       else { //Fall back to standard unix path in case of issue
+               basePath = @"/usr/share/blender";
+       }
+       
+       /* Ensure path exists, creates it if needed */
+       fileManager = [NSFileManager defaultManager];
+       if (![fileManager fileExistsAtPath:basePath isDirectory:NULL]) {
+               [fileManager createDirectoryAtPath:basePath attributes:nil];
+       }
+       
+       strcpy((char*)tempPath, [basePath cStringUsingEncoding:NSASCIIStringEncoding]);
+       
+       [pool drain];
+       return tempPath;
 }
 
 GHOST_TUns8* GHOST_SystemCocoa::getUserDir() const
 {
+       static GHOST_TUns8 tempPath[512] = "";
+       NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+       NSFileManager *fileManager;
+       NSString *basePath;
+       NSArray *paths;
+
+       paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
 
+       if ([paths count] > 0)
+               basePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Blender"];
+       else { //Fall back to HOME in case of issue
+               basePath = [NSHomeDirectory() stringByAppendingPathComponent:@".blender"];
+       }
+       
+       /* Ensure path exists, creates it if needed */
+       fileManager = [NSFileManager defaultManager];
+       if (![fileManager fileExistsAtPath:basePath isDirectory:NULL]) {
+               [fileManager createDirectoryAtPath:basePath attributes:nil];
+       }
+       
+       strcpy((char*)tempPath, [basePath cStringUsingEncoding:NSASCIIStringEncoding]);
+       
+       [pool drain];
+       return tempPath;
 }