Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e8e6f14474 | |||
| 7cd9643702 | |||
| 32d86da7c2 | |||
| bf90bccef8 | |||
| 7d80600657 | |||
|
|
5ab8f64e88 | ||
|
|
a61230b012 | ||
|
|
a74f4c9888 | ||
|
|
69a2d18218 | ||
|
|
c698097ffd | ||
|
|
8daef60a03 | ||
|
|
d282d2cd50 | ||
|
|
6251e0ad5c | ||
|
|
db2578ea92 | ||
|
|
ba947a35f8 | ||
|
|
503eee4441 | ||
|
|
4b0aaab709 | ||
|
|
94bb46164f | ||
|
|
3dfc03aebd | ||
|
|
7bc56e4974 | ||
|
|
d42152428f | ||
|
|
619409ffc4 | ||
|
|
8e7b939a78 | ||
|
|
d19aadb4d2 | ||
|
|
7556615e78 | ||
|
|
1636812abc | ||
|
|
d979d8a2d1 | ||
|
|
d23eabffea | ||
|
|
ffd99b67e2 | ||
|
|
72df2c0adb | ||
|
|
dac1075025 | ||
|
|
d3ad5323fb | ||
|
|
445f0b6d13 | ||
|
|
a0955acabd | ||
|
|
e9665b08c2 | ||
|
|
c66aec1c86 |
21
.travis.yml
Normal file
21
.travis.yml
Normal file
@@ -0,0 +1,21 @@
|
||||
language: rust
|
||||
rust:
|
||||
- nightly
|
||||
- beta
|
||||
- stable
|
||||
before_script:
|
||||
- |
|
||||
pip install 'travis-cargo<0.2' --user &&
|
||||
export PATH=$HOME/.local/bin:$PATH
|
||||
script:
|
||||
- |
|
||||
travis-cargo build &&
|
||||
travis-cargo test &&
|
||||
travis-cargo --only stable doc
|
||||
after_success:
|
||||
- travis-cargo --only stable doc-upload
|
||||
env:
|
||||
global:
|
||||
- TRAVIS_CARGO_NIGHTLY_FEATURE=""
|
||||
- secure: IjahTjjwmuvig/wTDMwxpk1F3Ywi2d0r17+JmOXHPdbfSfhi4puiTzsOgMjBhFtosTwcvlBeFwwFeTtl9KFNG165xm9Fqbhcez9sx+hS+EWZR/MBPFhKorJlgva0nuH8L1cxDUP+mDkcV/BdXCDeT7ml+y/FqEDAI4N0lwEsVMk=
|
||||
- secure: B8QPcNgwOmbJ8dNJn/p/tE1cLeUOLTo/Oj7nOBkK1tatMgS6yfQFa5pyNEqeTyZAyNptssqSH1BpRC3RxTJ/b+kFzdy8Kq1nkJfp9R9zTY34w5gWukfmmTH4Qe2lyWY/DKL1lGVjb+8mroV9sPaS5Y4DqYHbeYtpWF8Gi27vIL4=
|
||||
10
Cargo.toml
10
Cargo.toml
@@ -1,14 +1,16 @@
|
||||
[package]
|
||||
|
||||
name = "libnotify"
|
||||
version = "0.0.1"
|
||||
authors = ["Mika Attila <radiantstatue@gmail.com>"]
|
||||
version = "0.4.1"
|
||||
authors = ["Mika Attila <radiantstatue@gmail.com>", "Julian Ospald <hasufell@posteo.de>"]
|
||||
license = "MIT"
|
||||
description = "Rust bindings to libnotify"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/crumblingstatue/rust-libnotify"
|
||||
documentation = "http://crumblingstatue.github.io/doc/libnotify/libnotify/"
|
||||
keywords = ["libnotify", "notification"]
|
||||
|
||||
[dependencies]
|
||||
libnotify-sys = "*"
|
||||
glib-2_0-sys = "*"
|
||||
libnotify-sys = "0.3"
|
||||
glib-2-0-sys = "0.46.0"
|
||||
gtypes = "0.2"
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
[](https://travis-ci.org/crumblingstatue/rust-libnotify)
|
||||
|
||||
# libnotify-rs
|
||||
Rust binding to libnotify
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
extern crate libnotify;
|
||||
|
||||
fn main() {
|
||||
let notify = libnotify::Context::new("hello").unwrap();
|
||||
let n = notify.new_notification("This is the summary.",
|
||||
Some("This is the optional body text."),
|
||||
None).unwrap();
|
||||
// Create a libnotify context
|
||||
let notify = libnotify::Context::new("myapp").unwrap();
|
||||
// Create a new notification and show it
|
||||
let n = notify.new_notification("Summary", Some("Optional Body"), None).unwrap();
|
||||
n.show().unwrap();
|
||||
// You can also use the .show() convenience method on the context
|
||||
notify.show("I am another notification", None, None).unwrap();
|
||||
}
|
||||
|
||||
184
src/lib.rs
184
src/lib.rs
@@ -1,27 +1,92 @@
|
||||
extern crate "libnotify-sys" as sys;
|
||||
extern crate "glib-2_0-sys" as glib;
|
||||
//! Rustic bindings to [libnotify](https://developer.gnome.org/libnotify/)
|
||||
//!
|
||||
//! ```rust
|
||||
//! extern crate libnotify;
|
||||
//!
|
||||
//! fn main() {
|
||||
//! // Create a libnotify context
|
||||
//! let notify = libnotify::Context::new("myapp").unwrap();
|
||||
//! // Create a new notification and show it
|
||||
//! let n = notify.new_notification("Summary",
|
||||
//! Some("Optional Body"),
|
||||
//! None).unwrap();
|
||||
//! n.show().unwrap();
|
||||
//! // You can also use the .show() convenience method on the context
|
||||
//! notify.show("I am another notification", None, None).unwrap();
|
||||
//! }
|
||||
//!
|
||||
//! ```
|
||||
|
||||
use std::ffi::CString;
|
||||
#![warn(missing_docs)]
|
||||
|
||||
extern crate libnotify_sys as sys;
|
||||
extern crate glib_2_0_sys as glib;
|
||||
extern crate gtypes;
|
||||
|
||||
use std::ffi::{self, CStr, CString};
|
||||
use std::marker::PhantomData;
|
||||
use std::fmt;
|
||||
use std::error::Error;
|
||||
|
||||
use glib::types::{
|
||||
TRUE,
|
||||
FALSE
|
||||
};
|
||||
use gtypes::{TRUE, FALSE};
|
||||
|
||||
/// Error that can happen on context creation
|
||||
#[derive(Debug)]
|
||||
pub enum ContextCreationError {
|
||||
/// Context already exists
|
||||
/// Context already exists.
|
||||
AlreadyExists,
|
||||
InitFailure,
|
||||
NulError
|
||||
/// Failed to initialize libnotify.
|
||||
InitError,
|
||||
/// A nul byte was found in the provided string.
|
||||
NulError(ffi::NulError),
|
||||
}
|
||||
|
||||
impl fmt::Display for ContextCreationError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use ContextCreationError::*;
|
||||
match *self {
|
||||
AlreadyExists => write!(f, "A Libnotify context already exists."),
|
||||
InitError => write!(f, "Failed to initialize libnotify."),
|
||||
NulError(ref e) => write!(f, "{}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ffi::NulError> for ContextCreationError {
|
||||
fn from(src: ffi::NulError) -> Self {
|
||||
ContextCreationError::NulError(src)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// An error that can happen when attempting to create a notification.
|
||||
pub enum NotificationCreationError {
|
||||
NulError,
|
||||
Unknown
|
||||
/// A nul byte was found in the provided string.
|
||||
NulError(ffi::NulError),
|
||||
/// An unknown error happened.
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl fmt::Display for NotificationCreationError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use NotificationCreationError::*;
|
||||
match *self {
|
||||
NulError(ref e) => write!(f, "{}", e),
|
||||
Unknown => write!(f, "Unknown error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ffi::NulError> for NotificationCreationError {
|
||||
fn from(src: ffi::NulError) -> Self {
|
||||
NotificationCreationError::NulError(src)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for NotificationCreationError {
|
||||
fn description(&self) -> &str {
|
||||
"notification creation error"
|
||||
}
|
||||
}
|
||||
|
||||
/// The context which within libnotify operates.
|
||||
@@ -40,12 +105,9 @@ impl Context {
|
||||
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)
|
||||
};
|
||||
let app_name = try!(CString::new(app_name));
|
||||
if sys::notify_init(app_name.as_ptr()) == FALSE {
|
||||
return Err(ContextCreationError::InitFailure);
|
||||
return Err(ContextCreationError::InitError);
|
||||
}
|
||||
}
|
||||
Ok(Context)
|
||||
@@ -57,47 +119,54 @@ impl Context {
|
||||
/// - summary: Required summary text
|
||||
/// - body: Optional body text
|
||||
/// - icon: Optional icon theme icon name or filename
|
||||
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)
|
||||
};
|
||||
pub fn new_notification(&self,
|
||||
summary: &str,
|
||||
body: Option<&str>,
|
||||
icon: Option<&str>)
|
||||
-> Result<Notification, NotificationCreationError> {
|
||||
let summary = try!(CString::new(summary));
|
||||
let body = match body {
|
||||
Some(body) => match CString::new(body) {
|
||||
Ok(cstr) => Some(cstr),
|
||||
Err(_) => return Err(NotificationCreationError::NulError)
|
||||
},
|
||||
None => None
|
||||
Some(body) => Some(try!(CString::new(body))),
|
||||
None => None,
|
||||
};
|
||||
let body_ptr = match body {
|
||||
Some(body) => body.as_ptr(),
|
||||
None => std::ptr::null()
|
||||
Some(ref 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
|
||||
Some(icon) => Some(try!(CString::new(icon))),
|
||||
None => None,
|
||||
};
|
||||
let icon_ptr = match icon {
|
||||
Some(icon) => icon.as_ptr(),
|
||||
None => std::ptr::null()
|
||||
Some(ref icon) => icon.as_ptr(),
|
||||
None => std::ptr::null(),
|
||||
};
|
||||
|
||||
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() {
|
||||
return Err(NotificationCreationError::Unknown);
|
||||
}
|
||||
|
||||
Ok(Notification{handle: n, _phantom: PhantomData})
|
||||
Ok(Notification {
|
||||
handle: n,
|
||||
_phantom: PhantomData,
|
||||
})
|
||||
}
|
||||
}
|
||||
/// Show a notification.
|
||||
///
|
||||
/// This is a convenience method that creates a new notification,
|
||||
/// and shows it in one step.
|
||||
pub fn show(&self,
|
||||
summary: &str,
|
||||
body: Option<&str>,
|
||||
icon: Option<&str>)
|
||||
-> Result<(), Box<Error>> {
|
||||
let notif = try!(self.new_notification(summary, body, icon));
|
||||
try!(notif.show());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Context {
|
||||
@@ -111,21 +180,42 @@ impl Drop for Context {
|
||||
/// A passive pop-up notification
|
||||
pub struct Notification<'a> {
|
||||
handle: *mut sys::NotifyNotification,
|
||||
_phantom: PhantomData<&'a Context>
|
||||
_phantom: PhantomData<&'a Context>,
|
||||
}
|
||||
|
||||
impl<'a> Notification<'a> {
|
||||
/// Tells the notification server to display the notification
|
||||
/// on the screen.
|
||||
pub fn show(&'a self) -> Result<(), ()> {
|
||||
pub fn show(&'a self) -> Result<(), NotificationShowError> {
|
||||
unsafe {
|
||||
let mut err: *mut glib::GError = std::ptr::null_mut();
|
||||
sys::notify_notification_show(self.handle, &mut err);
|
||||
if !err.is_null() {
|
||||
let result = Err(NotificationShowError {
|
||||
message: CStr::from_ptr((*err).message).to_string_lossy().into_owned(),
|
||||
});
|
||||
glib::g_error_free(err);
|
||||
return Err(())
|
||||
return result;
|
||||
}
|
||||
return Ok(())
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An error that can happen when attempting to show a notification.
|
||||
#[derive(Debug)]
|
||||
pub struct NotificationShowError {
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl fmt::Display for NotificationShowError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Error showing notification: {}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for NotificationShowError {
|
||||
fn description(&self) -> &str {
|
||||
"notification show error"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user