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