Skip to content

Commit

Permalink
Merge pull request #396 from deltachat/chat-array
Browse files Browse the repository at this point in the history
Remove dc_array_t from chat.rs
  • Loading branch information
Alexander Krotov authored Aug 19, 2019
2 parents d47a693 + e7cf5a5 commit 1a8e08e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 38 deletions.
9 changes: 8 additions & 1 deletion deltachat-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 2 additions & 6 deletions examples/repl/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_job::*;
Expand Down Expand Up @@ -917,18 +916,15 @@ 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 {
print!(", Msg#{}", data);
}
}
print!("\n");
dc_array_unref(images);
}
"archive" | "unarchive" => {
ensure!(!arg1.is_empty(), "Argument <chat-id> missing.");
Expand Down
48 changes: 17 additions & 31 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::chatlist::*;
use crate::constants::*;
use crate::contact::*;
use crate::context::Context;
use crate::dc_array::*;
use crate::dc_job::*;
use crate::dc_msg::*;
use crate::dc_tools::*;
Expand Down Expand Up @@ -1200,7 +1199,7 @@ pub fn get_chat_media(
msg_type: Viewtype,
msg_type2: Viewtype,
msg_type3: Viewtype,
) -> *mut dc_array_t {
) -> Vec<u32> {
context.sql.query_map(
"SELECT id FROM msgs WHERE chat_id=? AND (type=? OR type=? OR type=?) ORDER BY timestamp, id;",
params![
Expand All @@ -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(
Expand All @@ -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 {
Expand All @@ -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> {
Expand Down

0 comments on commit 1a8e08e

Please sign in to comment.