@@ -37,6 +37,7 @@ mod imp {
3737 <arg type='u' name='delay' direction='in'/>
3838 <arg type='s' name='greet' direction='out'/>
3939 </method>
40+ <method name='GoodBye'></method>
4041 </interface>
4142</node>
4243"# ;
@@ -56,6 +57,7 @@ mod imp {
5657 enum HelloMethod {
5758 Hello ( Hello ) ,
5859 SlowHello ( SlowHello ) ,
60+ GoodBye ,
5961 }
6062
6163 impl DBusMethodCall for HelloMethod {
@@ -68,6 +70,7 @@ mod imp {
6870 match method {
6971 "Hello" => Ok ( params. get :: < Hello > ( ) . map ( Self :: Hello ) ) ,
7072 "SlowHello" => Ok ( params. get :: < SlowHello > ( ) . map ( Self :: SlowHello ) ) ,
73+ "GoodBye" => Ok ( Some ( Self :: GoodBye ) ) ,
7174 _ => Err ( glib:: Error :: new ( IOErrorEnum :: Failed , "No such method" ) ) ,
7275 }
7376 . and_then ( |p| {
@@ -94,24 +97,35 @@ mod imp {
9497 connection
9598 . register_object ( "/com/github/gtk_rs/examples/HelloWorld" , & example)
9699 . typed_method_call :: < HelloMethod > ( )
97- . invoke_and_return_future_local ( |_, sender, call| {
98- println ! ( "Method call from {sender:?}" ) ;
99- async {
100- match call {
101- HelloMethod :: Hello ( Hello { name } ) => {
102- let greet = format ! ( "Hello {name}!" ) ;
103- println ! ( "{greet}" ) ;
104- Ok ( Some ( greet. to_variant ( ) ) )
105- }
106- HelloMethod :: SlowHello ( SlowHello { name, delay } ) => {
107- glib:: timeout_future ( Duration :: from_secs ( delay as u64 ) ) . await ;
108- let greet = format ! ( "Hello {name} after {delay} seconds!" ) ;
109- println ! ( "{greet}" ) ;
110- Ok ( Some ( greet. to_variant ( ) ) )
100+ . invoke_and_return_future_local ( glib:: clone!(
101+ #[ weak_allow_none( rename_to = app) ]
102+ self . obj( ) ,
103+ move |_, sender, call| {
104+ println!( "Method call from {sender:?}" ) ;
105+ let app = app. clone( ) ;
106+ async move {
107+ match call {
108+ HelloMethod :: Hello ( Hello { name } ) => {
109+ let greet = format!( "Hello {name}!" ) ;
110+ println!( "{greet}" ) ;
111+ Ok ( Some ( greet. to_variant( ) ) )
112+ }
113+ HelloMethod :: SlowHello ( SlowHello { name, delay } ) => {
114+ glib:: timeout_future( Duration :: from_secs( delay as u64 ) ) . await ;
115+ let greet = format!( "Hello {name} after {delay} seconds!" ) ;
116+ println!( "{greet}" ) ;
117+ Ok ( Some ( greet. to_variant( ) ) )
118+ }
119+ HelloMethod :: GoodBye => {
120+ if let Some ( app) = app {
121+ app. quit( ) ;
122+ }
123+ Ok ( None )
124+ }
111125 }
112126 }
113127 }
114- } )
128+ ) )
115129 . build ( )
116130 }
117131 }
@@ -140,9 +154,9 @@ mod imp {
140154 Ok ( ( ) )
141155 }
142156
143- fn shutdown ( & self ) {
157+ fn dbus_unregister ( & self , connection : & DBusConnection , object_path : & str ) {
158+ self . parent_dbus_unregister ( connection, object_path) ;
144159 if let Some ( id) = self . registration_id . take ( ) {
145- let connection = self . obj ( ) . dbus_connection ( ) . expect ( "connection" ) ;
146160 if connection. unregister_object ( id) . is_ok ( ) {
147161 println ! ( "Unregistered object" ) ;
148162 } else {
@@ -151,9 +165,16 @@ mod imp {
151165 }
152166 }
153167
168+ fn shutdown ( & self ) {
169+ self . parent_shutdown ( ) ;
170+ println ! ( "Good bye!" ) ;
171+ }
172+
154173 fn activate ( & self ) {
155174 println ! ( "Waiting for DBus Hello method to be called. Call the following command from another terminal:" ) ;
156175 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" ) ;
176+ println ! ( "Quit with the following command:" ) ;
177+ 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" ) ;
157178 }
158179 }
159180}
0 commit comments