@@ -13,12 +13,11 @@ class FlatpakPortalManager {
1313 async initialize ( ) {
1414 if ( this . initialized ) return
1515
16- // Clean up any stale connection first
1716 if ( this . bus ) {
1817 try {
1918 this . bus . disconnect ( )
2019 } catch ( err ) {
21- // Ignore errors disconnecting stale connection
20+ // Ignore disconnect errors
2221 }
2322 this . bus = null
2423 this . portal = null
@@ -45,13 +44,10 @@ class FlatpakPortalManager {
4544 * @returns {Promise<boolean> } - True if the autostart status was successfully set.
4645 */
4746 async setAutostart ( enabled ) {
48- // Ensure initialized (safe to call multiple times)
4947 await this . initialize ( )
5048
5149 try {
5250 const background = this . portal . getInterface ( 'org.freedesktop.portal.Background' )
53-
54- // Create a unique handle token for this request
5551 const handleToken = `stretchly_autostart_${ Date . now ( ) } _${ Math . random ( ) . toString ( 36 ) . substring ( 7 ) } `
5652
5753 const options = {
@@ -61,79 +57,74 @@ class FlatpakPortalManager {
6157 'dbus-activatable' : new Variant ( 'b' , false )
6258 }
6359
64- // When DISABLING , the portal doesn't create a persistent request object
65- // It's a fire-and-forget operation - no Response signal to wait for
60+ // When disabling autostart , the portal doesn't create a persistent request object.
61+ // No Response signal is emitted, so we can return immediately.
6662 if ( ! enabled ) {
6763 await background . RequestBackground ( '' , options )
68- log . info ( 'Stretchly: Autostart disabled via XDG Portal (no response expected). ' )
64+ log . info ( 'Stretchly: Autostart disabled via XDG Portal' )
6965 return true
7066 }
7167
72- // When ENABLING , we must wait for the Response signal
68+ // When enabling autostart , we must wait for the Response signal
7369 return new Promise ( ( resolve ) => {
74- const timeoutMs = 30000 // 30 second timeout
70+ const timeoutMs = 30000
7571 let timeoutId = null
76- let messageListener = null // Store the listener function here
72+ let messageListener = null
7773
7874 const cleanup = ( ) => {
7975 if ( timeoutId ) {
8076 clearTimeout ( timeoutId )
8177 timeoutId = null
8278 }
8379 if ( messageListener && this . bus ) {
84- // The actual listener removal
8580 this . bus . off ( 'message' , messageListener )
8681 messageListener = null
8782 }
8883 }
8984
90- // Define the listener function
9185 messageListener = ( msg ) => {
92- // Check if this message is the one we're waiting for
9386 if ( msg . interface === 'org.freedesktop.portal.Request' &&
9487 msg . member === 'Response' &&
9588 msg . path . includes ( handleToken ) ) {
9689 const [ response , results ] = msg . body
9790
98- cleanup ( ) // Clean up immediately (removes listener + timeout)
91+ cleanup ( )
9992
10093 log . info ( `Stretchly: Portal Response signal received - response: ${ response } , results:` , results )
10194
102- if ( response === 0 ) { // Success
95+ // Response codes: 0 = success, 1 = user cancelled, 2 = other error
96+ if ( response === 0 ) {
10397 const autostartGranted = results && results . autostart && results . autostart . value === enabled
10498 if ( autostartGranted ) {
105- log . info ( 'Stretchly: Autostart successfully enabled via XDG Portal. ' )
99+ log . info ( 'Stretchly: Autostart enabled via XDG Portal' )
106100 } else {
107- log . warn ( 'Stretchly: Autostart status did not match request. Results: ' , results )
101+ log . warn ( 'Stretchly: Autostart status did not match request' , results )
108102 }
109103 resolve ( autostartGranted )
110- } else if ( response === 1 ) { // User cancelled
111- log . warn ( 'Stretchly: User cancelled the portal request. ' )
104+ } else if ( response === 1 ) {
105+ log . warn ( 'Stretchly: User cancelled the portal request' )
112106 resolve ( false )
113- } else { // Other error
107+ } else {
114108 log . error ( `Stretchly: Portal request failed with response code: ${ response } ` )
115109 resolve ( false )
116110 }
117111 }
118112 }
119113
120- // Set the timeout
121114 timeoutId = setTimeout ( ( ) => {
122- cleanup ( ) // This will now remove the listener
115+ cleanup ( )
123116 log . error ( 'Stretchly: Portal request timeout after 30 seconds' )
124117 resolve ( false )
125118 } , timeoutMs )
126119
127- // Listen for Response signals
128- this . bus . on ( 'message' , messageListener ) // Register the named listener
120+ this . bus . on ( 'message' , messageListener )
129121
130- // NOW make the request
131122 background . RequestBackground ( '' , options )
132123 . then ( requestPath => {
133- log . info ( `Stretchly: RequestBackground called for autostart=true , request path: ${ requestPath } ` )
124+ log . info ( `Stretchly: RequestBackground called, request path: ${ requestPath } ` )
134125 } )
135126 . catch ( err => {
136- cleanup ( ) // This will now remove the listener
127+ cleanup ( )
137128 log . error ( 'Stretchly: Failed to call RequestBackground:' , err )
138129 resolve ( false )
139130 } )
0 commit comments