1 /*
   2  * Copyright (c) 2011, 2016, 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 "JNIUtilities.h"
  29 #import "ThreadUtilities.h"
  30 
  31 #import <Cocoa/Cocoa.h>
  32 
  33 /*
  34  * Class:     com_apple_eio_FileManager
  35  * Method:    _setFileTypeAndCreator
  36  * Signature: (Ljava/lang/String;II)V
  37  */
  38 JNIEXPORT void JNICALL Java_com_apple_eio_FileManager__1setFileTypeAndCreator
  39 (JNIEnv *env, jclass clz, jstring javaFilename, jint type, jint creator)
  40 {
  41 JNI_COCOA_ENTER(env);
  42         NSString *filename = NormalizedPathNSStringFromJavaString(env, javaFilename);
  43         NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:
  44                                                         [NSNumber numberWithInt:type], NSFileHFSTypeCode,
  45                                                         [NSNumber numberWithInt:creator], NSFileHFSCreatorCode, nil];
  46     [[NSFileManager defaultManager] changeFileAttributes:attr atPath:filename];
  47 JNI_COCOA_EXIT(env);
  48 }
  49 
  50 /*
  51  * Class:     com_apple_eio_FileManager
  52  * Method:    _setFileType
  53  * Signature: (Ljava/lang/String;I)V
  54  */
  55 JNIEXPORT void JNICALL Java_com_apple_eio_FileManager__1setFileType
  56 (JNIEnv *env, jclass ckz, jstring javaFilename, jint type)
  57 {
  58 JNI_COCOA_ENTER(env);
  59         NSString *filename = NormalizedPathNSStringFromJavaString(env, javaFilename);
  60         NSDictionary *attr = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:type] forKey:NSFileHFSTypeCode];
  61     [[NSFileManager defaultManager] changeFileAttributes:attr atPath:filename];
  62 JNI_COCOA_EXIT(env);
  63 }
  64 
  65 /*
  66  * Class:     com_apple_eio_FileManager
  67  * Method:    _setFileCreator
  68  * Signature: (Ljava/lang/String;I)V
  69  */
  70 JNIEXPORT void JNICALL Java_com_apple_eio_FileManager__1setFileCreator
  71 (JNIEnv *env, jclass clz, jstring javaFilename, jint creator)
  72 {
  73 JNI_COCOA_ENTER(env);
  74         NSString *filename = NormalizedPathNSStringFromJavaString(env, javaFilename);
  75         NSDictionary *attr = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:creator] forKey:NSFileHFSCreatorCode];
  76     [[NSFileManager defaultManager] changeFileAttributes:attr atPath:filename];
  77 JNI_COCOA_EXIT(env);
  78 }
  79 
  80 /*
  81  * Class:     com_apple_eio_FileManager
  82  * Method:    _getFileType
  83  * Signature: (Ljava/lang/String;)I
  84  */
  85 JNIEXPORT jint JNICALL Java_com_apple_eio_FileManager__1getFileType
  86 (JNIEnv *env, jclass clz, jstring javaFilename)
  87 {
  88     jint type = 0;
  89 JNI_COCOA_ENTER(env);
  90         NSString *filename = NormalizedPathNSStringFromJavaString(env, javaFilename);
  91     NSDictionary *attributes = [[NSFileManager defaultManager] fileAttributesAtPath:filename traverseLink:YES];
  92     NSNumber *val = [attributes objectForKey:NSFileHFSTypeCode];
  93     type = [val intValue];
  94 JNI_COCOA_EXIT(env);
  95     return type;
  96 }
  97 
  98 /*
  99  * Class:     com_apple_eio_FileManager
 100  * Method:    _getFileCreator
 101  * Signature: (Ljava/lang/String;)I
 102  */
 103 JNIEXPORT jint JNICALL Java_com_apple_eio_FileManager__1getFileCreator
 104   (JNIEnv *env, jclass clz, jstring javaFilename)
 105 {
 106     jint creator = 0;
 107 JNI_COCOA_ENTER(env);
 108         NSString *filename = NormalizedPathNSStringFromJavaString(env, javaFilename);
 109     NSDictionary *attributes = [[NSFileManager defaultManager] fileAttributesAtPath:filename traverseLink:YES];
 110     NSNumber *val = [attributes objectForKey:NSFileHFSCreatorCode];
 111     creator = [val intValue];
 112 JNI_COCOA_EXIT(env);
 113     return creator;
 114 }
 115 
 116 /*
 117  * Class:     com_apple_eio_FileManager
 118  * Method:    _findFolder
 119  * Signature: (SIZ)Ljava/lang/String;
 120  */
 121 JNIEXPORT jstring JNICALL Java_com_apple_eio_FileManager__1findFolder__SIZ
 122 (JNIEnv *env, jclass clz, jshort domain, jint folderType, jboolean createIfNeeded)
 123 {
 124     jstring filename = nil;
 125 JNI_COCOA_ENTER(env);
 126 
 127     FSRef foundRef;
 128     createIfNeeded = createIfNeeded || (folderType == kTemporaryFolderType) || (folderType == kChewableItemsFolderType);
 129     if (FSFindFolder((SInt16)domain, (OSType)folderType, (Boolean)createIfNeeded, &foundRef) == noErr) {
 130         char path[PATH_MAX];
 131         if (FSRefMakePath(&foundRef, (UInt8 *)path, sizeof(path)) == noErr) {
 132             NSString *filenameString = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:path length:strlen(path)];
 133             filename = NormalizedPathJavaStringFromNSString(env, filenameString);
 134         }
 135     }
 136 
 137 JNI_COCOA_EXIT(env);
 138     return filename;
 139 }
 140 
 141 
 142 /*
 143  * Class:     com_apple_eio_FileManager
 144  * Method:    _openURL
 145  * Signature: (Ljava/lang/String;)V
 146  */
 147 JNIEXPORT void JNICALL Java_com_apple_eio_FileManager__1openURL
 148 (JNIEnv *env, jclass clz, jstring urlString)
 149 {
 150 JNI_COCOA_ENTER(env);
 151 
 152     NSURL *url = [NSURL URLWithString:NormalizedPathNSStringFromJavaString(env, urlString)];
 153 
 154         // Radar 3208005: Run this on the main thread; file:// style URLs will hang otherwise.
 155     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
 156         [[NSWorkspace sharedWorkspace] openURL:url];
 157     }];
 158 
 159 JNI_COCOA_EXIT(env);
 160 }
 161 
 162 
 163 /*
 164  * Class:     com_apple_eio_FileManager
 165  * Method:    getNativeResourceFromBundle
 166  * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 167  */
 168 JNIEXPORT jstring JNICALL Java_com_apple_eio_FileManager_getNativeResourceFromBundle
 169 (JNIEnv *env, jclass clz, jstring javaResourceName, jstring javaSubDirName, jstring javaTypeName)
 170 {
 171     jstring filename = NULL;
 172 JNI_COCOA_ENTER(env);
 173 
 174     NSString *resourceName = NormalizedPathNSStringFromJavaString(env, javaResourceName);
 175         NSString *subDirectory = NormalizedPathNSStringFromJavaString(env, javaSubDirName);
 176         NSString *typeName = NormalizedPathNSStringFromJavaString(env, javaTypeName);
 177 
 178     NSString *path = [[NSBundle mainBundle] pathForResource:resourceName
 179                                                      ofType:typeName
 180                                                 inDirectory:subDirectory];
 181 
 182     filename = NormalizedPathJavaStringFromNSString(env, path);
 183 
 184 JNI_COCOA_EXIT(env);
 185     return filename;
 186 }
 187 
 188 
 189 /*
 190  * Class:     com_apple_eio_FileManager
 191  * Method:    getNativePathToApplicationBundle
 192  * Signature: ()Ljava/lang/String;
 193  */
 194 JNIEXPORT jstring JNICALL Java_com_apple_eio_FileManager_getNativePathToApplicationBundle
 195 (JNIEnv *env, jclass clazz)
 196 {
 197         jstring filename = nil;
 198 JNI_COCOA_ENTER(env);
 199 
 200         NSBundle *mainBundle = [NSBundle mainBundle];
 201         filename = NormalizedPathJavaStringFromNSString(env, [mainBundle bundlePath]);
 202 
 203 JNI_COCOA_EXIT(env);
 204         return filename;
 205 }
 206 
 207 
 208 /*
 209  * Class:     com_apple_eio_FileManager
 210  * Method:    __moveToTrash
 211  * Signature: (Ljava/lang/String;)V
 212  */
 213 
 214 JNIEXPORT jboolean JNICALL Java_com_apple_eio_FileManager__1moveToTrash
 215 (JNIEnv *env, jclass clz, jstring fileName)
 216 {
 217     __block BOOL returnValue = NO;
 218 JNI_COCOA_ENTER(env);
 219 
 220     NSString * path = NormalizedPathNSStringFromJavaString(env, fileName);
 221     NSURL *url = [NSURL fileURLWithPath:path];
 222     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 223 
 224         returnValue  = [[NSFileManager defaultManager] trashItemAtURL:url
 225                                                      resultingItemURL:nil
 226                                                                 error:nil];
 227     }];
 228 
 229 JNI_COCOA_EXIT(env);
 230 
 231         return returnValue ? JNI_TRUE: JNI_FALSE;
 232 }
 233 
 234 /*
 235  * Class:     com_apple_eio_FileManager
 236  * Method:    __revealInFinder
 237  * Signature: (Ljava/lang/String;)V
 238  */
 239 
 240 JNIEXPORT jboolean JNICALL Java_com_apple_eio_FileManager__1revealInFinder
 241 (JNIEnv *env, jclass clz, jstring url)
 242 {
 243         __block jboolean returnValue = JNI_FALSE;
 244 JNI_COCOA_ENTER(env);
 245 
 246     NSString *path = NormalizedPathNSStringFromJavaString(env, url);
 247     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 248         returnValue = [[NSWorkspace sharedWorkspace] selectFile:path inFileViewerRootedAtPath:@""];
 249     }];
 250 
 251 JNI_COCOA_EXIT(env);
 252 
 253         return returnValue;
 254 }