Return Vec<dc_location> from dc_get_locations

This commit is contained in:
Alexander Krotov
2019-08-15 20:29:36 +03:00
committed by holger krekel
parent d4dfc5443c
commit 37622af55a
3 changed files with 20 additions and 28 deletions

View File

@@ -1035,13 +1035,14 @@ pub unsafe extern "C" fn dc_get_locations(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
dc_location::dc_get_locations( let res = dc_location::dc_get_locations(
context, context,
chat_id, chat_id,
contact_id, contact_id,
timestamp_begin as i64, timestamp_begin as i64,
timestamp_end as i64, timestamp_end as i64,
) );
dc_array_t::from(res).into_raw()
} }
#[no_mangle] #[no_mangle]

View File

@@ -839,36 +839,28 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
} }
"getlocations" => { "getlocations" => {
let contact_id = arg1.parse().unwrap_or_default(); let contact_id = arg1.parse().unwrap_or_default();
let loc = dc_get_locations(context, dc_chat_get_id(sel_chat), contact_id, 0, 0); let locations = dc_get_locations(context, dc_chat_get_id(sel_chat), contact_id, 0, 0);
let mut j = 0; let default_marker = "-".to_string();
while j < dc_array_get_cnt(loc) { for location in &locations {
let timestr_0 = dc_timestamp_to_str(dc_array_get_timestamp(loc, j as size_t)); let marker = location.marker.as_ref().unwrap_or(&default_marker);
let marker = dc_array_get_marker(loc, j as size_t);
info!( info!(
context, context,
0, 0,
"Loc#{}: {}: lat={} lng={} acc={} Chat#{} Contact#{} Msg#{} {}", "Loc#{}: {}: lat={} lng={} acc={} Chat#{} Contact#{} Msg#{} {}",
dc_array_get_id(loc, j as size_t), location.location_id,
&timestr_0, dc_timestamp_to_str(location.timestamp),
dc_array_get_latitude(loc, j as size_t), location.latitude,
dc_array_get_longitude(loc, j as size_t), location.longitude,
dc_array_get_accuracy(loc, j as size_t), location.accuracy,
dc_array_get_chat_id(loc, j as size_t), location.chat_id,
dc_array_get_contact_id(loc, j as size_t), location.contact_id,
dc_array_get_msg_id(loc, j as size_t), location.msg_id,
if !marker.is_null() { marker
as_str(marker)
} else {
"-"
},
); );
free(marker as *mut libc::c_void);
j += 1
} }
if dc_array_get_cnt(loc) == 0 { if locations.is_empty() {
info!(context, 0, "No locations."); info!(context, 0, "No locations.");
} }
dc_array_unref(loc);
} }
"sendlocations" => { "sendlocations" => {
ensure!(!sel_chat.is_null(), "No chat selected."); ensure!(!sel_chat.is_null(), "No chat selected.");

View File

@@ -3,7 +3,6 @@ use std::ffi::CString;
use crate::constants::Event; use crate::constants::Event;
use crate::constants::*; use crate::constants::*;
use crate::context::*; use crate::context::*;
use crate::dc_array::*;
use crate::dc_chat::*; use crate::dc_chat::*;
use crate::dc_job::*; use crate::dc_job::*;
use crate::dc_msg::*; use crate::dc_msg::*;
@@ -201,7 +200,7 @@ pub fn dc_get_locations(
contact_id: uint32_t, contact_id: uint32_t,
timestamp_from: i64, timestamp_from: i64,
mut timestamp_to: i64, mut timestamp_to: i64,
) -> *mut dc_array_t { ) -> Vec<dc_location> {
if timestamp_to == 0 { if timestamp_to == 0 {
timestamp_to = time() + 10; timestamp_to = time() + 10;
} }
@@ -252,10 +251,10 @@ pub fn dc_get_locations(
for location in locations { for location in locations {
ret.push(location?); ret.push(location?);
} }
Ok(dc_array_t::from(ret).into_raw()) Ok(ret)
}, },
) )
.unwrap_or_else(|_| std::ptr::null_mut()) .unwrap_or_default()
} }
fn is_marker(txt: &str) -> bool { fn is_marker(txt: &str) -> bool {