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 #include "glass_general.h"
  26 
  27 #include <jni.h>
  28 #include <gtk/gtk.h>
  29 
  30 char const * const GDK_WINDOW_DATA_CONTEXT = "glass_window_context";
  31 
  32 jclass jStringCls;
  33 jclass jByteBufferCls;
  34 jmethodID jByteBufferArray;
  35 jmethodID jByteBufferWrap;
  36 
  37 jclass jRunnableCls;
  38 jmethodID jRunnableRun;
  39 
  40 jclass jArrayListCls;
  41 jmethodID jArrayListInit;
  42 jmethodID jArrayListAdd;
  43 jmethodID jArrayListGetIdx;
  44 
  45 jmethodID jPixelsAttachData;
  46 
  47 jclass jGtkPixelsCls;
  48 jmethodID jGtkPixelsInit;
  49 
  50 jclass jScreenCls;
  51 jmethodID jScreenInit;
  52 jmethodID jScreenNotifySettingsChanged;
  53 
  54 jmethodID jViewNotifyResize;
  55 jmethodID jViewNotifyMouse;
  56 jmethodID jViewNotifyRepaint;
  57 jmethodID jViewNotifyKey;
  58 jmethodID jViewNotifyView;
  59 jmethodID jViewNotifyDragEnter;
  60 jmethodID jViewNotifyDragOver;
  61 jmethodID jViewNotifyDragDrop;
  62 jmethodID jViewNotifyDragLeave;
  63 jmethodID jViewNotifyScroll;
  64 jmethodID jViewNotifyInputMethod;
  65 jmethodID jViewNotifyInputMethodDraw;
  66 jmethodID jViewNotifyInputMethodCaret;
  67 jmethodID jViewNotifyPreeditMode;
  68 jmethodID jViewNotifyMenu;
  69 jfieldID  jViewPtr;
  70 
  71 jmethodID jWindowNotifyResize;
  72 jmethodID jWindowNotifyMove;
  73 jmethodID jWindowNotifyDestroy;
  74 jmethodID jWindowNotifyClose;
  75 jmethodID jWindowNotifyFocus;
  76 jmethodID jWindowNotifyFocusDisabled;
  77 jmethodID jWindowNotifyFocusUngrab;
  78 jmethodID jWindowNotifyMoveToAnotherScreen;
  79 jmethodID jWindowNotifyLevelChanged;
  80 jmethodID jWindowIsEnabled;
  81 jmethodID jWindowNotifyDelegatePtr;
  82 jfieldID jWindowPtr;
  83 jfieldID jCursorPtr;
  84 
  85 jmethodID jGtkWindowNotifyStateChanged;
  86 
  87 jmethodID jClipboardContentChanged;
  88 
  89 jmethodID jSizeInit;
  90 
  91 jmethodID jMapGet;
  92 jmethodID jMapKeySet;
  93 jmethodID jMapContainsKey;
  94 
  95 jclass jHashSetCls;
  96 jmethodID jHashSetInit;
  97 
  98 jmethodID jSetAdd;
  99 jmethodID jSetSize;
 100 jmethodID jSetToArray;
 101 
 102 jmethodID jIterableIterator;
 103 jmethodID jIteratorHasNext;
 104 jmethodID jIteratorNext;
 105 
 106 jclass jApplicationCls;
 107 jfieldID jApplicationDisplay;
 108 jfieldID jApplicationScreen;
 109 jfieldID jApplicationVisualID;
 110 jmethodID jApplicationReportException;
 111 jmethodID jApplicationGetApplication;
 112 jmethodID jApplicationGetName;
 113 
 114 static jboolean displayValid = JNI_FALSE;
 115 
 116 jboolean
 117 is_display_valid() {
 118     return displayValid;
 119 }
 120 
 121 JavaVM* javaVM;
 122 
 123 JNIEXPORT jint JNICALL
 124 JNI_OnLoad(JavaVM *jvm, void *reserved)
 125 {
 126     (void)reserved;
 127 
 128     JNIEnv *env;
 129     jclass clazz;
 130     Display* display;
 131 
 132     javaVM = jvm;
 133     if (jvm->GetEnv((void **)&env, JNI_VERSION_1_6)) {
 134          return JNI_ERR; /* JNI version not supported */
 135     }
 136 
 137     clazz = env->FindClass("java/lang/String");
 138     if (env->ExceptionCheck()) return JNI_ERR;
 139     jStringCls = (jclass) env->NewGlobalRef(clazz);
 140 
 141     clazz = env->FindClass("java/nio/ByteBuffer");
 142     if (env->ExceptionCheck()) return JNI_ERR;
 143     jByteBufferCls = (jclass) env->NewGlobalRef(clazz);
 144     jByteBufferArray = env->GetMethodID(jByteBufferCls, "array", "()[B");
 145     if (env->ExceptionCheck()) return JNI_ERR;
 146     jByteBufferWrap = env->GetStaticMethodID(jByteBufferCls, "wrap", "([B)Ljava/nio/ByteBuffer;");
 147     if (env->ExceptionCheck()) return JNI_ERR;
 148 
 149     clazz = env->FindClass("java/lang/Runnable");
 150     if (env->ExceptionCheck()) return JNI_ERR;
 151 
 152     jRunnableRun = env->GetMethodID(clazz, "run", "()V");
 153     if (env->ExceptionCheck()) return JNI_ERR;
 154 
 155     clazz = env->FindClass("java/util/ArrayList");
 156     if (env->ExceptionCheck()) return JNI_ERR;
 157     jArrayListCls = (jclass) env->NewGlobalRef(clazz);
 158     jArrayListInit = env->GetMethodID(jArrayListCls, "<init>", "()V");
 159     if (env->ExceptionCheck()) return JNI_ERR;
 160     jArrayListAdd = env->GetMethodID(jArrayListCls, "add", "(Ljava/lang/Object;)Z");
 161     if (env->ExceptionCheck()) return JNI_ERR;
 162     jArrayListGetIdx = env->GetMethodID(jArrayListCls, "get", "(I)Ljava/lang/Object;");
 163     if (env->ExceptionCheck()) return JNI_ERR;
 164     clazz = env->FindClass("com/sun/glass/ui/Pixels");
 165     if (env->ExceptionCheck()) return JNI_ERR;
 166     jPixelsAttachData = env->GetMethodID(clazz, "attachData", "(J)V");
 167     if (env->ExceptionCheck()) return JNI_ERR;
 168 
 169     clazz = env->FindClass("com/sun/glass/ui/gtk/GtkPixels");
 170     if (env->ExceptionCheck()) return JNI_ERR;
 171 
 172     jGtkPixelsCls = (jclass) env->NewGlobalRef(clazz);
 173     jGtkPixelsInit = env->GetMethodID(jGtkPixelsCls, "<init>", "(IILjava/nio/ByteBuffer;)V");
 174     if (env->ExceptionCheck()) return JNI_ERR;
 175 
 176     clazz = env->FindClass("com/sun/glass/ui/Screen");
 177     if (env->ExceptionCheck()) return JNI_ERR;
 178     jScreenCls = (jclass) env->NewGlobalRef(clazz);
 179     jScreenInit = env->GetMethodID(jScreenCls, "<init>", "(JIIIIIIIIIIIIIIIFFFF)V");
 180     if (env->ExceptionCheck()) return JNI_ERR;
 181     jScreenNotifySettingsChanged = env->GetStaticMethodID(jScreenCls, "notifySettingsChanged", "()V");
 182     if (env->ExceptionCheck()) return JNI_ERR;
 183 
 184     clazz = env->FindClass("com/sun/glass/ui/View");
 185     if (env->ExceptionCheck()) return JNI_ERR;
 186     jViewNotifyResize = env->GetMethodID(clazz, "notifyResize", "(II)V");
 187     if (env->ExceptionCheck()) return JNI_ERR;
 188     jViewNotifyMouse = env->GetMethodID(clazz, "notifyMouse", "(IIIIIIIZZ)V");
 189     if (env->ExceptionCheck()) return JNI_ERR;
 190     jViewNotifyRepaint = env->GetMethodID(clazz, "notifyRepaint", "(IIII)V");
 191     if (env->ExceptionCheck()) return JNI_ERR;
 192     jViewNotifyKey = env->GetMethodID(clazz, "notifyKey", "(II[CI)V");
 193     if (env->ExceptionCheck()) return JNI_ERR;
 194     jViewNotifyView = env->GetMethodID(clazz, "notifyView", "(I)V");
 195     if (env->ExceptionCheck()) return JNI_ERR;
 196     jViewNotifyDragEnter = env->GetMethodID(clazz, "notifyDragEnter", "(IIIII)I");
 197     if (env->ExceptionCheck()) return JNI_ERR;
 198     jViewNotifyDragOver = env->GetMethodID(clazz, "notifyDragOver", "(IIIII)I");
 199     if (env->ExceptionCheck()) return JNI_ERR;
 200     jViewNotifyDragDrop = env->GetMethodID(clazz, "notifyDragDrop", "(IIIII)I");
 201     if (env->ExceptionCheck()) return JNI_ERR;
 202     jViewNotifyDragLeave = env->GetMethodID(clazz, "notifyDragLeave", "()V");
 203     if (env->ExceptionCheck()) return JNI_ERR;
 204     jViewNotifyScroll = env->GetMethodID(clazz, "notifyScroll", "(IIIIDDIIIIIDD)V");
 205     if (env->ExceptionCheck()) return JNI_ERR;
 206     jViewNotifyInputMethod = env->GetMethodID(clazz, "notifyInputMethod", "(Ljava/lang/String;[I[I[BIII)V");
 207     if (env->ExceptionCheck()) return JNI_ERR;
 208     jViewNotifyMenu = env->GetMethodID(clazz, "notifyMenu", "(IIIIZ)V");
 209     if (env->ExceptionCheck()) return JNI_ERR;
 210     jViewPtr = env->GetFieldID(clazz, "ptr", "J");
 211     if (env->ExceptionCheck()) return JNI_ERR;
 212 
 213     clazz = env->FindClass("com/sun/glass/ui/gtk/GtkView");
 214     if (env->ExceptionCheck()) return JNI_ERR;
 215     jViewNotifyInputMethodDraw = env->GetMethodID(clazz, "notifyInputMethodDraw", "(Ljava/lang/String;III[B)V");
 216     if (env->ExceptionCheck()) return JNI_ERR;
 217     jViewNotifyInputMethodCaret = env->GetMethodID(clazz, "notifyInputMethodCaret", "(III)V");
 218     if (env->ExceptionCheck()) return JNI_ERR;
 219     jViewNotifyPreeditMode = env->GetMethodID(clazz, "notifyPreeditMode", "(Z)V");
 220     if (env->ExceptionCheck()) return JNI_ERR;
 221 
 222     clazz = env->FindClass("com/sun/glass/ui/Window");
 223     if (env->ExceptionCheck()) return JNI_ERR;
 224     jWindowNotifyResize = env->GetMethodID(clazz, "notifyResize", "(III)V");
 225     if (env->ExceptionCheck()) return JNI_ERR;
 226     jWindowNotifyMove = env->GetMethodID(clazz, "notifyMove", "(II)V");
 227     if (env->ExceptionCheck()) return JNI_ERR;
 228     jWindowNotifyDestroy = env->GetMethodID(clazz, "notifyDestroy", "()V");
 229     if (env->ExceptionCheck()) return JNI_ERR;
 230     jWindowNotifyClose = env->GetMethodID(clazz, "notifyClose", "()V");
 231     if (env->ExceptionCheck()) return JNI_ERR;
 232     jWindowNotifyFocus = env->GetMethodID(clazz, "notifyFocus", "(I)V");
 233     if (env->ExceptionCheck()) return JNI_ERR;
 234     jWindowNotifyFocusDisabled = env->GetMethodID(clazz, "notifyFocusDisabled", "()V");
 235     if (env->ExceptionCheck()) return JNI_ERR;
 236     jWindowNotifyFocusUngrab = env->GetMethodID(clazz, "notifyFocusUngrab", "()V");
 237     if (env->ExceptionCheck()) return JNI_ERR;
 238     jWindowNotifyMoveToAnotherScreen = env->GetMethodID(clazz, "notifyMoveToAnotherScreen", "(Lcom/sun/glass/ui/Screen;)V");
 239     if (env->ExceptionCheck()) return JNI_ERR;
 240     jWindowNotifyLevelChanged = env->GetMethodID(clazz, "notifyLevelChanged", "(I)V");
 241     if (env->ExceptionCheck()) return JNI_ERR;
 242     jWindowIsEnabled = env->GetMethodID(clazz, "isEnabled", "()Z");
 243     if (env->ExceptionCheck()) return JNI_ERR;
 244     jWindowNotifyDelegatePtr = env->GetMethodID(clazz, "notifyDelegatePtr", "(J)V");
 245     if (env->ExceptionCheck()) return JNI_ERR;
 246     jWindowPtr = env->GetFieldID(clazz, "ptr", "J");
 247     if (env->ExceptionCheck()) return JNI_ERR;
 248 
 249     clazz = env->FindClass("com/sun/glass/ui/gtk/GtkWindow");
 250     if (env->ExceptionCheck()) return JNI_ERR;
 251     jGtkWindowNotifyStateChanged =
 252             env->GetMethodID(clazz, "notifyStateChanged", "(I)V");
 253     if (env->ExceptionCheck()) return JNI_ERR;
 254 
 255     clazz = env->FindClass("com/sun/glass/ui/Clipboard");
 256     if (env->ExceptionCheck()) return JNI_ERR;
 257     jClipboardContentChanged = env->GetMethodID(clazz, "contentChanged", "()V");
 258     if (env->ExceptionCheck()) return JNI_ERR;
 259 
 260     clazz = env->FindClass("com/sun/glass/ui/Cursor");
 261     if (env->ExceptionCheck()) return JNI_ERR;
 262     jCursorPtr = env->GetFieldID(clazz, "ptr", "J");
 263     if (env->ExceptionCheck()) return JNI_ERR;
 264 
 265     clazz = env->FindClass("com/sun/glass/ui/Size");
 266     if (env->ExceptionCheck()) return JNI_ERR;
 267     jSizeInit = env->GetMethodID(clazz, "<init>", "(II)V");
 268     if (env->ExceptionCheck()) return JNI_ERR;
 269 
 270     clazz = env->FindClass("java/util/Map");
 271     if (env->ExceptionCheck()) return JNI_ERR;
 272     jMapGet = env->GetMethodID(clazz, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
 273     if (env->ExceptionCheck()) return JNI_ERR;
 274     jMapKeySet = env->GetMethodID(clazz, "keySet", "()Ljava/util/Set;");
 275     if (env->ExceptionCheck()) return JNI_ERR;
 276     jMapContainsKey = env->GetMethodID(clazz, "containsKey", "(Ljava/lang/Object;)Z");
 277     if (env->ExceptionCheck()) return JNI_ERR;
 278 
 279     clazz = env->FindClass("java/util/HashSet");
 280     if (env->ExceptionCheck()) return JNI_ERR;
 281     jHashSetCls = (jclass) env->NewGlobalRef(clazz);
 282     jHashSetInit = env->GetMethodID(jHashSetCls, "<init>", "()V");
 283     if (env->ExceptionCheck()) return JNI_ERR;
 284 
 285     clazz = env->FindClass("java/util/Set");
 286     if (env->ExceptionCheck()) return JNI_ERR;
 287     jSetAdd = env->GetMethodID(clazz, "add", "(Ljava/lang/Object;)Z");
 288     if (env->ExceptionCheck()) return JNI_ERR;
 289     jSetSize = env->GetMethodID(clazz, "size", "()I");
 290     if (env->ExceptionCheck()) return JNI_ERR;
 291     jSetToArray = env->GetMethodID(clazz, "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;");
 292     if (env->ExceptionCheck()) return JNI_ERR;
 293 
 294     clazz = env->FindClass("java/lang/Iterable");
 295     if (env->ExceptionCheck()) return JNI_ERR;
 296     jIterableIterator = env->GetMethodID(clazz, "iterator", "()Ljava/util/Iterator;");
 297     if (env->ExceptionCheck()) return JNI_ERR;
 298 
 299     clazz = env->FindClass("java/util/Iterator");
 300     if (env->ExceptionCheck()) return JNI_ERR;
 301     jIteratorHasNext = env->GetMethodID(clazz, "hasNext", "()Z");
 302     if (env->ExceptionCheck()) return JNI_ERR;
 303     jIteratorNext = env->GetMethodID(clazz, "next", "()Ljava/lang/Object;");
 304     if (env->ExceptionCheck()) return JNI_ERR;
 305 
 306     clazz = env->FindClass("com/sun/glass/ui/gtk/GtkApplication");
 307     if (env->ExceptionCheck()) return JNI_ERR;
 308     jApplicationCls = (jclass) env->NewGlobalRef(clazz);
 309     jApplicationDisplay = env->GetStaticFieldID(jApplicationCls, "display", "J");
 310     if (env->ExceptionCheck()) return JNI_ERR;
 311     jApplicationScreen = env->GetStaticFieldID(jApplicationCls, "screen", "I");
 312     if (env->ExceptionCheck()) return JNI_ERR;
 313     jApplicationVisualID = env->GetStaticFieldID(jApplicationCls, "visualID", "J");
 314     if (env->ExceptionCheck()) return JNI_ERR;
 315     jApplicationReportException = env->GetStaticMethodID(
 316         jApplicationCls, "reportException", "(Ljava/lang/Throwable;)V");
 317     if (env->ExceptionCheck()) return JNI_ERR;
 318     jApplicationGetApplication = env->GetStaticMethodID(
 319         jApplicationCls, "GetApplication", "()Lcom/sun/glass/ui/Application;");
 320     if (env->ExceptionCheck()) return JNI_ERR;
 321     jApplicationGetName = env->GetMethodID(jApplicationCls, "getName", "()Ljava/lang/String;");
 322     if (env->ExceptionCheck()) return JNI_ERR;
 323 
 324     // Before doing anything with GTK we validate that the DISPLAY can be opened
 325     display = XOpenDisplay(NULL);
 326     if (display != NULL) {
 327         XCloseDisplay(display);
 328         displayValid = JNI_TRUE;
 329     } else {
 330         // Invalid DISPLAY, skip initialization
 331         return JNI_VERSION_1_6;
 332     }
 333 
 334     return JNI_VERSION_1_6;
 335 }
 336 
 337 void
 338 glass_throw_exception(JNIEnv * env,
 339                       const char * exceptionClass,
 340                       const char * exceptionMessage) {
 341     jclass throwableClass = env->FindClass(exceptionClass);
 342     if (check_and_clear_exception(env)) return;
 343     env->ThrowNew(throwableClass, exceptionMessage);
 344     check_and_clear_exception(env);
 345 }
 346 
 347 int
 348 glass_throw_oom(JNIEnv * env, const char * message) {
 349     glass_throw_exception(env, "java/lang/OutOfMemoryError", message);
 350     // must return a non-zero value, see HANDLE_MEM_ALLOC_ERROR
 351     return 1;
 352 }
 353 
 354 
 355 guint8* convert_BGRA_to_RGBA(const int* pixels, int stride, int height) {
 356   guint8* new_pixels = (guint8*) g_malloc(height * stride);
 357   int i = 0;
 358 
 359   for (i = 0; i < height * stride; i += 4) {
 360       new_pixels[i] = (guint8)(*pixels >> 16);
 361       new_pixels[i + 1] = (guint8)(*pixels >> 8);
 362       new_pixels[i + 2] = (guint8)(*pixels);
 363       new_pixels[i + 3] = (guint8)(*pixels >> 24);
 364       pixels++;
 365   }
 366 
 367   return new_pixels;
 368 }
 369 
 370 
 371 void dump_jstring_array(JNIEnv* env, jobjectArray arr) {
 372     if (arr == NULL) {
 373         LOG0("dump: Array is null\n")
 374         return;
 375     }
 376     jsize len = env->GetArrayLength(arr);
 377     LOG1("dump: length = %d\n", len)
 378     int i = 0;
 379     jboolean isCopy;
 380     for(i = 0; i < len; i++) {
 381         jstring jstr = (jstring) env->GetObjectArrayElement(arr, i);
 382         check_and_clear_exception(env);
 383         const char* str = env->GetStringUTFChars(jstr, &isCopy);
 384         LOG2("dump: s[%d]: %s\n", i, str)
 385     }
 386 }
 387 
 388 gboolean check_and_clear_exception(JNIEnv *env) {
 389     jthrowable t = env->ExceptionOccurred();
 390     if (t) {
 391         env->ExceptionClear();
 392         env->CallStaticVoidMethod(jApplicationCls, jApplicationReportException, t);
 393         //Clear in case our reporting upcall failed too!
 394         env->ExceptionClear();
 395         return TRUE;
 396     }
 397     return FALSE;
 398 }
 399 
 400 // The returned string should be freed with g_free().
 401 gchar* get_application_name() {
 402     gchar* ret = NULL;
 403 
 404     jobject japp = mainEnv->CallStaticObjectMethod(jApplicationCls, jApplicationGetApplication);
 405     CHECK_JNI_EXCEPTION_RET(mainEnv, NULL);
 406     jstring jname = (jstring) mainEnv->CallObjectMethod(japp, jApplicationGetName);
 407     CHECK_JNI_EXCEPTION_RET(mainEnv, NULL);
 408     if (const gchar *name = mainEnv->GetStringUTFChars(jname, NULL)) {
 409         ret = g_strdup(name);
 410         mainEnv->ReleaseStringUTFChars(jname, name);
 411     }
 412     return ret;
 413 }
 414 
 415 gpointer glass_try_malloc_n(gsize m, gsize n,
 416         gboolean zer0 /* initialized to 0 if true*/) {
 417     if (n > 0 && m > G_MAXSIZE / n) {
 418         return NULL;
 419     }
 420     return (zer0)
 421             ? g_try_malloc0(m * n)
 422             : g_try_malloc(m * n);
 423 }
 424 
 425 /*
 426  * Since we support glib 2.18 we can't use g_try_malloc_n and g_try_malloc0_n
 427  * which was introduced in 2.24.
 428  * glass_try_malloc_n and glass_try_malloc0_n is replacement for those functions
 429  */
 430 gpointer glass_try_malloc0_n(gsize m, gsize n) {
 431     return glass_try_malloc_n(m, n, TRUE);
 432 }
 433 
 434 gpointer glass_try_malloc_n(gsize m, gsize n) {
 435     return glass_try_malloc_n(m, n, FALSE);
 436 }
 437 
 438 gsize get_files_count(gchar **uris) {
 439     if (!uris) {
 440         return 0;
 441     }
 442 
 443     guint size = g_strv_length(uris);
 444     guint files_cnt = 0;
 445 
 446     for (guint i = 0; i < size; ++i) {
 447         if (g_str_has_prefix(uris[i], FILE_PREFIX)) {
 448             files_cnt++;
 449         }
 450     }
 451     return files_cnt;
 452 }
 453 
 454 // Note: passed uris will be freed by this function
 455 jobject uris_to_java(JNIEnv *env, gchar **uris, gboolean files) {
 456     if (uris == NULL) {
 457         return NULL;
 458     }
 459 
 460     jobject result = NULL;
 461 
 462     guint size = g_strv_length(uris);
 463     guint files_cnt = get_files_count(uris);
 464 
 465     if (files) {
 466         if (files_cnt) {
 467             result = env->NewObjectArray(files_cnt, jStringCls, NULL);
 468             check_and_clear_exception(env);
 469 
 470             for (gsize i = 0; i < size; ++i) {
 471                 if (g_str_has_prefix(uris[i], FILE_PREFIX)) {
 472                     gchar* path = g_filename_from_uri(uris[i], NULL, NULL);
 473                     jstring str = env->NewStringUTF(path);
 474                     check_and_clear_exception(env);
 475                     env->SetObjectArrayElement((jobjectArray) result, i, str);
 476                     check_and_clear_exception(env);
 477                     g_free(path);
 478                 }
 479             }
 480         }
 481     } else if (size - files_cnt) {
 482         GString* str = g_string_new(NULL); //http://www.ietf.org/rfc/rfc2483.txt
 483 
 484         for (guint i = 0; i < size; ++i) {
 485             if (!g_str_has_prefix(uris[i], FILE_PREFIX)
 486                     && !g_str_has_prefix(uris[i], URI_LIST_COMMENT_PREFIX)) {
 487                 g_string_append(str, uris[i]);
 488                 g_string_append(str, URI_LIST_LINE_BREAK);
 489             }
 490         }
 491 
 492         if (str->len > 2) {
 493             g_string_erase(str, str->len - 2, 2);
 494         }
 495 
 496         result = env->NewStringUTF(str->str);
 497         check_and_clear_exception(env);
 498 
 499         g_string_free(str, TRUE);
 500     }
 501     g_strfreev(uris);
 502     return result;
 503 }