Merge pull request #498 from deltachat/return_bool

Resolve "should return bool" TODOs
This commit is contained in:
Alexander Krotov
2019-09-14 14:31:50 +00:00
committed by GitHub
10 changed files with 79 additions and 129 deletions

View File

@@ -868,7 +868,7 @@ unsafe fn set_draft_raw(context: &Context, chat_id: u32, mut msg: Option<&mut Me
} else if msgtype_has_file(msg.type_0) {
if let Some(path_filename) = msg.param.get(Param::File) {
let mut path_filename = path_filename.to_string();
if 0 != dc_msg_is_increation(msg) && !dc_is_blobdir_path(context, &path_filename) {
if dc_msg_is_increation(msg) && !dc_is_blobdir_path(context, &path_filename) {
OK_TO_CONTINUE = false;
} else if !dc_make_rel_and_copy(context, &mut path_filename) {
OK_TO_CONTINUE = false;
@@ -1271,7 +1271,7 @@ pub unsafe fn create_group_chat(
let chat_id = sql::get_rowid(context, &context.sql, "chats", "grpid", grpid);
if chat_id != 0 {
if 0 != add_to_chat_contacts_table(context, chat_id, 1) {
if add_to_chat_contacts_table(context, chat_id, 1) {
let mut draft_msg = dc_msg_new(Viewtype::Text);
dc_msg_set_text(&mut draft_msg, draft_txt.as_ptr());
set_draft_raw(context, chat_id, Some(&mut draft_msg));
@@ -1285,8 +1285,7 @@ pub unsafe fn create_group_chat(
/* you MUST NOT modify this or the following strings */
// Context functions to work with chats
// TODO should return bool /rtn
pub fn add_to_chat_contacts_table(context: &Context, chat_id: u32, contact_id: u32) -> libc::c_int {
pub fn add_to_chat_contacts_table(context: &Context, chat_id: u32, contact_id: u32) -> bool {
// add a contact to a chat; the function does not check the type or if any of the record exist or are already
// added to the chat!
sql::execute(
@@ -1295,27 +1294,26 @@ pub fn add_to_chat_contacts_table(context: &Context, chat_id: u32, contact_id: u
"INSERT INTO chats_contacts (chat_id, contact_id) VALUES(?, ?)",
params![chat_id as i32, contact_id as i32],
)
.is_ok() as libc::c_int
.is_ok()
}
pub unsafe fn add_contact_to_chat(context: &Context, chat_id: u32, contact_id: u32) -> libc::c_int {
pub unsafe fn add_contact_to_chat(context: &Context, chat_id: u32, contact_id: u32) -> bool {
add_contact_to_chat_ex(context, chat_id, contact_id, 0)
}
// TODO should return bool /rtn
#[allow(non_snake_case)]
pub fn add_contact_to_chat_ex(
context: &Context,
chat_id: u32,
contact_id: u32,
flags: libc::c_int,
) -> libc::c_int {
) -> bool {
let mut OK_TO_CONTINUE = true;
let mut success: libc::c_int = 0;
let mut success = false;
let contact = Contact::get_by_id(context, contact_id);
if contact.is_err() || chat_id <= DC_CHAT_ID_LAST_SPECIAL {
return 0;
return false;
}
let mut msg = dc_msg_new_untyped();
@@ -1352,7 +1350,7 @@ pub fn add_contact_to_chat_ex(
if 0 != is_contact_in_chat(context, chat_id, contact_id) {
if 0 == flags & 0x1 {
success = 1;
success = true;
OK_TO_CONTINUE = false;
}
} else {
@@ -1367,7 +1365,7 @@ pub fn add_contact_to_chat_ex(
}
}
if OK_TO_CONTINUE {
if 0 == add_to_chat_contacts_table(context, chat_id, contact_id) {
if !add_to_chat_contacts_table(context, chat_id, contact_id) {
OK_TO_CONTINUE = false;
}
}
@@ -1392,7 +1390,7 @@ pub fn add_contact_to_chat_ex(
);
}
context.call_cb(Event::MSGS_CHANGED, chat_id as uintptr_t, 0 as uintptr_t);
success = 1;
success = true;
}
}
}

View File

@@ -168,7 +168,7 @@ pub unsafe fn dc_initiate_key_transfer(context: &Context) -> *mut libc::c_char {
}
std::thread::sleep(std::time::Duration::from_secs(1));
if let Ok(msg) = dc_get_msg(context, msg_id) {
if 0 != dc_msg_is_sent(&msg) {
if dc_msg_is_sent(&msg) {
info!(context, "... setup message sent.",);
break;
}
@@ -520,7 +520,7 @@ pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: &Job) {
if ok_to_continue {
match what {
1 => {
if 0 != export_self_keys(context, param1.as_ptr()) {
if export_self_keys(context, param1.as_ptr()) {
info!(context, "Import/export completed.",);
success = 1
}
@@ -532,13 +532,13 @@ pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: &Job) {
}
}
11 => {
if 0 != export_backup(context, param1.as_ptr()) {
if export_backup(context, param1.as_ptr()) {
info!(context, "Import/export completed.",);
success = 1
}
}
12 => {
if 0 != import_backup(context, param1.as_ptr()) {
if import_backup(context, param1.as_ptr()) {
info!(context, "Import/export completed.",);
success = 1
}
@@ -561,9 +561,8 @@ pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: &Job) {
* Import backup
******************************************************************************/
// TODO should return bool /rtn
#[allow(non_snake_case)]
unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char) -> libc::c_int {
unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char) -> bool {
info!(
context,
"Import \"{}\" to \"{}\".",
@@ -576,7 +575,7 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char
if dc_is_configured(context) {
error!(context, "Cannot import backups to accounts in use.");
return 0;
return false;
}
&context.sql.close(&context);
dc_delete_file(context, context.get_dbfile().unwrap());
@@ -585,7 +584,7 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char
context,
"Cannot import backups: Cannot delete the old file.",
);
return 0;
return false;
}
if !dc_copy_file(
@@ -593,7 +592,7 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char
as_path(backup_to_import),
context.get_dbfile().unwrap(),
) {
return 0;
return false;
}
/* error already logged */
/* re-open copied database file */
@@ -601,7 +600,7 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char
.sql
.open(&context, &context.get_dbfile().unwrap(), 0)
{
return 0;
return false;
}
let total_files_cnt = context
@@ -680,7 +679,7 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char
sql::try_execute(context, &context.sql, "VACUUM;").ok();
Ok(())
})
.is_ok() as libc::c_int
.is_ok()
}
/*******************************************************************************
@@ -688,11 +687,10 @@ unsafe fn import_backup(context: &Context, backup_to_import: *const libc::c_char
******************************************************************************/
/* the FILE_PROGRESS macro calls the callback with the permille of files processed.
The macro avoids weird values of 0% or 100% while still working. */
// TODO should return bool /rtn
#[allow(non_snake_case)]
unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_int {
unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> bool {
let mut ok_to_continue: bool;
let mut success: libc::c_int = 0;
let mut success = false;
let mut delete_dest_file: libc::c_int = 0;
// get a fine backup file name (the name includes the date so that multiple backup instances are possible)
@@ -858,7 +856,7 @@ unsafe fn export_backup(context: &Context, dir: *const libc::c_char) -> libc::c_
dest_pathNfilename as uintptr_t,
0,
);
success = 1;
success = true;
}
}
} else {
@@ -1006,8 +1004,7 @@ unsafe fn import_self_keys(context: &Context, dir_name: *const libc::c_char) ->
imported_cnt
}
// TODO should return bool /rtn
unsafe fn export_self_keys(context: &Context, dir: *const libc::c_char) -> libc::c_int {
unsafe fn export_self_keys(context: &Context, dir: *const libc::c_char) -> bool {
let mut export_errors = 0;
context
@@ -1029,14 +1026,14 @@ unsafe fn export_self_keys(context: &Context, dir: *const libc::c_char) -> libc:
for key_pair in keys {
let (id, public_key, private_key, is_default) = key_pair?;
if let Some(key) = public_key {
if 0 == export_key_to_asc_file(context, dir, id, &key, is_default) {
if export_key_to_asc_file(context, dir, id, &key, is_default) {
export_errors += 1;
}
} else {
export_errors += 1;
}
if let Some(key) = private_key {
if 0 == export_key_to_asc_file(context, dir, id, &key, is_default) {
if export_key_to_asc_file(context, dir, id, &key, is_default) {
export_errors += 1;
}
} else {
@@ -1049,25 +1046,20 @@ unsafe fn export_self_keys(context: &Context, dir: *const libc::c_char) -> libc:
)
.unwrap();
if export_errors == 0 {
1
} else {
0
}
export_errors == 0
}
/*******************************************************************************
* Classic key export
******************************************************************************/
// TODO should return bool /rtn
unsafe fn export_key_to_asc_file(
context: &Context,
dir: *const libc::c_char,
id: libc::c_int,
key: &Key,
is_default: libc::c_int,
) -> libc::c_int {
let mut success: libc::c_int = 0i32;
) -> bool {
let mut success = false;
let file_name;
if 0 != is_default {
file_name = dc_mprintf(
@@ -1101,7 +1093,7 @@ unsafe fn export_key_to_asc_file(
file_name as uintptr_t,
0i32 as uintptr_t,
);
success = 1i32
success = true;
}
free(file_name as *mut libc::c_void);

View File

@@ -39,7 +39,7 @@ pub struct dc_mimefactory_t<'a> {
pub loaded: dc_mimefactory_loaded_t,
pub msg: Message,
pub chat: Option<Chat>,
pub increation: libc::c_int,
pub increation: bool,
pub in_reply_to: *mut libc::c_char,
pub references: *mut libc::c_char,
pub req_mdn: libc::c_int,
@@ -102,7 +102,7 @@ pub unsafe fn dc_mimefactory_load_msg(
loaded: DC_MF_NOTHING_LOADED,
msg,
chat: Some(chat),
increation: 0,
increation: false,
in_reply_to: ptr::null_mut(),
references: ptr::null_mut(),
req_mdn: 0,
@@ -290,7 +290,7 @@ pub unsafe fn dc_mimefactory_load_mdn<'a>(
loaded: DC_MF_NOTHING_LOADED,
msg,
chat: None,
increation: 0,
increation: false,
in_reply_to: ptr::null_mut(),
references: ptr::null_mut(),
req_mdn: 0,
@@ -334,11 +334,7 @@ pub unsafe fn dc_mimefactory_load_mdn<'a>(
Ok(factory)
}
// TODO should return bool /rtn
pub unsafe fn dc_mimefactory_render(
context: &Context,
factory: &mut dc_mimefactory_t,
) -> libc::c_int {
pub unsafe fn dc_mimefactory_render(context: &Context, factory: &mut dc_mimefactory_t) -> bool {
let subject: *mut mailimf_subject;
let mut ok_to_continue = true;
let imf_fields: *mut mailimf_fields;
@@ -348,7 +344,7 @@ pub unsafe fn dc_mimefactory_render(
let mut subject_str: *mut libc::c_char = ptr::null_mut();
let mut afwd_email: libc::c_int = 0;
let mut col: libc::c_int = 0;
let mut success: libc::c_int = 0;
let mut success = false;
let mut parts: libc::c_int = 0;
let mut e2ee_guaranteed: libc::c_int = 0;
let mut min_verified: libc::c_int = 0;
@@ -1046,7 +1042,7 @@ pub unsafe fn dc_mimefactory_render(
}
factory.out = mmap_string_new(b"\x00" as *const u8 as *const libc::c_char);
mailmime_write_mem(factory.out, &mut col, message);
success = 1;
success = true;
}
}

View File

@@ -1033,14 +1033,12 @@ unsafe fn dc_mimeparser_add_single_part_if_known(
|| (*mime_data).dt_data.dt_text.dt_length <= 0)
{
/* regard `Content-Transfer-Encoding:` */
if !(0
== mailmime_transfer_decode(
mime,
&mut decoded_data,
&mut decoded_data_bytes,
&mut transfer_decoding_buffer,
))
{
if mailmime_transfer_decode(
mime,
&mut decoded_data,
&mut decoded_data_bytes,
&mut transfer_decoding_buffer,
) {
/* no always error - but no data */
match mime_type {
60 | 70 => {
@@ -1352,13 +1350,12 @@ unsafe fn do_add_single_part(parser: &mut dc_mimeparser_t, mut part: dc_mimepart
parser.parts.push(part);
}
// TODO should return bool /rtn
pub unsafe fn mailmime_transfer_decode(
mime: *mut mailmime,
ret_decoded_data: *mut *const libc::c_char,
ret_decoded_data_bytes: *mut size_t,
ret_to_mmap_string_unref: *mut *mut libc::c_char,
) -> libc::c_int {
) -> bool {
let mut mime_transfer_encoding: libc::c_int = MAILMIME_MECHANISM_BINARY as libc::c_int;
let mime_data: *mut mailmime_data;
/* must not be free()'d */
@@ -1374,7 +1371,7 @@ pub unsafe fn mailmime_transfer_decode(
|| *ret_decoded_data_bytes != 0
|| !(*ret_to_mmap_string_unref).is_null()
{
return 0i32;
return false;
}
mime_data = (*mime).mm_data.mm_single;
if !(*mime).mm_mime_fields.is_null() {
@@ -1408,7 +1405,7 @@ pub unsafe fn mailmime_transfer_decode(
decoded_data = (*mime_data).dt_data.dt_text.dt_data;
decoded_data_bytes = (*mime_data).dt_data.dt_text.dt_length;
if decoded_data.is_null() || decoded_data_bytes <= 0 {
return 0i32;
return false;
}
} else {
let r: libc::c_int;
@@ -1425,7 +1422,7 @@ pub unsafe fn mailmime_transfer_decode(
|| transfer_decoding_buffer.is_null()
|| decoded_data_bytes <= 0
{
return 0i32;
return false;
}
decoded_data = transfer_decoding_buffer
}
@@ -1433,7 +1430,7 @@ pub unsafe fn mailmime_transfer_decode(
*ret_decoded_data_bytes = decoded_data_bytes;
*ret_to_mmap_string_unref = transfer_decoding_buffer;
1
true
}
pub unsafe fn dc_mimeparser_is_mailinglist_message(mimeparser: &dc_mimeparser_t) -> bool {

View File

@@ -831,7 +831,7 @@ unsafe fn handle_reports(
let mut report_body_bytes = 0;
let mut to_mmap_string_unref = std::ptr::null_mut();
if 0 != mailmime_transfer_decode(
if mailmime_transfer_decode(
report_data,
&mut report_body,
&mut report_body_bytes,

View File

@@ -1093,15 +1093,12 @@ Sent with my Delta Chat Messenger: https://delta.chat";
let mut decoded_data_bytes = 0;
let mut transfer_decoding_buffer: *mut libc::c_char = ptr::null_mut();
assert_eq!(
mailmime_transfer_decode(
msg1,
&mut decoded_data,
&mut decoded_data_bytes,
&mut transfer_decoding_buffer,
),
1
);
assert!(mailmime_transfer_decode(
msg1,
&mut decoded_data,
&mut decoded_data_bytes,
&mut transfer_decoding_buffer,
));
println!(
"{:?}",
String::from_utf8_lossy(std::slice::from_raw_parts(

View File

@@ -680,7 +680,7 @@ pub unsafe fn job_send_msg(context: &Context, msg_id: uint32_t) -> libc::c_int {
}
}
/* create message */
if 0 == dc_mimefactory_render(context, &mut mimefactory) {
if !dc_mimefactory_render(context, &mut mimefactory) {
dc_set_msg_failed(context, msg_id, as_opt_str(mimefactory.error));
} else if 0
!= mimefactory
@@ -990,7 +990,7 @@ fn connect_to_inbox(context: &Context, inbox: &Imap) -> libc::c_int {
fn send_mdn(context: &Context, msg_id: uint32_t) {
if let Ok(mut mimefactory) = unsafe { dc_mimefactory_load_mdn(context, msg_id) } {
if 0 != unsafe { dc_mimefactory_render(context, &mut mimefactory) } {
if unsafe { dc_mimefactory_render(context, &mut mimefactory) } {
add_smtp_job(context, Action::SendMdn, &mut mimefactory);
}
}

View File

@@ -87,7 +87,7 @@ impl Lot {
self.text1 = Some(context.stock_str(StockMessage::Draft).to_owned().into());
self.text1_meaning = Meaning::Text1Draft;
} else if msg.from_id == DC_CONTACT_ID_SELF {
if 0 != dc_msg_is_info(msg) || chat.is_self_talk() {
if dc_msg_is_info(msg) || chat.is_self_talk() {
self.text1 = None;
self.text1_meaning = Meaning::None;
} else {
@@ -95,7 +95,7 @@ impl Lot {
self.text1_meaning = Meaning::Text1Self;
}
} else if chat.typ == Chattype::Group || chat.typ == Chattype::VerifiedGroup {
if 0 != dc_msg_is_info(msg) || contact.is_none() {
if dc_msg_is_info(msg) || contact.is_none() {
self.text1 = None;
self.text1_meaning = Meaning::None;
} else {
@@ -699,13 +699,8 @@ pub fn dc_msg_get_duration(msg: &Message) -> libc::c_int {
msg.param.get_int(Param::Duration).unwrap_or_default()
}
// TODO should return bool /rtn
pub fn dc_msg_get_showpadlock(msg: &Message) -> libc::c_int {
if msg.param.get_int(Param::GuranteeE2ee).unwrap_or_default() != 0 {
return 1;
}
0
pub fn dc_msg_get_showpadlock(msg: &Message) -> bool {
msg.param.get_int(Param::GuranteeE2ee).unwrap_or_default() != 0
}
pub fn dc_msg_get_summary(context: &Context, msg: &mut Message, chat: Option<&Chat>) -> Lot {
@@ -825,48 +820,27 @@ pub unsafe fn dc_msg_has_deviating_timestamp(msg: &Message) -> libc::c_int {
(sort_timestamp / 86400 != send_timestamp / 86400) as libc::c_int
}
// TODO should return bool /rtn
pub fn dc_msg_is_sent(msg: &Message) -> libc::c_int {
if msg.state as i32 >= MessageState::OutDelivered as i32 {
1
} else {
0
}
pub fn dc_msg_is_sent(msg: &Message) -> bool {
msg.state as i32 >= MessageState::OutDelivered as i32
}
pub fn dc_msg_is_starred(msg: &Message) -> bool {
msg.starred
}
// TODO should return bool /rtn
pub fn dc_msg_is_forwarded(msg: &Message) -> libc::c_int {
if 0 != msg.param.get_int(Param::Forwarded).unwrap_or_default() {
1
} else {
0
}
pub fn dc_msg_is_forwarded(msg: &Message) -> bool {
0 != msg.param.get_int(Param::Forwarded).unwrap_or_default()
}
// TODO should return bool /rtn
pub fn dc_msg_is_info(msg: &Message) -> libc::c_int {
pub fn dc_msg_is_info(msg: &Message) -> bool {
let cmd = msg.param.get_int(Param::Cmd).unwrap_or_default();
if msg.from_id == 2i32 as libc::c_uint
msg.from_id == 2i32 as libc::c_uint
|| msg.to_id == 2i32 as libc::c_uint
|| 0 != cmd && cmd != 6i32
{
return 1;
}
0
}
// TODO should return bool /rtn
pub fn dc_msg_is_increation(msg: &Message) -> libc::c_int {
if chat::msgtype_has_file(msg.type_0) && msg.state == MessageState::OutPreparing {
1
} else {
0
}
pub fn dc_msg_is_increation(msg: &Message) -> bool {
chat::msgtype_has_file(msg.type_0) && msg.state == MessageState::OutPreparing
}
pub fn dc_msg_is_setupmessage(msg: &Message) -> bool {