2 /*======================================================================
3 FILE: jlibical_utils_cxx.cpp
4 CREATOR: Srinivasa Boppana/George Norman
5 (C) COPYRIGHT 2002, Critical Path
6 ======================================================================*/
8 #ifndef JLIBICAL_UTILS_CXX_H
9 #include "jlibical_utils_cxx.h"
12 #ifndef JLIBICAL_CONSTS_CXX_H
13 #include "jlibical_consts_cxx.h"
16 #ifndef ICALPARAMETER_CXX_H
17 #include "icalparameter_cxx.h"
20 #ifndef VCOMPONENT_CXX_H
21 #include "vcomponent.h"
24 #ifndef ICALPROPERTY_CXX_H
25 #include "icalproperty_cxx.h"
28 #ifndef ICALVALUE_CXX_H
29 #include "icalvalue_cxx.h"
32 #ifndef _jni_ICalTimeType_H
33 #include "jniICalTimeType_cxx.h"
36 #ifndef _jni_ICalTriggerType_H
37 #include "jniICalTriggerType_cxx.h"
40 #ifndef _jni_ICalDurationType_H
41 #include "jniICalDurationType_cxx.h"
44 #ifndef _jni_ICalRecurrenceType_H
45 #include "jniICalRecurrenceType_cxx.h"
48 #ifndef _jni_ICalPeriodType_H
49 #include "jniICalPeriodType_cxx.h"
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)
58 jclass jcls = env->GetObjectClass(surrogate);
59 jfieldID fid = env->GetFieldID(jcls,"m_Obj","J");
63 // this should never happen.
64 throwException( env, JLIBICAL_ERR_CLIENT_INTERNAL );
68 result = (void*)env->GetLongField(surrogate,fid);
71 // the proxy object (java) has no subject (c++ object)
72 throwException( env, JLIBICAL_ERR_CLIENT_INTERNAL );
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)
85 jclass jcls = env->GetObjectClass(surrogate);
86 jfieldID fid = env->GetFieldID(jcls,"m_Obj","J");
90 throwException( env, JLIBICAL_ERR_CLIENT_INTERNAL );
94 env->SetLongField(surrogate,fid,(long)subject);
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)
103 VComponent* result = (VComponent*)(getCObjectPtr(env,surrogateComponent));
107 throwException(env, exceptionType );
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)
119 ICalProperty* result = (ICalProperty*)(getCObjectPtr(env,surrogateProperty));
123 throwException(env, exceptionType );
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)
135 ICalValue* result = (ICalValue*)(getCObjectPtr(env,surrogateValue));
139 throwException( env, exceptionType );
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)
151 ICalParameter* result = (ICalParameter*)(getCObjectPtr(env,surrogateParameter));
155 throwException( env, exceptionType );
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)
172 if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALTIMETYPE)))
174 jni_GetAll_from_ICalTimeType(dest, env, src);
179 throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
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)
196 if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALTRIGGERTYPE)))
198 jni_GetAll_from_ICalTriggerType(dest, env, src);
203 throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
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)
220 if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALDURATIONTYPE)))
222 jni_GetAll_from_ICalDurationType(dest, env, src);
227 throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
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)
244 if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALRECURRENCETYPE)))
246 jni_GetAll_from_ICalRecurrenceType(dest, env, src);
251 throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
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)
268 if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALPERIODTYPE)))
270 jni_GetAll_from_ICalPeriodType(dest, env, src);
275 throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
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)
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;
302 return(doCreateNewSurrogate(env,env->FindClass(classname),(jlong)subject));
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)
312 return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALPROPERTY),(jlong)subject));
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)
322 return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALVALUE),(jlong)subject));
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)
332 return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALPARAMETER),(jlong)subject));
335 //-------------------------------------------------------
336 // Create a new ICalTimeType object from the given source struct.
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)
343 return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALTIMETYPE),(jlong)source));
346 //-------------------------------------------------------
347 // Create a new ICalTriggerType object from the given source struct.
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)
354 return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALTRIGGERTYPE),(jlong)source));
357 //-------------------------------------------------------
358 // Create a new ICalDurationType object from the given source struct.
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)
365 return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALDURATIONTYPE),(jlong)source));
368 //-------------------------------------------------------
369 // Create a new ICalRecurrenceType object from the given source struct.
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)
376 return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALRECURRENCETYPE),(jlong)source));
379 //-------------------------------------------------------
380 // Create a new ICalPeriodType object from the given source struct.
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)
387 return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALPERIODTYPE),(jlong)source));
390 //-------------------------------------------------------
391 // Creat a new surrogate of the given type for the given subject.
392 //-------------------------------------------------------
393 jobject doCreateNewSurrogate(JNIEnv *env, jclass surrogateClass, jlong subject)
395 jobject result = NULL;
399 jmethodID jconstructorID = env->GetMethodID(surrogateClass, "<init>", "(J)V");
401 result = env->NewObject(surrogateClass, jconstructorID, subject);
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)
415 jclass jexceptionClass;
416 jthrowable jexceptionObj;
417 jmethodID jconstructorID;
418 const char* exClassName;
420 if (env->ExceptionOccurred())
427 case JLIBICAL_ERR_NETWORK:
428 exClassName = "net.cp.jlibical/JLCNetworkException";
431 case JLIBICAL_ERR_SERVER_INTERNAL:
432 exClassName = "net.cp.jlibical/JLCServerInternalException";
435 case JLIBICAL_ERR_CLIENT_INTERNAL:
436 exClassName = "net.cp.jlibical/JLCClientInternalException";
439 case JLIBICAL_ERR_ILLEGAL_ARGUMENT:
440 exClassName = "net.cp.jlibical/JLCIllegalArgumentException";
443 case JLIBICAL_ERR_API_NOT_INITED:
444 exClassName = "net.cp.jlibical/JLCNotInitedException";
447 case JLIBICAL_ERR_HOST_INVALID:
448 exClassName = "net.cp.jlibical/JLCHostInvalidException";
452 exClassName = "net.cp.jlibical/JLCClientInternalException";
453 printf("*** JLIBICAL JNI throwException: unknown error code: %d\n", cpErr );
457 env->ThrowNew(env->FindClass(exClassName),"");