bump to 1.0.0 and clean up spec file
[platform/upstream/libical.git] / src / java / jlibical_utils_cxx.cpp
1 /* -*- Mode: C -*- */
2 /*======================================================================
3  FILE: jlibical_utils_cxx.cpp
4  CREATOR: Srinivasa Boppana/George Norman
5  (C) COPYRIGHT 2002, Critical Path
6 ======================================================================*/
7
8 #ifndef JLIBICAL_UTILS_CXX_H
9 #include "jlibical_utils_cxx.h"
10 #endif
11
12 #ifndef JLIBICAL_CONSTS_CXX_H
13 #include "jlibical_consts_cxx.h"
14 #endif
15
16 #ifndef ICALPARAMETER_CXX_H
17 #include "icalparameter_cxx.h"
18 #endif
19
20 #ifndef VCOMPONENT_CXX_H
21 #include "vcomponent.h"
22 #endif
23
24 #ifndef ICALPROPERTY_CXX_H
25 #include "icalproperty_cxx.h"
26 #endif
27
28 #ifndef ICALVALUE_CXX_H
29 #include "icalvalue_cxx.h"
30 #endif
31
32 #ifndef _jni_ICalTimeType_H
33 #include "jniICalTimeType_cxx.h"
34 #endif
35
36 #ifndef _jni_ICalTriggerType_H
37 #include "jniICalTriggerType_cxx.h"
38 #endif
39
40 #ifndef _jni_ICalDurationType_H
41 #include "jniICalDurationType_cxx.h"
42 #endif
43
44 #ifndef _jni_ICalRecurrenceType_H
45 #include "jniICalRecurrenceType_cxx.h"
46 #endif
47
48 #ifndef _jni_ICalPeriodType_H
49 #include "jniICalPeriodType_cxx.h"
50 #endif
51
52 //-------------------------------------------------------
53 // Returns a pointer to the subject (a c++ object) for the given surrogate (a java object)
54 //-------------------------------------------------------
55 void* getCObjectPtr(JNIEnv *env, jobject surrogate)
56 {
57         void* result = 0;
58     jclass jcls = env->GetObjectClass(surrogate);
59     jfieldID fid = env->GetFieldID(jcls,"m_Obj","J");
60
61     if (fid == NULL)
62     {
63                 // this should never happen.
64         throwException( env, JLIBICAL_ERR_CLIENT_INTERNAL );
65         return(NULL);
66     }
67
68         result = (void*)env->GetLongField(surrogate,fid);
69     if (result == NULL)
70         {
71                 // the proxy object (java) has no subject (c++ object)
72         throwException( env, JLIBICAL_ERR_CLIENT_INTERNAL );
73         return(NULL);
74         }
75
76         return(result);
77 }
78
79 //-------------------------------------------------------
80 // Set the subject (a c++ object) for the given surrogate (a java object).
81 // Throws exception if the m_Obj field can not be found.
82 //-------------------------------------------------------
83 void setCObjectPtr(JNIEnv *env, jobject surrogate, void* subject)
84 {
85     jclass jcls = env->GetObjectClass(surrogate);
86     jfieldID fid = env->GetFieldID(jcls,"m_Obj","J");
87
88     if (fid == NULL)
89     {
90         throwException( env, JLIBICAL_ERR_CLIENT_INTERNAL );
91         return;
92     }
93
94         env->SetLongField(surrogate,fid,(long)subject);
95 }
96
97 //-------------------------------------------------------
98 // Return the pointer to the subject (as an VComponent*) from the given surrogate.
99 // If the subject is not an VComponent type, or if the subject is NULL, then return NULL.
100 //-------------------------------------------------------
101 VComponent* getSubjectAsVComponent(JNIEnv *env, jobject surrogateComponent, int exceptionType)
102 {
103         VComponent* result = (VComponent*)(getCObjectPtr(env,surrogateComponent));
104
105         if (result == NULL)
106         {
107         throwException(env, exceptionType );
108         }
109
110         return(result);
111 }
112
113 //-------------------------------------------------------
114 // Return the pointer to the subject (as an ICalProperty*) from the given surrogate.
115 // If the subject is not an ICalProperty type, or if the subject is NULL, then return NULL.
116 //-------------------------------------------------------
117 ICalProperty* getSubjectAsICalProperty(JNIEnv *env, jobject surrogateProperty, int exceptionType)
118 {
119         ICalProperty* result = (ICalProperty*)(getCObjectPtr(env,surrogateProperty));
120
121         if (result == NULL)
122         {
123         throwException(env, exceptionType );
124         }
125
126         return(result);
127 }
128
129 //-------------------------------------------------------
130 // Return the pointer to the subject (as an ICalValue*) from the given surrogate.
131 // If the subject is not an ICalValue type, or if the subject is NULL, then return NULL.
132 //-------------------------------------------------------
133 ICalValue* getSubjectAsICalValue(JNIEnv *env, jobject surrogateValue, int exceptionType)
134 {
135         ICalValue* result = (ICalValue*)(getCObjectPtr(env,surrogateValue));
136
137         if (result == NULL)
138         {
139         throwException( env, exceptionType );
140         }
141
142         return(result);
143 }
144
145 //-------------------------------------------------------
146 // Return the pointer to the subject (as an ICalParameter*) from the given surrogate.
147 // If the subject is not an ICalParameter type, or if the subject is NULL, then return NULL.
148 //-------------------------------------------------------
149 ICalParameter* getSubjectAsICalParameter(JNIEnv *env, jobject surrogateParameter, int exceptionType)
150 {
151         ICalParameter* result = (ICalParameter*)(getCObjectPtr(env,surrogateParameter));
152
153         if (result == NULL)
154         {
155         throwException( env, exceptionType );
156         }
157
158         return(result);
159 }
160
161 //-------------------------------------------------------
162 // Copy the data from the src (a java ICalTimeType object)
163 // to the dest (a c struct icaltimetype*).
164 // Returns true if success.  False if exception is thrown:
165 //      - the src java object is not an ICalTimeType type
166 //      - the dest c struct is null.
167 //-------------------------------------------------------
168 bool copyObjToicaltimetype(JNIEnv *env, jobject src, icaltimetype* dest)
169 {
170         bool result = false;
171
172         if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALTIMETYPE)))
173         {
174                 jni_GetAll_from_ICalTimeType(dest, env, src);
175                 result = true;
176         }
177         else
178         {
179         throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
180         }
181
182         return(result);
183 }
184
185 //-------------------------------------------------------
186 // Copy the data from the src (a java ICalTriggerType object)
187 // to the dest (a c struct icaltriggertype*).
188 // Returns true if success.  False if exception is thrown:
189 //      - the src java object is not an ICalTriggerType type
190 //      - the dest c struct is null.
191 //-------------------------------------------------------
192 bool copyObjToicaltriggertype(JNIEnv *env, jobject src, icaltriggertype* dest)
193 {
194         bool result = false;
195
196         if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALTRIGGERTYPE)))
197         {
198                 jni_GetAll_from_ICalTriggerType(dest, env, src);
199                 result = true;
200         }
201         else
202         {
203         throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
204         }
205
206         return(result);
207 }
208
209 //-------------------------------------------------------
210 // Copy the data from the src (a java ICalDurationType object)
211 // to the dest (a c struct icaldurationtype*).
212 // Returns true if success.  False if exception is thrown:
213 //      - the src java object is not an ICalDurationType type
214 //      - the dest c struct is null.
215 //-------------------------------------------------------
216 bool copyObjToicaldurationtype(JNIEnv *env, jobject src, icaldurationtype* dest)
217 {
218         bool result = false;
219
220         if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALDURATIONTYPE)))
221         {
222                 jni_GetAll_from_ICalDurationType(dest, env, src);
223                 result = true;
224         }
225         else
226         {
227         throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
228         }
229
230         return(result);
231 }
232
233 //-------------------------------------------------------
234 // Copy the data from the src (a java ICalRecurrenceType object)
235 // to the dest (a c struct icalrecurrencetype*).
236 // Returns true if success.  False if exception is thrown:
237 //      - the src java object is not an ICalRecurrenceType type
238 //      - the dest c struct is null.
239 //-------------------------------------------------------
240 bool copyObjToicalrecurrencetype(JNIEnv *env, jobject src, icalrecurrencetype* dest)
241 {
242         bool result = false;
243
244         if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALRECURRENCETYPE)))
245         {
246                 jni_GetAll_from_ICalRecurrenceType(dest, env, src);
247                 result = true;
248         }
249         else
250         {
251         throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
252         }
253
254         return(result);
255 }
256
257 //-------------------------------------------------------
258 // Copy the data from the src (a java ICalPeriodType object)
259 // to the dest (a c struct icalperiodtype*).
260 // Returns true if success.  False if exception is thrown:
261 //      - the src java object is not an ICalPeriodType type
262 //      - the dest c struct is null.
263 //-------------------------------------------------------
264 bool copyObjToicalperiodtype(JNIEnv *env, jobject src, icalperiodtype* dest)
265 {
266         bool result = false;
267
268         if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALPERIODTYPE)))
269         {
270                 jni_GetAll_from_ICalPeriodType(dest, env, src);
271                 result = true;
272         }
273         else
274         {
275         throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
276         }
277
278         return(result);
279 }
280
281 //-------------------------------------------------------
282 // Create a new VComponent surrogate for given subject.
283 // If subject is NULL, then returns NULL (will not create a
284 // surrogate to a NULL subject);
285 //-------------------------------------------------------
286 jobject createNewVComponentSurrogate(JNIEnv *env, VComponent* subject)
287 {
288         char* classname = JLIBICAL_CLASS_VCOMPONENT;
289         if (dynamic_cast<VAlarm*>(subject))
290                 classname = JLIBICAL_CLASS_VALARM;
291         else if (dynamic_cast<VCalendar*>(subject))
292                 classname = JLIBICAL_CLASS_VCALENDAR;
293         else if (dynamic_cast<VEvent*>(subject))
294                 classname = JLIBICAL_CLASS_VEVENT;
295         else if (dynamic_cast<VQuery*>(subject))
296                 classname = JLIBICAL_CLASS_VQUERY;
297         else if (dynamic_cast<VToDo*>(subject))
298                 classname = JLIBICAL_CLASS_VTODO;
299         else if (dynamic_cast<VAgenda*>(subject))
300                 classname = JLIBICAL_CLASS_VAGENDA;
301
302         return(doCreateNewSurrogate(env,env->FindClass(classname),(jlong)subject));
303 }
304
305 //-------------------------------------------------------
306 // Create a new ICalProperty surrogate for given subject.
307 // If subject is NULL, then returns NULL (will not create a
308 // surrogate to a NULL subject);
309 //-------------------------------------------------------
310 jobject createNewICalPropertySurrogate(JNIEnv *env, ICalProperty* subject)
311 {
312         return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALPROPERTY),(jlong)subject));
313 }
314
315 //-------------------------------------------------------
316 // Create a new ICalValue surrogate for given subject.
317 // If subject is NULL, then returns NULL (will not create a
318 // surrogate to a NULL subject);
319 //-------------------------------------------------------
320 jobject createNewICalValueSurrogate(JNIEnv *env, ICalValue* subject)
321 {
322         return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALVALUE),(jlong)subject));
323 }
324
325 //-------------------------------------------------------
326 // Create a new ICalParameter surrogate for given subject.
327 // If subject is NULL, then returns NULL (will not create a
328 // surrogate to a NULL subject);
329 //-------------------------------------------------------
330 jobject createNewICalParameterSurrogate(JNIEnv *env, ICalParameter* subject)
331 {
332         return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALPARAMETER),(jlong)subject));
333 }
334
335 //-------------------------------------------------------
336 // Create a new ICalTimeType object from the given source struct.
337 // A copy is made,  .
338 // If source is NULL, then returns NULL (will not create an
339 // object to a NULL source);
340 //-------------------------------------------------------
341 jobject createNewICalTimeType(JNIEnv *env, icaltimetype* source)
342 {
343         return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALTIMETYPE),(jlong)source));
344 }
345
346 //-------------------------------------------------------
347 // Create a new ICalTriggerType object from the given source struct.
348 // A copy is made,  .
349 // If source is NULL, then returns NULL (will not create an
350 // object to a NULL source);
351 //-------------------------------------------------------
352 jobject createNewICalTriggerType(JNIEnv *env, icaltriggertype* source)
353 {
354         return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALTRIGGERTYPE),(jlong)source));
355 }
356
357 //-------------------------------------------------------
358 // Create a new ICalDurationType object from the given source struct.
359 // A copy is made,  .
360 // If source is NULL, then returns NULL (will not create an
361 // object to a NULL source);
362 //-------------------------------------------------------
363 jobject createNewICalDurationType(JNIEnv *env, icaldurationtype* source)
364 {
365         return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALDURATIONTYPE),(jlong)source));
366 }
367
368 //-------------------------------------------------------
369 // Create a new ICalRecurrenceType object from the given source struct.
370 // A copy is made,  .
371 // If source is NULL, then returns NULL (will not create an
372 // object to a NULL source);
373 //-------------------------------------------------------
374 jobject createNewICalRecurrenceType(JNIEnv *env, icalrecurrencetype* source)
375 {
376         return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALRECURRENCETYPE),(jlong)source));
377 }
378
379 //-------------------------------------------------------
380 // Create a new ICalPeriodType object from the given source struct.
381 // A copy is made,  .
382 // If source is NULL, then returns NULL (will not create an
383 // object to a NULL source);
384 //-------------------------------------------------------
385 jobject createNewICalPeriodType(JNIEnv *env, icalperiodtype* source)
386 {
387         return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALPERIODTYPE),(jlong)source));
388 }
389
390 //-------------------------------------------------------
391 // Creat a new surrogate of the given type for the given subject.
392 //-------------------------------------------------------
393 jobject doCreateNewSurrogate(JNIEnv *env, jclass surrogateClass, jlong subject)
394 {
395         jobject result = NULL;
396
397         if (subject != NULL)
398         {
399                 jmethodID jconstructorID = env->GetMethodID(surrogateClass, "<init>", "(J)V");
400
401                 result = env->NewObject(surrogateClass, jconstructorID, subject);
402         }
403
404         return(result);
405 }
406
407
408 //-------------------------------------------------------
409 // For the given cpErr, create a new Exception and send it to env.
410 // Note: Throw does not throw anything.  It only sets the state.
411 //              The JVM will check this and throw an exception later.
412 //-------------------------------------------------------
413 void throwException(JNIEnv *env, int cpErr)
414 {
415         jclass jexceptionClass;
416         jthrowable jexceptionObj;
417         jmethodID jconstructorID;
418         const char* exClassName;
419
420         if (env->ExceptionOccurred())
421         {
422                 return;
423         }
424
425     switch ( cpErr )
426     {
427     case JLIBICAL_ERR_NETWORK:
428             exClassName = "net.cp.jlibical/JLCNetworkException";
429             break;
430
431     case JLIBICAL_ERR_SERVER_INTERNAL:
432             exClassName = "net.cp.jlibical/JLCServerInternalException";
433             break;
434
435     case JLIBICAL_ERR_CLIENT_INTERNAL:
436             exClassName = "net.cp.jlibical/JLCClientInternalException";
437             break;
438
439     case JLIBICAL_ERR_ILLEGAL_ARGUMENT:
440             exClassName = "net.cp.jlibical/JLCIllegalArgumentException";
441             break;
442
443     case JLIBICAL_ERR_API_NOT_INITED:
444             exClassName = "net.cp.jlibical/JLCNotInitedException";
445             break;
446
447     case JLIBICAL_ERR_HOST_INVALID:
448             exClassName = "net.cp.jlibical/JLCHostInvalidException";
449             break;
450
451     default:
452             exClassName = "net.cp.jlibical/JLCClientInternalException";
453             printf("*** JLIBICAL JNI throwException: unknown error code: %d\n", cpErr );
454             break;
455     }
456
457     env->ThrowNew(env->FindClass(exClassName),"");
458 }