diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index d66e7cf69..5d65e75d7 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -498,7 +498,14 @@ pub unsafe extern "C" fn dc_get_chat_media( let or_msg_type3 = from_prim(or_msg_type3).expect(&format!("incorrect or_msg_type3 = {}", or_msg_type3)); - chat::get_chat_media(context, chat_id, msg_type, or_msg_type2, or_msg_type3) + dc_array_t::from(chat::get_chat_media( + context, + chat_id, + msg_type, + or_msg_type2, + or_msg_type3, + )) + .into_raw() } #[no_mangle] diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index 9cc46c1ba..d1454e0e3 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -7,7 +7,6 @@ use deltachat::config; use deltachat::constants::*; use deltachat::contact::*; use deltachat::context::*; -use deltachat::dc_array::*; use deltachat::dc_configure::*; use deltachat::dc_imex::*; use deltachat::dc_location::*; @@ -917,10 +916,8 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E Viewtype::Gif, Viewtype::Video, ); - let icnt: libc::c_int = dc_array_get_cnt(images) as libc::c_int; - println!("{} images or videos: ", icnt); - for i in 0..icnt { - let data = dc_array_get_id(images, i as size_t); + println!("{} images or videos: ", images.len()); + for (i, data) in images.iter().enumerate() { if 0 == i { print!("Msg#{}", data); } else { @@ -928,7 +925,6 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E } } print!("\n"); - dc_array_unref(images); } "archive" | "unarchive" => { ensure!(!arg1.is_empty(), "Argument missing."); diff --git a/src/chat.rs b/src/chat.rs index 56c180cb6..a86d988cd 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -5,7 +5,6 @@ use crate::chatlist::*; use crate::constants::*; use crate::contact::*; use crate::context::Context; -use crate::dc_array::*; use crate::dc_msg::*; use crate::dc_tools::*; use crate::error::Error; @@ -1200,7 +1199,7 @@ pub fn get_chat_media( msg_type: Viewtype, msg_type2: Viewtype, msg_type3: Viewtype, -) -> *mut dc_array_t { +) -> Vec { context.sql.query_map( "SELECT id FROM msgs WHERE chat_id=? AND (type=? OR type=? OR type=?) ORDER BY timestamp, id;", params![ @@ -1222,9 +1221,9 @@ pub fn get_chat_media( for id in ids { ret.push(id? as u32); } - Ok(dc_array_t::from(ret).into_raw()) + Ok(ret) } - ).unwrap_or_else(|_| std::ptr::null_mut()) + ).unwrap_or_default() } pub unsafe fn get_next_media( @@ -1235,14 +1234,10 @@ pub unsafe fn get_next_media( msg_type2: Viewtype, msg_type3: Viewtype, ) -> u32 { - let mut ret_msg_id: u32 = 0i32 as u32; + let mut ret = 0; let msg: *mut dc_msg_t = dc_msg_new_untyped(context); - let mut list: *mut dc_array_t = ptr::null_mut(); - let mut i: libc::c_int; - let cnt: libc::c_int; - if dc_msg_load_from_db(msg, context, curr_msg_id) { - list = get_chat_media( + let list = get_chat_media( context, (*msg).chat_id, if msg_type != Viewtype::Unknown { @@ -1253,33 +1248,24 @@ pub unsafe fn get_next_media( msg_type2, msg_type3, ); - if !list.is_null() { - cnt = dc_array_get_cnt(list) as libc::c_int; - i = 0i32; - while i < cnt { - if curr_msg_id == dc_array_get_id(list, i as size_t) { - if dir > 0i32 { - if i + 1i32 < cnt { - ret_msg_id = dc_array_get_id(list, (i + 1i32) as size_t) - } - } else if dir < 0i32 { - if i - 1i32 >= 0i32 { - ret_msg_id = dc_array_get_id(list, (i - 1i32) as size_t) - } + for i in 0..list.len() { + if curr_msg_id == list[i] { + if dir > 0 { + if i + 1 < list.len() { + ret = list[i + 1] + } + } else if dir < 0 { + if i >= 1 { + ret = list[i - 1]; } - break; - } else { - i += 1 } + break; } } } - - if !list.is_null() { - dc_array_unref(list); - } dc_msg_unref(msg); - ret_msg_id + + ret } pub fn archive(context: &Context, chat_id: u32, archive: bool) -> Result<(), Error> {