Implement a rudimentary API for showing a message.

This commit is contained in:
Mika Attila 2015-03-07 13:55:19 +01:00
parent 67a9f17d0e
commit 2d06fb7cc3
4 changed files with 109 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
Cargo.lock
target/
.cargo/

View File

@ -3,3 +3,9 @@
name = "libnotify"
version = "0.0.1"
authors = ["Mika Attila <radiantstatue@gmail.com>"]
[dependencies.libnotify-sys]
git = "https://github.com/crumblingstatue/rust-libnotify-sys.git"
[dependencies.glib-2_0-sys]
git = "https://github.com/gi-rust/glib-sys.git"

9
examples/show.rs Normal file
View File

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

View File

@ -1 +1,93 @@
fn test() {}
#![feature(std_misc)]
extern crate "libnotify-sys" as sys;
extern crate "glib-2_0-sys" as glib;
use std::ffi::CString;
use glib::types::{
TRUE,
FALSE
};
/// Error that can happen on context creation
#[derive(Debug)]
pub enum ContextCreationError {
/// Context already exists
AlreadyExists,
InitFailure,
NulError
}
#[derive(Debug)]
pub enum NotificationCreationError {
NulError,
Unknown
}
pub struct Context;
impl Context {
pub fn new(app_name: &str) -> Result<Context, ContextCreationError> {
unsafe {
if sys::notify_is_initted() == TRUE {
return Err(ContextCreationError::AlreadyExists);
}
let app_name = match CString::new(app_name) {
Ok(name) => name,
Err(_) => return Err(ContextCreationError::NulError)
};
if sys::notify_init(app_name.as_ptr()) == FALSE {
return Err(ContextCreationError::InitFailure);
}
}
Ok(Context)
}
pub fn new_notification(&self, summary: &str, body: &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)
};
unsafe {
let n = sys::notify_notification_new(summary.as_ptr(),
body.as_ptr(),
std::ptr::null());
if n.is_null() {
return Err(NotificationCreationError::Unknown);
}
Ok(Notification{handle: n})
}
}
}
impl Drop for Context {
fn drop(&mut self) {
unsafe {
sys::notify_uninit();
}
}
}
pub struct Notification {
handle: *mut sys::NotifyNotification
}
impl Notification {
pub fn show(&self) -> Result<(), ()> {
unsafe {
let mut err: *mut glib::GError = std::ptr::null_mut();
sys::notify_notification_show(self.handle, &mut err);
if !err.is_null() {
glib::g_error_free(err);
return Err(())
}
return Ok(())
}
}
}