introduce set_stock_translation and remove call to DC_EVENT_GET_STRING

This commit is contained in:
holger krekel
2019-10-09 09:48:24 +02:00
parent 24d744b94c
commit af9ae153ae
4 changed files with 64 additions and 30 deletions

View File

@@ -402,6 +402,16 @@ int dc_set_config (dc_context_t* context, const char*
*/ */
char* dc_get_config (dc_context_t* context, const char* key); char* dc_get_config (dc_context_t* context, const char* key);
/**
* Set (translated) stock string
*
* @param context The context object
* @param stock_id the integer id of the stock message
* @param stock_msg the message to be used
* @return void
*/
int dc_set_stock_translation(dc_context_t* context, uint32_t, const char* value);
/** /**
* Get information about the context. * Get information about the context.
@@ -4316,7 +4326,8 @@ void dc_array_add_id (dc_array_t*, uint32_t); // depreca
#define DC_STR_MSGLOCATIONENABLED 64 #define DC_STR_MSGLOCATIONENABLED 64
#define DC_STR_MSGLOCATIONDISABLED 65 #define DC_STR_MSGLOCATIONDISABLED 65
#define DC_STR_LOCATION 66 #define DC_STR_LOCATION 66
#define DC_STR_COUNT 66 #define DC_STR_STICKER 67
#define DC_STR_COUNT 67
void dc_str_unref (char*); void dc_str_unref (char*);

View File

@@ -342,6 +342,29 @@ pub unsafe extern "C" fn dc_get_config(
} }
} }
#[no_mangle]
pub unsafe extern "C" fn dc_set_stock_translation(
context: *mut dc_context_t,
stock_id: u32,
stock_msg: *mut libc::c_char,
) -> libc::c_int {
if context.is_null() || stock_msg.is_null() {
eprintln!("ignoring careless call to dc_set_stock_string");
return;
}
let msg = as_str(stock_msg);
let ffi_context = &*context;
ffi_context
.with_inner(|ctx| match ctx.set_stock_translation(stock_id, msg) {
Ok(()) => 1,
Err(err) => {
warn!(ctx, "could not set translation: {}", err);
0
}
})
.unwrap_or(())
}
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn dc_get_info(context: *mut dc_context_t) -> *mut libc::c_char { pub unsafe extern "C" fn dc_get_info(context: *mut dc_context_t) -> *mut libc::c_char {
if context.is_null() { if context.is_null() {

View File

@@ -63,6 +63,7 @@ pub struct Context {
pub running_state: Arc<RwLock<RunningState>>, pub running_state: Arc<RwLock<RunningState>>,
/// Mutex to avoid generating the key for the user more than once. /// Mutex to avoid generating the key for the user more than once.
pub generating_key_mutex: Mutex<()>, pub generating_key_mutex: Mutex<()>,
pub translated_stockstrings: HashMap<usize, String>,
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
@@ -145,6 +146,7 @@ impl Context {
probe_imap_network: Arc::new(RwLock::new(false)), probe_imap_network: Arc::new(RwLock::new(false)),
perform_inbox_jobs_needed: Arc::new(RwLock::new(false)), perform_inbox_jobs_needed: Arc::new(RwLock::new(false)),
generating_key_mutex: Mutex::new(()), generating_key_mutex: Mutex::new(()),
translated_stockstrings: HashMap::new(),
}; };
ensure!( ensure!(

View File

@@ -5,8 +5,7 @@ use strum_macros::EnumProperty;
use crate::contact::*; use crate::contact::*;
use crate::context::Context; use crate::context::Context;
use crate::dc_tools::*; use crate::error::Error;
use crate::events::Event;
/// Stock strings /// Stock strings
/// ///
@@ -123,19 +122,26 @@ impl StockMessage {
} }
impl Context { impl Context {
/// Set the stock string for the [StockMessage].
///
pub fn set_stock_translation(
&mut self,
id: StockMessage,
stockstring: String,
) -> Result<(), Error> {
self.translated_stockstrings
.insert(id as usize, stockstring);
Ok(())
}
/// Return the stock string for the [StockMessage]. /// Return the stock string for the [StockMessage].
/// ///
/// If the context callback responds with a string to use, e.g. a /// Return a translation (if it was set with set_stock_translation before)
/// translation, then this string will be returned. Otherwise a /// or a default (English) string.
/// default (English) string is returned.
pub fn stock_str(&self, id: StockMessage) -> Cow<str> { pub fn stock_str(&self, id: StockMessage) -> Cow<str> {
let ptr = self.call_cb(Event::GetString { id, count: 0 }) as *mut libc::c_char; match self.translated_stockstrings.get(&(id as usize)) {
if ptr.is_null() { Some(x) => Cow::Borrowed(x),
Cow::Borrowed(id.fallback()) None => Cow::Borrowed(id.fallback()),
} else {
let ret = to_string_lossy(ptr);
unsafe { libc::free(ptr as *mut libc::c_void) };
Cow::Owned(ret)
} }
} }
@@ -239,7 +245,6 @@ mod tests {
use crate::test_utils::*; use crate::test_utils::*;
use crate::constants::DC_CONTACT_ID_SELF; use crate::constants::DC_CONTACT_ID_SELF;
use libc::uintptr_t;
use num_traits::ToPrimitive; use num_traits::ToPrimitive;
@@ -255,25 +260,18 @@ mod tests {
} }
#[test] #[test]
fn test_stock_str() { fn test_set_stock_translation() {
let t = dummy_context(); let mut t = dummy_context();
assert_eq!(t.ctx.stock_str(StockMessage::NoMessages), "No messages."); t.ctx
} .set_stock_translation(StockMessage::NoMessages, "xyz".to_string())
.unwrap();
fn test_stock_str_no_fallback_cb(_ctx: &Context, evt: Event) -> uintptr_t { assert_eq!(t.ctx.stock_str(StockMessage::NoMessages), "xyz")
match evt {
Event::GetString {
id: StockMessage::NoMessages,
..
} => unsafe { "Hello there".strdup() as usize },
_ => 0,
}
} }
#[test] #[test]
fn test_stock_str_no_fallback() { fn test_stock_str() {
let t = test_context(Some(Box::new(test_stock_str_no_fallback_cb))); let t = dummy_context();
assert_eq!(t.ctx.stock_str(StockMessage::NoMessages), "Hello there"); assert_eq!(t.ctx.stock_str(StockMessage::NoMessages), "No messages.");
} }
#[test] #[test]