-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
dc_wrapper.c
2186 lines (1629 loc) · 61.9 KB
/
dc_wrapper.c
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Purpose: The C part of the Java<->C Wrapper, see also DcContext.java
#include <jni.h>
#include <stdlib.h>
#include <string.h>
#include "deltachat-core-rust/deltachat-ffi/deltachat.h"
#if __ANDROID_API__ == 16
unsigned long getauxval(unsigned long type) {
return 0;
}
#include <sys/socket.h>
#include <unistd.h>
int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
int flags)
{
if (flags != 0) {
// Not supported by the fallback.
return -1;
}
if (vlen == 0) {
return 0;
}
ssize_t n = sendmsg(sockfd, &msgvec->msg_hdr, flags);
if (n == -1) {
return -1;
}
(*msgvec).msg_len = n;
return 1;
}
int recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
int flags, struct timespec *timeout)
{
if (flags != 0) {
// Not supported by the fallback.
return -1;
}
if (vlen == 0) {
return 0;
}
int n = recvmsg(sockfd, &msgvec->msg_hdr, flags);
if (n == -1) {
return -1;
}
(*msgvec).msg_len = n;
return 1;
}
#endif
static dc_msg_t* get_dc_msg(JNIEnv *env, jobject obj);
// passing a NULL-jstring results in a NULL-ptr - this is needed by functions using eg. NULL for "delete"
#define CHAR_REF(a) \
char* a##Ptr = char_ref__(env, (a));
static char* char_ref__(JNIEnv* env, jstring a) {
if (a==NULL) {
return NULL;
}
/* we do not use the JNI functions GetStringUTFChars()/ReleaseStringUTFChars()
as they do not work on some older systems for code points >0xffff, eg. emojis.
as a workaround, we're calling back to java-land's String.getBytes() which works as expected */
static jclass s_strCls = NULL;
static jmethodID s_getBytes = NULL;
static jclass s_strEncode = NULL;
if (s_getBytes==NULL) {
s_strCls = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "java/lang/String"));
s_getBytes = (*env)->GetMethodID(env, s_strCls, "getBytes", "(Ljava/lang/String;)[B");
s_strEncode = (*env)->NewGlobalRef(env, (*env)->NewStringUTF(env, "UTF-8"));
}
const jbyteArray stringJbytes = (jbyteArray)(*env)->CallObjectMethod(env, a, s_getBytes, s_strEncode);
const jsize length = (*env)->GetArrayLength(env, stringJbytes);
jbyte* pBytes = (*env)->GetByteArrayElements(env, stringJbytes, NULL);
if (pBytes==NULL) {
return NULL;
}
char* cstr = strndup((const char*)pBytes, length);
(*env)->ReleaseByteArrayElements(env, stringJbytes, pBytes, JNI_ABORT);
(*env)->DeleteLocalRef(env, stringJbytes);
return cstr;
}
#define CHAR_UNREF(a) \
free(a##Ptr);
#define JSTRING_NEW(a) jstring_new__(env, (a))
static jstring jstring_new__(JNIEnv* env, const char* a)
{
if (a==NULL || a[0]==0) {
return (*env)->NewStringUTF(env, "");
}
/* for non-empty strings, do not use NewStringUTF() as this is buggy on some Android versions.
Instead, create the string using `new String(ByteArray, "UTF-8);` which seems to be programmed more properly.
(eg. on KitKat a simple "SMILING FACE WITH SMILING EYES" (U+1F60A, UTF-8 F0 9F 98 8A) will let the app crash, reporting 0xF0 is a bad UTF-8 start,
see http://stackoverflow.com/questions/12127817/android-ics-4-0-ndk-newstringutf-is-crashing-down-the-app ) */
static jclass s_strCls = NULL;
static jmethodID s_strCtor = NULL;
static jclass s_strEncode = NULL;
if (s_strCtor==NULL) {
s_strCls = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "java/lang/String"));
s_strCtor = (*env)->GetMethodID(env, s_strCls, "<init>", "([BLjava/lang/String;)V");
s_strEncode = (*env)->NewGlobalRef(env, (*env)->NewStringUTF(env, "UTF-8"));
}
int a_bytes = strlen(a);
jbyteArray array = (*env)->NewByteArray(env, a_bytes);
(*env)->SetByteArrayRegion(env, array, 0, a_bytes, (const jbyte*)a);
jstring ret = (jstring) (*env)->NewObject(env, s_strCls, s_strCtor, array, s_strEncode);
(*env)->DeleteLocalRef(env, array); /* we have to delete the reference as it is not returned to Java, AFAIK */
return ret;
}
// convert c-timestamp to java-timestamp
#define JTIMESTAMP(a) (((jlong)a)*((jlong)1000))
// convert java-timestamp to c-timestamp
#define CTIMESTAMP(a) (((jlong)a)/((jlong)1000))
static jbyteArray ptr2jbyteArray(JNIEnv *env, const void* ptr, size_t len) {
if (ptr == NULL || len <= 0) {
return NULL;
}
jbyteArray ret = (*env)->NewByteArray(env, len);
if (ret == NULL) {
return NULL;
}
(*env)->SetByteArrayRegion(env, ret, 0, len, (const jbyte*)ptr);
return ret;
}
static jintArray dc_array2jintArray_n_unref(JNIEnv *env, dc_array_t* ca)
{
/* takes a C-array of type dc_array_t and converts it it a Java-Array.
then the C-array is freed and the Java-Array is returned. */
int i, icnt = ca? dc_array_get_cnt(ca) : 0;
jintArray ret = (*env)->NewIntArray(env, icnt); if (ret==NULL) { return NULL; }
if (ca) {
if (icnt) {
jint* temp = calloc(icnt, sizeof(jint));
for (i = 0; i < icnt; i++) {
temp[i] = (jint)dc_array_get_id(ca, i);
}
(*env)->SetIntArrayRegion(env, ret, 0, icnt, temp);
free(temp);
}
dc_array_unref(ca);
}
return ret;
}
static uint32_t* jintArray2uint32Pointer(JNIEnv* env, jintArray ja, int* ret_icnt)
{
/* takes a Java-Array and converts it to a C-Array. */
uint32_t* ret = NULL;
if (ret_icnt) { *ret_icnt = 0; }
if (env && ja && ret_icnt)
{
int i, icnt = (*env)->GetArrayLength(env, ja);
if (icnt > 0)
{
jint* temp = (*env)->GetIntArrayElements(env, ja, NULL);
if (temp)
{
ret = calloc(icnt, sizeof(uint32_t));
if (ret)
{
for (i = 0; i < icnt; i++) {
ret[i] = (uint32_t)temp[i];
}
*ret_icnt = icnt;
}
(*env)->ReleaseIntArrayElements(env, ja, temp, 0);
}
}
}
return ret;
}
/*******************************************************************************
* DcAccounts
******************************************************************************/
static dc_accounts_t* get_dc_accounts(JNIEnv *env, jobject obj)
{
static jfieldID fid = 0;
if (fid==0) {
jclass cls = (*env)->GetObjectClass(env, obj);
fid = (*env)->GetFieldID(env, cls, "accountsCPtr", "J" /*Signature, J=long*/);
}
if (fid) {
return (dc_accounts_t*)(*env)->GetLongField(env, obj, fid);
}
return NULL;
}
JNIEXPORT jlong Java_com_b44t_messenger_DcAccounts_createAccountsCPtr(JNIEnv *env, jobject obj, jstring dir)
{
CHAR_REF(dir);
int writable = 1;
jlong accountsCPtr = (jlong)dc_accounts_new(dirPtr, writable);
CHAR_UNREF(dir);
return accountsCPtr;
}
JNIEXPORT void Java_com_b44t_messenger_DcAccounts_unrefAccountsCPtr(JNIEnv *env, jobject obj)
{
dc_accounts_unref(get_dc_accounts(env, obj));
}
JNIEXPORT jlong Java_com_b44t_messenger_DcAccounts_getEventEmitterCPtr(JNIEnv *env, jobject obj)
{
return (jlong)dc_accounts_get_event_emitter(get_dc_accounts(env, obj));
}
JNIEXPORT jlong Java_com_b44t_messenger_DcAccounts_getJsonrpcInstanceCPtr(JNIEnv *env, jobject obj)
{
return (jlong)dc_jsonrpc_init(get_dc_accounts(env, obj));
}
JNIEXPORT void Java_com_b44t_messenger_DcAccounts_startIo(JNIEnv *env, jobject obj)
{
dc_accounts_start_io(get_dc_accounts(env, obj));
}
JNIEXPORT void Java_com_b44t_messenger_DcAccounts_stopIo(JNIEnv *env, jobject obj)
{
dc_accounts_stop_io(get_dc_accounts(env, obj));
}
JNIEXPORT void Java_com_b44t_messenger_DcAccounts_maybeNetwork(JNIEnv *env, jobject obj)
{
dc_accounts_maybe_network(get_dc_accounts(env, obj));
}
JNIEXPORT jint Java_com_b44t_messenger_DcAccounts_addAccount(JNIEnv *env, jobject obj)
{
return dc_accounts_add_account(get_dc_accounts(env, obj));
}
JNIEXPORT jint Java_com_b44t_messenger_DcAccounts_addClosedAccount(JNIEnv *env, jobject obj)
{
return dc_accounts_add_closed_account(get_dc_accounts(env, obj));
}
JNIEXPORT jint Java_com_b44t_messenger_DcAccounts_migrateAccount(JNIEnv *env, jobject obj, jstring dbfile)
{
CHAR_REF(dbfile);
jint accountId = dc_accounts_migrate_account(get_dc_accounts(env, obj), dbfilePtr);
CHAR_UNREF(dbfile);
return accountId;
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcAccounts_removeAccount(JNIEnv *env, jobject obj, jint accountId)
{
return dc_accounts_remove_account(get_dc_accounts(env, obj), accountId) != 0;
}
JNIEXPORT jintArray Java_com_b44t_messenger_DcAccounts_getAll(JNIEnv *env, jobject obj)
{
dc_array_t* ca = dc_accounts_get_all(get_dc_accounts(env, obj));
return dc_array2jintArray_n_unref(env, ca);
}
JNIEXPORT jlong Java_com_b44t_messenger_DcAccounts_getAccountCPtr(JNIEnv *env, jobject obj, jint accountId)
{
return (jlong)dc_accounts_get_account(get_dc_accounts(env, obj), accountId);
}
JNIEXPORT jlong Java_com_b44t_messenger_DcAccounts_getSelectedAccountCPtr(JNIEnv *env, jobject obj)
{
return (jlong)dc_accounts_get_selected_account(get_dc_accounts(env, obj));
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcAccounts_selectAccount(JNIEnv *env, jobject obj, jint accountId)
{
return dc_accounts_select_account(get_dc_accounts(env, obj), accountId) != 0;
}
/*******************************************************************************
* DcContext
******************************************************************************/
static dc_context_t* get_dc_context(JNIEnv *env, jobject obj)
{
static jfieldID fid = 0;
if (fid==0) {
jclass cls = (*env)->GetObjectClass(env, obj);
fid = (*env)->GetFieldID(env, cls, "contextCPtr", "J" /*Signature, J=long*/);
}
if (fid) {
return (dc_context_t*)(*env)->GetLongField(env, obj, fid);
}
return NULL;
}
JNIEXPORT jlong Java_com_b44t_messenger_DcContext_createContextCPtr(JNIEnv *env, jobject obj, jstring osname, jstring dbfile)
{
CHAR_REF(osname);
CHAR_REF(dbfile)
jlong contextCPtr = (jlong)dc_context_new(osnamePtr, dbfilePtr, NULL);
CHAR_UNREF(dbfile)
CHAR_UNREF(osname);
return contextCPtr;
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_open(JNIEnv *env, jobject obj, jstring passphrase)
{
CHAR_REF(passphrase);
jboolean ret = dc_context_open(get_dc_context(env, obj), passphrasePtr);
CHAR_UNREF(passphrase);
return ret;
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_isOpen(JNIEnv *env, jobject obj)
{
return dc_context_is_open(get_dc_context(env, obj));
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_unrefContextCPtr(JNIEnv *env, jobject obj)
{
dc_context_unref(get_dc_context(env, obj));
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_getAccountId(JNIEnv *env, jobject obj)
{
return (jint)dc_get_id(get_dc_context(env, obj));
}
/* DcContext - open/configure/connect/fetch */
JNIEXPORT void Java_com_b44t_messenger_DcContext_setStockTranslation(JNIEnv *env, jobject obj, jint stock_id, jstring translation)
{
CHAR_REF(translation);
dc_set_stock_translation(get_dc_context(env, obj), stock_id, translationPtr);
CHAR_UNREF(translation)
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_setConfigFromQr(JNIEnv *env, jobject obj, jstring qr)
{
CHAR_REF(qr);
jboolean ret = dc_set_config_from_qr(get_dc_context(env, obj), qrPtr);
CHAR_UNREF(qr);
return ret;
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getBlobdir(JNIEnv *env, jobject obj)
{
char* temp = dc_get_blobdir(get_dc_context(env, obj));
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getLastError(JNIEnv *env, jobject obj)
{
char* temp = dc_get_last_error(get_dc_context(env, obj));
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_configure(JNIEnv *env, jobject obj)
{
dc_configure(get_dc_context(env, obj));
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_stopOngoingProcess(JNIEnv *env, jobject obj)
{
dc_stop_ongoing_process(get_dc_context(env, obj));
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_isConfigured(JNIEnv *env, jobject obj)
{
return (jint)dc_is_configured(get_dc_context(env, obj));
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_startIo(JNIEnv *env, jobject obj)
{
dc_start_io(get_dc_context(env, obj));
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_stopIo(JNIEnv *env, jobject obj)
{
dc_stop_io(get_dc_context(env, obj));
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_maybeNetwork(JNIEnv *env, jobject obj)
{
dc_maybe_network(get_dc_context(env, obj));
}
JNIEXPORT jlong Java_com_b44t_messenger_DcContext_getEventEmitterCPtr(JNIEnv *env, jobject obj)
{
return (jlong)dc_get_event_emitter(get_dc_context(env, obj));
}
/* DcContext - handle contacts */
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_mayBeValidAddr(JNIEnv *env, jobject obj, jstring addr)
{
CHAR_REF(addr);
jboolean ret = dc_may_be_valid_addr(addrPtr);
CHAR_UNREF(addr);
return ret;
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_lookupContactIdByAddr(JNIEnv *env, jobject obj, jstring addr)
{
CHAR_REF(addr);
jint ret = dc_lookup_contact_id_by_addr(get_dc_context(env, obj), addrPtr);
CHAR_UNREF(addr);
return ret;
}
JNIEXPORT jintArray Java_com_b44t_messenger_DcContext_getContacts(JNIEnv *env, jobject obj, jint flags, jstring query)
{
CHAR_REF(query);
dc_array_t* ca = dc_get_contacts(get_dc_context(env, obj), flags, queryPtr);
CHAR_UNREF(query);
return dc_array2jintArray_n_unref(env, ca);
}
JNIEXPORT jintArray Java_com_b44t_messenger_DcContext_getBlockedContacts(JNIEnv *env, jobject obj)
{
dc_array_t* ca = dc_get_blocked_contacts(get_dc_context(env, obj));
return dc_array2jintArray_n_unref(env, ca);
}
JNIEXPORT jlong Java_com_b44t_messenger_DcContext_getContactCPtr(JNIEnv *env, jobject obj, jint contact_id)
{
return (jlong)dc_get_contact(get_dc_context(env, obj), contact_id);
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_createContact(JNIEnv *env, jobject obj, jstring name, jstring addr)
{
CHAR_REF(name);
CHAR_REF(addr);
jint ret = (jint)dc_create_contact(get_dc_context(env, obj), namePtr, addrPtr);
CHAR_UNREF(addr);
CHAR_UNREF(name);
return ret;
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_blockContact(JNIEnv *env, jobject obj, jint contact_id, jint block)
{
dc_block_contact(get_dc_context(env, obj), contact_id, block);
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_deleteContact(JNIEnv *env, jobject obj, jint contact_id)
{
return (jboolean)dc_delete_contact(get_dc_context(env, obj), contact_id);
}
/* DcContext - handle chats */
JNIEXPORT jlong Java_com_b44t_messenger_DcContext_getChatlistCPtr(JNIEnv *env, jobject obj, jint listflags, jstring query, jint queryId)
{
jlong ret;
if (query) {
CHAR_REF(query);
ret = (jlong)dc_get_chatlist(get_dc_context(env, obj), listflags, queryPtr, queryId);
CHAR_UNREF(query);
}
else {
ret = (jlong)dc_get_chatlist(get_dc_context(env, obj), listflags, NULL, queryId);
}
return ret;
}
JNIEXPORT jlong Java_com_b44t_messenger_DcContext_getChatCPtr(JNIEnv *env, jobject obj, jint chat_id)
{
return (jlong)dc_get_chat(get_dc_context(env, obj), chat_id);
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_getChatIdByContactId(JNIEnv *env, jobject obj, jint contact_id)
{
return (jint)dc_get_chat_id_by_contact_id(get_dc_context(env, obj), contact_id);
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_markseenMsgs(JNIEnv *env, jobject obj, jintArray msg_ids)
{
int msg_ids_cnt = 0;
uint32_t* msg_ids_ptr = jintArray2uint32Pointer(env, msg_ids, &msg_ids_cnt);
dc_markseen_msgs(get_dc_context(env, obj), msg_ids_ptr, msg_ids_cnt);
free(msg_ids_ptr);
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getChatEncrInfo(JNIEnv *env, jobject obj, jint chat_id)
{
char* temp = dc_get_chat_encrinfo(get_dc_context(env, obj), chat_id);
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_marknoticedChat(JNIEnv *env, jobject obj, jint chat_id)
{
dc_marknoticed_chat(get_dc_context(env, obj), chat_id);
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_setChatVisibility(JNIEnv *env, jobject obj, jint chat_id, jint visibility)
{
dc_set_chat_visibility(get_dc_context(env, obj), chat_id, visibility);
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_createChatByContactId(JNIEnv *env, jobject obj, jint contact_id)
{
return (jint)dc_create_chat_by_contact_id(get_dc_context(env, obj), contact_id);
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_createGroupChat(JNIEnv *env, jobject obj, jboolean verified, jstring name)
{
CHAR_REF(name);
jint ret = (jint)dc_create_group_chat(get_dc_context(env, obj), verified, namePtr);
CHAR_UNREF(name);
return ret;
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_createBroadcastList(JNIEnv *env, jobject obj)
{
return (jint)dc_create_broadcast_list(get_dc_context(env, obj));
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_isContactInChat(JNIEnv *env, jobject obj, jint chat_id, jint contact_id)
{
return (jboolean)dc_is_contact_in_chat(get_dc_context(env, obj), chat_id, contact_id);
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_addContactToChat(JNIEnv *env, jobject obj, jint chat_id, jint contact_id)
{
return (jint)dc_add_contact_to_chat(get_dc_context(env, obj), chat_id, contact_id);
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_removeContactFromChat(JNIEnv *env, jobject obj, jint chat_id, jint contact_id)
{
return (jint)dc_remove_contact_from_chat(get_dc_context(env, obj), chat_id, contact_id);
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_setDraft(JNIEnv *env, jobject obj, jint chat_id, jobject msg /* NULL=delete */)
{
dc_set_draft(get_dc_context(env, obj), chat_id, get_dc_msg(env, msg));
}
JNIEXPORT jlong Java_com_b44t_messenger_DcContext_getDraftCPtr(JNIEnv *env, jobject obj, jint chat_id)
{
return (jlong)dc_get_draft(get_dc_context(env, obj), chat_id);
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_setChatName(JNIEnv *env, jobject obj, jint chat_id, jstring name)
{
CHAR_REF(name);
jint ret = (jint)dc_set_chat_name(get_dc_context(env, obj), chat_id, namePtr);
CHAR_UNREF(name);
return ret;
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_setChatProfileImage(JNIEnv *env, jobject obj, jint chat_id, jstring image/*NULL=delete*/)
{
CHAR_REF(image);
jint ret = (jint)dc_set_chat_profile_image(get_dc_context(env, obj), chat_id, imagePtr/*CHAR_REF() preserves NULL*/);
CHAR_UNREF(image);
return ret;
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_deleteChat(JNIEnv *env, jobject obj, jint chat_id)
{
dc_delete_chat(get_dc_context(env, obj), chat_id);
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_blockChat(JNIEnv *env, jobject obj, jint chat_id)
{
dc_block_chat(get_dc_context(env, obj), chat_id);
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_acceptChat(JNIEnv *env, jobject obj, jint chat_id)
{
dc_accept_chat(get_dc_context(env, obj), chat_id);
}
/* DcContext - handle messages */
JNIEXPORT jint Java_com_b44t_messenger_DcContext_getFreshMsgCount(JNIEnv *env, jobject obj, jint chat_id)
{
return dc_get_fresh_msg_cnt(get_dc_context(env, obj), chat_id);
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_estimateDeletionCount(JNIEnv *env, jobject obj, jboolean from_server, jlong seconds)
{
return dc_estimate_deletion_cnt(get_dc_context(env, obj), from_server, seconds);
}
JNIEXPORT jlong Java_com_b44t_messenger_DcContext_getMsgCPtr(JNIEnv *env, jobject obj, jint id)
{
return (jlong)dc_get_msg(get_dc_context(env, obj), id);
}
JNIEXPORT jlong Java_com_b44t_messenger_DcContext_createMsgCPtr(JNIEnv *env, jobject obj, jint viewtype)
{
return (jlong)dc_msg_new(get_dc_context(env, obj), viewtype);
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getMsgInfo(JNIEnv *env, jobject obj, jint msg_id)
{
char* temp = dc_get_msg_info(get_dc_context(env, obj), msg_id);
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getMsgHtml(JNIEnv *env, jobject obj, jint msg_id)
{
char* temp = dc_get_msg_html(get_dc_context(env, obj), msg_id);
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_downloadFullMsg(JNIEnv *env, jobject obj, jint msg_id)
{
dc_download_full_msg(get_dc_context(env, obj), msg_id);
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_deleteMsgs(JNIEnv *env, jobject obj, jintArray msg_ids)
{
int msg_ids_cnt = 0;
uint32_t* msg_ids_ptr = jintArray2uint32Pointer(env, msg_ids, &msg_ids_cnt);
dc_delete_msgs(get_dc_context(env, obj), msg_ids_ptr, msg_ids_cnt);
free(msg_ids_ptr);
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_forwardMsgs(JNIEnv *env, jobject obj, jintArray msg_ids, jint chat_id)
{
int msg_ids_cnt = 0;
uint32_t* msg_ids_ptr = jintArray2uint32Pointer(env, msg_ids, &msg_ids_cnt);
dc_forward_msgs(get_dc_context(env, obj), msg_ids_ptr, msg_ids_cnt, chat_id);
free(msg_ids_ptr);
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_resendMsgs(JNIEnv *env, jobject obj, jintArray msg_ids)
{
int msg_ids_cnt = 0;
uint32_t* msg_ids_ptr = jintArray2uint32Pointer(env, msg_ids, &msg_ids_cnt);
jboolean ret = dc_resend_msgs(get_dc_context(env, obj), msg_ids_ptr, msg_ids_cnt) != 0;
free(msg_ids_ptr);
return ret;
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_prepareMsg(JNIEnv *env, jobject obj, jint chat_id, jobject msg)
{
return dc_prepare_msg(get_dc_context(env, obj), chat_id, get_dc_msg(env, msg));
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_sendMsg(JNIEnv *env, jobject obj, jint chat_id, jobject msg)
{
return dc_send_msg(get_dc_context(env, obj), chat_id, get_dc_msg(env, msg));
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_sendTextMsg(JNIEnv *env, jobject obj, jint chat_id, jstring text)
{
CHAR_REF(text);
jint msg_id = dc_send_text_msg(get_dc_context(env, obj), chat_id, textPtr);
CHAR_UNREF(text);
return msg_id;
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_sendVideochatInvitation(JNIEnv *env, jobject obj, jint chat_id)
{
return (jint)dc_send_videochat_invitation(get_dc_context(env, obj), chat_id);
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_sendWebxdcStatusUpdate(JNIEnv *env, jobject obj, jint msg_id, jstring payload, jstring descr)
{
CHAR_REF(payload);
CHAR_REF(descr);
jboolean ret = dc_send_webxdc_status_update(get_dc_context(env, obj), msg_id, payloadPtr, descrPtr) != 0;
CHAR_UNREF(descr);
CHAR_UNREF(payload);
return ret;
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getWebxdcStatusUpdates(JNIEnv *env, jobject obj, jint msg_id, jint last_known_serial)
{
char* temp = dc_get_webxdc_status_updates(get_dc_context(env, obj), msg_id, last_known_serial);
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_addDeviceMsg(JNIEnv *env, jobject obj, jstring label, jobject msg)
{
CHAR_REF(label);
int msg_id = dc_add_device_msg(get_dc_context(env, obj), labelPtr, get_dc_msg(env, msg));
CHAR_UNREF(label);
return msg_id;
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_wasDeviceMsgEverAdded(JNIEnv *env, jobject obj, jstring label)
{
CHAR_REF(label);
jboolean ret = dc_was_device_msg_ever_added(get_dc_context(env, obj), labelPtr) != 0;
CHAR_UNREF(label);
return ret;
}
/* DcContext - handle config */
JNIEXPORT void Java_com_b44t_messenger_DcContext_setConfig(JNIEnv *env, jobject obj, jstring key, jstring value /*may be NULL*/)
{
CHAR_REF(key);
CHAR_REF(value);
dc_set_config(get_dc_context(env, obj), keyPtr, valuePtr /*is NULL if value is NULL, CHAR_REF() handles this*/);
CHAR_UNREF(key);
CHAR_UNREF(value);
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getConfig(JNIEnv *env, jobject obj, jstring key)
{
CHAR_REF(key);
char* temp = dc_get_config(get_dc_context(env, obj), keyPtr);
jstring ret = NULL;
if (temp) {
ret = JSTRING_NEW(temp);
}
dc_str_unref(temp);
CHAR_UNREF(key);
return ret; /* returns NULL only if key is unset and "def" is NULL */
}
/* DcContext - out-of-band verification */
JNIEXPORT jlong Java_com_b44t_messenger_DcContext_checkQrCPtr(JNIEnv *env, jobject obj, jstring qr)
{
CHAR_REF(qr);
jlong ret = (jlong)dc_check_qr(get_dc_context(env, obj), qrPtr);
CHAR_UNREF(qr);
return ret;
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getSecurejoinQr(JNIEnv *env, jobject obj, jint chat_id)
{
char* temp = dc_get_securejoin_qr(get_dc_context(env, obj), chat_id);
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getSecurejoinQrSvg(JNIEnv *env, jobject obj, jint chat_id)
{
char* temp = dc_get_securejoin_qr_svg(get_dc_context(env, obj), chat_id);
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_joinSecurejoin(JNIEnv *env, jobject obj, jstring qr)
{
CHAR_REF(qr);
jint ret = dc_join_securejoin(get_dc_context(env, obj), qrPtr);
CHAR_UNREF(qr);
return ret;
}
/* DcContext - misc. */
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getInfo(JNIEnv *env, jobject obj)
{
char* temp = dc_get_info(get_dc_context(env, obj));
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_getConnectivity(JNIEnv *env, jobject obj)
{
return dc_get_connectivity(get_dc_context(env, obj));
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getConnectivityHtml(JNIEnv *env, jobject obj)
{
char* temp = dc_get_connectivity_html(get_dc_context(env, obj));
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getOauth2Url(JNIEnv *env, jobject obj, jstring addr, jstring redirectUrl)
{
CHAR_REF(addr);
CHAR_REF(redirectUrl);
char* temp = dc_get_oauth2_url(get_dc_context(env, obj), addrPtr, redirectUrlPtr);
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
CHAR_UNREF(redirectUrl);
CHAR_UNREF(addr);
return ret;
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_getContactEncrInfo(JNIEnv *env, jobject obj, jint contact_id)
{
char* temp = dc_get_contact_encrinfo(get_dc_context(env, obj), contact_id);
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_initiateKeyTransfer(JNIEnv *env, jobject obj)
{
jstring setup_code = NULL;
char* temp = dc_initiate_key_transfer(get_dc_context(env, obj));
if (temp) {
setup_code = JSTRING_NEW(temp);
dc_str_unref(temp);
}
return setup_code;
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_continueKeyTransfer(JNIEnv *env, jobject obj, jint msg_id, jstring setupCode)
{
CHAR_REF(setupCode);
jboolean ret = dc_continue_key_transfer(get_dc_context(env, obj), msg_id, setupCodePtr);
CHAR_UNREF(setupCode);
return ret;
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_imex(JNIEnv *env, jobject obj, jint what, jstring dir)
{
CHAR_REF(dir);
dc_imex(get_dc_context(env, obj), what, dirPtr, "");
CHAR_UNREF(dir);
}
JNIEXPORT jstring Java_com_b44t_messenger_DcContext_imexHasBackup(JNIEnv *env, jobject obj, jstring dir)
{
CHAR_REF(dir);
jstring ret = NULL;
char* temp = dc_imex_has_backup(get_dc_context(env, obj), dirPtr);
if (temp) {
ret = JSTRING_NEW(temp);
dc_str_unref(temp);
}
CHAR_UNREF(dir);
return ret; /* may be NULL! */
}
JNIEXPORT jlong Java_com_b44t_messenger_DcContext_newBackupProviderCPtr(JNIEnv *env, jobject obj)
{
return (jlong)dc_backup_provider_new(get_dc_context(env, obj));
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_receiveBackup(JNIEnv *env, jobject obj, jstring qr)
{
CHAR_REF(qr);
jboolean ret = dc_receive_backup(get_dc_context(env, obj), qrPtr);
CHAR_UNREF(qr);
return ret;
}
JNIEXPORT jint Java_com_b44t_messenger_DcContext_addAddressBook(JNIEnv *env, jobject obj, jstring adrbook)
{
CHAR_REF(adrbook);
int modify_count = dc_add_address_book(get_dc_context(env, obj), adrbookPtr);
CHAR_UNREF(adrbook);
return modify_count;
}
JNIEXPORT void Java_com_b44t_messenger_DcContext_sendLocationsToChat(JNIEnv *env, jobject obj, jint chat_id, jint seconds)
{
dc_send_locations_to_chat(get_dc_context(env, obj), chat_id, seconds);
}
JNIEXPORT jboolean Java_com_b44t_messenger_DcContext_isSendingLocationsToChat(JNIEnv *env, jobject obj, jint chat_id)
{
return (dc_is_sending_locations_to_chat(get_dc_context(env, obj), chat_id)!=0);
}