-
-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathmain.rs
More file actions
194 lines (172 loc) · 6.4 KB
/
Copy pathmain.rs
File metadata and controls
194 lines (172 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use gio::prelude::*;
mod hello_world;
glib::wrapper! {
pub struct SampleApplication(ObjectSubclass<imp::SampleApplication>)
@extends gio::Application,
@implements gio::ActionGroup, gio::ActionMap;
}
impl Default for SampleApplication {
fn default() -> Self {
glib::Object::builder()
.property(
"application-id",
"com.github.gtk-rs.examples.RegisterDBusObject",
)
.build()
}
}
mod imp {
use std::cell::RefCell;
use std::time::Duration;
use gio::prelude::*;
use gio::subclass::prelude::*;
use gio::{DBusConnection, DBusError};
const EXAMPLE_XML: &str = r#"
<node>
<interface name='com.github.gtk_rs.examples.HelloWorld'>
<method name='Hello'>
<arg type='s' name='name' direction='in'/>
<arg type='s' name='greet' direction='out'/>
</method>
<method name='SlowHello'>
<arg type='s' name='name' direction='in'/>
<arg type='u' name='delay' direction='in'/>
<arg type='s' name='greet' direction='out'/>
</method>
<method name='GoodBye'></method>
</interface>
</node>
"#;
#[derive(Debug, glib::Variant)]
struct Hello {
name: String,
}
#[derive(Debug, glib::Variant)]
struct SlowHello {
name: String,
delay: u32,
}
#[derive(Debug)]
enum HelloMethod {
Hello(Hello),
SlowHello(SlowHello),
GoodBye,
}
impl DBusMethodCall for HelloMethod {
fn parse_call(
_obj_path: &str,
_interface: Option<&str>,
method: &str,
params: glib::Variant,
) -> Result<Self, glib::Error> {
match method {
"Hello" => Ok(params.get::<Hello>().map(Self::Hello)),
"SlowHello" => Ok(params.get::<SlowHello>().map(Self::SlowHello)),
"GoodBye" => Ok(Some(Self::GoodBye)),
_ => Err(glib::Error::new(DBusError::UnknownMethod, "No such method")),
}
.and_then(|p| {
p.ok_or_else(|| glib::Error::new(DBusError::InvalidArgs, "Invalid parameters"))
})
}
}
#[derive(Default)]
pub struct SampleApplication {
registration_id: RefCell<Option<gio::RegistrationId>>,
}
impl SampleApplication {
fn register_object(
&self,
connection: &DBusConnection,
) -> Result<gio::RegistrationId, glib::Error> {
let example = gio::DBusNodeInfo::for_xml(EXAMPLE_XML)
.ok()
.and_then(|e| e.lookup_interface("com.github.gtk_rs.examples.HelloWorld"))
.expect("Example interface");
connection
.register_object("/com/github/gtk_rs/examples/HelloWorld", &example)
.typed_method_call::<HelloMethod>()
.invoke_and_return_future_local(glib::clone!(
#[weak_allow_none(rename_to = app)]
self.obj(),
move |_, sender, call| {
println!("Method call from {sender:?}");
let app = app.clone();
async move {
match call {
HelloMethod::Hello(Hello { name }) => {
let greet = format!("Hello {name}!");
println!("{greet}");
Ok(Some(greet.to_variant()))
}
HelloMethod::SlowHello(SlowHello { name, delay }) => {
glib::timeout_future(Duration::from_secs(delay as u64)).await;
let greet = format!("Hello {name} after {delay} seconds!");
println!("{greet}");
Ok(Some(greet.to_variant()))
}
HelloMethod::GoodBye => {
if let Some(app) = app {
app.quit();
}
Ok(None)
}
}
}
}
))
.build()
}
}
#[glib::object_subclass]
impl ObjectSubclass for SampleApplication {
const NAME: &'static str = "SampleApplication";
type Type = super::SampleApplication;
type ParentType = gio::Application;
}
impl ObjectImpl for SampleApplication {}
impl ApplicationImpl for SampleApplication {
fn dbus_register(
&self,
connection: &DBusConnection,
object_path: &str,
) -> Result<(), glib::Error> {
self.parent_dbus_register(connection, object_path)?;
self.registration_id
.replace(Some(self.register_object(connection)?));
println!("registered object on session bus");
Ok(())
}
fn dbus_unregister(&self, connection: &DBusConnection, object_path: &str) {
self.parent_dbus_unregister(connection, object_path);
if let Some(id) = self.registration_id.take() {
if connection.unregister_object(id).is_ok() {
println!("Unregistered object");
} else {
eprintln!("Could not unregister object");
}
}
}
fn shutdown(&self) {
self.parent_shutdown();
println!("Good bye!");
}
fn activate(&self) {
println!(
"Waiting for DBus Hello method to be called. Call the following command from another terminal:"
);
println!(
"dbus-send --print-reply --dest=com.github.gtk-rs.examples.RegisterDBusObject /com/github/gtk_rs/examples/HelloWorld com.github.gtk_rs.examples.HelloWorld.Hello string:YourName"
);
println!("Quit with the following command:");
println!(
"dbus-send --print-reply --dest=com.github.gtk-rs.examples.RegisterDBusObject /com/github/gtk_rs/examples/HelloWorld com.github.gtk_rs.examples.HelloWorld.GoodBye"
);
}
}
}
fn main() -> glib::ExitCode {
let app = SampleApplication::default();
let _guard = app.hold();
app.run()
}