michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: // vim:set ts=2 sts=2 sw=2 et cin: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "CocoaFileUtils.h" michael@0: #include "nsCocoaUtils.h" michael@0: #include michael@0: #include "nsObjCExceptions.h" michael@0: #include "nsDebug.h" michael@0: michael@0: namespace CocoaFileUtils { michael@0: michael@0: nsresult RevealFileInFinder(CFURLRef url) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: if (NS_WARN_IF(!url)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init]; michael@0: BOOL success = [[NSWorkspace sharedWorkspace] selectFile:[(NSURL*)url path] inFileViewerRootedAtPath:@""]; michael@0: [ap release]; michael@0: michael@0: return (success ? NS_OK : NS_ERROR_FAILURE); michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: nsresult OpenURL(CFURLRef url) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: if (NS_WARN_IF(!url)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init]; michael@0: BOOL success = [[NSWorkspace sharedWorkspace] openURL:(NSURL*)url]; michael@0: [ap release]; michael@0: michael@0: return (success ? NS_OK : NS_ERROR_FAILURE); michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: nsresult GetFileCreatorCode(CFURLRef url, OSType *creatorCode) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: if (NS_WARN_IF(!url) || NS_WARN_IF(!creatorCode)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: nsAutoreleasePool localPool; michael@0: michael@0: NSString *resolvedPath = [[(NSURL*)url path] stringByResolvingSymlinksInPath]; michael@0: if (!resolvedPath) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil]; michael@0: if (!dict) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NSNumber* creatorNum = (NSNumber*)[dict objectForKey:NSFileHFSCreatorCode]; michael@0: if (!creatorNum) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: *creatorCode = [creatorNum unsignedLongValue]; michael@0: return NS_OK; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: nsresult SetFileCreatorCode(CFURLRef url, OSType creatorCode) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: if (NS_WARN_IF(!url)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init]; michael@0: NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:creatorCode] forKey:NSFileHFSCreatorCode]; michael@0: BOOL success = [[NSFileManager defaultManager] setAttributes:dict ofItemAtPath:[(NSURL*)url path] error:nil]; michael@0: [ap release]; michael@0: return (success ? NS_OK : NS_ERROR_FAILURE); michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: nsresult GetFileTypeCode(CFURLRef url, OSType *typeCode) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: if (NS_WARN_IF(!url) || NS_WARN_IF(!typeCode)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: nsAutoreleasePool localPool; michael@0: michael@0: NSString *resolvedPath = [[(NSURL*)url path] stringByResolvingSymlinksInPath]; michael@0: if (!resolvedPath) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil]; michael@0: if (!dict) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NSNumber* typeNum = (NSNumber*)[dict objectForKey:NSFileHFSTypeCode]; michael@0: if (!typeNum) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: *typeCode = [typeNum unsignedLongValue]; michael@0: return NS_OK; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: nsresult SetFileTypeCode(CFURLRef url, OSType typeCode) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: if (NS_WARN_IF(!url)) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init]; michael@0: NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:typeCode] forKey:NSFileHFSTypeCode]; michael@0: BOOL success = [[NSFileManager defaultManager] setAttributes:dict ofItemAtPath:[(NSURL*)url path] error:nil]; michael@0: [ap release]; michael@0: return (success ? NS_OK : NS_ERROR_FAILURE); michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: } // namespace CocoaFileUtils