[Mac] screen shots directory can now be moved around (on the same volume). If it is renamed, Oolite deliberately ignores it and creates a new one on the desktop. The dock tile plug-in has not yet been updated.

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@3672 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2010-07-15 13:15:12 +00:00
parent 61bbc84257
commit 6d4a55504a
9 changed files with 354 additions and 40 deletions

View File

@ -76,6 +76,8 @@
1A11C2B211CFC35000F3EE77 /* OOJSEngineTimeManagement.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A11C2B011CFC35000F3EE77 /* OOJSEngineTimeManagement.m */; };
1A11F84A0F35F60C001C886C /* OOJSShipGroup.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A11F8480F35F60C001C886C /* OOJSShipGroup.m */; };
1A11F84B0F35F60C001C886C /* OOJSShipGroup.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A11F8490F35F60C001C886C /* OOJSShipGroup.h */; };
1A143A4811EF22C5001BAB8D /* JAPersistentFileReference.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A143A4611EF22C5001BAB8D /* JAPersistentFileReference.h */; };
1A143A4911EF22C5001BAB8D /* JAPersistentFileReference.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A143A4711EF22C5001BAB8D /* JAPersistentFileReference.m */; };
1A1502F60C1201C30032F3E8 /* oolite-unknown-ship.dat in Copy Models */ = {isa = PBXBuildFile; fileRef = 1A1502F50C1201C30032F3E8 /* oolite-unknown-ship.dat */; };
1A15049E0C12CA070032F3E8 /* OOProbabilisticTextureManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A15049C0C12CA070032F3E8 /* OOProbabilisticTextureManager.h */; };
1A15049F0C12CA070032F3E8 /* OOProbabilisticTextureManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A15049D0C12CA070032F3E8 /* OOProbabilisticTextureManager.m */; };
@ -1222,6 +1224,8 @@
1A11C2B011CFC35000F3EE77 /* OOJSEngineTimeManagement.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOJSEngineTimeManagement.m; sourceTree = "<group>"; };
1A11F8480F35F60C001C886C /* OOJSShipGroup.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOJSShipGroup.m; sourceTree = "<group>"; };
1A11F8490F35F60C001C886C /* OOJSShipGroup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOJSShipGroup.h; sourceTree = "<group>"; };
1A143A4611EF22C5001BAB8D /* JAPersistentFileReference.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JAPersistentFileReference.h; sourceTree = "<group>"; };
1A143A4711EF22C5001BAB8D /* JAPersistentFileReference.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JAPersistentFileReference.m; sourceTree = "<group>"; };
1A1502F50C1201C30032F3E8 /* oolite-unknown-ship.dat */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "oolite-unknown-ship.dat"; sourceTree = "<group>"; };
1A1504490C12C50D0032F3E8 /* OOSkyDrawable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOSkyDrawable.h; sourceTree = "<group>"; };
1A15044A0C12C50D0032F3E8 /* OOSkyDrawable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOSkyDrawable.m; sourceTree = "<group>"; };
@ -2735,6 +2739,8 @@
1A1E99B50EF04837008B48E2 /* OOProgressBar.m */,
1AAF56160F1A198400A2F2E6 /* Comparison.h */,
1A5A798D105044DD00A8F4B5 /* OOPrefixHeader.h */,
1A143A4611EF22C5001BAB8D /* JAPersistentFileReference.h */,
1A143A4711EF22C5001BAB8D /* JAPersistentFileReference.m */,
);
name = "Mac-specific";
path = ../Cocoa;
@ -3350,6 +3356,7 @@
1A062C8911B28D8A00727C1D /* NSObjectOOExtensions.h in Headers */,
1AABA83E11B941D1003487D5 /* OOPixMapTextureLoader.h in Headers */,
1A11C2B111CFC35000F3EE77 /* OOJSEngineTimeManagement.h in Headers */,
1A143A4811EF22C5001BAB8D /* JAPersistentFileReference.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -3731,6 +3738,7 @@
1A062C8A11B28D8A00727C1D /* NSObjectOOExtensions.m in Sources */,
1AABA83F11B941D1003487D5 /* OOPixMapTextureLoader.m in Sources */,
1A11C2B211CFC35000F3EE77 /* OOJSEngineTimeManagement.m in Sources */,
1A143A4911EF22C5001BAB8D /* JAPersistentFileReference.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -1196,6 +1196,10 @@
"gameoverscreen-game-over" = "Game Over";
"gameoverscreen-press-space" = "Press Space";
// Screen shots
"snapshots-directory-name" = "snapshots";
"snapshots-directory-name-mac" = "Oolite Screen Shots";
// Various HUD dials and/or messages and miscellaneous strings
"no-target-string" = "No target";
"communications-log-string" = "Communications Log";

View File

@ -0,0 +1,26 @@
//
// JAPersistentFileReference.h
// Oolite
//
// Created by Jens Ayton on 2010-07-15.
// Copyright 2010 the Oolite team. All rights reserved.
//
#import <Foundation/Foundation.h>
enum
{
kJAPersistentFileReferenceWithoutUI = 0x00000001UL, // Avoid user interaction.
kJAPersistentFileReferenceWithoutMounting = 0x00000002UL, // Avoid mounting volumes.
kJAPersistentFileReferenceReturnReferenceURL = 0x00000004UL // Return a file reference URL if possible.
};
typedef uint32_t JAPersistentFileReferenceResolveFlags;
NSDictionary *JAPersistentFileReferenceFromURL(NSURL *url);
NSURL *JAURLFromPersistentFileReference(NSDictionary *fileRef, JAPersistentFileReferenceResolveFlags flags, BOOL *isStale);
NSDictionary *JAPersistentFileReferenceFromPath(NSString *path);
NSString *JAPathFromPersistentFileReference(NSDictionary *fileRef, JAPersistentFileReferenceResolveFlags flags, BOOL *isStale);

View File

@ -0,0 +1,211 @@
//
// JAPersistentFileReference.m
// Oolite
//
// Created by Jens Ayton on 2010-07-15.
// Copyright 2010 the Oolite team. All rights reserved.
//
#import "JAPersistentFileReference.h"
#import <CoreServices/CoreServices.h>
#define kURLKey @"url"
#define kAliasKey @"alias"
#define kBookmarkKey @"bookmark"
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
#define BookmarkDataSupported() (YES)
#else
#define BookmarkDataSupported() ([NSURL instancesRespondToSelector:@selector(bookmarkDataWithOptions:includingResourceValuesForKeys:relativeToURL:error:)])
@interface NSURL (SnowLeopardMethods)
- (NSData *)bookmarkDataWithOptions:(unsigned long)options includingResourceValuesForKeys:(NSArray *)keys relativeToURL:(NSURL *)relativeURL error:(NSError **)error;
- (NSURL *)fileReferenceURL;
- (NSURL *)filePathURL;
- (BOOL)isFileReferenceURL;
+ (id)URLByResolvingBookmarkData:(NSData *)bookmarkData options:(unsigned long)options relativeToURL:(NSURL *)relativeURL bookmarkDataIsStale:(BOOL *)isStale error:(NSError **)error;
@end
enum
{
NSURLBookmarkResolutionWithoutUI = ( 1UL << 8 ),
NSURLBookmarkResolutionWithoutMounting = ( 1UL << 9 ),
};
#endif
NSDictionary *JAPersistentFileReferenceFromURL(NSURL *url)
{
if (url == nil) return nil;
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:3];
[result setObject:[url absoluteString] forKey:kURLKey];
if ([url isFileURL])
{
FSRef fsRef;
if (CFURLGetFSRef((CFURLRef)[url absoluteURL], &fsRef))
{
AliasHandle alias = NULL;
if (FSNewAlias(NULL, &fsRef, &alias) == noErr)
{
NSData *aliasData = [NSData dataWithBytes:*alias length:GetAliasSize(alias)];
if (aliasData != NULL)
{
[result setObject:aliasData forKey:kAliasKey];
}
}
}
}
if (BookmarkDataSupported())
{
NSURL *refURL = [url fileReferenceURL];
if (refURL != nil)
{
NSData *bookmarkData = [refURL bookmarkDataWithOptions:0
includingResourceValuesForKeys:nil
relativeToURL:nil
error:NULL];
if (bookmarkData != nil)
{
[result setObject:bookmarkData forKey:kBookmarkKey];
}
}
}
return result;
}
static inline unsigned long BookmarkOptionsFromFlags(JAPersistentFileReferenceResolveFlags flags)
{
unsigned long result = 0;
if (flags & kJAPersistentFileReferenceWithoutUI) result |= NSURLBookmarkResolutionWithoutUI;
if (flags & NSURLBookmarkResolutionWithoutMounting) result |= NSURLBookmarkResolutionWithoutMounting;
return result;
}
static inline unsigned long AliasMountFlagsFromFlags(JAPersistentFileReferenceResolveFlags flags)
{
unsigned long result = 0;
if (flags & kJAPersistentFileReferenceWithoutUI) result |= kResolveAliasFileNoUI;
return result;
}
NSURL *JAURLFromPersistentFileReference(NSDictionary *fileRef, JAPersistentFileReferenceResolveFlags flags, BOOL *isStale)
{
NSURL *result = nil;
BOOL stale = NO, staleIfFile = NO;
// Try bookmark.
if (BookmarkDataSupported())
{
NSData *bookmarkData = [fileRef objectForKey:kBookmarkKey];
if ([bookmarkData isKindOfClass:[NSData class]])
{
result = [NSURL URLByResolvingBookmarkData:bookmarkData
options:BookmarkOptionsFromFlags(flags)
relativeToURL:nil
bookmarkDataIsStale:&stale
error:NULL];
}
else staleIfFile = YES;
}
// Try alias.
if (result == nil)
{
NSData *aliasData = [fileRef objectForKey:kAliasKey];
if ([aliasData isKindOfClass:[NSData class]])
{
size_t size = [aliasData length];
AliasHandle alias = (AliasHandle)NewHandle(size);
if (alias != NULL)
{
memcpy(*alias, [aliasData bytes], size);
FSRef fsRef;
Boolean carbonStale;
if (FSResolveAliasWithMountFlags(NULL, alias, &fsRef, &carbonStale, AliasMountFlagsFromFlags(flags)) == noErr)
{
stale = carbonStale;
result = (NSURL *)CFURLCreateFromFSRef(kCFAllocatorDefault, &fsRef);
[result autorelease];
}
}
}
else staleIfFile = YES;
}
// Try URL.
if (result == nil)
{
NSString *urlString = [fileRef objectForKey:kURLKey];
if ([urlString isKindOfClass:[NSString class]])
{
result = [NSURL URLWithString:urlString relativeToURL:nil];
if ([result isFileURL] && ![[NSFileManager defaultManager] fileExistsAtPath:[result path]])
{
result = nil;
}
}
}
// If we got nothing, it's definitely stale.
if (result == nil)
{
stale = YES;
}
else
{
if ([result isFileURL] && staleIfFile) stale = YES;
// Convert to/from file reference URL as appropriate.
if (BookmarkDataSupported())
{
if (flags & kJAPersistentFileReferenceReturnReferenceURL)
{
if (![result isFileReferenceURL] && [result isFileURL])
{
NSURL *refURL = [result fileReferenceURL];
if (refURL != nil) result = refURL;
}
}
else
{
if ([result isFileReferenceURL])
{
NSURL *pathURL = [result filePathURL];
if (pathURL != nil) result = pathURL;
}
}
}
}
if (isStale != NULL) *isStale = stale;
return result;
}
NSDictionary *JAPersistentFileReferenceFromPath(NSString *path)
{
return JAPersistentFileReferenceFromURL([NSURL fileURLWithPath:path]);
}
NSString *JAPathFromPersistentFileReference(NSDictionary *fileRef, JAPersistentFileReferenceResolveFlags flags, BOOL *isStale)
{
NSURL *url = JAURLFromPersistentFileReference(fileRef, flags & ~kJAPersistentFileReferenceReturnReferenceURL, isStale);
if ([url isFileURL]) return [url path];
return nil;
}

View File

@ -313,9 +313,15 @@ static NSString * kOOLogKeyDown = @"input.keyMapping.keyPress.keyDown";
unsigned char *blue = (unsigned char *) malloc( nPixels);
// backup the previous directory
NSString* originalDirectory = [[NSFileManager defaultManager] currentDirectoryPath];
NSString *originalDirectory = [[NSFileManager defaultManager] currentDirectoryPath];
// use the snapshots directory
[[NSFileManager defaultManager] chdirToSnapshotPath];
NSString *snapshotsDirectory = [[[GameController sharedController] snapshotsURLCreatingIfNeeded:YES] path];
if (![[NSFileManager defaultManager] changeCurrentDirectoryPath:snapshotsDirectory])
{
NSBeep();
OOLog(@"savedSnapshot.defaultPath.chdir.failed", @"Could not navigate to %@", snapshotsDirectory);
return;
}
static unsigned imageNo = 0;
NSString *pathToPic = nil;

View File

@ -170,4 +170,6 @@ MA 02110-1301, USA.
- (void)setUpBasicOpenGLStateWithSize:(NSSize)viewSize;
- (NSURL *) snapshotsURLCreatingIfNeeded:(BOOL)create;
@end

View File

@ -39,6 +39,10 @@ MA 02110-1301, USA.
#define kOOLogUnconvertedNSLog @"unclassified.GameController"
#if OOLITE_MAC_OS_X
#import "JAPersistentFileReference.h"
#endif
static GameController *sSharedController = nil;
@ -789,9 +793,81 @@ static NSComparisonResult CompareDisplayModes(id arg1, id arg2, void *context)
}
- (NSURL *) snapshotsURLCreatingIfNeeded:(BOOL)create
{
BOOL stale = NO;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSDictionary *snapshotDirDict = [prefs dictionaryForKey:@"snapshots-directory-reference"];
NSURL *url = nil;
NSString *name = DESC(@"snapshots-directory-name-mac");
if (snapshotDirDict != nil)
{
url = JAURLFromPersistentFileReference(snapshotDirDict, kJAPersistentFileReferenceWithoutUI | kJAPersistentFileReferenceWithoutMounting, &stale);
if (url != nil)
{
NSString *existingName = [[url path] lastPathComponent];
if ([existingName compare:name options:NSCaseInsensitiveSearch] != 0)
{
// Check name from previous access, because we might have changed localizations.
NSString *originalOldName = [prefs stringForKey:@"snapshots-directory-name"];
if ([existingName compare:originalOldName options:NSCaseInsensitiveSearch] != 0)
{
url = nil;
}
}
}
}
if (url == nil)
{
NSString *path = nil;
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
if ([searchPaths count] > 0)
{
path = [[searchPaths objectAtIndex:0] stringByAppendingPathComponent:name];
}
url = [NSURL fileURLWithPath:path];
if (url != nil)
{
stale = YES;
if (create)
{
NSFileManager *fmgr = [NSFileManager defaultManager];
if (![fmgr fileExistsAtPath:path])
{
#if OOLITE_LEOPARD
[fmgr createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
#else
[fmgr createDirectoryAtPath:path attributes:nil];
#endif
}
}
}
}
if (stale)
{
snapshotDirDict = JAPersistentFileReferenceFromURL(url);
if (snapshotDirDict != nil)
{
[prefs setObject:snapshotDirDict forKey:@"snapshots-directory-reference"];
[prefs setObject:[[url path] lastPathComponent] forKey:@"snapshots-directory-name"];
}
else
{
[prefs removeObjectForKey:@"snapshots-directory-reference"];
}
}
return url;
}
- (IBAction) showSnapshotsAction:sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@SNAPSHOTDIR]]]; // also in myOpenGLView snapShot
[[NSWorkspace sharedWorkspace] openURL:[self snapshotsURLCreatingIfNeeded:YES]];
}
@ -847,7 +923,7 @@ static NSComparisonResult CompareDisplayModes(id arg1, id arg2, void *context)
if (action == @selector(showSnapshotsAction:))
{
BOOL pathIsDirectory;
if(![[NSFileManager defaultManager] fileExistsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@SNAPSHOTDIR] isDirectory:&pathIsDirectory]) return NO;
if(![[NSFileManager defaultManager] fileExistsAtPath:[[self snapshotsURLCreatingIfNeeded:NO] path] isDirectory:&pathIsDirectory]) return NO;
return pathIsDirectory;
}
@ -909,6 +985,22 @@ static NSComparisonResult CompareDisplayModes(id arg1, id arg2, void *context)
return [gameView inFullScreenMode];
}
- (NSURL *) snapshotsURLCreatingIfNeeded:(BOOL)create
{
NSURL *url = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:DESC(@"snapshots-directory-name")]];
if (create)
{
NSString *path = [url path];
NSFileManager *fmgr = [NSFileManager defaultManager];
if (![fmgr fileExistsAtPath:path])
{
[fmgr createDirectoryAtPath:path attributes:nil];
}
}
}
#else
#error Unknown environment!
#endif

View File

@ -30,18 +30,10 @@ MA 02110-1301, USA.
#define SAVEDIR "oolite-saves"
#if OOLITE_MAC_OS_X
#define SNAPSHOTDIR "Desktop/Oolite Screen Shots"
#else
#define SNAPSHOTDIR "snapshots"
#endif
@interface NSFileManager (OOExtensions)
- (NSArray*) commanderContentsOfPath:(NSString*) savePath;
- (NSString*) defaultCommanderPath;
- (BOOL)chdirToSnapshotPath;
@end

View File

@ -30,6 +30,7 @@ MA 02110-1301, USA.
#import "NSFileManagerOOExtensions.h"
#import "ResourceManager.h"
#import "OOPListParsing.h"
#import "GameController.h"
#define kOOLogUnconvertedNSLog @"unclassified.NSFileManagerOOExtensions"
@ -115,34 +116,6 @@ MA 02110-1301, USA.
return savedir;
}
- (BOOL) chdirToSnapshotPath
{
#if OOLITE_MAC_OS_X
// Macs: the default path for snapshots is ~/Desktop/Oolite Snap Shots
NSString *savedir = [NSHomeDirectory() stringByAppendingPathComponent:@SNAPSHOTDIR];
#else
// SDL: the default path for snapshots is oolite.app/oolite-saves/snapshots
NSString *savedir = [[NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR] stringByAppendingPathComponent:@SNAPSHOTDIR];
#endif
if (![self changeCurrentDirectoryPath: savedir])
{
// it probably doesn't exist.
if (![self createDirectoryAtPath: savedir attributes: nil])
{
OOLog(@"savedSnapshot.defaultPath.create.failed", @"Unable to create directory %@", savedir);
return NO;
}
if (![self changeCurrentDirectoryPath: savedir])
{
OOLog(@"savedSnapshot.defaultPath.chdir.failed", @"Created %@ but couldn't make it the current directory.", savedir);
return NO;
}
}
return YES;
}
@end