1 /* |
|
2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 #import "com_apple_eio_FileManager.h" |
|
27 |
|
28 #import <Cocoa/Cocoa.h> |
|
29 #import <JavaNativeFoundation/JavaNativeFoundation.h> |
|
30 |
|
31 #import "ThreadUtilities.h" |
|
32 |
|
33 |
|
34 /* |
|
35 * Class: com_apple_eio_FileManager |
|
36 * Method: _setFileTypeAndCreator |
|
37 * Signature: (Ljava/lang/String;II)V |
|
38 */ |
|
39 JNIEXPORT void JNICALL Java_com_apple_eio_FileManager__1setFileTypeAndCreator |
|
40 (JNIEnv *env, jclass clz, jstring javaFilename, jint type, jint creator) |
|
41 { |
|
42 JNF_COCOA_ENTER(env); |
|
43 NSString *filename = JNFNormalizedNSStringForPath(env, javaFilename); |
|
44 NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys: |
|
45 [NSNumber numberWithInt:type], NSFileHFSTypeCode, |
|
46 [NSNumber numberWithInt:creator], NSFileHFSCreatorCode, nil]; |
|
47 [[NSFileManager defaultManager] changeFileAttributes:attr atPath:filename]; |
|
48 JNF_COCOA_EXIT(env); |
|
49 } |
|
50 |
|
51 /* |
|
52 * Class: com_apple_eio_FileManager |
|
53 * Method: _setFileType |
|
54 * Signature: (Ljava/lang/String;I)V |
|
55 */ |
|
56 JNIEXPORT void JNICALL Java_com_apple_eio_FileManager__1setFileType |
|
57 (JNIEnv *env, jclass ckz, jstring javaFilename, jint type) |
|
58 { |
|
59 JNF_COCOA_ENTER(env); |
|
60 NSString *filename = JNFNormalizedNSStringForPath(env, javaFilename); |
|
61 NSDictionary *attr = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:type] forKey:NSFileHFSTypeCode]; |
|
62 [[NSFileManager defaultManager] changeFileAttributes:attr atPath:filename]; |
|
63 JNF_COCOA_EXIT(env); |
|
64 } |
|
65 |
|
66 /* |
|
67 * Class: com_apple_eio_FileManager |
|
68 * Method: _setFileCreator |
|
69 * Signature: (Ljava/lang/String;I)V |
|
70 */ |
|
71 JNIEXPORT void JNICALL Java_com_apple_eio_FileManager__1setFileCreator |
|
72 (JNIEnv *env, jclass clz, jstring javaFilename, jint creator) |
|
73 { |
|
74 JNF_COCOA_ENTER(env); |
|
75 NSString *filename = JNFNormalizedNSStringForPath(env, javaFilename); |
|
76 NSDictionary *attr = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:creator] forKey:NSFileHFSCreatorCode]; |
|
77 [[NSFileManager defaultManager] changeFileAttributes:attr atPath:filename]; |
|
78 JNF_COCOA_EXIT(env); |
|
79 } |
|
80 |
|
81 /* |
|
82 * Class: com_apple_eio_FileManager |
|
83 * Method: _getFileType |
|
84 * Signature: (Ljava/lang/String;)I |
|
85 */ |
|
86 JNIEXPORT jint JNICALL Java_com_apple_eio_FileManager__1getFileType |
|
87 (JNIEnv *env, jclass clz, jstring javaFilename) |
|
88 { |
|
89 jint type = 0; |
|
90 JNF_COCOA_ENTER(env); |
|
91 NSString *filename = JNFNormalizedNSStringForPath(env, javaFilename); |
|
92 NSDictionary *attributes = [[NSFileManager defaultManager] fileAttributesAtPath:filename traverseLink:YES]; |
|
93 NSNumber *val = [attributes objectForKey:NSFileHFSTypeCode]; |
|
94 type = [val intValue]; |
|
95 JNF_COCOA_EXIT(env); |
|
96 return type; |
|
97 } |
|
98 |
|
99 /* |
|
100 * Class: com_apple_eio_FileManager |
|
101 * Method: _getFileCreator |
|
102 * Signature: (Ljava/lang/String;)I |
|
103 */ |
|
104 JNIEXPORT jint JNICALL Java_com_apple_eio_FileManager__1getFileCreator |
|
105 (JNIEnv *env, jclass clz, jstring javaFilename) |
|
106 { |
|
107 jint creator = 0; |
|
108 JNF_COCOA_ENTER(env); |
|
109 NSString *filename = JNFNormalizedNSStringForPath(env, javaFilename); |
|
110 NSDictionary *attributes = [[NSFileManager defaultManager] fileAttributesAtPath:filename traverseLink:YES]; |
|
111 NSNumber *val = [attributes objectForKey:NSFileHFSCreatorCode]; |
|
112 creator = [val intValue]; |
|
113 JNF_COCOA_EXIT(env); |
|
114 return creator; |
|
115 } |
|
116 |
|
117 /* |
|
118 * Class: com_apple_eio_FileManager |
|
119 * Method: _findFolder |
|
120 * Signature: (SIZ)Ljava/lang/String; |
|
121 */ |
|
122 JNIEXPORT jstring JNICALL Java_com_apple_eio_FileManager__1findFolder__SIZ |
|
123 (JNIEnv *env, jclass clz, jshort domain, jint folderType, jboolean createIfNeeded) |
|
124 { |
|
125 jstring filename = nil; |
|
126 JNF_COCOA_ENTER(env); |
|
127 |
|
128 FSRef foundRef; |
|
129 createIfNeeded = createIfNeeded || (folderType == kTemporaryFolderType) || (folderType == kChewableItemsFolderType); |
|
130 if (FSFindFolder((SInt16)domain, (OSType)folderType, (Boolean)createIfNeeded, &foundRef) == noErr) { |
|
131 char path[PATH_MAX]; |
|
132 if (FSRefMakePath(&foundRef, (UInt8 *)path, sizeof(path)) == noErr) { |
|
133 NSString *filenameString = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:path length:strlen(path)]; |
|
134 filename = JNFNormalizedJavaStringForPath(env, filenameString); |
|
135 } |
|
136 } |
|
137 |
|
138 JNF_COCOA_EXIT(env); |
|
139 return filename; |
|
140 } |
|
141 |
|
142 |
|
143 /* |
|
144 * Class: com_apple_eio_FileManager |
|
145 * Method: _openURL |
|
146 * Signature: (Ljava/lang/String;)V |
|
147 */ |
|
148 JNIEXPORT void JNICALL Java_com_apple_eio_FileManager__1openURL |
|
149 (JNIEnv *env, jclass clz, jstring urlString) |
|
150 { |
|
151 JNF_COCOA_ENTER(env); |
|
152 |
|
153 NSURL *url = [NSURL URLWithString:JNFNormalizedNSStringForPath(env, urlString)]; |
|
154 |
|
155 // Radar 3208005: Run this on the main thread; file:// style URLs will hang otherwise. |
|
156 [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){ |
|
157 [[NSWorkspace sharedWorkspace] openURL:url]; |
|
158 }]; |
|
159 |
|
160 JNF_COCOA_EXIT(env); |
|
161 } |
|
162 |
|
163 |
|
164 /* |
|
165 * Class: com_apple_eio_FileManager |
|
166 * Method: getNativeResourceFromBundle |
|
167 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; |
|
168 */ |
|
169 JNIEXPORT jstring JNICALL Java_com_apple_eio_FileManager_getNativeResourceFromBundle |
|
170 (JNIEnv *env, jclass clz, jstring javaResourceName, jstring javaSubDirName, jstring javaTypeName) |
|
171 { |
|
172 jstring filename = NULL; |
|
173 JNF_COCOA_ENTER(env); |
|
174 |
|
175 NSString *resourceName = JNFNormalizedNSStringForPath(env, javaResourceName); |
|
176 NSString *subDirectory = JNFNormalizedNSStringForPath(env, javaSubDirName); |
|
177 NSString *typeName = JNFNormalizedNSStringForPath(env, javaTypeName); |
|
178 |
|
179 NSString *path = [[NSBundle mainBundle] pathForResource:resourceName |
|
180 ofType:typeName |
|
181 inDirectory:subDirectory]; |
|
182 |
|
183 filename = JNFNormalizedJavaStringForPath(env, path); |
|
184 |
|
185 JNF_COCOA_EXIT(env); |
|
186 return filename; |
|
187 } |
|
188 |
|
189 |
|
190 /* |
|
191 * Class: com_apple_eio_FileManager |
|
192 * Method: getNativePathToApplicationBundle |
|
193 * Signature: ()Ljava/lang/String; |
|
194 */ |
|
195 JNIEXPORT jstring JNICALL Java_com_apple_eio_FileManager_getNativePathToApplicationBundle |
|
196 (JNIEnv *env, jclass clazz) |
|
197 { |
|
198 jstring filename = nil; |
|
199 JNF_COCOA_ENTER(env); |
|
200 |
|
201 NSBundle *mainBundle = [NSBundle mainBundle]; |
|
202 filename = JNFNormalizedJavaStringForPath(env, [mainBundle bundlePath]); |
|
203 |
|
204 JNF_COCOA_EXIT(env); |
|
205 return filename; |
|
206 } |
|
207 |
|
208 |
|
209 /* |
|
210 * Class: com_apple_eio_FileManager |
|
211 * Method: __moveToTrash |
|
212 * Signature: (Ljava/lang/String;)V |
|
213 */ |
|
214 |
|
215 JNIEXPORT jboolean JNICALL Java_com_apple_eio_FileManager__1moveToTrash |
|
216 (JNIEnv *env, jclass clz, jstring url) |
|
217 { |
|
218 __block jboolean returnValue = JNI_FALSE; |
|
219 JNF_COCOA_ENTER(env); |
|
220 |
|
221 NSString *path = JNFNormalizedNSStringForPath(env, url); |
|
222 [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){ |
|
223 NSInteger res = 0; |
|
224 [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation |
|
225 source:[path stringByDeletingLastPathComponent] |
|
226 destination:nil |
|
227 files:[NSArray arrayWithObject:[path lastPathComponent]] |
|
228 tag:&res]; |
|
229 returnValue = (res == 0); |
|
230 }]; |
|
231 |
|
232 JNF_COCOA_EXIT(env); |
|
233 |
|
234 return returnValue; |
|
235 } |
|
236 |
|
237 /* |
|
238 * Class: com_apple_eio_FileManager |
|
239 * Method: __revealInFinder |
|
240 * Signature: (Ljava/lang/String;)V |
|
241 */ |
|
242 |
|
243 JNIEXPORT jboolean JNICALL Java_com_apple_eio_FileManager__1revealInFinder |
|
244 (JNIEnv *env, jclass clz, jstring url) |
|
245 { |
|
246 __block jboolean returnValue = JNI_FALSE; |
|
247 JNF_COCOA_ENTER(env); |
|
248 |
|
249 NSString *path = JNFNormalizedNSStringForPath(env, url); |
|
250 [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){ |
|
251 returnValue = [[NSWorkspace sharedWorkspace] selectFile:path inFileViewerRootedAtPath:@""]; |
|
252 }]; |
|
253 |
|
254 JNF_COCOA_EXIT(env); |
|
255 |
|
256 return returnValue; |
|
257 } |
|