| 1 |
/* exported FlatpakSessionBusModel */ |
| 2 |
|
| 3 |
/* sessionBus.js |
| 4 |
* |
| 5 |
* Copyright 2020 Martin Abente Lahaye |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
const {GObject} = imports.gi; |
| 22 |
|
| 23 |
const {FlatpakSharedModel} = imports.models.shared; |
| 24 |
const {FlatsealOverrideStatus} = imports.models.overrideStatus; |
| 25 |
|
| 26 |
|
| 27 |
var FlatpakSessionBusModel = GObject.registerClass({ |
| 28 |
GTypeName: 'FlatpakSessionBusModel', |
| 29 |
}, class FlatpakSessionBusModel extends FlatpakSharedModel { |
| 30 |
_init() { |
| 31 |
super._init({}); |
| 32 |
} |
| 33 |
|
| 34 |
getPermissions() { |
| 35 |
return { |
| 36 |
'session-talk': { |
| 37 |
supported: this._info.supports('0.4.0'), |
| 38 |
description: _('Talks'), |
| 39 |
option: 'talk', |
| 40 |
value: this.constructor.getDefault(), |
| 41 |
example: 'e.g. org.freedesktop.Notifications', |
| 42 |
}, |
| 43 |
'session-own': { |
| 44 |
supported: this._info.supports('0.4.0'), |
| 45 |
description: _('Owns'), |
| 46 |
option: 'own', |
| 47 |
value: this.constructor.getDefault(), |
| 48 |
example: 'e.g. org.gnome.Contacts.SearchProvider', |
| 49 |
}, |
| 50 |
}; |
| 51 |
} |
| 52 |
|
| 53 |
static getDefault() { |
| 54 |
return ''; |
| 55 |
} |
| 56 |
|
| 57 |
static getType() { |
| 58 |
return 'bus'; |
| 59 |
} |
| 60 |
|
| 61 |
static getGroup() { |
| 62 |
return 'Session Bus Policy'; |
| 63 |
} |
| 64 |
|
| 65 |
static getKey() { |
| 66 |
return null; |
| 67 |
} |
| 68 |
|
| 69 |
static getPrefix() { |
| 70 |
return 'session'; |
| 71 |
} |
| 72 |
|
| 73 |
static getStyle() { |
| 74 |
return 'session'; |
| 75 |
} |
| 76 |
|
| 77 |
static getTitle() { |
| 78 |
return 'Session Bus'; |
| 79 |
} |
| 80 |
|
| 81 |
static getDescription() { |
| 82 |
return _('List of well-known names on the session bus'); |
| 83 |
} |
| 84 |
|
| 85 |
getOptions() { // eslint-disable-line class-methods-use-this |
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
updateFromProxyProperty(property, value) { |
| 90 |
const prefix = this.constructor.getPrefix(); |
| 91 |
const option = property.replace(`${prefix}-`, ''); |
| 92 |
|
| 93 |
const originals = {...this._originals, ...this._globals}; |
| 94 |
const values = this.constructor.deserialize(value); |
| 95 |
|
| 96 |
/* Reset overrides on Talk since it's the first to update */ |
| 97 |
const overrides = option === 'talk' ? {} : this._overrides; |
| 98 |
const missing = option === 'talk' ? {} : this._missing; |
| 99 |
|
| 100 |
values |
| 101 |
.filter(n => n.length !== 0) |
| 102 |
.filter(n => !(n in originals) || originals[n] !== option) |
| 103 |
.forEach(n => { |
| 104 |
overrides[n] = option; |
| 105 |
}); |
| 106 |
|
| 107 |
Object.keys(originals) |
| 108 |
.filter(n => originals[n] === option) |
| 109 |
.filter(n => values.indexOf(n) === -1) |
| 110 |
.forEach(n => { |
| 111 |
missing[n] = 'none'; |
| 112 |
}); |
| 113 |
|
| 114 |
/* Preserve previously negated overrides */ |
| 115 |
Object.keys(this._overrides) |
| 116 |
.filter(n => this._overrides[n] === 'none') |
| 117 |
.filter(n => values.indexOf(n) === -1) |
| 118 |
.forEach(n => { |
| 119 |
missing[n] = 'none'; |
| 120 |
}); |
| 121 |
|
| 122 |
this._overrides = overrides; |
| 123 |
this._missing = missing; |
| 124 |
|
| 125 |
/* Add missing ones after Own since it's the last to update */ |
| 126 |
if (option !== 'own') |
| 127 |
return; |
| 128 |
|
| 129 |
this._overrides = {...this._missing, ...this._overrides}; |
| 130 |
} |
| 131 |
|
| 132 |
_getStatusForPermission(name) { |
| 133 |
let status = FlatsealOverrideStatus.ORIGINAL; |
| 134 |
|
| 135 |
if (name in this._globals) |
| 136 |
status = FlatsealOverrideStatus.GLOBAL; |
| 137 |
if (name in this._overrides) |
| 138 |
status = FlatsealOverrideStatus.USER; |
| 139 |
|
| 140 |
return status; |
| 141 |
} |
| 142 |
|
| 143 |
updateStatusProperty(proxy) { |
| 144 |
const prefix = this.constructor.getPrefix(); |
| 145 |
|
| 146 |
const talk_values = proxy[`${prefix}-talk`] |
| 147 |
.split(';') |
| 148 |
.filter(n => n.length !== 0) |
| 149 |
.map(n => this._getStatusForPermission(n)); |
| 150 |
|
| 151 |
const own_values = proxy[`${prefix}-own`] |
| 152 |
.split(';') |
| 153 |
.filter(n => n.length !== 0) |
| 154 |
.map(n => this._getStatusForPermission(n)); |
| 155 |
|
| 156 |
proxy.set_property(`${prefix}-talk-status`, talk_values.join(';')); |
| 157 |
proxy.set_property(`${prefix}-own-status`, own_values.join(';')); |
| 158 |
} |
| 159 |
|
| 160 |
updateProxyProperty(proxy) { |
| 161 |
const options = {talk: [], own: [], none: []}; |
| 162 |
const values = {...this._originals, ...this._globals, ...this._overrides}; |
| 163 |
|
| 164 |
Object.entries(values).forEach(([name, option]) => { |
| 165 |
if (!(option in options)) |
| 166 |
return; |
| 167 |
options[option].push(name); |
| 168 |
}); |
| 169 |
|
| 170 |
const prefix = this.constructor.getPrefix(); |
| 171 |
proxy.set_property(`${prefix}-talk`, this.constructor.serialize(options['talk'])); |
| 172 |
proxy.set_property(`${prefix}-own`, this.constructor.serialize(options['own'])); |
| 173 |
} |
| 174 |
|
| 175 |
loadFromKeyFile(group, name, option, overrides, global) { |
| 176 |
const dictionary = this._findProperSet(overrides, global); |
| 177 |
dictionary[name] = option; |
| 178 |
} |
| 179 |
|
| 180 |
saveToKeyFile(keyFile) { |
| 181 |
const group = this.constructor.getGroup(); |
| 182 |
Object.entries(this._overrides).forEach(([key, value]) => { |
| 183 |
keyFile.set_value(group, key, value); |
| 184 |
}); |
| 185 |
} |
| 186 |
|
| 187 |
reset() { |
| 188 |
this._overrides = {}; |
| 189 |
this._globals = {}; |
| 190 |
this._originals = {}; |
| 191 |
this._missing = {}; |
| 192 |
} |
| 193 |
}); |