| 1 |
/* exported FlatpakVariablesModel */ |
| 2 |
|
| 3 |
/* variables.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 |
var VAR_REGEXP = /^[^;\s]+=[\S ]+$/; |
| 27 |
|
| 28 |
var FlatpakVariablesModel = GObject.registerClass({ |
| 29 |
GTypeName: 'FlatpakVariablesModel', |
| 30 |
}, class FlatpakVariablesModel extends FlatpakSharedModel { |
| 31 |
_init() { |
| 32 |
super._init({}); |
| 33 |
this.reset(); |
| 34 |
} |
| 35 |
|
| 36 |
getPermissions() { |
| 37 |
return { |
| 38 |
variables: { |
| 39 |
supported: this._info.supports('0.4.0'), |
| 40 |
description: _('Variables'), |
| 41 |
option: null, |
| 42 |
value: this.constructor.getDefault(), |
| 43 |
example: _('e.g. GTK_DEBUG=interactive'), |
| 44 |
}, |
| 45 |
}; |
| 46 |
} |
| 47 |
|
| 48 |
static getDefault() { |
| 49 |
return ''; |
| 50 |
} |
| 51 |
|
| 52 |
static getType() { |
| 53 |
return 'variable'; |
| 54 |
} |
| 55 |
|
| 56 |
static getGroup() { |
| 57 |
return 'Environment'; |
| 58 |
} |
| 59 |
|
| 60 |
static getKey() { |
| 61 |
return null; |
| 62 |
} |
| 63 |
|
| 64 |
static getStyle() { |
| 65 |
return 'environment'; |
| 66 |
} |
| 67 |
|
| 68 |
static getTitle() { |
| 69 |
return 'Environment'; |
| 70 |
} |
| 71 |
|
| 72 |
static getDescription() { |
| 73 |
return _('List of variables exported to the application'); |
| 74 |
} |
| 75 |
|
| 76 |
static deserialize(value) { |
| 77 |
return value |
| 78 |
.split(/(?=;[^;]+=)/) |
| 79 |
.map(v => v.replace(/^;/, '')); |
| 80 |
} |
| 81 |
|
| 82 |
getOptions() { // eslint-disable-line class-methods-use-this |
| 83 |
return null; |
| 84 |
} |
| 85 |
|
| 86 |
updateFromProxyProperty(property, value) { |
| 87 |
const overrides = {}; |
| 88 |
const variables = {}; |
| 89 |
const originals = {...this._originals, ...this._globals}; |
| 90 |
|
| 91 |
this.constructor.deserialize(value) |
| 92 |
.filter(v => this._expression.test(v)) |
| 93 |
.map(v => v.split(/[=](.*)/s)) |
| 94 |
.forEach(([k, v]) => { |
| 95 |
variables[k] = v; |
| 96 |
}); |
| 97 |
|
| 98 |
Object.entries(variables) |
| 99 |
.filter(([k, v]) => !(k in originals) || originals[k] !== v) |
| 100 |
.forEach(([k, v]) => { |
| 101 |
overrides[k] = v; |
| 102 |
}); |
| 103 |
|
| 104 |
Object.keys(originals) |
| 105 |
.filter(k => !(k in variables)) |
| 106 |
.filter(k => originals[k] !== '') |
| 107 |
.forEach(k => { |
| 108 |
overrides[k] = ''; |
| 109 |
}); |
| 110 |
|
| 111 |
/* Preserve previously negated overrides */ |
| 112 |
Object.keys(this._overrides) |
| 113 |
.filter(k => !(k in variables)) |
| 114 |
.filter(k => this._overrides[k] === '') |
| 115 |
.forEach(k => { |
| 116 |
overrides[k] = ''; |
| 117 |
}); |
| 118 |
|
| 119 |
this._overrides = overrides; |
| 120 |
} |
| 121 |
|
| 122 |
_getStatusForPermission(variable) { |
| 123 |
const [key] = variable.split(/[=](.*)/s); |
| 124 |
|
| 125 |
let status = FlatsealOverrideStatus.ORIGINAL; |
| 126 |
if (key in this._globals) |
| 127 |
status = FlatsealOverrideStatus.GLOBAL; |
| 128 |
if (key in this._overrides) |
| 129 |
status = FlatsealOverrideStatus.USER; |
| 130 |
|
| 131 |
return status; |
| 132 |
} |
| 133 |
|
| 134 |
updateStatusProperty(proxy) { |
| 135 |
const values = this.constructor.deserialize(proxy.variables) |
| 136 |
.filter(v => v.length !== 0) |
| 137 |
.map(v => this._getStatusForPermission(v)); |
| 138 |
|
| 139 |
proxy.set_property('variables-status', values.join(';')); |
| 140 |
} |
| 141 |
|
| 142 |
updateProxyProperty(proxy) { |
| 143 |
let variables = {...this._originals, ...this._globals, ...this._overrides}; |
| 144 |
|
| 145 |
variables = Object.entries(variables) |
| 146 |
.filter(([, value]) => value.length !== 0) |
| 147 |
.map(([key, value]) => `${key}=${value}`); |
| 148 |
|
| 149 |
proxy.set_property('variables', this.constructor.serialize(variables)); |
| 150 |
} |
| 151 |
|
| 152 |
loadFromKeyFile(group, key, value, overrides, global) { |
| 153 |
if (!overrides && value.length === 0) |
| 154 |
return; |
| 155 |
|
| 156 |
const dict = this._findProperSet(overrides, global); |
| 157 |
dict[key] = value; |
| 158 |
} |
| 159 |
|
| 160 |
saveToKeyFile(keyFile) { |
| 161 |
const group = this.constructor.getGroup(); |
| 162 |
Object.entries(this._overrides).forEach(([key, value]) => { |
| 163 |
keyFile.set_value(group, key, value); |
| 164 |
}); |
| 165 |
} |
| 166 |
|
| 167 |
reset() { |
| 168 |
this._overrides = {}; |
| 169 |
this._globals = {}; |
| 170 |
this._originals = {}; |
| 171 |
this._expression = new RegExp(VAR_REGEXP); |
| 172 |
} |
| 173 |
}); |