From 2d06fb7cc37eb8c9c3a601e59f7c5300930f7638 Mon Sep 17 00:00:00 2001 From: Mika Attila Date: Sat, 7 Mar 2015 13:55:19 +0100 Subject: [PATCH] Implement a rudimentary API for showing a message. --- .gitignore | 1 + Cargo.toml | 6 ++++ examples/show.rs | 9 +++++ src/lib.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 examples/show.rs diff --git a/.gitignore b/.gitignore index 1e7caa9e..ce062cbd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ Cargo.lock target/ +.cargo/ diff --git a/Cargo.toml b/Cargo.toml index 4c3b1d15..809adcb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,3 +3,9 @@ name = "libnotify" version = "0.0.1" authors = ["Mika Attila "] + +[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" diff --git a/examples/show.rs b/examples/show.rs new file mode 100644 index 00000000..a38f10a2 --- /dev/null +++ b/examples/show.rs @@ -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(); +} diff --git a/src/lib.rs b/src/lib.rs index b811777c..b35a484b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,93 @@ -fn test() {} \ No newline at end of file +#![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 { + 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 { + 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(()) + } + } +}