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