使用dbus-rs实现D-Bus桌面通知

7
我希望通过使用https://crates.io/crates/dbus来通过D-BUS发送桌面通知。
我的当前方法是:
    let c = Connection::get_private(BusType::Session).unwrap();
//let m = Message::new_method_call("org.freedesktop.DBus", "/", "org.freedesktop.DBus", "ListNames").unwrap();
let mut m = Message::new_method_call(
    "org.freedesktop.Notifications",
    "/org/freedesktop/Notifications",
    "org.freedesktop.Notifications",
    "Notify"
    ).unwrap();
m.append_items(&[
       MessageItem::Str("appname".to_string()),         // appname
       MessageItem::UInt32(0),                          // notification to update
       MessageItem::Str("icon".to_string()),            // icon
       MessageItem::Str("summary".to_string()),         // summary (title)
       MessageItem::Str("body".to_string()),            // body
       ???,                                             // actions
       ???,                                             // hints
       MessageItem::UInt32(9000),                       // timeout

]);

我无法想到一种有意义的方法来满足Notify方法的接口。根据D-Feet的显示,它看起来像这样:

Notify(
    String app_name,
    UInt32 replaces_id,
    String app_icon,
    String summary,
    String body,
    Array of [String] actions,
    Dict of {String, Variant} hints,
    Int32
)

尤其是Array of [String]Dict of {String, Variant}让我感到困惑。


我猜 Array of [String] 应该可以用 MessageItem::Array 枚举变量来表示,但是关于 Dict 我不确定。虽然有 MessageItem::DictEntry,但我无法确定它应该如何使用。 - Vladimir Matveev
有一个 from_dict ... 可能 Dict 被表示为键值对数组,而 DictEntry 只是单个的键值对。 - Shepmaster
1个回答

2

过了一会儿,我通过@payload弄明白了这个问题。

    m.append_items(&[
                   MessageItem::Str(appname.to_string()),         // appname
                   MessageItem::UInt32(0),                        // notification to update
                   MessageItem::Str(icon.to_string()),            // icon
                   MessageItem::Str(summary.to_string()),         // summary (title)
                   MessageItem::Str(body.to_string()),            // body
                   MessageItem::new_array(                        // actions
                       vec!( MessageItem::Str("".to_string()))),
                   MessageItem::new_array(                        // hints
                       vec!(
                           MessageItem::DictEntry(
                               Box::new(MessageItem::Str("".to_string())),
                               Box::new(MessageItem::Variant(
                                       Box::new(MessageItem::Str("".to_string()))
                                       ))
                           ),
                       )
                   ),
                   MessageItem::Int32(9000),                       // timeout
               ]);

这是我一个有趣的小项目,使用了这个库


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接