|
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/. */ |
|
6 |
|
7 #include "CocoaFileUtils.h" |
|
8 #include "nsCocoaUtils.h" |
|
9 #include <Cocoa/Cocoa.h> |
|
10 #include "nsObjCExceptions.h" |
|
11 #include "nsDebug.h" |
|
12 |
|
13 namespace CocoaFileUtils { |
|
14 |
|
15 nsresult RevealFileInFinder(CFURLRef url) |
|
16 { |
|
17 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
18 |
|
19 if (NS_WARN_IF(!url)) |
|
20 return NS_ERROR_INVALID_ARG; |
|
21 |
|
22 NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init]; |
|
23 BOOL success = [[NSWorkspace sharedWorkspace] selectFile:[(NSURL*)url path] inFileViewerRootedAtPath:@""]; |
|
24 [ap release]; |
|
25 |
|
26 return (success ? NS_OK : NS_ERROR_FAILURE); |
|
27 |
|
28 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
29 } |
|
30 |
|
31 nsresult OpenURL(CFURLRef url) |
|
32 { |
|
33 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
34 |
|
35 if (NS_WARN_IF(!url)) |
|
36 return NS_ERROR_INVALID_ARG; |
|
37 |
|
38 NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init]; |
|
39 BOOL success = [[NSWorkspace sharedWorkspace] openURL:(NSURL*)url]; |
|
40 [ap release]; |
|
41 |
|
42 return (success ? NS_OK : NS_ERROR_FAILURE); |
|
43 |
|
44 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
45 } |
|
46 |
|
47 nsresult GetFileCreatorCode(CFURLRef url, OSType *creatorCode) |
|
48 { |
|
49 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
50 |
|
51 if (NS_WARN_IF(!url) || NS_WARN_IF(!creatorCode)) |
|
52 return NS_ERROR_INVALID_ARG; |
|
53 |
|
54 nsAutoreleasePool localPool; |
|
55 |
|
56 NSString *resolvedPath = [[(NSURL*)url path] stringByResolvingSymlinksInPath]; |
|
57 if (!resolvedPath) { |
|
58 return NS_ERROR_FAILURE; |
|
59 } |
|
60 |
|
61 NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil]; |
|
62 if (!dict) { |
|
63 return NS_ERROR_FAILURE; |
|
64 } |
|
65 |
|
66 NSNumber* creatorNum = (NSNumber*)[dict objectForKey:NSFileHFSCreatorCode]; |
|
67 if (!creatorNum) { |
|
68 return NS_ERROR_FAILURE; |
|
69 } |
|
70 |
|
71 *creatorCode = [creatorNum unsignedLongValue]; |
|
72 return NS_OK; |
|
73 |
|
74 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
75 } |
|
76 |
|
77 nsresult SetFileCreatorCode(CFURLRef url, OSType creatorCode) |
|
78 { |
|
79 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
80 |
|
81 if (NS_WARN_IF(!url)) |
|
82 return NS_ERROR_INVALID_ARG; |
|
83 |
|
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); |
|
89 |
|
90 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
91 } |
|
92 |
|
93 nsresult GetFileTypeCode(CFURLRef url, OSType *typeCode) |
|
94 { |
|
95 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
96 |
|
97 if (NS_WARN_IF(!url) || NS_WARN_IF(!typeCode)) |
|
98 return NS_ERROR_INVALID_ARG; |
|
99 |
|
100 nsAutoreleasePool localPool; |
|
101 |
|
102 NSString *resolvedPath = [[(NSURL*)url path] stringByResolvingSymlinksInPath]; |
|
103 if (!resolvedPath) { |
|
104 return NS_ERROR_FAILURE; |
|
105 } |
|
106 |
|
107 NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil]; |
|
108 if (!dict) { |
|
109 return NS_ERROR_FAILURE; |
|
110 } |
|
111 |
|
112 NSNumber* typeNum = (NSNumber*)[dict objectForKey:NSFileHFSTypeCode]; |
|
113 if (!typeNum) { |
|
114 return NS_ERROR_FAILURE; |
|
115 } |
|
116 |
|
117 *typeCode = [typeNum unsignedLongValue]; |
|
118 return NS_OK; |
|
119 |
|
120 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
121 } |
|
122 |
|
123 nsresult SetFileTypeCode(CFURLRef url, OSType typeCode) |
|
124 { |
|
125 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; |
|
126 |
|
127 if (NS_WARN_IF(!url)) |
|
128 return NS_ERROR_INVALID_ARG; |
|
129 |
|
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); |
|
135 |
|
136 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; |
|
137 } |
|
138 |
|
139 } // namespace CocoaFileUtils |