|
| 1 | +import dbus from '@particle/dbus-next' |
| 2 | +import log from 'electron-log/main.js' |
| 3 | + |
| 4 | +const { Variant } = dbus |
| 5 | + |
| 6 | +class FlatpakPortalManager { |
| 7 | + constructor (settings) { |
| 8 | + this.settings = settings |
| 9 | + this.bus = null |
| 10 | + this.portal = null |
| 11 | + this.initialized = false |
| 12 | + this.portalRequestTimeoutMs = 30000 |
| 13 | + } |
| 14 | + |
| 15 | + async initialize () { |
| 16 | + if (this.initialized) return |
| 17 | + |
| 18 | + if (this.bus) { |
| 19 | + try { |
| 20 | + this.bus.disconnect() |
| 21 | + } catch { |
| 22 | + // Ignore disconnect errors |
| 23 | + } |
| 24 | + this.bus = null |
| 25 | + this.portal = null |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + this.bus = dbus.sessionBus() |
| 30 | + this.portal = await this.bus.getProxyObject( |
| 31 | + 'org.freedesktop.portal.Desktop', |
| 32 | + '/org/freedesktop/portal/desktop' |
| 33 | + ) |
| 34 | + this.initialized = true |
| 35 | + log.info('Stretchly: XDG Background Portal initialized successfully') |
| 36 | + } catch (error) { |
| 37 | + log.error('Stretchly: Failed to initialize XDG Background Portal:', error) |
| 38 | + this.initialized = false |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Sets the autostart status for the application using the XDG Background Portal. |
| 44 | + * @param {boolean} enabled - True to enable autostart, false to disable. |
| 45 | + * @returns {Promise<boolean>} - True if the autostart status was successfully set. |
| 46 | + */ |
| 47 | + async setAutostart (enabled) { |
| 48 | + await this.initialize() |
| 49 | + |
| 50 | + if (!this.initialized) { |
| 51 | + log.error('Stretchly: Cannot set autostart - portal not initialized') |
| 52 | + return false |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + const background = this.portal.getInterface('org.freedesktop.portal.Background') |
| 57 | + const handleToken = `stretchly_autostart_${Date.now()}_${Math.random().toString(36).substring(7)}` |
| 58 | + |
| 59 | + const options = { |
| 60 | + handle_token: new Variant('s', handleToken), |
| 61 | + reason: new Variant('s', 'Stretchly needs to run in the background to remind you to take breaks'), |
| 62 | + autostart: new Variant('b', enabled), |
| 63 | + 'dbus-activatable': new Variant('b', false) |
| 64 | + } |
| 65 | + |
| 66 | + // When disabling autostart, the portal doesn't create a persistent request object. |
| 67 | + // No Response signal is emitted, so we can return immediately. |
| 68 | + if (!enabled) { |
| 69 | + try { |
| 70 | + await background.RequestBackground('', options) |
| 71 | + log.info('Stretchly: Autostart disabled via XDG Portal') |
| 72 | + return true |
| 73 | + } catch (error) { |
| 74 | + log.error('Stretchly: Failed to disable autostart via XDG Portal:', error) |
| 75 | + return false |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // When enabling autostart, we must wait for the Response signal. |
| 80 | + // We start listening BEFORE calling the method to avoid race conditions. |
| 81 | + const responsePromise = this._waitForBusResponse(handleToken, enabled) |
| 82 | + |
| 83 | + const requestPath = await background.RequestBackground('', options) |
| 84 | + log.info(`Stretchly: RequestBackground called, request path: ${requestPath}`) |
| 85 | + |
| 86 | + return await responsePromise |
| 87 | + } catch (error) { |
| 88 | + log.error(`Stretchly: Failed to set autostart=${enabled} via XDG Portal:`, error) |
| 89 | + return false |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Waits for the Portal Request Response signal. |
| 95 | + * @private |
| 96 | + */ |
| 97 | + _waitForBusResponse (handleToken, expectingEnabled) { |
| 98 | + return new Promise((resolve) => { |
| 99 | + let timeoutId = null |
| 100 | + let messageListener = null |
| 101 | + |
| 102 | + const cleanup = () => { |
| 103 | + if (timeoutId) { |
| 104 | + clearTimeout(timeoutId) |
| 105 | + timeoutId = null |
| 106 | + } |
| 107 | + if (messageListener && this.bus) { |
| 108 | + this.bus.off('message', messageListener) |
| 109 | + messageListener = null |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + messageListener = (msg) => { |
| 114 | + if (msg.interface === 'org.freedesktop.portal.Request' && |
| 115 | + msg.member === 'Response' && |
| 116 | + msg.path.endsWith(handleToken)) { |
| 117 | + const [response, results] = msg.body |
| 118 | + |
| 119 | + cleanup() |
| 120 | + |
| 121 | + log.info(`Stretchly: Portal Response signal received - response: ${response}, results:`, results) |
| 122 | + |
| 123 | + // Response codes: 0 = success, 1 = user cancelled, 2 = other error |
| 124 | + if (response === 0) { |
| 125 | + const autostartGranted = results && results.autostart && results.autostart.value === expectingEnabled |
| 126 | + if (autostartGranted) { |
| 127 | + log.info('Stretchly: Autostart enabled via XDG Portal') |
| 128 | + } else { |
| 129 | + log.warn('Stretchly: Autostart status did not match request', results) |
| 130 | + } |
| 131 | + resolve(autostartGranted) |
| 132 | + } else if (response === 1) { |
| 133 | + log.warn('Stretchly: User cancelled the portal request') |
| 134 | + resolve(false) |
| 135 | + } else { |
| 136 | + log.error(`Stretchly: Portal request failed with response code: ${response}`) |
| 137 | + resolve(false) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + timeoutId = setTimeout(() => { |
| 143 | + cleanup() |
| 144 | + log.error(`Stretchly: Portal request timeout after ${this.portalRequestTimeoutMs / 1000} seconds`) |
| 145 | + resolve(false) |
| 146 | + }, this.portalRequestTimeoutMs) |
| 147 | + |
| 148 | + this.bus.on('message', messageListener) |
| 149 | + }) |
| 150 | + } |
| 151 | + |
| 152 | + async enableAutostart () { |
| 153 | + try { |
| 154 | + const success = await this.setAutostart(true) |
| 155 | + if (success) { |
| 156 | + this.settings.set('flatpakAutostart', true) |
| 157 | + } |
| 158 | + return success |
| 159 | + } catch (error) { |
| 160 | + log.error('Stretchly: Failed to set autostart (enable) via XDG Portal', error) |
| 161 | + return false |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + async disableAutostart () { |
| 166 | + try { |
| 167 | + const success = await this.setAutostart(false) |
| 168 | + if (success) { |
| 169 | + this.settings.set('flatpakAutostart', false) |
| 170 | + } |
| 171 | + return success |
| 172 | + } catch (error) { |
| 173 | + log.error('Stretchly: Failed to set autostart (disable) via XDG Portal', error) |
| 174 | + return false |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + async isAutostartEnabled () { |
| 179 | + // XDG portals don't provide a reliable query method, so we read from our cache |
| 180 | + return this.settings.get('flatpakAutostart') |
| 181 | + } |
| 182 | + |
| 183 | + disconnect () { |
| 184 | + if (this.bus) { |
| 185 | + this.bus.disconnect() |
| 186 | + this.bus = null |
| 187 | + this.portal = null |
| 188 | + this.initialized = false |
| 189 | + log.info('Stretchly: XDG Background Portal disconnected') |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +export default FlatpakPortalManager |
0 commit comments