1 /*
   2  * Copyright (c) 2011, 2014, 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 <sys/stat.h>
  27 #import <Cocoa/Cocoa.h>
  28 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  29 
  30 #import "CFileDialog.h"
  31 #import "ThreadUtilities.h"
  32 
  33 #import "java_awt_FileDialog.h"
  34 #import "sun_lwawt_macosx_CFileDialog.h"
  35 
  36 @implementation CFileDialog
  37 
  38 - (id)initWithFilter:(jboolean)inHasFilter
  39           fileDialog:(jobject)inDialog
  40                title:(NSString *)inTitle
  41            directory:(NSString *)inPath
  42                 file:(NSString *)inFile
  43                 mode:(jint)inMode
  44         multipleMode:(BOOL)inMultipleMode
  45       shouldNavigate:(BOOL)inNavigateApps
  46 canChooseDirectories:(BOOL)inChooseDirectories
  47              withEnv:(JNIEnv*)env;
  48 {
  49     if (self == [super init]) {
  50         fHasFileFilter = inHasFilter;
  51         fFileDialog = JNFNewGlobalRef(env, inDialog);
  52         fDirectory = inPath;
  53         [fDirectory retain];
  54         fFile = inFile;
  55         [fFile retain];
  56         fTitle = inTitle;
  57         [fTitle retain];
  58         fMode = inMode;
  59         fMultipleMode = inMultipleMode;
  60         fNavigateApps = inNavigateApps;
  61         fChooseDirectories = inChooseDirectories;
  62         fPanelResult = NSCancelButton;
  63     }
  64 
  65     return self;
  66 }
  67 
  68 -(void) disposer {
  69     if (fFileDialog != NULL) {
  70         JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
  71         JNFDeleteGlobalRef(env, fFileDialog);
  72         fFileDialog = NULL;
  73     }
  74 }
  75 
  76 -(void) dealloc {
  77     [fDirectory release];
  78     fDirectory = nil;
  79 
  80     [fFile release];
  81     fFile = nil;
  82 
  83     [fTitle release];
  84     fTitle = nil;
  85 
  86     [fURLs release];
  87     fURLs = nil;
  88 
  89     [super dealloc];
  90 }
  91 
  92 - (void)safeSaveOrLoad {
  93     NSSavePanel *thePanel = nil;
  94 
  95     /* 
  96      * 8013553: turns off extension hiding for the native file dialog.
  97      * This way is used because setExtensionHidden(NO) doesn't work
  98      * as expected.
  99      */
 100     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 101     [defaults setBool:NO forKey:@"NSNavLastUserSetHideExtensionButtonState"];
 102 
 103     if (fMode == java_awt_FileDialog_SAVE) {
 104         thePanel = [NSSavePanel savePanel];
 105         [thePanel setAllowsOtherFileTypes:YES];
 106     } else {
 107         thePanel = [NSOpenPanel openPanel];
 108     }
 109 
 110     if (thePanel != nil) {
 111         [thePanel setTitle:fTitle];
 112 
 113         if (fNavigateApps) {
 114             [thePanel setTreatsFilePackagesAsDirectories:YES];
 115         }
 116 
 117         if (fMode == java_awt_FileDialog_LOAD) {
 118             NSOpenPanel *openPanel = (NSOpenPanel *)thePanel;
 119             [openPanel setAllowsMultipleSelection:fMultipleMode];
 120             [openPanel setCanChooseFiles:!fChooseDirectories];
 121             [openPanel setCanChooseDirectories:fChooseDirectories];
 122             [openPanel setCanCreateDirectories:YES];
 123             if (fChooseDirectories) {
 124                 [openPanel setPrompt:@" Choose "];
 125             }
 126         }
 127         
 128         [thePanel setDelegate:self];
 129         fPanelResult = [thePanel runModalForDirectory:fDirectory file:fFile];
 130         [thePanel setDelegate:nil];
 131 
 132         if ([self userClickedOK]) {
 133             if (fMode == java_awt_FileDialog_LOAD) {
 134                 NSOpenPanel *openPanel = (NSOpenPanel *)thePanel;
 135                 fURLs = [openPanel URLs];
 136             } else {
 137                 fURLs = [NSArray arrayWithObject:[thePanel URL]];
 138             }
 139             [fURLs retain];
 140         }
 141     }
 142 
 143     [self disposer];
 144 }
 145 
 146 - (BOOL) askFilenameFilter:(NSString *)filename {
 147     JNIEnv *env = [ThreadUtilities getJNIEnv];
 148     jstring jString = JNFNormalizedJavaStringForPath(env, filename);
 149 
 150     static JNF_CLASS_CACHE(jc_CFileDialog, "sun/lwawt/macosx/CFileDialog");
 151     static JNF_MEMBER_CACHE(jm_queryFF, jc_CFileDialog, "queryFilenameFilter", "(Ljava/lang/String;)Z");
 152     BOOL returnValue = JNFCallBooleanMethod(env, fFileDialog, jm_queryFF, jString); // AWT_THREADING Safe (AWTRunLoopMode)
 153     (*env)->DeleteLocalRef(env, jString);
 154 
 155     return returnValue;
 156 }
 157 
 158 - (BOOL)panel:(id)sender shouldEnableURL:(NSURL *)url {
 159     if (!fHasFileFilter) return YES; // no filter, no problem!
 160 
 161     // check if it's not a normal file
 162     NSNumber *isFile = nil;
 163     if ([url getResourceValue:&isFile forKey:NSURLIsRegularFileKey error:nil]) {
 164         if (![isFile boolValue]) return YES; // always show directories and non-file entities (browsing servers/mounts, etc)
 165     }
 166 
 167     // if in directory-browsing mode, don't offer files
 168     if ((fMode != java_awt_FileDialog_LOAD) && (fMode != java_awt_FileDialog_SAVE)) {
 169         return NO;
 170     }
 171 
 172     // ask the file filter up in Java
 173     NSString* filePath = (NSString*)CFURLCopyFileSystemPath((CFURLRef)url, kCFURLPOSIXPathStyle);
 174     BOOL shouldEnableFile = [self askFilenameFilter:filePath];
 175     [filePath release];
 176     return shouldEnableFile;
 177 }
 178 
 179 - (BOOL) userClickedOK {
 180     return fPanelResult == NSOKButton;
 181 }
 182 
 183 - (NSArray *)URLs {
 184     return [[fURLs retain] autorelease];
 185 }
 186 @end
 187 
 188 /*
 189  * Class:     sun_lwawt_macosx_CFileDialog
 190  * Method:    nativeRunFileDialog
 191  * Signature: (Ljava/lang/String;ILjava/io/FilenameFilter;
 192  *             Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;
 193  */
 194 JNIEXPORT jobjectArray JNICALL
 195 Java_sun_lwawt_macosx_CFileDialog_nativeRunFileDialog
 196 (JNIEnv *env, jobject peer, jstring title, jint mode, jboolean multipleMode,
 197  jboolean navigateApps, jboolean chooseDirectories, jboolean hasFilter,
 198  jstring directory, jstring file)
 199 {
 200     jobjectArray returnValue = NULL;
 201 
 202 JNF_COCOA_ENTER(env);
 203     NSString *dialogTitle = JNFJavaToNSString(env, title);
 204     if ([dialogTitle length] == 0) {
 205         dialogTitle = @" ";
 206     }
 207 
 208     CFileDialog *dialogDelegate = [[CFileDialog alloc] initWithFilter:hasFilter
 209                                                            fileDialog:peer
 210                                                                 title:dialogTitle
 211                                                             directory:JNFJavaToNSString(env, directory)
 212                                                                  file:JNFJavaToNSString(env, file)
 213                                                                  mode:mode
 214                                                          multipleMode:multipleMode
 215                                                        shouldNavigate:navigateApps
 216                                                  canChooseDirectories:chooseDirectories
 217                                                               withEnv:env];
 218 
 219     [JNFRunLoop performOnMainThread:@selector(safeSaveOrLoad)
 220                                  on:dialogDelegate
 221                          withObject:nil
 222                       waitUntilDone:YES];
 223 
 224     if ([dialogDelegate userClickedOK]) {
 225         NSArray *urls = [dialogDelegate URLs];
 226         jsize count = [urls count];
 227 
 228         static JNF_CLASS_CACHE(jc_String, "java/lang/String");
 229         returnValue = JNFNewObjectArray(env, &jc_String, count);
 230 
 231         [urls enumerateObjectsUsingBlock:^(id url, NSUInteger index, BOOL *stop) {
 232             jstring filename = JNFNormalizedJavaStringForPath(env, [url path]);
 233             (*env)->SetObjectArrayElement(env, returnValue, index, filename);
 234             (*env)->DeleteLocalRef(env, filename);
 235         }];
 236     }
 237 
 238     [dialogDelegate release];
 239 JNF_COCOA_EXIT(env);
 240     return returnValue;
 241 }