Context::new_notification: Make body arg optional, add icon arg

This commit is contained in:
Mika Attila 2015-03-08 16:59:15 +01:00
parent a0a98a6521
commit 0e83385a01
2 changed files with 27 additions and 7 deletions

View File

@ -2,6 +2,6 @@ extern crate libnotify;
fn main() {
let notify = libnotify::Context::new("hello").unwrap();
let n = notify.new_notification("Hello, ", "World!").unwrap();
let n = notify.new_notification("Hello, ", Some("World!"), None).unwrap();
n.show().unwrap();
}

View File

@ -42,20 +42,40 @@ impl Context {
}
Ok(Context)
}
pub fn new_notification(&self, summary: &str, body: &str)
pub fn new_notification(&self, summary: &str,
body: Option<&str>,
icon: Option<&str>)
-> Result<Notification, NotificationCreationError> {
let summary = match CString::new(summary) {
Ok(cstr) => cstr,
Err(_) => return Err(NotificationCreationError::NulError)
};
let body = match CString::new(body) {
Ok(cstr) => cstr,
Err(_) => return Err(NotificationCreationError::NulError)
let body = match body {
Some(body) => match CString::new(body) {
Ok(cstr) => Some(cstr),
Err(_) => return Err(NotificationCreationError::NulError)
},
None => None
};
let body_ptr = match body {
Some(body) => body.as_ptr(),
None => std::ptr::null()
};
let icon = match icon {
Some(icon) => match CString::new(icon) {
Ok(cstr) => Some(cstr),
Err(_) => return Err(NotificationCreationError::NulError)
},
None => None
};
let icon_ptr = match icon {
Some(icon) => icon.as_ptr(),
None => std::ptr::null()
};
unsafe {
let n = sys::notify_notification_new(summary.as_ptr(),
body.as_ptr(),
std::ptr::null());
body_ptr,
icon_ptr);
if n.is_null() {
return Err(NotificationCreationError::Unknown);
}