refactor(params): rustify

This commit is contained in:
Friedel Ziegelmayer
2019-07-29 01:49:53 +02:00
committed by GitHub
parent 669ed0e0df
commit 188da2a020
23 changed files with 912 additions and 1191 deletions

View File

@@ -7,8 +7,8 @@ use crate::dc_contact::*;
use crate::dc_job::*;
use crate::dc_lot::dc_lot_t;
use crate::dc_lot::*;
use crate::dc_param::*;
use crate::dc_tools::*;
use crate::param::*;
use crate::pgp::*;
use crate::sql;
use crate::stock::StockMessage;
@@ -16,7 +16,7 @@ use crate::types::*;
use crate::x::*;
/* * the structure behind dc_msg_t */
#[derive(Copy, Clone)]
#[derive(Clone)]
#[repr(C)]
pub struct dc_msg_t<'a> {
pub magic: uint32_t,
@@ -41,7 +41,7 @@ pub struct dc_msg_t<'a> {
pub starred: libc::c_int,
pub chat_blocked: libc::c_int,
pub location_id: uint32_t,
pub param: *mut dc_param_t,
pub param: Params,
}
// handle messages
@@ -144,26 +144,30 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
ret += ", Location sent";
}
let e2ee_errors = dc_param_get_int((*msg).param, DC_PARAM_ERRONEOUS_E2EE as i32, 0);
let e2ee_errors = (*msg)
.param
.get_int(Param::ErroneousE2ee)
.unwrap_or_default();
if 0 != e2ee_errors {
if 0 != e2ee_errors & 0x2 {
ret += ", Encrypted, no valid signature";
}
} else if 0 != dc_param_get_int((*msg).param, DC_PARAM_GUARANTEE_E2EE as i32, 0) {
} else if 0
!= (*msg)
.param
.get_int(Param::GuranteeE2ee)
.unwrap_or_default()
{
ret += ", Encrypted";
}
ret += "\n";
p = dc_param_get(
(*msg).param,
DC_PARAM_ERROR as i32,
0 as *const libc::c_char,
);
if !p.is_null() {
ret += &format!("Error: {}", as_str(p));
free(p as *mut libc::c_void);
match (*msg).param.get(Param::Error) {
Some(err) => ret += &format!("Error: {}", err),
_ => {}
}
p = dc_msg_get_file(msg);
if !p.is_null() && 0 != *p.offset(0isize) as libc::c_int {
ret += &format!(
@@ -190,12 +194,12 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
ret += &format!("Mimetype: {}\n", as_str(p));
free(p as *mut libc::c_void);
}
let w = dc_param_get_int((*msg).param, DC_PARAM_WIDTH as i32, 0);
let h = dc_param_get_int((*msg).param, DC_PARAM_HEIGHT as i32, 0);
let w = (*msg).param.get_int(Param::Width).unwrap_or_default();
let h = (*msg).param.get_int(Param::Height).unwrap_or_default();
if w != 0 || h != 0 {
ret += &format!("Dimension: {} x {}\n", w, h,);
}
let duration = dc_param_get_int((*msg).param, DC_PARAM_DURATION as i32, 0);
let duration = (*msg).param.get_int(Param::Duration).unwrap_or_default();
if duration != 0 {
ret += &format!("Duration: {} ms\n", duration,);
}
@@ -233,16 +237,33 @@ pub unsafe fn dc_msg_new_untyped<'a>(context: &'a Context) -> *mut dc_msg_t<'a>
// approx. max. length returned by dc_msg_get_text()
// approx. max. length returned by dc_get_msg_info()
pub unsafe fn dc_msg_new<'a>(context: &'a Context, viewtype: libc::c_int) -> *mut dc_msg_t<'a> {
let mut msg: *mut dc_msg_t;
msg = calloc(1, ::std::mem::size_of::<dc_msg_t>()) as *mut dc_msg_t;
assert!(!msg.is_null());
(*msg).context = context;
(*msg).magic = 0x11561156i32 as uint32_t;
(*msg).type_0 = viewtype;
(*msg).state = 0;
(*msg).param = dc_param_new();
let msg = dc_msg_t {
magic: 0x11561156,
id: 0,
from_id: 0,
to_id: 0,
chat_id: 0,
move_state: 0,
type_0: viewtype,
state: 0,
hidden: 0,
timestamp_sort: 0,
timestamp_sent: 0,
timestamp_rcvd: 0,
text: std::ptr::null_mut(),
context,
rfc724_mid: std::ptr::null_mut(),
in_reply_to: std::ptr::null_mut(),
server_folder: std::ptr::null_mut(),
server_uid: 0,
is_dc_message: 0,
starred: 0,
chat_blocked: 0,
location_id: 0,
param: Params::new(),
};
msg
Box::into_raw(Box::new(msg))
}
pub unsafe fn dc_msg_unref(mut msg: *mut dc_msg_t) {
@@ -250,9 +271,8 @@ pub unsafe fn dc_msg_unref(mut msg: *mut dc_msg_t) {
return;
}
dc_msg_empty(msg);
dc_param_unref((*msg).param);
(*msg).magic = 0i32 as uint32_t;
free(msg as *mut libc::c_void);
Box::from_raw(msg);
}
pub unsafe fn dc_msg_empty(mut msg: *mut dc_msg_t) {
@@ -267,37 +287,38 @@ pub unsafe fn dc_msg_empty(mut msg: *mut dc_msg_t) {
(*msg).in_reply_to = 0 as *mut libc::c_char;
free((*msg).server_folder as *mut libc::c_void);
(*msg).server_folder = 0 as *mut libc::c_char;
dc_param_set_packed((*msg).param, 0 as *const libc::c_char);
(*msg).param = Params::new();
(*msg).hidden = 0i32;
}
pub unsafe fn dc_msg_get_filemime(msg: *const dc_msg_t) -> *mut libc::c_char {
let mut ret: *mut libc::c_char = 0 as *mut libc::c_char;
let mut file: *mut libc::c_char = 0 as *mut libc::c_char;
let mut ret = 0 as *mut libc::c_char;
if !(msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint) {
ret = dc_param_get(
(*msg).param,
DC_PARAM_MIMETYPE as i32,
0 as *const libc::c_char,
);
if ret.is_null() {
file = dc_param_get((*msg).param, DC_PARAM_FILE as i32, 0 as *const libc::c_char);
if !file.is_null() {
dc_msg_guess_msgtype_from_suffix(file, 0 as *mut libc::c_int, &mut ret);
if ret.is_null() {
ret = dc_strdup(
b"application/octet-stream\x00" as *const u8 as *const libc::c_char,
)
match (*msg).param.get(Param::MimeType) {
Some(m) => {
ret = to_cstring(m);
}
None => {
if let Some(file) = (*msg).param.get(Param::File) {
let file_c = to_cstring(file);
dc_msg_guess_msgtype_from_suffix(file_c, 0 as *mut libc::c_int, &mut ret);
if ret.is_null() {
ret = dc_strdup(
b"application/octet-stream\x00" as *const u8 as *const libc::c_char,
)
}
free(file_c as *mut _);
}
}
}
}
free(file as *mut libc::c_void);
return if !ret.is_null() {
ret
} else {
dc_strdup(0 as *const libc::c_char)
};
if !ret.is_null() {
return ret;
}
dc_strdup(0 as *const libc::c_char)
}
#[allow(non_snake_case)]
@@ -356,20 +377,20 @@ pub unsafe fn dc_msg_guess_msgtype_from_suffix(
}
pub unsafe fn dc_msg_get_file(msg: *const dc_msg_t) -> *mut libc::c_char {
let mut file_rel: *mut libc::c_char = 0 as *mut libc::c_char;
let mut file_abs: *mut libc::c_char = 0 as *mut libc::c_char;
let mut file_abs = 0 as *mut libc::c_char;
if !(msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint) {
file_rel = dc_param_get((*msg).param, DC_PARAM_FILE as i32, 0 as *const libc::c_char);
if !file_rel.is_null() {
file_abs = dc_get_abs_path((*msg).context, file_rel)
if let Some(file_rel) = (*msg).param.get(Param::File) {
let file_rel_c = to_cstring(file_rel);
file_abs = dc_get_abs_path((*msg).context, file_rel_c);
free(file_rel_c as *mut _);
}
}
free(file_rel as *mut libc::c_void);
return if !file_abs.is_null() {
if !file_abs.is_null() {
file_abs
} else {
dc_strdup(0 as *const libc::c_char)
};
}
}
/**
@@ -407,7 +428,7 @@ pub unsafe fn dc_msg_has_location(msg: *const dc_msg_t) -> bool {
* @return None.
*/
pub unsafe fn dc_msg_set_location(
msg: *const dc_msg_t,
msg: *mut dc_msg_t,
latitude: libc::c_double,
longitude: libc::c_double,
) {
@@ -418,12 +439,8 @@ pub unsafe fn dc_msg_set_location(
return;
}
dc_param_set_float((*msg).param, DC_PARAM_SET_LATITUDE as libc::c_int, latitude);
dc_param_set_float(
(*msg).param,
DC_PARAM_SET_LONGITUDE as libc::c_int,
longitude,
);
(*msg).param.set_float(Param::SetLatitude, latitude);
(*msg).param.set_float(Param::SetLongitude, longitude);
}
pub unsafe fn dc_msg_get_timestamp(msg: *const dc_msg_t) -> i64 {
@@ -474,10 +491,7 @@ pub fn dc_msg_load_from_db<'a>(msg: *mut dc_msg_t<'a>, context: &'a Context, id:
(*msg).state = row.get(13)?;
(*msg).is_dc_message = row.get(14)?;
(*msg).text = to_cstring(row.get::<_, String>(15).unwrap_or_default());
dc_param_set_packed(
(*msg).param,
to_cstring(row.get::<_, String>(16)?)
);
(*msg).param = row.get::<_, String>(16)?.parse().unwrap_or_default();
(*msg).starred = row.get(17)?;
(*msg).hidden = row.get(18)?;
(*msg).location_id = row.get(19)?;
@@ -520,18 +534,18 @@ pub unsafe fn dc_delete_msgs(context: &Context, msg_ids: *const uint32_t, msg_cn
dc_update_msg_chat_id(context, *msg_ids.offset(i as isize), 3i32 as uint32_t);
dc_job_add(
context,
110i32,
110,
*msg_ids.offset(i as isize) as libc::c_int,
0 as *const libc::c_char,
0i32,
Params::new(),
0,
);
i += 1
}
if 0 != msg_cnt {
context.call_cb(Event::MSGS_CHANGED, 0i32 as uintptr_t, 0i32 as uintptr_t);
dc_job_kill_action(context, 105i32);
dc_job_add(context, 105i32, 0i32, 0 as *const libc::c_char, 10i32);
context.call_cb(Event::MSGS_CHANGED, 0 as uintptr_t, 0 as uintptr_t);
dc_job_kill_action(context, 105);
dc_job_add(context, 105, 0, Params::new(), 10);
};
}
@@ -581,7 +595,7 @@ pub fn dc_markseen_msgs(context: &Context, msg_ids: *const u32, msg_cnt: usize)
dc_update_msg_state(context, id, DC_STATE_IN_SEEN);
info!(context, 0, "Seen message #{}.", id);
unsafe { dc_job_add(context, 130, id as i32, 0 as *const libc::c_char, 0) };
unsafe { dc_job_add(context, 130, id as i32, Params::new(), 0) };
send_event = true;
}
} else if curr_state == DC_STATE_IN_FRESH {
@@ -711,81 +725,86 @@ pub unsafe fn dc_msg_get_text(msg: *const dc_msg_t) -> *mut libc::c_char {
#[allow(non_snake_case)]
pub unsafe fn dc_msg_get_filename(msg: *const dc_msg_t) -> *mut libc::c_char {
let mut ret: *mut libc::c_char = 0 as *mut libc::c_char;
let mut pathNfilename: *mut libc::c_char = 0 as *mut libc::c_char;
let mut ret = 0 as *mut libc::c_char;
if !(msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint) {
pathNfilename = dc_param_get((*msg).param, DC_PARAM_FILE as i32, 0 as *const libc::c_char);
if !pathNfilename.is_null() {
ret = dc_get_filename(pathNfilename)
if let Some(file) = (*msg).param.get(Param::File) {
let file_c = to_cstring(file);
ret = dc_get_filename(file_c);
free(file_c as *mut _);
}
}
free(pathNfilename as *mut libc::c_void);
return if !ret.is_null() {
if !ret.is_null() {
ret
} else {
dc_strdup(0 as *const libc::c_char)
};
}
}
pub unsafe fn dc_msg_get_filebytes(msg: *const dc_msg_t) -> uint64_t {
let mut ret: uint64_t = 0i32 as uint64_t;
let mut file: *mut libc::c_char = 0 as *mut libc::c_char;
let mut ret = 0;
if !(msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint) {
file = dc_param_get((*msg).param, DC_PARAM_FILE as i32, 0 as *const libc::c_char);
if !file.is_null() {
ret = dc_get_filebytes((*msg).context, file)
if let Some(file) = (*msg).param.get(Param::File) {
let file_c = to_cstring(file);
ret = dc_get_filebytes((*msg).context, file_c);
free(file_c as *mut _);
}
}
free(file as *mut libc::c_void);
ret
}
pub unsafe fn dc_msg_get_width(msg: *const dc_msg_t) -> libc::c_int {
if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint {
return 0i32;
}
dc_param_get_int((*msg).param, DC_PARAM_WIDTH as i32, 0)
}
pub unsafe fn dc_msg_get_height(msg: *const dc_msg_t) -> libc::c_int {
if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint {
return 0i32;
}
dc_param_get_int((*msg).param, DC_PARAM_HEIGHT as i32, 0)
}
pub unsafe fn dc_msg_get_duration(msg: *const dc_msg_t) -> libc::c_int {
if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint {
return 0;
}
dc_param_get_int((*msg).param, DC_PARAM_DURATION as i32, 0)
(*msg).param.get_int(Param::Width).unwrap_or_default()
}
pub unsafe fn dc_msg_get_height(msg: *const dc_msg_t) -> libc::c_int {
if msg.is_null() || (*msg).magic != 0x11561156 as libc::c_uint {
return 0;
}
(*msg).param.get_int(Param::Height).unwrap_or_default()
}
pub unsafe fn dc_msg_get_duration(msg: *const dc_msg_t) -> libc::c_int {
if msg.is_null() || (*msg).magic != 0x11561156 as libc::c_uint {
return 0;
}
(*msg).param.get_int(Param::Duration).unwrap_or_default()
}
// TODO should return bool /rtn
pub unsafe fn dc_msg_get_showpadlock(msg: *const dc_msg_t) -> libc::c_int {
if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint {
return 0i32;
if msg.is_null() || (*msg).magic != 0x11561156 as libc::c_uint {
return 0;
}
if dc_param_get_int((*msg).param, DC_PARAM_GUARANTEE_E2EE as i32, 0) != 0i32 {
return 1i32;
if (*msg)
.param
.get_int(Param::GuranteeE2ee)
.unwrap_or_default()
!= 0
{
return 1;
}
0
}
pub unsafe fn dc_msg_get_summary<'a>(
msg: *const dc_msg_t<'a>,
msg: *mut dc_msg_t<'a>,
mut chat: *const Chat<'a>,
) -> *mut dc_lot_t {
let current_block: u64;
let ret: *mut dc_lot_t = dc_lot_new();
let mut contact: *mut dc_contact_t = 0 as *mut dc_contact_t;
let mut chat_to_delete: *mut Chat = 0 as *mut Chat;
if !(msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint) {
if !(msg.is_null() || (*msg).magic != 0x11561156 as libc::c_uint) {
if chat.is_null() {
chat_to_delete = dc_get_chat((*msg).context, (*msg).chat_id);
if chat_to_delete.is_null() {
@@ -800,8 +819,8 @@ pub unsafe fn dc_msg_get_summary<'a>(
match current_block {
15204159476013091401 => {}
_ => {
if (*msg).from_id != 1i32 as libc::c_uint
&& ((*chat).type_0 == 120i32 || (*chat).type_0 == 130i32)
if (*msg).from_id != 1 as libc::c_uint
&& ((*chat).type_0 == 120 || (*chat).type_0 == 130)
{
contact = dc_get_contact((*chat).context, (*msg).from_id)
}
@@ -816,7 +835,7 @@ pub unsafe fn dc_msg_get_summary<'a>(
}
pub unsafe fn dc_msg_get_summarytext(
msg: *const dc_msg_t,
msg: *mut dc_msg_t,
approx_characters: libc::c_int,
) -> *mut libc::c_char {
if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint {
@@ -826,7 +845,7 @@ pub unsafe fn dc_msg_get_summarytext(
dc_msg_get_summarytext_by_raw(
(*msg).type_0,
(*msg).text,
(*msg).param,
&mut (*msg).param,
approx_characters,
(*msg).context,
)
@@ -837,7 +856,7 @@ pub unsafe fn dc_msg_get_summarytext(
pub unsafe fn dc_msg_get_summarytext_by_raw(
type_0: libc::c_int,
text: *const libc::c_char,
param: *mut dc_param_t,
param: &mut Params,
approx_characters: libc::c_int,
context: &Context,
) -> *mut libc::c_char {
@@ -853,15 +872,11 @@ pub unsafe fn dc_msg_get_summarytext_by_raw(
50 => prefix = to_cstring(context.stock_str(StockMessage::Video)),
41 => prefix = to_cstring(context.stock_str(StockMessage::VoiceMessage)),
40 | 60 => {
if dc_param_get_int(param, DC_PARAM_CMD as i32, 0) == 6 {
if param.get_int(Param::Cmd) == Some(6) {
prefix = to_cstring(context.stock_str(StockMessage::AcSetupMsgSubject));
append_text = 0i32
} else {
pathNfilename = dc_param_get(
param,
DC_PARAM_FILE as i32,
b"ErrFilename\x00" as *const u8 as *const libc::c_char,
);
pathNfilename = to_cstring(param.get(Param::File).unwrap_or_else(|| "ErrFilename"));
value = dc_get_filename(pathNfilename);
let label = CString::new(
context
@@ -881,9 +896,9 @@ pub unsafe fn dc_msg_get_summarytext_by_raw(
}
}
_ => {
if dc_param_get_int(param, DC_PARAM_CMD as i32, 0) == 9i32 {
if param.get_int(Param::Cmd) == Some(9) {
prefix = to_cstring(context.stock_str(StockMessage::Location));
append_text = 0i32
append_text = 0;
}
}
}
@@ -946,26 +961,26 @@ pub unsafe fn dc_msg_is_starred(msg: *const dc_msg_t) -> libc::c_int {
// TODO should return bool /rtn
pub unsafe fn dc_msg_is_forwarded(msg: *const dc_msg_t) -> libc::c_int {
if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint {
return 0i32;
return 0;
}
return if 0 != dc_param_get_int((*msg).param, DC_PARAM_FORWARDED as i32, 0) {
1i32
if 0 != (*msg).param.get_int(Param::Forwarded).unwrap_or_default() {
1
} else {
0i32
};
0
}
}
// TODO should return bool /rtn
pub unsafe fn dc_msg_is_info(msg: *const dc_msg_t) -> libc::c_int {
if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint {
return 0i32;
return 0;
}
let cmd: libc::c_int = dc_param_get_int((*msg).param, DC_PARAM_CMD as i32, 0);
let cmd = (*msg).param.get_int(Param::Cmd).unwrap_or_default();
if (*msg).from_id == 2i32 as libc::c_uint
|| (*msg).to_id == 2i32 as libc::c_uint
|| 0 != cmd && cmd != 6i32
{
return 1i32;
return 1;
}
0
@@ -992,7 +1007,7 @@ pub unsafe fn dc_msg_is_setupmessage(msg: *const dc_msg_t) -> bool {
return false;
}
dc_param_get_int((*msg).param, DC_PARAM_CMD as i32, 0) == 6
(*msg).param.get_int(Param::Cmd) == Some(6)
}
pub unsafe fn dc_msg_get_setupcodebegin(msg: *const dc_msg_t) -> *mut libc::c_char {
@@ -1061,23 +1076,27 @@ pub unsafe fn dc_msg_set_file(
if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint {
return;
}
dc_param_set((*msg).param, DC_PARAM_FILE as i32, file);
dc_param_set((*msg).param, DC_PARAM_MIMETYPE as i32, filemime);
if !file.is_null() {
(*msg).param.set(Param::File, as_str(file));
}
if !filemime.is_null() {
(*msg).param.set(Param::MimeType, as_str(filemime));
}
}
pub unsafe fn dc_msg_set_dimension(msg: *mut dc_msg_t, width: libc::c_int, height: libc::c_int) {
if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint {
return;
}
dc_param_set_int((*msg).param, DC_PARAM_WIDTH as i32, width);
dc_param_set_int((*msg).param, DC_PARAM_HEIGHT as i32, height);
(*msg).param.set_int(Param::Width, width);
(*msg).param.set_int(Param::Height, height);
}
pub unsafe fn dc_msg_set_duration(msg: *mut dc_msg_t, duration: libc::c_int) {
if msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint {
return;
}
dc_param_set_int((*msg).param, DC_PARAM_DURATION as i32, duration);
(*msg).param.set_int(Param::Duration, duration);
}
pub unsafe fn dc_msg_latefiling_mediasize(
@@ -1087,12 +1106,12 @@ pub unsafe fn dc_msg_latefiling_mediasize(
duration: libc::c_int,
) {
if !(msg.is_null() || (*msg).magic != 0x11561156i32 as libc::c_uint) {
if width > 0i32 && height > 0i32 {
dc_param_set_int((*msg).param, DC_PARAM_WIDTH as i32, width);
dc_param_set_int((*msg).param, DC_PARAM_HEIGHT as i32, height);
if width > 0 && height > 0 {
(*msg).param.set_int(Param::Width, width);
(*msg).param.set_int(Param::Height, height);
}
if duration > 0i32 {
dc_param_set_int((*msg).param, DC_PARAM_DURATION as i32, duration);
if duration > 0 {
(*msg).param.set_int(Param::Duration, duration);
}
dc_msg_save_param_to_disk(msg);
};
@@ -1107,7 +1126,7 @@ pub unsafe fn dc_msg_save_param_to_disk(msg: *mut dc_msg_t) -> bool {
(*msg).context,
&(*msg).context.sql,
"UPDATE msgs SET param=? WHERE id=?;",
params![as_str((*(*msg).param).packed), (*msg).id as i32],
params![(*msg).param.to_string(), (*msg).id as i32],
)
.is_ok()
}
@@ -1197,7 +1216,7 @@ pub unsafe fn dc_set_msg_failed(context: &Context, msg_id: uint32_t, error: *con
(*msg).state = DC_STATE_OUT_FAILED;
}
if !error.is_null() {
dc_param_set((*msg).param, DC_PARAM_ERROR as i32, error);
(*msg).param.set(Param::Error, as_str(error));
error!(context, 0, "{}", as_str(error),);
}
@@ -1205,7 +1224,7 @@ pub unsafe fn dc_set_msg_failed(context: &Context, msg_id: uint32_t, error: *con
context,
&context.sql,
"UPDATE msgs SET state=?, param=? WHERE id=?;",
params![(*msg).state, as_str((*(*msg).param).packed), msg_id as i32],
params![(*msg).state, (*msg).param.to_string(), msg_id as i32],
)
.is_ok()
{