Bumping manifests a=b2g-bump
[gecko.git] / xpcom / io / CocoaFileUtils.mm
blob3f6872745536ae7c39dc3e95b6ed28c057c04331
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"
11 #include "nsDebug.h"
13 namespace CocoaFileUtils {
15 nsresult RevealFileInFinder(CFURLRef url)
17   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
19   if (NS_WARN_IF(!url))
20     return NS_ERROR_INVALID_ARG;
22   NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
23   BOOL success = [[NSWorkspace sharedWorkspace] selectFile:[(NSURL*)url path] inFileViewerRootedAtPath:@""];
24   [ap release];
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;
35   if (NS_WARN_IF(!url))
36     return NS_ERROR_INVALID_ARG;
38   NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
39   BOOL success = [[NSWorkspace sharedWorkspace] openURL:(NSURL*)url];
40   [ap release];
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];
57   if (!resolvedPath) {
58     return NS_ERROR_FAILURE;
59   }
61   NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil];
62   if (!dict) {
63     return NS_ERROR_FAILURE;
64   }
66   NSNumber* creatorNum = (NSNumber*)[dict objectForKey:NSFileHFSCreatorCode];
67   if (!creatorNum) {
68     return NS_ERROR_FAILURE;
69   }
71   *creatorCode = [creatorNum unsignedLongValue];
72   return NS_OK;
74   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
77 nsresult SetFileCreatorCode(CFURLRef url, OSType creatorCode)
79   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
81   if (NS_WARN_IF(!url))
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];
87   [ap release];
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];
103   if (!resolvedPath) {
104     return NS_ERROR_FAILURE;
105   }
107   NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil];
108   if (!dict) {
109     return NS_ERROR_FAILURE;
110   }
112   NSNumber* typeNum = (NSNumber*)[dict objectForKey:NSFileHFSTypeCode];
113   if (!typeNum) {
114     return NS_ERROR_FAILURE;
115   }
117   *typeCode = [typeNum unsignedLongValue];
118   return NS_OK;
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];
133   [ap release];
134   return (success ? NS_OK : NS_ERROR_FAILURE);
136   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
139 } // namespace CocoaFileUtils