GConfNativePeer.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* GConfNativePeer.c -- Implements native methods for class GConfNativePeer
  2. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <jni.h>
  34. #include <glib.h>
  35. #include <gdk/gdk.h>
  36. #include <gconf/gconf-client.h>
  37. #include "jcl.h"
  38. #include "gnu_java_util_prefs_gconf_GConfNativePeer.h"
  39. /*
  40. * Cached id, methods and objects
  41. */
  42. /** Reference count */
  43. static int reference_count = 0;
  44. /** GConfEngine backend */
  45. static GConfEngine *engine = NULL;
  46. /** java.util.ArrayList class */
  47. static jclass jlist_class = NULL;
  48. /** java.util.ArrayList constructor id */
  49. static jmethodID jlist_init_id = NULL;
  50. /** ava.util.ArrayList add id */
  51. static jmethodID jlist_add_id = NULL;
  52. /* ***** PRIVATE FUNCTIONS DELCARATION ***** */
  53. /**
  54. * Gets the reference of the default GConfEngine..
  55. * The client reference should be released with g_object_unref after use.
  56. */
  57. static void init_gconf (void);
  58. /**
  59. * Throws a new runtime exception after a failure, with the given message.
  60. */
  61. static void throw_exception (JNIEnv * env, const char *msg);
  62. /**
  63. * Throws the given exception after a failure, with the given message.
  64. */
  65. static void
  66. throw_exception_by_name (JNIEnv * env, const char *name, const char *msg);
  67. /**
  68. * Return a reference to a java.util.ArrayList class.
  69. */
  70. static gboolean set_jlist_class (JNIEnv * env);
  71. /**
  72. * Builds a new reference to a new java.util.ArrayList instace.
  73. * The instance should be freed by the caller after use.
  74. */
  75. static jclass get_jlist_reference (JNIEnv * env, jclass jlist_class);
  76. /* ***** END: PRIVATE FUNCTIONS DELCARATION ***** */
  77. /* ***** NATIVE FUNCTIONS ***** */
  78. /*
  79. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  80. * Method: init_class
  81. * Signature: ()V
  82. */
  83. JNIEXPORT void JNICALL
  84. Java_gnu_java_util_prefs_gconf_GConfNativePeer_init_1class
  85. (JNIEnv *env, jclass clazz)
  86. {
  87. if (reference_count == 0)
  88. {
  89. Java_gnu_java_util_prefs_gconf_GConfNativePeer_init_1id_1cache
  90. (env, clazz);
  91. return;
  92. }
  93. reference_count++;
  94. }
  95. /*
  96. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  97. * Method: init_id_chache
  98. * Signature: ()V
  99. */
  100. JNIEXPORT void JNICALL
  101. Java_gnu_java_util_prefs_gconf_GConfNativePeer_init_1id_1cache
  102. (JNIEnv *env, jclass clazz __attribute__ ((unused)))
  103. {
  104. reference_count++;
  105. init_gconf ();
  106. /* if engine is null, there is probably an out of memory */
  107. if (engine == NULL)
  108. {
  109. /* release the string and throw a runtime exception */
  110. throw_exception (env,
  111. "Unable to initialize GConfEngine in native code\n");
  112. return;
  113. }
  114. /* ***** java.util.ArrayList ***** */
  115. if (set_jlist_class (env) == FALSE)
  116. {
  117. throw_exception (env,
  118. "Unable to get valid reference to java.util.List in native code\n");
  119. return;
  120. }
  121. }
  122. /*
  123. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  124. * Method: gconf_all_keys
  125. * Signature: (Ljava/lang/String;)Ljava/util/List;
  126. */
  127. JNIEXPORT jobject JNICALL
  128. Java_gnu_java_util_prefs_gconf_GConfNativePeer_gconf_1all_1keys
  129. (JNIEnv *env, jclass clazz __attribute__ ((unused)), jstring node)
  130. {
  131. /* TODO: check all the calls to gdk_threads_enter/leave */
  132. const char *dir = NULL;
  133. const char *_val = NULL;
  134. const char *_val_unescaped = NULL;
  135. GError *err = NULL;
  136. GSList *entries = NULL;
  137. GSList *tmp;
  138. /* java.util.ArrayList */
  139. jobject jlist = NULL;
  140. dir = JCL_jstring_to_cstring (env, node);
  141. if (dir == NULL)
  142. {
  143. return NULL;
  144. }
  145. entries = gconf_engine_all_entries (engine, dir, &err);
  146. if (err != NULL)
  147. {
  148. throw_exception_by_name (env, "java/util/prefs/BackingStoreException",
  149. err->message);
  150. g_error_free (err);
  151. err = NULL;
  152. JCL_free_cstring (env, node, dir);
  153. return NULL;
  154. }
  155. jlist = get_jlist_reference (env, jlist_class);
  156. if (jlist == NULL)
  157. {
  158. throw_exception_by_name (env, "java/util/prefs/BackingStoreException",
  159. "Unable to get java.util.List reference in native code\n");
  160. JCL_free_cstring (env, node, dir);
  161. g_slist_foreach (entries, (GFunc) gconf_entry_free, NULL);
  162. g_slist_free (entries);
  163. return NULL;
  164. }
  165. tmp = entries;
  166. while (tmp != NULL)
  167. {
  168. _val = gconf_entry_get_key (tmp->data);
  169. _val = strrchr (_val, '/');
  170. ++_val;
  171. _val_unescaped = gconf_unescape_key (_val, strlen (_val));
  172. (*env)->CallBooleanMethod (env, jlist, jlist_add_id,
  173. (*env)->NewStringUTF (env, _val_unescaped));
  174. tmp = g_slist_next (tmp);
  175. g_free ((gpointer) _val_unescaped);
  176. }
  177. /* clean up things */
  178. JCL_free_cstring (env, node, dir);
  179. g_slist_foreach (entries, (GFunc) gconf_entry_free, NULL);
  180. g_slist_free (entries);
  181. return jlist;
  182. }
  183. /*
  184. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  185. * Method: gconf_all_nodes
  186. * Signature: (Ljava/lang/String;)Ljava/util/List;
  187. */
  188. JNIEXPORT jobject JNICALL
  189. Java_gnu_java_util_prefs_gconf_GConfNativePeer_gconf_1all_1nodes
  190. (JNIEnv *env, jclass clazz __attribute__ ((unused)), jstring node)
  191. {
  192. const char *dir = NULL;
  193. const char *_val = NULL;
  194. const char *_val_unescaped = NULL;
  195. GError *err = NULL;
  196. GSList *entries = NULL;
  197. GSList *tmp;
  198. /* java.util.ArrayList */
  199. jobject jlist = NULL;
  200. dir = JCL_jstring_to_cstring (env, node);
  201. if (dir == NULL)
  202. {
  203. return NULL;
  204. }
  205. entries = gconf_engine_all_dirs (engine, dir, &err);
  206. if (err != NULL)
  207. {
  208. throw_exception_by_name (env, "java/util/prefs/BackingStoreException",
  209. err->message);
  210. g_error_free (err);
  211. err = NULL;
  212. JCL_free_cstring (env, node, dir);
  213. return NULL;
  214. }
  215. jlist = get_jlist_reference (env, jlist_class);
  216. if (jlist == NULL)
  217. {
  218. throw_exception_by_name (env, "java/util/prefs/BackingStoreException",
  219. "Unable to get java.util.List reference in native code\n");
  220. JCL_free_cstring (env, node, dir);
  221. g_slist_foreach (entries, (GFunc) gconf_entry_free, NULL);
  222. g_slist_free (entries);
  223. return NULL;
  224. }
  225. tmp = entries;
  226. while (tmp != NULL)
  227. {
  228. _val = tmp->data;
  229. _val = strrchr (_val, '/');
  230. ++_val;
  231. _val_unescaped = gconf_unescape_key (_val, strlen (_val));
  232. (*env)->CallBooleanMethod (env, jlist, jlist_add_id,
  233. (*env)->NewStringUTF (env, _val_unescaped));
  234. tmp = g_slist_next (tmp);
  235. g_free ((gpointer) _val_unescaped);
  236. }
  237. /* clean up things */
  238. JCL_free_cstring (env, node, dir);
  239. g_slist_foreach (entries, (GFunc) gconf_entry_free, NULL);
  240. g_slist_free (entries);
  241. return jlist;
  242. }
  243. /*
  244. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  245. * Method: gconf_suggest_sync
  246. * Signature: ()V
  247. */
  248. JNIEXPORT void JNICALL
  249. Java_gnu_java_util_prefs_gconf_GConfNativePeer_gconf_1suggest_1sync
  250. (JNIEnv *env, jclass clazz __attribute__ ((unused)))
  251. {
  252. GError *err = NULL;
  253. gconf_engine_suggest_sync (engine, &err);
  254. if (err != NULL)
  255. {
  256. throw_exception_by_name (env, "java/util/prefs/BackingStoreException",
  257. err->message);
  258. g_error_free (err);
  259. err = NULL;
  260. }
  261. }
  262. /*
  263. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  264. * Method: gconf_unset
  265. * Signature: (Ljava/lang/String;)Z
  266. */
  267. JNIEXPORT jboolean JNICALL
  268. Java_gnu_java_util_prefs_gconf_GConfNativePeer_gconf_1unset
  269. (JNIEnv *env, jclass clazz __attribute__ ((unused)), jstring key)
  270. {
  271. const char *_key = NULL;
  272. gboolean result = JNI_FALSE;
  273. GError *err = NULL;
  274. _key = JCL_jstring_to_cstring (env, key);
  275. if (_key == NULL)
  276. {
  277. return JNI_FALSE;
  278. }
  279. result = gconf_engine_unset (engine, _key, &err);
  280. if (err != NULL)
  281. {
  282. result = JNI_FALSE;
  283. g_error_free (err);
  284. err = NULL;
  285. }
  286. JCL_free_cstring (env, key, _key);
  287. return result;
  288. }
  289. /*
  290. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  291. * Method: gconf_get_string
  292. * Signature: (Ljava/lang/String;)Ljava/lang/String;
  293. */
  294. JNIEXPORT jstring JNICALL
  295. Java_gnu_java_util_prefs_gconf_GConfNativePeer_gconf_1get_1string
  296. (JNIEnv *env, jclass clazz __attribute__ ((unused)), jstring key)
  297. {
  298. const char *_key = NULL;
  299. const char *_value = NULL;
  300. GError *err = NULL;
  301. jstring result = NULL;
  302. _key = JCL_jstring_to_cstring (env, key);
  303. if (_key == NULL)
  304. {
  305. return NULL;
  306. }
  307. _value = gconf_engine_get_string (engine, _key, &err);
  308. JCL_free_cstring (env, key, _key);
  309. if (err != NULL)
  310. {
  311. /* just in case */
  312. if (_value != NULL) g_free ((gpointer) _value);
  313. g_error_free (err);
  314. err = NULL;
  315. return NULL;
  316. }
  317. /* Even if Gconf reported no error it is possible that NULL was returned */
  318. /* and it should be prevented to create a Java string from that value. */
  319. if (_value != NULL)
  320. {
  321. result = (*env)->NewStringUTF (env, _value);
  322. g_free ((gpointer) _value);
  323. }
  324. gconf_engine_suggest_sync (engine, NULL);
  325. return result;
  326. }
  327. /*
  328. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  329. * Method: gconf_set_string
  330. * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
  331. */
  332. JNIEXPORT jboolean JNICALL
  333. Java_gnu_java_util_prefs_gconf_GConfNativePeer_gconf_1set_1string
  334. (JNIEnv *env, jclass clazz __attribute__ ((unused)),
  335. jstring key, jstring value)
  336. {
  337. const char *_key = NULL;
  338. const char *_value = NULL;
  339. GError *err = NULL;
  340. gboolean result = JNI_FALSE;
  341. /* load an UTF string from the virtual machine. */
  342. _key = JCL_jstring_to_cstring (env, key);
  343. _value = JCL_jstring_to_cstring (env, value);
  344. if (_key == NULL || _value == NULL)
  345. {
  346. return JNI_FALSE;
  347. }
  348. result = gconf_engine_set_string (engine, _key, _value, &err);
  349. if (err != NULL)
  350. {
  351. g_error_free (err);
  352. err = NULL;
  353. result = JNI_FALSE;
  354. }
  355. JCL_free_cstring (env, key, _key);
  356. JCL_free_cstring (env, value, _value);
  357. return (jboolean) result;
  358. }
  359. /*
  360. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  361. * Method: gconf_dir_exists
  362. * Signature: (Ljava/lang/String;)Z
  363. */
  364. JNIEXPORT jboolean JNICALL
  365. Java_gnu_java_util_prefs_gconf_GConfNativePeer_gconf_1dir_1exists
  366. (JNIEnv *env, jclass clazz __attribute__ ((unused)), jstring node)
  367. {
  368. const char *dir = NULL;
  369. GError *err = NULL;
  370. jboolean value = JNI_FALSE;
  371. dir = JCL_jstring_to_cstring (env, node);
  372. if (dir == NULL)
  373. return value;
  374. /* on error return false */
  375. value = gconf_engine_dir_exists (engine, dir, &err);
  376. if (err != NULL)
  377. value = JNI_FALSE;
  378. JCL_free_cstring (env, node, dir);
  379. return value;
  380. }
  381. /*
  382. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  383. * Method: finalize_class
  384. * Signature: ()V
  385. */
  386. JNIEXPORT void JNICALL
  387. Java_gnu_java_util_prefs_gconf_GConfNativePeer_finalize_1class
  388. (JNIEnv *env, jclass clazz __attribute__ ((unused)))
  389. {
  390. if (reference_count == 0)
  391. {
  392. /* last reference, free all resources and return */
  393. g_object_unref (G_OBJECT (engine));
  394. (*env)->DeleteGlobalRef (env, jlist_class);
  395. jlist_class = NULL;
  396. jlist_init_id = NULL;
  397. jlist_add_id = NULL;
  398. return;
  399. }
  400. reference_count--;
  401. }
  402. /*
  403. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  404. * Method: Java_gnu_java_util_prefs_gconf_GConfNativePeer_gconf_1escape_1key
  405. * Signature: (Ljava/lang/String;)Z
  406. */
  407. JNIEXPORT jstring JNICALL
  408. Java_gnu_java_util_prefs_gconf_GConfNativePeer_gconf_1escape_1key
  409. (JNIEnv *env, jclass clazz __attribute__ ((unused)), jstring plain)
  410. {
  411. const char *escaped = NULL;
  412. const char *_plain = NULL;
  413. jstring result = NULL;
  414. _plain = JCL_jstring_to_cstring (env, plain);
  415. if (_plain == NULL)
  416. {
  417. return NULL;
  418. }
  419. escaped = gconf_escape_key (_plain, strlen (_plain));
  420. JCL_free_cstring (env, plain, _plain);
  421. /* check for NULL, if so prevent string creation */
  422. if (escaped != NULL)
  423. {
  424. result = (*env)->NewStringUTF (env, escaped);
  425. g_free ((gpointer) escaped);
  426. }
  427. return result;
  428. }
  429. /*
  430. * Class: gnu_java_util_prefs_gconf_GConfNativePeer
  431. * Method: Java_gnu_java_util_prefs_gconf_GConfNativePeer_gconf_1unescape_1key
  432. * Signature: (Ljava/lang/String;)Z
  433. */
  434. JNIEXPORT jstring JNICALL
  435. Java_gnu_java_util_prefs_gconf_GConfNativePeer_gconf_1unescape_1key
  436. (JNIEnv *env, jclass clazz __attribute__ ((unused)), jstring escaped)
  437. {
  438. const char *plain = NULL;
  439. const char *_escaped = NULL;
  440. jstring result = NULL;
  441. _escaped = JCL_jstring_to_cstring (env, escaped);
  442. if (_escaped == NULL)
  443. {
  444. return NULL;
  445. }
  446. plain = gconf_unescape_key (_escaped, strlen (_escaped));
  447. JCL_free_cstring (env, escaped, _escaped);
  448. /* check for NULL, if so prevent string creation */
  449. if (plain != NULL)
  450. {
  451. result = (*env)->NewStringUTF (env, plain);
  452. g_free ((gpointer) plain);
  453. }
  454. return result;
  455. }
  456. /* ***** END: NATIVE FUNCTIONS ***** */
  457. /* ***** PRIVATE FUNCTIONS IMPLEMENTATION ***** */
  458. static void throw_exception (JNIEnv *env, const char *msg)
  459. {
  460. throw_exception_by_name (env, "java/lang/RuntimeException", msg);
  461. }
  462. static void
  463. throw_exception_by_name (JNIEnv *env, const char *name, const char *msg)
  464. {
  465. JCL_ThrowException (env, name, msg);
  466. }
  467. static void init_gconf (void)
  468. {
  469. engine = gconf_engine_get_default ();
  470. }
  471. static gboolean set_jlist_class (JNIEnv *env)
  472. {
  473. jclass local_jlist_class = NULL;
  474. /* gets a reference to the ArrayList class */
  475. local_jlist_class = JCL_FindClass (env, "java/util/ArrayList");
  476. if (local_jlist_class == NULL)
  477. {
  478. return FALSE;
  479. }
  480. jlist_class = (*env)->NewGlobalRef (env, local_jlist_class);
  481. (*env)->DeleteLocalRef (env, local_jlist_class);
  482. if (jlist_class == NULL)
  483. {
  484. return FALSE;
  485. }
  486. /* and initialize it */
  487. jlist_init_id = (*env)->GetMethodID (env, jlist_class, "<init>", "()V");
  488. if (jlist_init_id == NULL)
  489. {
  490. return FALSE;
  491. }
  492. jlist_add_id = (*env)->GetMethodID (env, jlist_class, "add",
  493. "(Ljava/lang/Object;)Z");
  494. if (jlist_add_id == NULL)
  495. {
  496. return FALSE;
  497. }
  498. return TRUE;
  499. }
  500. static jobject get_jlist_reference (JNIEnv *env, jclass jlist_class)
  501. {
  502. return (*env)->NewObject (env, jlist_class, jlist_init_id);
  503. }
  504. /* ***** END: PRIVATE FUNCTIONS IMPLEMENTATION ***** */