summaryrefslogtreecommitdiff
path: root/src/java/jlibical_utils_cxx.cpp
blob: e52e33f4e567d2acd50412a6c6b8ad531e31af57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*======================================================================
 FILE: jlibical_utils_cxx.cpp
 CREATOR: Srinivasa Boppana/George Norman

 SPDX-FileCopyrightText: 2002, Critical Path

 SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0

======================================================================*/

#ifndef JLIBICAL_UTILS_CXX_H
#include "jlibical_utils_cxx.h"
#endif

#ifndef JLIBICAL_CONSTS_CXX_H
#include "jlibical_consts_cxx.h"
#endif

#ifndef ICALPARAMETER_CXX_H
#include "icalparameter_cxx.h"
#endif

#ifndef VCOMPONENT_CXX_H
#include "vcomponent_cxx.h"
#endif

#ifndef ICALPROPERTY_CXX_H
#include "icalproperty_cxx.h"
#endif

#ifndef ICALVALUE_CXX_H
#include "icalvalue_cxx.h"
#endif

#ifndef _jni_ICalTimeType_H
#include "jniICalTimeType_cxx.h"
#endif

#ifndef _jni_ICalTriggerType_H
#include "jniICalTriggerType_cxx.h"
#endif

#ifndef _jni_ICalDurationType_H
#include "jniICalDurationType_cxx.h"
#endif

#ifndef _jni_ICalRecurrenceType_H
#include "jniICalRecurrenceType_cxx.h"
#endif

#ifndef _jni_ICalPeriodType_H
#include "jniICalPeriodType_cxx.h"
#endif

//-------------------------------------------------------
// Returns a pointer to the subject (a c++ object) for the given surrogate (a java object)
//-------------------------------------------------------
void* getCObjectPtr(JNIEnv *env, jobject surrogate)
{
        void* result = 0;
    jclass jcls = env->GetObjectClass(surrogate);
    jfieldID fid = env->GetFieldID(jcls,"m_Obj","J");

    if (fid == NULL)
    {
                // this should never happen.
        throwException( env, JLIBICAL_ERR_CLIENT_INTERNAL );
        return(NULL);
    }

        result = (void*)env->GetLongField(surrogate,fid);
    if (result == NULL)
        {
                // the proxy object (java) has no subject (c++ object)
        throwException( env, JLIBICAL_ERR_CLIENT_INTERNAL );
        return(NULL);
        }

        return(result);
}

//-------------------------------------------------------
// Set the subject (a c++ object) for the given surrogate (a java object).
// Throws exception if the m_Obj field can not be found.
//-------------------------------------------------------
void setCObjectPtr(JNIEnv *env, jobject surrogate, void* subject)
{
    jclass jcls = env->GetObjectClass(surrogate);
    jfieldID fid = env->GetFieldID(jcls,"m_Obj","J");

    if (fid == NULL)
    {
        throwException( env, JLIBICAL_ERR_CLIENT_INTERNAL );
        return;
    }

        env->SetLongField(surrogate,fid,(long)subject);
}

//-------------------------------------------------------
// Return the pointer to the subject (as an VComponent*) from the given surrogate.
// If the subject is not an VComponent type, or if the subject is NULL, then return NULL.
//-------------------------------------------------------
VComponent* getSubjectAsVComponent(JNIEnv *env, jobject surrogateComponent, int exceptionType)
{
        VComponent* result = (VComponent*)(getCObjectPtr(env,surrogateComponent));

        if (result == NULL)
        {
        throwException(env, exceptionType );
        }

        return(result);
}

//-------------------------------------------------------
// Return the pointer to the subject (as an ICalProperty*) from the given surrogate.
// If the subject is not an ICalProperty type, or if the subject is NULL, then return NULL.
//-------------------------------------------------------
ICalProperty* getSubjectAsICalProperty(JNIEnv *env, jobject surrogateProperty, int exceptionType)
{
        ICalProperty* result = (ICalProperty*)(getCObjectPtr(env,surrogateProperty));

        if (result == NULL)
        {
        throwException(env, exceptionType );
        }

        return(result);
}

//-------------------------------------------------------
// Return the pointer to the subject (as an ICalValue*) from the given surrogate.
// If the subject is not an ICalValue type, or if the subject is NULL, then return NULL.
//-------------------------------------------------------
ICalValue* getSubjectAsICalValue(JNIEnv *env, jobject surrogateValue, int exceptionType)
{
        ICalValue* result = (ICalValue*)(getCObjectPtr(env,surrogateValue));

        if (result == NULL)
        {
        throwException( env, exceptionType );
        }

        return(result);
}

//-------------------------------------------------------
// Return the pointer to the subject (as an ICalParameter*) from the given surrogate.
// If the subject is not an ICalParameter type, or if the subject is NULL, then return NULL.
//-------------------------------------------------------
ICalParameter* getSubjectAsICalParameter(JNIEnv *env, jobject surrogateParameter, int exceptionType)
{
        ICalParameter* result = (ICalParameter*)(getCObjectPtr(env,surrogateParameter));

        if (result == NULL)
        {
        throwException( env, exceptionType );
        }

        return(result);
}

//-------------------------------------------------------
// Copy the data from the src (a java ICalTimeType object)
// to the dest (a c struct icaltimetype*).
// Returns true if success.  False if exception is thrown:
//      - the src java object is not an ICalTimeType type
//      - the dest c struct is null.
//-------------------------------------------------------
bool copyObjToicaltimetype(JNIEnv *env, jobject src, icaltimetype* dest)
{
        bool result = false;

        if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALTIMETYPE)))
        {
                jni_GetAll_from_ICalTimeType(dest, env, src);
                result = true;
        }
        else
        {
        throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
        }

        return(result);
}

//-------------------------------------------------------
// Copy the data from the src (a java ICalTriggerType object)
// to the dest (a c struct icaltriggertype*).
// Returns true if success.  False if exception is thrown:
//      - the src java object is not an ICalTriggerType type
//      - the dest c struct is null.
//-------------------------------------------------------
bool copyObjToicaltriggertype(JNIEnv *env, jobject src, icaltriggertype* dest)
{
        bool result = false;

        if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALTRIGGERTYPE)))
        {
                jni_GetAll_from_ICalTriggerType(dest, env, src);
                result = true;
        }
        else
        {
        throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
        }

        return(result);
}

//-------------------------------------------------------
// Copy the data from the src (a java ICalDurationType object)
// to the dest (a c struct icaldurationtype*).
// Returns true if success.  False if exception is thrown:
//      - the src java object is not an ICalDurationType type
//      - the dest c struct is null.
//-------------------------------------------------------
bool copyObjToicaldurationtype(JNIEnv *env, jobject src, icaldurationtype* dest)
{
        bool result = false;

        if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALDURATIONTYPE)))
        {
                jni_GetAll_from_ICalDurationType(dest, env, src);
                result = true;
        }
        else
        {
        throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
        }

        return(result);
}

//-------------------------------------------------------
// Copy the data from the src (a java ICalRecurrenceType object)
// to the dest (a c struct icalrecurrencetype*).
// Returns true if success.  False if exception is thrown:
//      - the src java object is not an ICalRecurrenceType type
//      - the dest c struct is null.
//-------------------------------------------------------
bool copyObjToicalrecurrencetype(JNIEnv *env, jobject src, icalrecurrencetype* dest)
{
        bool result = false;

        if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALRECURRENCETYPE)))
        {
                jni_GetAll_from_ICalRecurrenceType(dest, env, src);
                result = true;
        }
        else
        {
        throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
        }

        return(result);
}

//-------------------------------------------------------
// Copy the data from the src (a java ICalPeriodType object)
// to the dest (a c struct icalperiodtype*).
// Returns true if success.  False if exception is thrown:
//      - the src java object is not an ICalPeriodType type
//      - the dest c struct is null.
//-------------------------------------------------------
bool copyObjToicalperiodtype(JNIEnv *env, jobject src, icalperiodtype* dest)
{
        bool result = false;

        if (dest != NULL && env->IsInstanceOf(src,env->FindClass(JLIBICAL_CLASS_ICALPERIODTYPE)))
        {
                jni_GetAll_from_ICalPeriodType(dest, env, src);
                result = true;
        }
        else
        {
        throwException( env, JLIBICAL_ERR_ILLEGAL_ARGUMENT );
        }

        return(result);
}

//-------------------------------------------------------
// Create a new VComponent surrogate for given subject.
// If subject is NULL, then returns NULL (will not create a
// surrogate to a NULL subject);
//-------------------------------------------------------
jobject createNewVComponentSurrogate(JNIEnv *env, VComponent* subject)
{
        char* classname = JLIBICAL_CLASS_VCOMPONENT;
        if (dynamic_cast<VAlarm*>(subject))
                classname = JLIBICAL_CLASS_VALARM;
        else if (dynamic_cast<VCalendar*>(subject))
                classname = JLIBICAL_CLASS_VCALENDAR;
        else if (dynamic_cast<VEvent*>(subject))
                classname = JLIBICAL_CLASS_VEVENT;
        else if (dynamic_cast<VQuery*>(subject))
                classname = JLIBICAL_CLASS_VQUERY;
        else if (dynamic_cast<VToDo*>(subject))
                classname = JLIBICAL_CLASS_VTODO;
        else if (dynamic_cast<VAgenda*>(subject))
                classname = JLIBICAL_CLASS_VAGENDA;

        return(doCreateNewSurrogate(env,env->FindClass(classname),(jlong)subject));
}

//-------------------------------------------------------
// Create a new ICalProperty surrogate for given subject.
// If subject is NULL, then returns NULL (will not create a
// surrogate to a NULL subject);
//-------------------------------------------------------
jobject createNewICalPropertySurrogate(JNIEnv *env, ICalProperty* subject)
{
        return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALPROPERTY),(jlong)subject));
}

//-------------------------------------------------------
// Create a new ICalValue surrogate for given subject.
// If subject is NULL, then returns NULL (will not create a
// surrogate to a NULL subject);
//-------------------------------------------------------
jobject createNewICalValueSurrogate(JNIEnv *env, ICalValue* subject)
{
        return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALVALUE),(jlong)subject));
}

//-------------------------------------------------------
// Create a new ICalParameter surrogate for given subject.
// If subject is NULL, then returns NULL (will not create a
// surrogate to a NULL subject);
//-------------------------------------------------------
jobject createNewICalParameterSurrogate(JNIEnv *env, ICalParameter* subject)
{
        return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALPARAMETER),(jlong)subject));
}

//-------------------------------------------------------
// Create a new ICalTimeType object from the given source struct.
// A copy is made,  .
// If source is NULL, then returns NULL (will not create an
// object to a NULL source);
//-------------------------------------------------------
jobject createNewICalTimeType(JNIEnv *env, icaltimetype* source)
{
        return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALTIMETYPE),(jlong)source));
}

//-------------------------------------------------------
// Create a new ICalTriggerType object from the given source struct.
// A copy is made,  .
// If source is NULL, then returns NULL (will not create an
// object to a NULL source);
//-------------------------------------------------------
jobject createNewICalTriggerType(JNIEnv *env, icaltriggertype* source)
{
        return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALTRIGGERTYPE),(jlong)source));
}

//-------------------------------------------------------
// Create a new ICalDurationType object from the given source struct.
// A copy is made,  .
// If source is NULL, then returns NULL (will not create an
// object to a NULL source);
//-------------------------------------------------------
jobject createNewICalDurationType(JNIEnv *env, icaldurationtype* source)
{
        return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALDURATIONTYPE),(jlong)source));
}

//-------------------------------------------------------
// Create a new ICalRecurrenceType object from the given source struct.
// A copy is made,  .
// If source is NULL, then returns NULL (will not create an
// object to a NULL source);
//-------------------------------------------------------
jobject createNewICalRecurrenceType(JNIEnv *env, icalrecurrencetype* source)
{
        return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALRECURRENCETYPE),(jlong)source));
}

//-------------------------------------------------------
// Create a new ICalPeriodType object from the given source struct.
// A copy is made,  .
// If source is NULL, then returns NULL (will not create an
// object to a NULL source);
//-------------------------------------------------------
jobject createNewICalPeriodType(JNIEnv *env, icalperiodtype* source)
{
        return(doCreateNewSurrogate(env,env->FindClass(JLIBICAL_CLASS_ICALPERIODTYPE),(jlong)source));
}

//-------------------------------------------------------
// Create a new surrogate of the given type for the given subject.
//-------------------------------------------------------
jobject doCreateNewSurrogate(JNIEnv *env, jclass surrogateClass, jlong subject)
{
        jobject result = NULL;

        if (subject != NULL)
        {
                jmethodID jconstructorID = env->GetMethodID(surrogateClass, "<init>", "(J)V");

                result = env->NewObject(surrogateClass, jconstructorID, subject);
        }

        return(result);
}

//-------------------------------------------------------
// For the given cpErr, create a new Exception and send it to env.
// Note: Throw does not throw anything.  It only sets the state.
//              The JVM will check this and throw an exception later.
//-------------------------------------------------------
void throwException(JNIEnv *env, int cpErr)
{
        jclass jexceptionClass;
        jthrowable jexceptionObj;
        jmethodID jconstructorID;
        const char* exClassName;

        if (env->ExceptionOccurred())
        {
                return;
        }

    switch ( cpErr )
    {
    case JLIBICAL_ERR_NETWORK:
            exClassName = "net.cp.jlibical/JLCNetworkException";
            break;

    case JLIBICAL_ERR_SERVER_INTERNAL:
            exClassName = "net.cp.jlibical/JLCServerInternalException";
            break;

    case JLIBICAL_ERR_CLIENT_INTERNAL:
            exClassName = "net.cp.jlibical/JLCClientInternalException";
            break;

    case JLIBICAL_ERR_ILLEGAL_ARGUMENT:
            exClassName = "net.cp.jlibical/JLCIllegalArgumentException";
            break;

    case JLIBICAL_ERR_API_NOT_INITED:
            exClassName = "net.cp.jlibical/JLCNotInitedException";
            break;

    case JLIBICAL_ERR_HOST_INVALID:
            exClassName = "net.cp.jlibical/JLCHostInvalidException";
            break;

    default:
            exClassName = "net.cp.jlibical/JLCClientInternalException";
            printf("*** JLIBICAL JNI throwException: unknown error code: %d\n", cpErr );
            break;
    }

    env->ThrowNew(env->FindClass(exClassName),"");
}