1 /*
   2  * Copyright (c) 2010, 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 #include <jni.h>
  27 #include <stdio.h>
  28 #include <jni_util.h>
  29 #include <string.h>
  30 #include "gtk2_interface.h"
  31 #include "sun_awt_X11_GtkFileDialogPeer.h"
  32 #include "java_awt_FileDialog.h"
  33 #include "debug_assert.h"
  34 

  35 static JavaVM *jvm;
  36 
  37 /* To cache some method IDs */
  38 static jmethodID filenameFilterCallbackMethodID = NULL;
  39 static jmethodID setFileInternalMethodID = NULL;
  40 static jfieldID  widgetFieldID = NULL;
  41 
  42 JNIEXPORT void JNICALL Java_sun_awt_X11_GtkFileDialogPeer_initIDs
  43 (JNIEnv *env, jclass cx)
  44 {
  45     filenameFilterCallbackMethodID = (*env)->GetMethodID(env, cx,
  46             "filenameFilterCallback", "(Ljava/lang/String;)Z");
  47     DASSERT(filenameFilterCallbackMethodID != NULL);
  48     CHECK_NULL(filenameFilterCallbackMethodID);
  49 
  50     setFileInternalMethodID = (*env)->GetMethodID(env, cx,
  51             "setFileInternal", "(Ljava/lang/String;[Ljava/lang/String;)V");
  52     DASSERT(setFileInternalMethodID != NULL);
  53     CHECK_NULL(setFileInternalMethodID);
  54 
  55     widgetFieldID = (*env)->GetFieldID(env, cx, "widget", "J");
  56     DASSERT(widgetFieldID != NULL);
  57 }
  58 
  59 static gboolean filenameFilterCallback(const GtkFileFilterInfo * filter_info, gpointer obj)
  60 {
  61     JNIEnv *env;
  62     jstring filename;
  63 
  64     env = (JNIEnv *) JNU_GetEnv(jvm, JNI_VERSION_1_2);
  65 
  66     filename = (*env)->NewStringUTF(env, filter_info->filename);
  67     JNU_CHECK_EXCEPTION_RETURN(env, FALSE);
  68 
  69     return (*env)->CallBooleanMethod(env, obj, filenameFilterCallbackMethodID,
  70             filename);
  71 }
  72 
  73 static void quit(JNIEnv * env, jobject jpeer, gboolean isSignalHandler)
  74 {
  75     jthrowable pendingException;
  76     if (pendingException = (*env)->ExceptionOccurred(env)) {
  77          (*env)->ExceptionClear(env);
  78     }
  79 
  80     GtkWidget * dialog = (GtkWidget*)jlong_to_ptr(
  81             (*env)->GetLongField(env, jpeer, widgetFieldID));
  82 
  83     if (dialog != NULL)
  84     {
  85         // Callbacks from GTK signals are made within the GTK lock
  86         // So, within a signal handler there is no need to call
  87         // gdk_threads_enter() / fp_gdk_threads_leave()
  88         if (!isSignalHandler) {
  89             fp_gdk_threads_enter();
  90         }
  91 
  92         fp_gtk_widget_hide (dialog);
  93         fp_gtk_widget_destroy (dialog);
  94 
  95         fp_gtk_main_quit ();
  96 
  97         (*env)->SetLongField(env, jpeer, widgetFieldID, 0);
  98 
  99         if (!isSignalHandler) {
 100             fp_gdk_threads_leave();
 101         }
 102     }
 103 
 104     if (pendingException) {
 105          (*env)->Throw(env, pendingException);
 106     }
 107 }
 108 
 109 /*
 110  * Class:     sun_awt_X11_GtkFileDialogPeer
 111  * Method:    quit
 112  * Signature: ()V
 113  */
 114 JNIEXPORT void JNICALL Java_sun_awt_X11_GtkFileDialogPeer_quit
 115 (JNIEnv * env, jobject jpeer)
 116 {
 117     quit(env, jpeer, FALSE);
 118 }
 119 
 120 /*
 121  * Class:     sun_awt_X11_GtkFileDialogPeer
 122  * Method:    toFront
 123  * Signature: ()V
 124  */
 125 JNIEXPORT void JNICALL Java_sun_awt_X11_GtkFileDialogPeer_toFront
 126 (JNIEnv * env, jobject jpeer)
 127 {
 128     GtkWidget * dialog;
 129 
 130     fp_gdk_threads_enter();
 131 
 132     dialog = (GtkWidget*)jlong_to_ptr(
 133             (*env)->GetLongField(env, jpeer, widgetFieldID));
 134 
 135     if (dialog != NULL) {
 136         fp_gtk_window_present((GtkWindow*)dialog);
 137     }
 138 
 139     fp_gdk_threads_leave();
 140 }
 141 
 142 /*
 143  * Class:     sun_awt_X11_GtkFileDialogPeer
 144  * Method:    setBounds
 145  * Signature: (IIIII)V
 146  */
 147 JNIEXPORT void JNICALL Java_sun_awt_X11_GtkFileDialogPeer_setBounds
 148 (JNIEnv * env, jobject jpeer, jint x, jint y, jint width, jint height, jint op)
 149 {
 150     GtkWindow* dialog;
 151 
 152     fp_gdk_threads_enter();
 153 
 154     dialog = (GtkWindow*)jlong_to_ptr(
 155         (*env)->GetLongField(env, jpeer, widgetFieldID));
 156 
 157     if (dialog != NULL) {
 158         if (x >= 0 && y >= 0) {
 159             fp_gtk_window_move(dialog, (gint)x, (gint)y);
 160         }
 161         if (width > 0 && height > 0) {
 162             fp_gtk_window_resize(dialog, (gint)width, (gint)height);
 163         }
 164     }
 165 
 166     fp_gdk_threads_leave();
 167 }
 168 
 169 /*
 170  * baseDir should be freed by user.
 171  */
 172 static gboolean isFromSameDirectory(GSList* list, gchar** baseDir) {
 173 
 174     GSList *it = list;
 175     gchar* prevDir = NULL;
 176     gboolean isAllDirsSame = TRUE;
 177 
 178     while (it) {
 179         gchar* dir = fp_g_path_get_dirname((gchar*) it->data);
 180 
 181         if (prevDir && strcmp(prevDir, dir) != 0) {
 182             isAllDirsSame = FALSE;
 183             fp_g_free(dir);
 184             break;
 185         }
 186 
 187         if (!prevDir) {
 188             prevDir = strdup(dir);
 189         }
 190         fp_g_free(dir);
 191 
 192         it = it->next;
 193     }
 194 
 195     if (isAllDirsSame) {
 196         *baseDir = prevDir;
 197     } else {
 198         free(prevDir);
 199         *baseDir = strdup("/");
 200     }
 201 
 202     return isAllDirsSame;
 203 }
 204 
 205 /**
 206  * Convert a GSList to an array of filenames
 207  */
 208 static jobjectArray toFilenamesArray(JNIEnv *env, GSList* list, jstring* jcurrent_folder)
 209 {
 210     jstring str;
 211     jclass stringCls;
 212     GSList *iterator;
 213     jobjectArray array;
 214     int i;
 215     gchar* entry;
 216     gchar * baseDir;
 217     gboolean isFromSameDir;
 218 
 219     if (list == NULL) {
 220         return NULL;
 221     }
 222 
 223     stringCls = (*env)->FindClass(env, "java/lang/String");
 224     if (stringCls == NULL) {
 225         (*env)->ExceptionClear(env);
 226         JNU_ThrowInternalError(env, "Could not get java.lang.String class");
 227         return NULL;
 228     }
 229 
 230     array = (*env)->NewObjectArray(env, fp_gtk_g_slist_length(list), stringCls, NULL);
 231     if (array == NULL) {
 232         (*env)->ExceptionClear(env);
 233         JNU_ThrowInternalError(env, "Could not instantiate array files array");
 234         return NULL;
 235     }
 236 
 237     isFromSameDir = isFromSameDirectory(list, &baseDir);
 238 
 239     *jcurrent_folder = (*env)->NewStringUTF(env, baseDir);
 240     if (*jcurrent_folder == NULL) {
 241         free(baseDir);
 242         return NULL;
 243     }
 244 
 245     for (iterator = list, i=0;
 246             iterator;
 247             iterator = iterator->next, i++) {
 248 
 249         entry = (gchar*) iterator->data;
 250 
 251         if (isFromSameDir) {
 252             entry = strrchr(entry, '/') + 1;
 253         } else if (entry[0] == '/') {
 254             entry++;
 255         }
 256 
 257         str = (*env)->NewStringUTF(env, entry);
 258         if((*env)->ExceptionCheck(env)){
 259             break;
 260         }
 261         if (str) {
 262             (*env)->SetObjectArrayElement(env, array, i, str);
 263             if((*env)->ExceptionCheck(env)){
 264                 break;
 265             }
 266         }
 267     }
 268 
 269     free(baseDir);
 270     return array;
 271 }
 272 
 273 static void handle_response(GtkWidget* aDialog, gint responseId, gpointer obj)
 274 {
 275     JNIEnv *env;
 276     GSList *filenames;
 277     jstring jcurrent_folder = NULL;
 278     jobjectArray jfilenames;
 279 
 280     env = (JNIEnv *) JNU_GetEnv(jvm, JNI_VERSION_1_2);
 281     filenames = NULL;
 282 
 283     if (responseId == GTK_RESPONSE_ACCEPT) {
 284         filenames = fp_gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(aDialog));
 285     }
 286 
 287     jfilenames = toFilenamesArray(env, filenames, &jcurrent_folder);
 288 
 289     if (!(*env)->ExceptionCheck(env)) {
 290         (*env)->CallVoidMethod(env, obj, setFileInternalMethodID,
 291                                jcurrent_folder, jfilenames);
 292     }
 293 
 294     quit(env, (jobject)obj, TRUE);
 295 }
 296 
 297 /*
 298  * Class:     sun_awt_X11_GtkFileDialogPeer
 299  * Method:    run
 300  * Signature: (Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Ljava/io/FilenameFilter;ZII)V
 301  */
 302 JNIEXPORT void JNICALL
 303 Java_sun_awt_X11_GtkFileDialogPeer_run(JNIEnv * env, jobject jpeer,
 304         jstring jtitle, jint mode, jstring jdir, jstring jfile,
 305         jobject jfilter, jboolean multiple, int x, int y)
 306 {
 307     GtkWidget *dialog = NULL;
 308     GtkFileFilter *filter;
 309 
 310     if (jvm == NULL) {
 311         (*env)->GetJavaVM(env, &jvm);
 312         JNU_CHECK_EXCEPTION(env);
 313     }
 314 
 315     fp_gdk_threads_enter();
 316 
 317     const char *title = jtitle == NULL? "": (*env)->GetStringUTFChars(env, jtitle, 0);
 318     if (title == NULL) {
 319         (*env)->ExceptionClear(env);
 320         JNU_ThrowOutOfMemoryError(env, "Could not get title");
 321         return;
 322     }
 323 
 324     if (mode == java_awt_FileDialog_SAVE) {
 325         /* Save action */
 326         dialog = fp_gtk_file_chooser_dialog_new(title, NULL,
 327                 GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL,
 328                 GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
 329     }
 330     else {
 331         /* Default action OPEN */
 332         dialog = fp_gtk_file_chooser_dialog_new(title, NULL,
 333                 GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL,
 334                 GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
 335 
 336         /* Set multiple selection mode, that is allowed only in OPEN action */
 337         if (multiple) {
 338             fp_gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog),
 339                     multiple);
 340         }
 341     }
 342 
 343     if (jtitle != NULL) {
 344       (*env)->ReleaseStringUTFChars(env, jtitle, title);
 345     }
 346 
 347     /* Set the directory */
 348     if (jdir != NULL) {
 349         const char *dir = (*env)->GetStringUTFChars(env, jdir, 0);
 350         if (dir == NULL) {
 351             (*env)->ExceptionClear(env);
 352             JNU_ThrowOutOfMemoryError(env, "Could not get dir");
 353             return;
 354         }
 355         fp_gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), dir);
 356         (*env)->ReleaseStringUTFChars(env, jdir, dir);
 357     }
 358 
 359     /* Set the filename */
 360     if (jfile != NULL) {
 361         const char *filename = (*env)->GetStringUTFChars(env, jfile, 0);
 362         if (filename == NULL) {
 363             (*env)->ExceptionClear(env);
 364             JNU_ThrowOutOfMemoryError(env, "Could not get filename");
 365             return;
 366         }
 367         if (mode == java_awt_FileDialog_SAVE) {
 368             fp_gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), filename);
 369         } else {
 370             fp_gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), filename);
 371         }
 372         (*env)->ReleaseStringUTFChars(env, jfile, filename);
 373     }
 374 
 375     /* Set the file filter */
 376     if (jfilter != NULL) {
 377         filter = fp_gtk_file_filter_new();
 378         fp_gtk_file_filter_add_custom(filter, GTK_FILE_FILTER_FILENAME,
 379                 filenameFilterCallback, jpeer, NULL);
 380         fp_gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
 381     }
 382 
 383     /* Other Properties */
 384     if (fp_gtk_check_version(2, 8, 0) == NULL) {
 385         fp_gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(

 386                 dialog), TRUE);
 387     }
 388 
 389     /* Set the initial location */
 390     if (x >= 0 && y >= 0) {
 391         fp_gtk_window_move((GtkWindow*)dialog, (gint)x, (gint)y);
 392 
 393         // NOTE: it doesn't set the initial size for the file chooser
 394         // as it seems like the file chooser overrides the size internally
 395     }
 396 
 397     fp_g_signal_connect(G_OBJECT(dialog), "response", G_CALLBACK(
 398             handle_response), jpeer);
 399 
 400     (*env)->SetLongField(env, jpeer, widgetFieldID, ptr_to_jlong(dialog));
 401 
 402     fp_gtk_widget_show(dialog);
 403 
 404     fp_gtk_main();
 405     fp_gdk_threads_leave();
 406 }
 407 
--- EOF ---