Cosmetics
This commit is contained in:
parent
95263c33f8
commit
73eb0cc68d
@ -4,7 +4,8 @@ fn main() {
|
|||||||
// Create a libnotify context
|
// Create a libnotify context
|
||||||
let notify = libnotify::Context::new("myapp").unwrap();
|
let notify = libnotify::Context::new("myapp").unwrap();
|
||||||
// Create a new notification and show it
|
// Create a new notification and show it
|
||||||
let n = notify.new_notification("Summary", Some("Optional Body"), None).unwrap();
|
let n = notify.new_notification("Summary", Some("Optional Body"), None)
|
||||||
|
.unwrap();
|
||||||
n.show().unwrap();
|
n.show().unwrap();
|
||||||
// You can also use the .show() convenience method on the context
|
// You can also use the .show() convenience method on the context
|
||||||
notify.show("I am another notification", None, None).unwrap();
|
notify.show("I am another notification", None, None).unwrap();
|
||||||
|
74
src/lib.rs
74
src/lib.rs
@ -19,19 +19,20 @@
|
|||||||
|
|
||||||
#![warn(missing_docs)]
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
extern crate libnotify_sys as sys;
|
|
||||||
extern crate glib_sys;
|
|
||||||
extern crate glib;
|
extern crate glib;
|
||||||
|
extern crate glib_sys;
|
||||||
extern crate gtypes;
|
extern crate gtypes;
|
||||||
|
extern crate libnotify_sys as sys;
|
||||||
|
|
||||||
use std::ffi::{self, CStr, CString};
|
|
||||||
use std::os::raw::c_int;
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
use std::fmt;
|
|
||||||
use std::error::Error;
|
|
||||||
use glib::translate::*;
|
use glib::translate::*;
|
||||||
|
|
||||||
use gtypes::{TRUE, FALSE};
|
use gtypes::{TRUE, FALSE};
|
||||||
|
use std::error::Error;
|
||||||
|
use std::ffi::{self, CStr, CString};
|
||||||
|
use std::fmt;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::os::raw::c_int;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Error that can happen on context creation
|
/// Error that can happen on context creation
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -61,6 +62,7 @@ impl From<ffi::NulError> for ContextCreationError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
/// An error that can happen when attempting to create a notification.
|
/// An error that can happen when attempting to create a notification.
|
||||||
pub enum NotificationCreationError {
|
pub enum NotificationCreationError {
|
||||||
@ -95,6 +97,7 @@ impl Error for NotificationCreationError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// The context which within libnotify operates.
|
/// The context which within libnotify operates.
|
||||||
///
|
///
|
||||||
/// Only one context can exist at a time.
|
/// Only one context can exist at a time.
|
||||||
@ -118,6 +121,7 @@ impl Context {
|
|||||||
}
|
}
|
||||||
Ok(Context)
|
Ok(Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new Notification.
|
/// Creates a new Notification.
|
||||||
///
|
///
|
||||||
/// Arguments:
|
/// Arguments:
|
||||||
@ -125,11 +129,12 @@ impl Context {
|
|||||||
/// - summary: Required summary text
|
/// - summary: Required summary text
|
||||||
/// - body: Optional body text
|
/// - body: Optional body text
|
||||||
/// - icon: Optional icon theme icon name or filename
|
/// - icon: Optional icon theme icon name or filename
|
||||||
pub fn new_notification(&self,
|
pub fn new_notification
|
||||||
summary: &str,
|
(&self,
|
||||||
body: Option<&str>,
|
summary: &str,
|
||||||
icon: Option<&str>)
|
body: Option<&str>,
|
||||||
-> Result<Notification, NotificationCreationError> {
|
icon: Option<&str>)
|
||||||
|
-> Result<Notification, NotificationCreationError> {
|
||||||
let summary = try!(CString::new(summary));
|
let summary = try!(CString::new(summary));
|
||||||
let body = match body {
|
let body = match body {
|
||||||
Some(body) => Some(try!(CString::new(body))),
|
Some(body) => Some(try!(CString::new(body))),
|
||||||
@ -149,17 +154,20 @@ impl Context {
|
|||||||
};
|
};
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let n = sys::notify_notification_new(summary.as_ptr(), body_ptr, icon_ptr);
|
let n = sys::notify_notification_new(summary.as_ptr(),
|
||||||
|
body_ptr,
|
||||||
|
icon_ptr);
|
||||||
if n.is_null() {
|
if n.is_null() {
|
||||||
return Err(NotificationCreationError::Unknown);
|
return Err(NotificationCreationError::Unknown);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Notification {
|
Ok(Notification {
|
||||||
handle: n,
|
handle: n,
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Show a notification.
|
/// Show a notification.
|
||||||
///
|
///
|
||||||
/// This is a convenience method that creates a new notification,
|
/// This is a convenience method that creates a new notification,
|
||||||
@ -183,6 +191,7 @@ impl Drop for Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// A passive pop-up notification
|
/// A passive pop-up notification
|
||||||
pub struct Notification<'a> {
|
pub struct Notification<'a> {
|
||||||
handle: *mut sys::NotifyNotification,
|
handle: *mut sys::NotifyNotification,
|
||||||
@ -198,8 +207,10 @@ impl<'a> Notification<'a> {
|
|||||||
sys::notify_notification_show(self.handle, &mut err);
|
sys::notify_notification_show(self.handle, &mut err);
|
||||||
if !err.is_null() {
|
if !err.is_null() {
|
||||||
let result = Err(NotificationShowError {
|
let result = Err(NotificationShowError {
|
||||||
message: CStr::from_ptr((*err).message).to_string_lossy().into_owned(),
|
message: CStr::from_ptr((*err).message)
|
||||||
});
|
.to_string_lossy()
|
||||||
|
.into_owned(),
|
||||||
|
});
|
||||||
glib_sys::g_error_free(err);
|
glib_sys::g_error_free(err);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -212,11 +223,7 @@ impl<'a> Notification<'a> {
|
|||||||
pub fn set_notification_timeout(&self, timeout: i32) {
|
pub fn set_notification_timeout(&self, timeout: i32) {
|
||||||
let _timeout: c_int = From::from(timeout);
|
let _timeout: c_int = From::from(timeout);
|
||||||
|
|
||||||
unsafe {
|
unsafe { sys::notify_notification_set_timeout(self.handle, _timeout) }
|
||||||
sys::notify_notification_set_timeout(self.handle,
|
|
||||||
_timeout)
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates the notification text and icon. This won't send the update
|
/// Updates the notification text and icon. This won't send the update
|
||||||
@ -225,7 +232,8 @@ impl<'a> Notification<'a> {
|
|||||||
pub fn update(&self,
|
pub fn update(&self,
|
||||||
summary: &str,
|
summary: &str,
|
||||||
body: Option<&str>,
|
body: Option<&str>,
|
||||||
icon: Option<&str>) -> Result<(), NotificationCreationError> {
|
icon: Option<&str>)
|
||||||
|
-> Result<(), NotificationCreationError> {
|
||||||
let summary = try!(CString::new(summary));
|
let summary = try!(CString::new(summary));
|
||||||
let body = match body {
|
let body = match body {
|
||||||
Some(body) => Some(try!(CString::new(body))),
|
Some(body) => Some(try!(CString::new(body))),
|
||||||
@ -246,9 +254,9 @@ impl<'a> Notification<'a> {
|
|||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let b = sys::notify_notification_update(self.handle,
|
let b = sys::notify_notification_update(self.handle,
|
||||||
summary.as_ptr(),
|
summary.as_ptr(),
|
||||||
body_ptr,
|
body_ptr,
|
||||||
icon_ptr);
|
icon_ptr);
|
||||||
if b == FALSE {
|
if b == FALSE {
|
||||||
return Err(NotificationCreationError::InvalidParameter);
|
return Err(NotificationCreationError::InvalidParameter);
|
||||||
}
|
}
|
||||||
@ -259,7 +267,10 @@ impl<'a> Notification<'a> {
|
|||||||
|
|
||||||
/// Sets a hint for `key` with value `value`. If value is `None`,
|
/// Sets a hint for `key` with value `value`. If value is `None`,
|
||||||
/// then key is unset.
|
/// then key is unset.
|
||||||
pub fn set_hint(&self, key: &str, value: Option<glib::variant::Variant>) -> Result<(), NotificationCreationError> {
|
pub fn set_hint(&self,
|
||||||
|
key: &str,
|
||||||
|
value: Option<glib::variant::Variant>)
|
||||||
|
-> Result<(), NotificationCreationError> {
|
||||||
let key = try!(CString::new(key));
|
let key = try!(CString::new(key));
|
||||||
|
|
||||||
let gvalue: *mut glib_sys::GVariant = {
|
let gvalue: *mut glib_sys::GVariant = {
|
||||||
@ -270,15 +281,14 @@ impl<'a> Notification<'a> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
sys::notify_notification_set_hint(self.handle,
|
sys::notify_notification_set_hint(self.handle, key.as_ptr(), gvalue)
|
||||||
key.as_ptr(),
|
|
||||||
gvalue)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// An error that can happen when attempting to show a notification.
|
/// An error that can happen when attempting to show a notification.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct NotificationShowError {
|
pub struct NotificationShowError {
|
||||||
|
Loading…
Reference in New Issue
Block a user