From 9c5cf84c9fccd9371cffe76dae90388020ab3f50 Mon Sep 17 00:00:00 2001 From: link2xt Date: Tue, 1 Apr 2025 22:44:01 +0000 Subject: [PATCH] api: add dc_make_vcard() and dc_import_vcard() --- deltachat-ffi/deltachat.h | 23 +++++++++++++++++++++ deltachat-ffi/src/lib.rs | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index 48975e5f1..4c15d2936 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -2162,6 +2162,29 @@ uint32_t dc_create_contact (dc_context_t* context, const char* int dc_add_address_book (dc_context_t* context, const char* addr_book); +/** + * Make a vCard. + * + * @memberof dc_context_t + * @param context The context object. + * @param contact_id The ID of the contact to make the vCard of. + * @return vCard, must be released using dc_str_unref() after usage. + */ +char* dc_make_vcard (dc_context_t* context, uint32_t contact_id); + + +/** + * Import a vCard. + * + * @memberof dc_context_t + * @param context The context object. + * @param vcard vCard contents. + * @return Returns the IDs of the contacts in the order they appear in the vCard. + * Must be dc_array_unref()'d after usage. + */ +dc_array_t* dc_import_vcard (dc_context_t* context, const char* vcard); + + /** * Returns known and unblocked contacts. * diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 3ec17cb6d..0e31a9f40 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -2170,6 +2170,48 @@ pub unsafe extern "C" fn dc_add_address_book( }) } +#[no_mangle] +pub unsafe extern "C" fn dc_make_vcard( + context: *mut dc_context_t, + contact_id: u32, +) -> *mut libc::c_char { + if context.is_null() { + eprintln!("ignoring careless call to dc_make_vcard()"); + return ptr::null_mut(); + } + let ctx = &*context; + let contact_id = ContactId::new(contact_id); + + block_on(contact::make_vcard(ctx, &[contact_id])) + .unwrap_or_log_default(ctx, "dc_make_vcard failed") + .strdup() +} + +#[no_mangle] +pub unsafe extern "C" fn dc_import_vcard( + context: *mut dc_context_t, + vcard: *const libc::c_char, +) -> *mut dc_array::dc_array_t { + if context.is_null() || vcard.is_null() { + eprintln!("ignoring careless call to dc_import_vcard()"); + return ptr::null_mut(); + } + let ctx = &*context; + + match block_on(contact::import_vcard(ctx, &to_string_lossy(vcard))) + .context("dc_import_vcard failed") + .log_err(ctx) + { + Ok(contact_ids) => Box::into_raw(Box::new(dc_array_t::from( + contact_ids + .iter() + .map(|id| id.to_u32()) + .collect::>(), + ))), + Err(_) => ptr::null_mut(), + } +} + #[no_mangle] pub unsafe extern "C" fn dc_get_contacts( context: *mut dc_context_t,