1+ use std:: cell:: Cell ;
12use std:: path:: Path ;
23use std:: path:: PathBuf ;
4+ use std:: pin:: Pin ;
5+ use std:: rc:: Rc ;
36
47use adw:: subclass:: prelude:: * ;
58use capnp_rpc:: { rpc_twoparty_capnp, twoparty, RpcSystem } ;
9+ use futures:: stream:: Stream ;
610use futures:: AsyncReadExt ;
711use gettextrs:: gettext;
812use gio:: SocketClient ;
@@ -242,6 +246,11 @@ impl NotifyApplication {
242246 let dbpath = glib:: user_data_dir ( ) . join ( "com.ranfdev.Notify.sqlite" ) ;
243247 info ! ( database_path = %dbpath. display( ) ) ;
244248
249+ // Here I'm sending notifications to the desktop environment and listening for network changes.
250+ // This should have been inside ntfy-daemon, but using portals from another thread causes the error
251+ // `Invalid client serial` and it's broken.
252+ // Until https://github.com/flatpak/xdg-dbus-proxy/issues/46 is solved, I have to handle these things
253+ // in the main thread. Uff.
245254 let ( tx, rx) = glib:: MainContext :: channel ( Default :: default ( ) ) ;
246255 let app = self . clone ( ) ;
247256 rx. attach ( None , move |n : models:: Notification | {
@@ -268,17 +277,39 @@ impl NotifyApplication {
268277 glib:: ControlFlow :: Continue
269278 } ) ;
270279
271- struct Proxy ( glib:: Sender < models:: Notification > ) ;
272- impl models:: NotificationProxy for Proxy {
280+ struct Proxies {
281+ notification : glib:: Sender < models:: Notification > ,
282+ }
283+ impl models:: NotificationProxy for Proxies {
273284 fn send ( & self , n : models:: Notification ) -> anyhow:: Result < ( ) > {
274- self . 0 . send ( n) ?;
285+ self . notification . send ( n) ?;
275286 Ok ( ( ) )
276287 }
277288 }
289+ impl models:: NetworkMonitorProxy for Proxies {
290+ fn listen ( & self ) -> Pin < Box < dyn Stream < Item = ( ) > > > {
291+ let ( tx, rx) = async_channel:: bounded ( 1 ) ;
292+ let mut prev_available = Rc :: new ( Cell :: new ( false ) ) ;
293+
294+ gio:: NetworkMonitor :: default ( ) . connect_network_changed ( move |_, available| {
295+ dbg ! ( "sent" , available) ;
296+ if available && !prev_available. get ( ) {
297+ if let Err ( e) = tx. send_blocking ( ( ) ) {
298+ warn ! ( error = %e) ;
299+ }
300+ }
301+ prev_available. replace ( available) ;
302+ } ) ;
303+
304+ Box :: pin ( rx)
305+ }
306+ }
307+ let proxies = std:: sync:: Arc :: new ( Proxies { notification : tx } ) ;
278308 ntfy_daemon:: system_client:: start (
279309 socket_path. to_owned ( ) ,
280310 dbpath. to_str ( ) . unwrap ( ) ,
281- std:: sync:: Arc :: new ( Proxy ( tx) ) ,
311+ proxies. clone ( ) ,
312+ proxies,
282313 )
283314 . unwrap ( ) ;
284315 self . imp ( ) . hold_guard . set ( self . hold ( ) ) . unwrap ( ) ;
0 commit comments