Implement a rudimentary API for showing a message.
This commit is contained in:
parent
67a9f17d0e
commit
2d06fb7cc3
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
Cargo.lock
|
Cargo.lock
|
||||||
target/
|
target/
|
||||||
|
.cargo/
|
||||||
|
@ -3,3 +3,9 @@
|
|||||||
name = "libnotify"
|
name = "libnotify"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
authors = ["Mika Attila <radiantstatue@gmail.com>"]
|
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
9
examples/show.rs
Normal 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();
|
||||||
|
}
|
94
src/lib.rs
94
src/lib.rs
@ -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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user