]> sjero.net Git - linphone/blob - tools/lpc2xml_jni.cc
update ms2 and cleanup dead function
[linphone] / tools / lpc2xml_jni.cc
1 /*
2 xml2lpc_jni.cc
3 Copyright (C) 2013  Belledonne Communications, Grenoble, France
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19
20 #include "my_jni.h"
21 extern "C" {
22 #include "lpc2xml.h"
23 }
24 #ifdef USE_JAVAH
25 #include "lpc2xml_jni.h"
26 #endif
27
28 #include <stdio.h>
29
30 struct jni_lpc2xml_ctx {
31         JNIEnv *env;
32         jobject obj;
33         lpc2xml_context *ctx;
34 };
35
36 static bool update_and_check_context(jni_lpc2xml_ctx *jni_ctx, JNIEnv *env, jobject obj) {
37         if(jni_ctx != NULL && jni_ctx->ctx != NULL) {
38                 jni_ctx->env = env;
39                 jni_ctx->obj = obj; 
40                 return true;
41         }
42         return false;
43 }
44
45 #define LPC2XML_CALLBACK_BUFFER_SIZE  1024
46
47 extern "C" void Java_org_linphone_tools_Lpc2Xml_callback (void *ctx, lpc2xml_log_level level, const char *fmt, va_list list) {
48         jni_lpc2xml_ctx *jni_ctx = (jni_lpc2xml_ctx *)ctx;
49         if(jni_ctx->ctx != NULL) {
50                 JNIEnv *env = jni_ctx->env;
51                 jobject obj = jni_ctx->obj;
52                 
53                 char buffer[LPC2XML_CALLBACK_BUFFER_SIZE];
54                 vsnprintf(buffer, LPC2XML_CALLBACK_BUFFER_SIZE, fmt, list);
55                 jstring javaString = env->NewStringUTF(buffer);
56                 jint javaLevel = level;
57                 my_jni::callVoidMethod<void>(env, obj, "Lpc2Xml", "printLog", "(ILjava/lang/String;)V", javaLevel, javaString);
58         }               
59 }
60
61 extern "C" void Java_org_linphone_tools_Lpc2Xml_init(JNIEnv *env, jobject obj) {
62         jni_lpc2xml_ctx *jni_ctx = new jni_lpc2xml_ctx();
63         jni_ctx->env = env;
64         jni_ctx->obj = obj; 
65         jni_ctx->ctx = lpc2xml_context_new(Java_org_linphone_tools_Lpc2Xml_callback, obj);
66         bool result = my_jni::setLongField<jni_lpc2xml_ctx*>(env, obj, "Lpc2Xml", "internalPtr", jni_ctx);
67         if(!result) {
68                 lpc2xml_context_destroy(jni_ctx->ctx);
69                 delete jni_ctx;
70         }       
71 }
72
73 extern "C" void Java_org_linphone_tools_Lpc2Xml_destroy(JNIEnv *env, jobject obj) {
74         jni_lpc2xml_ctx *jni_ctx = my_jni::getLongField<jni_lpc2xml_ctx*>(env, obj, "Lpc2Xml", "internalPtr");
75         if(jni_ctx != NULL) {
76                 jni_ctx->env = env;
77                 jni_ctx->obj = obj; 
78                 
79                 if(jni_ctx->ctx != NULL) {
80                         lpc2xml_context_destroy(jni_ctx->ctx);
81                 }
82                 delete jni_ctx;
83                 my_jni::setLongField<jni_lpc2xml_ctx*>(env, obj, "Lpc2Xml", "internalPtr", NULL);
84         }
85 }
86
87 extern "C" jint Java_org_linphone_tools_Lpc2Xml_setLpc(JNIEnv *env, jobject obj, jobject javaLpc) {
88         jni_lpc2xml_ctx *jni_ctx = my_jni::getLongField<jni_lpc2xml_ctx*>(env, obj, "Lpc2Xml", "internalPtr");
89         jint ret = -666;
90         if(update_and_check_context(jni_ctx, env, obj)) {
91                 LpConfig *lpc = my_jni::getLongField<LpConfig*>(env, javaLpc, "LpConfigImpl", "nativePtr");
92                 if(lpc != NULL) {
93                         lpc2xml_set_lpc(jni_ctx->ctx, lpc);
94                 } 
95         }
96         return ret;
97 }
98
99 extern "C" jint Java_org_linphone_tools_Lpc2Xml_convertFile(JNIEnv *env, jobject obj, jstring javaFile) {
100         jni_lpc2xml_ctx *jni_ctx = my_jni::getLongField<jni_lpc2xml_ctx*>(env, obj, "Lpc2Xml", "internalPtr");
101         jint ret = -666;
102         if(update_and_check_context(jni_ctx, env, obj)) {
103                 const char *file = env->GetStringUTFChars(javaFile, 0);
104                 ret = lpc2xml_convert_file(jni_ctx->ctx, file);
105                 env->ReleaseStringChars(javaFile, (jchar *)file);
106         }
107         return ret;
108 }
109
110 extern "C" jint Java_org_linphone_tools_Lpc2Xml_convertString(JNIEnv *env, jobject obj, jobject javaStringBuffer) {
111         jni_lpc2xml_ctx *jni_ctx = my_jni::getLongField<jni_lpc2xml_ctx*>(env, obj, "Lpc2Xml", "internalPtr");
112         jint ret = -666;
113         if(update_and_check_context(jni_ctx, env, obj)) {
114                 char *string = NULL;
115                 ret = lpc2xml_convert_string(jni_ctx->ctx, &string);
116                 if(string != NULL) {
117                         jstring javaString = env->NewStringUTF(string);
118                         my_jni::callObjectMethod<jobject>(env, obj, "StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;", javaString);
119                         env->ReleaseStringChars(javaString, (jchar *)string);
120                 }               
121         }
122         return ret;
123 }