mirror of
https://github.com/szymon-jozef/libnotify-rs.git
synced 2026-10-27 19:20:43 +01:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cb9af02a9c | |||
| 7ae93dca57 | |||
|
|
f37bf12370 | ||
| f850cea72a | |||
| 37787a5594 | |||
| a231712057 | |||
|
|
21a09f9722 | ||
| 173d04c39c | |||
| 6be65a249e | |||
|
|
2140751d2f | ||
| b4c8bceb4c |
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -47,9 +47,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-expr"
|
name = "cfg-expr"
|
||||||
version = "0.20.9"
|
version = "0.20.10"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fe4ece8474b5f766c63426647e7b4b316b67431ade1036a8313cee24a03ae917"
|
checksum = "9ba9e9ec16c447027685b1f897b720e18e9a8afd00bd7332c483537e38086c9f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"target-lexicon",
|
"target-lexicon",
|
||||||
@@ -231,9 +231,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.189"
|
version = "0.2.190"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"
|
checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libloading"
|
name = "libloading"
|
||||||
@@ -247,7 +247,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libnotify-rs"
|
name = "libnotify-rs"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bindgen",
|
"bindgen",
|
||||||
"glib",
|
"glib",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "libnotify-rs"
|
name = "libnotify-rs"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors = [ "szymon_jozef"]
|
authors = [ "szymon_jozef"]
|
||||||
description = "Rust bindings for libnotify"
|
description = "Rust bindings for libnotify"
|
||||||
|
|||||||
54
README.md
54
README.md
@@ -19,14 +19,7 @@
|
|||||||
|
|
||||||
This crate exposes a user friendly (at least I hope), Rust api for [libnotify](https://gitlab.gnome.org/GNOME/libnotify). Made using [bindgen](https://github.com/rust-lang/rust-bindgen).
|
This crate exposes a user friendly (at least I hope), Rust api for [libnotify](https://gitlab.gnome.org/GNOME/libnotify). Made using [bindgen](https://github.com/rust-lang/rust-bindgen).
|
||||||
|
|
||||||
## Structure
|
It comes with basic libnotify calls bindings and higher level abstraction over it.
|
||||||
Project is split into two modules:
|
|
||||||
- functions
|
|
||||||
- notifications
|
|
||||||
|
|
||||||
Which resembles libnotify structure. Every function is named similarly to its libnotify counterpart and it does the same things.
|
|
||||||
|
|
||||||
There are some helper structs and enums to make it more approachable to rust folks.
|
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
Available at [github pages](https://szymon-jozef.github.io/libnotify-rs/libnotify_rs/index.html);
|
Available at [github pages](https://szymon-jozef.github.io/libnotify-rs/libnotify_rs/index.html);
|
||||||
@@ -65,13 +58,15 @@ Refer to [examples dir](./examples)
|
|||||||
|
|
||||||
Sneak peek for making a notification:
|
Sneak peek for making a notification:
|
||||||
|
|
||||||
|
### Without my super cool wrapper (:()
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use libnotify_rs::{
|
use libnotify_rs::api::{
|
||||||
functions::{init, uninit},
|
functions::{init, uninit},
|
||||||
notification::Notification,
|
notification::{self, Notification},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), LibnotifyError> {
|
||||||
init("I love coffee")?;
|
init("I love coffee")?;
|
||||||
let mut notify = Notification::new(
|
let mut notify = Notification::new(
|
||||||
"Remember to drink coffe!",
|
"Remember to drink coffe!",
|
||||||
@@ -79,8 +74,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
None,
|
None,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
notify.set_timeout(libnotify_rs::notification::Timeout::Custom(1000 * 10)); // 10 secs
|
notify.set_timeout(notification::Timeout::Custom(1000 * 10)); // 10 secs
|
||||||
notify.set_urgency(libnotify_rs::notification::Urgency::Critical); // coffee is very important
|
notify.set_urgency(notification::Urgency::Critical); // coffee is very important
|
||||||
notify.set_category("Coffee")?;
|
notify.set_category("Coffee")?;
|
||||||
|
|
||||||
notify.show()?;
|
notify.show()?;
|
||||||
@@ -91,6 +86,36 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### With my super cool wrapper (:))
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use libnotify_rs::{builder::NotificationBuilder, context::LibnotifyContext};
|
||||||
|
|
||||||
|
/// Builder is recommended way to interact with libnotify notifications
|
||||||
|
|
||||||
|
fn main() -> Result<(), LibnotifyError> {
|
||||||
|
let ctx = LibnotifyContext::new("Coffee addicted reminder")?; // builder requires context
|
||||||
|
|
||||||
|
let notification_builder = NotificationBuilder::new(
|
||||||
|
"Remember about your coffee!",
|
||||||
|
"Only idiots forget about coffee!",
|
||||||
|
None,
|
||||||
|
&ctx,
|
||||||
|
)?
|
||||||
|
.set_urgency(libnotify_rs::api::notification::Urgency::Critical)
|
||||||
|
.set_timeout(libnotify_rs::api::notification::Timeout::Never)
|
||||||
|
.set_category("Coffee")?;
|
||||||
|
|
||||||
|
notification_builder.show()?;
|
||||||
|
|
||||||
|
// you don't need to uninitialize libnotify, cause context already does that!
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
To test those examples use:
|
To test those examples use:
|
||||||
```bash
|
```bash
|
||||||
cargo r --example <example name>
|
cargo r --example <example name>
|
||||||
@@ -116,6 +141,3 @@ It didn't write any of the actual code, nor any documentation.
|
|||||||
|
|
||||||
## Roadmap
|
## Roadmap
|
||||||
I will be happy to fix any bugs that you may find! Just create a new issue, or better yet a pull request.
|
I will be happy to fix any bugs that you may find! Just create a new issue, or better yet a pull request.
|
||||||
|
|
||||||
I'm thinking about a small wrapper around other code, which would initialize libnotify by itself, cleanup after itself, etc. Maybe someday…
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
None,
|
None,
|
||||||
&ctx,
|
&ctx,
|
||||||
)?
|
)?
|
||||||
.set_urgency(libnotify_rs::notification::Urgency::Critical)
|
.set_urgency(libnotify_rs::api::notification::Urgency::Critical)
|
||||||
.set_timeout(libnotify_rs::notification::Timeout::Never)
|
.set_timeout(libnotify_rs::api::notification::Timeout::Never)
|
||||||
.set_category("Coffee")?;
|
.set_category("Coffee")?;
|
||||||
|
|
||||||
notification_builder.show()?;
|
notification_builder.show()?;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use glib::MainLoop;
|
use glib::MainLoop;
|
||||||
use libnotify_rs::{
|
use libnotify_rs::{
|
||||||
functions::{init, uninit},
|
api::functions::{init, uninit},
|
||||||
notification::Notification,
|
api::notification::Notification,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use libnotify_rs::functions::{self, uninit};
|
use libnotify_rs::api::functions::{self, uninit};
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
functions::init("Morbius")?;
|
functions::init("Morbius")?;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use libnotify_rs::{
|
use libnotify_rs::api::{
|
||||||
functions::{init, uninit},
|
functions::{init, uninit},
|
||||||
notification::Notification,
|
notification::{self, Notification},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
@@ -11,8 +11,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
None,
|
None,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
notify.set_timeout(libnotify_rs::notification::Timeout::Custom(1000 * 10)); // 10 secs
|
notify.set_timeout(notification::Timeout::Custom(1000 * 10)); // 10 secs
|
||||||
notify.set_urgency(libnotify_rs::notification::Urgency::Critical); // coffee is very important
|
notify.set_urgency(notification::Urgency::Critical); // coffee is very important
|
||||||
notify.set_category("Coffee")?;
|
notify.set_category("Coffee")?;
|
||||||
|
|
||||||
notify.show()?;
|
notify.show()?;
|
||||||
|
|||||||
58
src/api/errors.rs
Normal file
58
src/api/errors.rs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/// Enum mapping every possible error that can happen in this lib, with a nice enum.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum LibnotifyError {
|
||||||
|
/// [Libnotify init](https://gnome.pages.gitlab.gnome.org/libnotify/func.init.html) failed
|
||||||
|
InitError,
|
||||||
|
/// `&str` contains a null terminator. Read more at [std documentation](https://doc.rust-lang.org/std/ffi/struct.NulError.html)
|
||||||
|
NulError(std::ffi::NulError),
|
||||||
|
/// [Libnotify notification constructor](https://gnome.pages.gitlab.gnome.org/libnotify/ctor.Notification.new.html) failed allocating
|
||||||
|
NewNotificationError,
|
||||||
|
/// Custom libnotify error with explanation.
|
||||||
|
GerrorError(String),
|
||||||
|
/// Data failed allocating. Could indicate that system doesn't have enough memory
|
||||||
|
AllocationError,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for LibnotifyError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
LibnotifyError::InitError => {
|
||||||
|
write!(f, "Libnotify failed to init")
|
||||||
|
}
|
||||||
|
|
||||||
|
LibnotifyError::NulError(_) => {
|
||||||
|
write!(f, "Argument contains a null terminator")
|
||||||
|
}
|
||||||
|
|
||||||
|
LibnotifyError::NewNotificationError => {
|
||||||
|
write!(f, "Notification constructor failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
LibnotifyError::GerrorError(e) => {
|
||||||
|
write!(f, "Libnotify returned an error: {}", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
LibnotifyError::AllocationError => {
|
||||||
|
write!(f, "Libnotify failed allocating memory")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for LibnotifyError {
|
||||||
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||||
|
match self {
|
||||||
|
LibnotifyError::InitError => None,
|
||||||
|
LibnotifyError::NulError(nul_error) => Some(nul_error),
|
||||||
|
LibnotifyError::NewNotificationError => None,
|
||||||
|
LibnotifyError::GerrorError(_) => None,
|
||||||
|
LibnotifyError::AllocationError => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::ffi::NulError> for LibnotifyError {
|
||||||
|
fn from(value: std::ffi::NulError) -> Self {
|
||||||
|
Self::NulError(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
use crate::utils::c_str_to_rs_str_and_free;
|
use crate::api::errors::LibnotifyError;
|
||||||
|
|
||||||
use crate::libnotify::*;
|
use super::utils::c_str_to_rs_str_and_free;
|
||||||
|
|
||||||
|
use super::libnotify::*;
|
||||||
|
|
||||||
/* Initting
|
/* Initting
|
||||||
*
|
*
|
||||||
@@ -10,7 +12,7 @@ use crate::libnotify::*;
|
|||||||
/// Initialized libnotify. This must be called before any other functions.
|
/// Initialized libnotify. This must be called before any other functions.
|
||||||
///
|
///
|
||||||
/// Starting from 0.8, if the provided app_name is NULL, libnotify will try to figure it out from the running application. Before it was not allowed, and was causing libnotify not to be initialized.
|
/// Starting from 0.8, if the provided app_name is NULL, libnotify will try to figure it out from the running application. Before it was not allowed, and was causing libnotify not to be initialized.
|
||||||
pub fn init<'a, T>(app_name: T) -> Result<(), Box<dyn std::error::Error>>
|
pub fn init<'a, T>(app_name: T) -> Result<(), LibnotifyError>
|
||||||
where
|
where
|
||||||
T: Into<Option<&'a str>>,
|
T: Into<Option<&'a str>>,
|
||||||
{
|
{
|
||||||
@@ -24,7 +26,7 @@ where
|
|||||||
)
|
)
|
||||||
} == 0
|
} == 0
|
||||||
{
|
{
|
||||||
return Err("Libnotify init failed".into());
|
return Err(LibnotifyError::InitError);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -116,6 +118,7 @@ pub fn get_server_caps() -> Option<Vec<String>> {
|
|||||||
if vec.is_empty() { None } else { Some(vec) }
|
if vec.is_empty() { None } else { Some(vec) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Contains server information. Refer to [libnotify get_server_info docs](https://gnome.pages.gitlab.gnome.org/libnotify/func.get_server_info.html) to better understand specific fields
|
||||||
pub struct ServerInfo {
|
pub struct ServerInfo {
|
||||||
/// A location to store the server name, or `None`
|
/// A location to store the server name, or `None`
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
@@ -160,7 +163,7 @@ pub fn get_server_info() -> Option<ServerInfo> {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/// Sets the application name
|
/// Sets the application name
|
||||||
pub fn set_app_name(app_name: &str) -> Result<(), std::ffi::NulError> {
|
pub fn set_app_name(app_name: &str) -> Result<(), LibnotifyError> {
|
||||||
let app_name_c = std::ffi::CString::new(app_name)?;
|
let app_name_c = std::ffi::CString::new(app_name)?;
|
||||||
unsafe { notify_set_app_name(app_name_c.as_ptr()) };
|
unsafe { notify_set_app_name(app_name_c.as_ptr()) };
|
||||||
|
|
||||||
@@ -171,7 +174,7 @@ pub fn set_app_name(app_name: &str) -> Result<(), std::ffi::NulError> {
|
|||||||
/// Available since: 0.8.4
|
/// Available since: 0.8.4
|
||||||
///
|
///
|
||||||
/// app_icon is icon name or path to an image
|
/// app_icon is icon name or path to an image
|
||||||
pub fn set_app_icon(app_icon: &str) -> Result<(), std::ffi::NulError> {
|
pub fn set_app_icon(app_icon: &str) -> Result<(), LibnotifyError> {
|
||||||
let app_icon_c = std::ffi::CString::new(app_icon)?;
|
let app_icon_c = std::ffi::CString::new(app_icon)?;
|
||||||
unsafe { notify_set_app_icon(app_icon_c.as_ptr()) };
|
unsafe { notify_set_app_icon(app_icon_c.as_ptr()) };
|
||||||
|
|
||||||
@@ -194,11 +197,10 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bad_init() {
|
fn test_bad_init() {
|
||||||
assert!(
|
assert!(matches!(
|
||||||
init("Mor\0ius")
|
init("Mor\0ius").unwrap_err(),
|
||||||
.unwrap_err()
|
LibnotifyError::NulError(_)
|
||||||
.is::<std::ffi::c_str::NulError>()
|
));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
8
src/api/mod.rs
Normal file
8
src/api/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
/// Custom error types
|
||||||
|
pub mod errors;
|
||||||
|
/// Same as [libnotify global functions](https://gnome.pages.gitlab.gnome.org/libnotify/index.html#functions)
|
||||||
|
pub mod functions;
|
||||||
|
mod libnotify;
|
||||||
|
/// Same as [libnotify notification](https://gnome.pages.gitlab.gnome.org/libnotify/class.Notification.html)
|
||||||
|
pub mod notification;
|
||||||
|
mod utils;
|
||||||
@@ -1,13 +1,20 @@
|
|||||||
use crate::libnotify::*;
|
use crate::api::errors::LibnotifyError;
|
||||||
|
|
||||||
|
use super::libnotify::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
/// Notification urgency level. Refer to [libnotify docs](https://gnome.pages.gitlab.gnome.org/libnotify/enum.Urgency.html) for more info
|
||||||
pub enum Urgency {
|
pub enum Urgency {
|
||||||
|
/// Low urgency. Used for unimportant notifications
|
||||||
Low,
|
Low,
|
||||||
|
/// Normal urgency. Used for most standard notifications
|
||||||
Normal,
|
Normal,
|
||||||
|
/// Critical urgency. Used for very important notifications
|
||||||
Critical,
|
Critical,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
/// Reason why notification was closed. Refer to [libnotify docs](https://gnome.pages.gitlab.gnome.org/libnotify/enum.ClosedReason.html)
|
||||||
pub enum ClosedReason {
|
pub enum ClosedReason {
|
||||||
/// Notification not closed
|
/// Notification not closed
|
||||||
Unset,
|
Unset,
|
||||||
@@ -29,25 +36,40 @@ pub enum ClosedReason {
|
|||||||
/// Used in set hint.
|
/// Used in set hint.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum HintValue {
|
pub enum HintValue {
|
||||||
|
#[allow(missing_docs)]
|
||||||
String(String),
|
String(String),
|
||||||
|
#[allow(missing_docs)]
|
||||||
Int32(i32),
|
Int32(i32),
|
||||||
|
#[allow(missing_docs)]
|
||||||
Boolean(bool),
|
Boolean(bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The timeout in milliseconds
|
/// Notification timeout, until close
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum Timeout {
|
pub enum Timeout {
|
||||||
|
#[allow(missing_docs)]
|
||||||
Default,
|
Default,
|
||||||
|
#[allow(missing_docs)]
|
||||||
Never,
|
Never,
|
||||||
|
/// Custom timeout in milliseconds
|
||||||
Custom(i32),
|
Custom(i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents one notification. Wraps most of [libnotify::notifaction](https://gnome.pages.gitlab.gnome.org/libnotify/class.Notification.html) functions.
|
||||||
pub struct Notification {
|
pub struct Notification {
|
||||||
inner: *mut NotifyNotification,
|
inner: *mut NotifyNotification,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Notification {
|
impl<'a> Notification {
|
||||||
pub fn new<T, Y>(summary: &str, body: T, icon: Y) -> Result<Self, Box<dyn std::error::Error>>
|
/// Create new notification.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// This function will return an error if the supplied bytes contain an
|
||||||
|
/// internal 0 byte. The [`NulError`](https://doc.rust-lang.org/stable/alloc/ffi/c_str/struct.NulError.html) returned will contain the bytes as well as
|
||||||
|
/// the position of the nul byte.
|
||||||
|
///
|
||||||
|
/// On libnotify fail it will return Err(&str.into). May change in the future.
|
||||||
|
pub fn new<T, Y>(summary: &str, body: T, icon: Y) -> Result<Self, LibnotifyError>
|
||||||
where
|
where
|
||||||
T: Into<Option<&'a str>>,
|
T: Into<Option<&'a str>>,
|
||||||
Y: Into<Option<&'a str>>,
|
Y: Into<Option<&'a str>>,
|
||||||
@@ -67,18 +89,27 @@ impl<'a> Notification {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if inner.is_null() {
|
if inner.is_null() {
|
||||||
return Err("notify_notification_new returned null".into());
|
return Err(LibnotifyError::NewNotificationError);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Self { inner })
|
Ok(Self { inner })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add callback to notification.
|
||||||
|
///
|
||||||
|
/// # Args
|
||||||
|
/// `action` – action identifier. `default` will work for most use cases. Other text will be
|
||||||
|
/// displayed as button names.
|
||||||
|
///
|
||||||
|
/// `label` – label for user
|
||||||
|
///
|
||||||
|
/// `callback` - any closure that accepts &str as an argument, where &str is action name.
|
||||||
pub fn add_action<F>(
|
pub fn add_action<F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
action: &str,
|
action: &str,
|
||||||
label: &str,
|
label: &str,
|
||||||
callback: F,
|
callback: F,
|
||||||
) -> Result<(), Box<dyn std::error::Error>>
|
) -> Result<(), LibnotifyError>
|
||||||
where
|
where
|
||||||
F: Fn(&str) + 'static,
|
F: Fn(&str) + 'static,
|
||||||
{
|
{
|
||||||
@@ -115,19 +146,19 @@ impl<'a> Notification {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Synchronously tells the notification server to hide the notification on the screen
|
/// Synchronously tells the notification server to hide the notification on the screen
|
||||||
pub fn close(&self) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn close(&self) -> Result<(), LibnotifyError> {
|
||||||
let mut gerror = std::ptr::null_mut() as *mut GError;
|
let mut gerror = std::ptr::null_mut() as *mut GError;
|
||||||
|
|
||||||
if unsafe { notify_notification_close(self.inner, &mut gerror) } == 0 {
|
if unsafe { notify_notification_close(self.inner, &mut gerror) } == 0 {
|
||||||
if gerror.is_null() {
|
if gerror.is_null() {
|
||||||
return Err("Unknown error".into());
|
return Err(LibnotifyError::AllocationError);
|
||||||
}
|
}
|
||||||
|
|
||||||
let e = unsafe { *gerror }.message;
|
let e = unsafe { *gerror }.message;
|
||||||
|
|
||||||
if e.is_null() {
|
if e.is_null() {
|
||||||
unsafe { g_error_free(gerror) };
|
unsafe { g_error_free(gerror) };
|
||||||
return Err("Unknown error".into());
|
return Err(LibnotifyError::AllocationError);
|
||||||
}
|
}
|
||||||
|
|
||||||
let e_str = unsafe { std::ffi::CStr::from_ptr(e) }
|
let e_str = unsafe { std::ffi::CStr::from_ptr(e) }
|
||||||
@@ -136,7 +167,7 @@ impl<'a> Notification {
|
|||||||
|
|
||||||
unsafe { g_error_free(gerror) };
|
unsafe { g_error_free(gerror) };
|
||||||
|
|
||||||
return Err(e_str.into());
|
return Err(LibnotifyError::GerrorError(e_str));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -162,7 +193,7 @@ impl<'a> Notification {
|
|||||||
/// `set_app_icon()`.
|
/// `set_app_icon()`.
|
||||||
///
|
///
|
||||||
/// Available since: 0.8.4
|
/// Available since: 0.8.4
|
||||||
pub fn set_app_icon(&mut self, app_icon: &str) -> Result<(), std::ffi::NulError> {
|
pub fn set_app_icon(&mut self, app_icon: &str) -> Result<(), LibnotifyError> {
|
||||||
let app_icon_c = std::ffi::CString::new(app_icon)?;
|
let app_icon_c = std::ffi::CString::new(app_icon)?;
|
||||||
unsafe { notify_notification_set_app_icon(self.inner, app_icon_c.as_ptr()) };
|
unsafe { notify_notification_set_app_icon(self.inner, app_icon_c.as_ptr()) };
|
||||||
|
|
||||||
@@ -174,7 +205,7 @@ impl<'a> Notification {
|
|||||||
/// If this function is not called, the application name will be set from the value used in init() or overridden with set_app_name().
|
/// If this function is not called, the application name will be set from the value used in init() or overridden with set_app_name().
|
||||||
///
|
///
|
||||||
/// Available since: 0.7.3
|
/// Available since: 0.7.3
|
||||||
pub fn set_app_name(&mut self, app_name: &str) -> Result<(), std::ffi::NulError> {
|
pub fn set_app_name(&mut self, app_name: &str) -> Result<(), LibnotifyError> {
|
||||||
let app_name_c = std::ffi::CString::new(app_name)?;
|
let app_name_c = std::ffi::CString::new(app_name)?;
|
||||||
unsafe { notify_notification_set_app_name(self.inner, app_name_c.as_ptr()) };
|
unsafe { notify_notification_set_app_name(self.inner, app_name_c.as_ptr()) };
|
||||||
|
|
||||||
@@ -184,7 +215,7 @@ impl<'a> Notification {
|
|||||||
/// Sets the category of this notification.
|
/// Sets the category of this notification.
|
||||||
///
|
///
|
||||||
/// This can be used by the notification server to filter or display the data in a certain way
|
/// This can be used by the notification server to filter or display the data in a certain way
|
||||||
pub fn set_category(&mut self, category: &str) -> Result<(), std::ffi::NulError> {
|
pub fn set_category(&mut self, category: &str) -> Result<(), LibnotifyError> {
|
||||||
let category_c = std::ffi::CString::new(category)?;
|
let category_c = std::ffi::CString::new(category)?;
|
||||||
unsafe { notify_notification_set_category(self.inner, category_c.as_ptr()) };
|
unsafe { notify_notification_set_category(self.inner, category_c.as_ptr()) };
|
||||||
|
|
||||||
@@ -194,11 +225,7 @@ impl<'a> Notification {
|
|||||||
/// Sets a hint for key with value value
|
/// Sets a hint for key with value value
|
||||||
///
|
///
|
||||||
/// Available since: 0.6
|
/// Available since: 0.6
|
||||||
pub fn set_hint(
|
pub fn set_hint(&mut self, key: &str, value: HintValue) -> Result<(), LibnotifyError> {
|
||||||
&mut self,
|
|
||||||
key: &str,
|
|
||||||
value: HintValue,
|
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let key_c = std::ffi::CString::new(key)?;
|
let key_c = std::ffi::CString::new(key)?;
|
||||||
|
|
||||||
let value_c = match value {
|
let value_c = match value {
|
||||||
@@ -213,7 +240,7 @@ impl<'a> Notification {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if value_c.is_null() {
|
if value_c.is_null() {
|
||||||
return Err("Couldn't create hint value".into());
|
return Err(LibnotifyError::AllocationError);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe { notify_notification_set_hint(self.inner, key_c.as_ptr(), value_c) };
|
unsafe { notify_notification_set_hint(self.inner, key_c.as_ptr(), value_c) };
|
||||||
@@ -246,19 +273,19 @@ impl<'a> Notification {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Tells the notification server to display the notification on the screen
|
/// Tells the notification server to display the notification on the screen
|
||||||
pub fn show(&self) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn show(&self) -> Result<(), LibnotifyError> {
|
||||||
let mut gerror = std::ptr::null_mut() as *mut GError;
|
let mut gerror = std::ptr::null_mut() as *mut GError;
|
||||||
|
|
||||||
if unsafe { notify_notification_show(self.inner, &mut gerror) } == 0 {
|
if unsafe { notify_notification_show(self.inner, &mut gerror) } == 0 {
|
||||||
if gerror.is_null() {
|
if gerror.is_null() {
|
||||||
return Err("Unknown error".into());
|
return Err(LibnotifyError::AllocationError);
|
||||||
}
|
}
|
||||||
|
|
||||||
let err_msg = unsafe { *gerror }.message;
|
let err_msg = unsafe { *gerror }.message;
|
||||||
|
|
||||||
if err_msg.is_null() {
|
if err_msg.is_null() {
|
||||||
unsafe { g_error_free(gerror) };
|
unsafe { g_error_free(gerror) };
|
||||||
return Err("Unknown error".into());
|
return Err(LibnotifyError::AllocationError);
|
||||||
}
|
}
|
||||||
|
|
||||||
let err_msg_str = unsafe { std::ffi::CStr::from_ptr(err_msg) }
|
let err_msg_str = unsafe { std::ffi::CStr::from_ptr(err_msg) }
|
||||||
@@ -266,7 +293,7 @@ impl<'a> Notification {
|
|||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
unsafe { g_error_free(gerror) };
|
unsafe { g_error_free(gerror) };
|
||||||
return Err(err_msg_str.into());
|
return Err(LibnotifyError::GerrorError(err_msg_str));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -7,7 +7,7 @@ pub unsafe fn c_str_to_rs_str_and_free(c_str: *mut std::os::raw::c_char) -> Opti
|
|||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
unsafe { crate::libnotify::g_free(c_str as *mut std::ffi::c_void) };
|
unsafe { super::libnotify::g_free(c_str as *mut std::ffi::c_void) };
|
||||||
|
|
||||||
Some(normal_string)
|
Some(normal_string)
|
||||||
}
|
}
|
||||||
23
src/lib.rs
23
src/lib.rs
@@ -1,11 +1,20 @@
|
|||||||
#![cfg_attr(not(doctest), doc = include_str!("./../README.md"))]
|
//! This crate exposes two modules:
|
||||||
|
//!
|
||||||
|
//! # Api
|
||||||
|
//! This module contains a basic wrapper for libnotify calls. It just translates data types, etc,
|
||||||
|
//! so usage is basically the same as libnotify.
|
||||||
|
//!
|
||||||
|
//! # Wrappers
|
||||||
|
//! This module is a higher level abstraction over api. Recommended to use over api.
|
||||||
|
//!
|
||||||
#![allow(non_upper_case_globals)]
|
#![allow(non_upper_case_globals)]
|
||||||
#![allow(non_camel_case_types)]
|
#![allow(non_camel_case_types)]
|
||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
pub mod builder;
|
/// Basic libnotify rust wrapper
|
||||||
pub mod context;
|
pub mod api;
|
||||||
pub mod functions;
|
/// Higher level abstraction over api
|
||||||
mod libnotify;
|
pub mod wrappers;
|
||||||
pub mod notification;
|
|
||||||
mod utils;
|
pub use wrappers::{builder, context};
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
context::LibnotifyContext,
|
api::{
|
||||||
|
errors::LibnotifyError,
|
||||||
notification::{HintValue, Notification, Timeout, Urgency},
|
notification::{HintValue, Notification, Timeout, Urgency},
|
||||||
|
},
|
||||||
|
wrappers::context::LibnotifyContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Higher level abstraction of `Notification`. Allow for builder pattern notification construction.
|
||||||
|
/// Requires `LibnotifyContext`
|
||||||
pub struct NotificationBuilder<'a> {
|
pub struct NotificationBuilder<'a> {
|
||||||
notification: Notification,
|
notification: Notification,
|
||||||
_context: &'a LibnotifyContext,
|
_context: &'a LibnotifyContext,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> NotificationBuilder<'a> {
|
impl<'a> NotificationBuilder<'a> {
|
||||||
|
/// Create new NotificationBuilder, with given context.
|
||||||
pub fn new<T, Y>(
|
pub fn new<T, Y>(
|
||||||
summary: &str,
|
summary: &str,
|
||||||
body: T,
|
body: T,
|
||||||
@@ -27,20 +33,25 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_app_name(mut self, app_name: &str) -> Result<Self, std::ffi::NulError> {
|
/// Sets app name for this specific notification.
|
||||||
|
pub fn set_app_name(mut self, app_name: &str) -> Result<Self, LibnotifyError> {
|
||||||
self.notification.set_app_name(app_name)?;
|
self.notification.set_app_name(app_name)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_app_icon(mut self, app_icon: &str) -> Result<Self, std::ffi::NulError> {
|
/// Sets app icon fot this specific notification.
|
||||||
|
pub fn set_app_icon(mut self, app_icon: &str) -> Result<Self, LibnotifyError> {
|
||||||
self.notification.set_app_icon(app_icon)?;
|
self.notification.set_app_icon(app_icon)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
pub fn set_category(mut self, category: &str) -> Result<Self, std::ffi::NulError> {
|
|
||||||
|
/// Sets category for this specific notification
|
||||||
|
pub fn set_category(mut self, category: &str) -> Result<Self, LibnotifyError> {
|
||||||
self.notification.set_category(category)?;
|
self.notification.set_category(category)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set hint for this specific notification
|
||||||
pub fn set_hint(
|
pub fn set_hint(
|
||||||
mut self,
|
mut self,
|
||||||
key: &str,
|
key: &str,
|
||||||
@@ -50,16 +61,19 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set timeout until close
|
||||||
pub fn set_timeout(mut self, timeout: Timeout) -> Self {
|
pub fn set_timeout(mut self, timeout: Timeout) -> Self {
|
||||||
self.notification.set_timeout(timeout);
|
self.notification.set_timeout(timeout);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set notification urgency level
|
||||||
pub fn set_urgency(mut self, urgency: Urgency) -> Self {
|
pub fn set_urgency(mut self, urgency: Urgency) -> Self {
|
||||||
self.notification.set_urgency(urgency);
|
self.notification.set_urgency(urgency);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add callback
|
||||||
pub fn add_action<F>(
|
pub fn add_action<F>(
|
||||||
mut self,
|
mut self,
|
||||||
action: &str,
|
action: &str,
|
||||||
@@ -73,11 +87,15 @@ impl<'a> NotificationBuilder<'a> {
|
|||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Show notification
|
||||||
|
///
|
||||||
|
/// Uses `&self` borrow, so you can use this object later, after showing.
|
||||||
pub fn show(&self) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn show(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
self.notification.show()?;
|
self.notification.show()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Close notification synchronously.
|
||||||
pub fn close(&self) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn close(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
self.notification.close()?;
|
self.notification.close()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -1,11 +1,20 @@
|
|||||||
use crate::functions::{init, is_initted, set_app_icon, set_app_name, uninit};
|
use crate::api::{
|
||||||
|
errors::LibnotifyError,
|
||||||
|
functions::{init, is_initted, set_app_icon, set_app_name, uninit},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Notification context. Handles libnotify initializing and uninitializing.
|
||||||
pub struct LibnotifyContext {}
|
pub struct LibnotifyContext {}
|
||||||
|
|
||||||
static IS_CONTEXT_CREATED: std::sync::atomic::AtomicBool =
|
static IS_CONTEXT_CREATED: std::sync::atomic::AtomicBool =
|
||||||
std::sync::atomic::AtomicBool::new(false);
|
std::sync::atomic::AtomicBool::new(false);
|
||||||
|
|
||||||
impl LibnotifyContext {
|
impl LibnotifyContext {
|
||||||
|
/// Try initializing libnotify
|
||||||
|
///
|
||||||
|
/// # Error
|
||||||
|
/// Returns an error, if libnotify cannot be initialized or if there already exists a context
|
||||||
|
/// object
|
||||||
pub fn new(app_name: &str) -> Result<LibnotifyContext, Box<dyn std::error::Error>> {
|
pub fn new(app_name: &str) -> Result<LibnotifyContext, Box<dyn std::error::Error>> {
|
||||||
if IS_CONTEXT_CREATED.swap(true, std::sync::atomic::Ordering::SeqCst) {
|
if IS_CONTEXT_CREATED.swap(true, std::sync::atomic::Ordering::SeqCst) {
|
||||||
return Err("You can create only one context at a time".into());
|
return Err("You can create only one context at a time".into());
|
||||||
@@ -18,12 +27,14 @@ impl LibnotifyContext {
|
|||||||
Ok(Self {})
|
Ok(Self {})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_new_name(&self, app_name: &str) -> Result<(), std::ffi::NulError> {
|
/// Sets global app name
|
||||||
|
pub fn set_new_name(&self, app_name: &str) -> Result<(), LibnotifyError> {
|
||||||
set_app_name(app_name)?;
|
set_app_name(app_name)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_app_icon(&self, app_icon: &str) -> Result<(), std::ffi::NulError> {
|
/// Sets global app icon
|
||||||
|
pub fn set_app_icon(&self, app_icon: &str) -> Result<(), LibnotifyError> {
|
||||||
set_app_icon(app_icon)?;
|
set_app_icon(app_icon)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
4
src/wrappers/mod.rs
Normal file
4
src/wrappers/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
/// Contains notification builder
|
||||||
|
pub mod builder;
|
||||||
|
/// Containx libnotify context
|
||||||
|
pub mod context;
|
||||||
Reference in New Issue
Block a user