1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 // vim:set ts=2 sts=2 sw=2 et cin:
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "CocoaFileUtils.h"
8 #include "nsCocoaUtils.h"
9 #include <Cocoa/Cocoa.h>
10 #include "nsObjCExceptions.h"
13 namespace CocoaFileUtils {
15 nsresult RevealFileInFinder(CFURLRef url)
17 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
20 return NS_ERROR_INVALID_ARG;
22 NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
23 BOOL success = [[NSWorkspace sharedWorkspace] selectFile:[(NSURL*)url path] inFileViewerRootedAtPath:@""];
26 return (success ? NS_OK : NS_ERROR_FAILURE);
28 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
31 nsresult OpenURL(CFURLRef url)
33 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
36 return NS_ERROR_INVALID_ARG;
38 NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
39 BOOL success = [[NSWorkspace sharedWorkspace] openURL:(NSURL*)url];
42 return (success ? NS_OK : NS_ERROR_FAILURE);
44 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
47 nsresult GetFileCreatorCode(CFURLRef url, OSType *creatorCode)
49 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
51 if (NS_WARN_IF(!url) || NS_WARN_IF(!creatorCode))
52 return NS_ERROR_INVALID_ARG;
54 nsAutoreleasePool localPool;
56 NSString *resolvedPath = [[(NSURL*)url path] stringByResolvingSymlinksInPath];
58 return NS_ERROR_FAILURE;
61 NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil];
63 return NS_ERROR_FAILURE;
66 NSNumber* creatorNum = (NSNumber*)[dict objectForKey:NSFileHFSCreatorCode];
68 return NS_ERROR_FAILURE;
71 *creatorCode = [creatorNum unsignedLongValue];
74 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
77 nsresult SetFileCreatorCode(CFURLRef url, OSType creatorCode)
79 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
82 return NS_ERROR_INVALID_ARG;
84 NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
85 NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:creatorCode] forKey:NSFileHFSCreatorCode];
86 BOOL success = [[NSFileManager defaultManager] setAttributes:dict ofItemAtPath:[(NSURL*)url path] error:nil];
88 return (success ? NS_OK : NS_ERROR_FAILURE);
90 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
93 nsresult GetFileTypeCode(CFURLRef url, OSType *typeCode)
95 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
97 if (NS_WARN_IF(!url) || NS_WARN_IF(!typeCode))
98 return NS_ERROR_INVALID_ARG;
100 nsAutoreleasePool localPool;
102 NSString *resolvedPath = [[(NSURL*)url path] stringByResolvingSymlinksInPath];
104 return NS_ERROR_FAILURE;
107 NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil];
109 return NS_ERROR_FAILURE;
112 NSNumber* typeNum = (NSNumber*)[dict objectForKey:NSFileHFSTypeCode];
114 return NS_ERROR_FAILURE;
117 *typeCode = [typeNum unsignedLongValue];
120 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
123 nsresult SetFileTypeCode(CFURLRef url, OSType typeCode)
125 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
127 if (NS_WARN_IF(!url))
128 return NS_ERROR_INVALID_ARG;
130 NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
131 NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:typeCode] forKey:NSFileHFSTypeCode];
132 BOOL success = [[NSFileManager defaultManager] setAttributes:dict ofItemAtPath:[(NSURL*)url path] error:nil];
134 return (success ? NS_OK : NS_ERROR_FAILURE);
136 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
139 } // namespace CocoaFileUtils