Debian Codegraph @

src/models/filesystemsOther.js

raw
1 /* exported FlatpakFilesystemsOtherModel */
2
3 /* filesystemsOther.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 {filesystems} = imports.models;
24 const {FlatpakSharedModel} = imports.models.shared;
25
26
27 var FlatpakFilesystemsOtherModel = GObject.registerClass({
28 GTypeName: 'FlatpakFilesystemsOtherModel',
29 }, class FlatpakFilesystemsOtherModel extends FlatpakSharedModel {
30 _init() {
31 super._init({});
32 this._filesystems = filesystems.getDefault();
33 }
34
35 getPermissions() {
36 return {
37 'filesystems-other': {
38 supported: this._info.supports('0.6.14'),
39 description: _('Other files'),
40 option: null,
41 value: this.constructor.getDefault(),
42 example: _('e.g. ~/games:ro, xdg-pictures, etc'),
43 },
44 };
45 }
46
47 static getGroup() {
48 return 'Context';
49 }
50
51 static getKey() {
52 return 'filesystems';
53 }
54
55 static getStyle() {
56 return 'filesystems';
57 }
58
59 static getTitle() {
60 return 'Filesystem';
61 }
62
63 static getDescription() {
64 return _('List of filesystem subsets available to the application');
65 }
66
67 static getType() {
68 return 'path';
69 }
70
71 static getDefault() {
72 return '';
73 }
74
75 getOptions() { // eslint-disable-line class-methods-use-this
76 return null;
77 }
78
79 static isNegated(value) {
80 return value.startsWith('!');
81 }
82
83 static negate(value) {
84 if (this.isNegated(value))
85 return value.replace('!', '');
86 return `!${value}`;
87 }
88
89 static isOverriden(set, value) {
90 let [path] = value.split(':');
91 path = path.replace('!', '');
92
93 return (
94 set.has(path) ||
95 set.has(`${path}:ro`) ||
96 set.has(`${path}:rw`) ||
97 set.has(`${path}:create`) ||
98 set.has(`!${path}`) ||
99 set.has(`!${path}:ro`) ||
100 set.has(`!${path}:rw`) ||
101 set.has(`!${path}:create`) ||
102 set.has(`!${path}:reset`));
103 }
104
105 static isStrictlyOverriden(set, value) {
106 return set.has(this.negate(value));
107 }
108
109 static isResetOverride(value) {
110 const [path, mode] = value.split(':');
111 return path.startsWith('!') && mode === 'reset';
112 }
113
114 static removeMode(value) {
115 const [path] = value.split(':');
116 return path;
117 }
118
119 updateFromProxyProperty(property, value) {
120 const paths = new Set(this.constructor.deserialize(value));
121
122 const added = new Set([...paths]
123 .filter(p => p.length !== 0)
124 .filter(p => !this._filesystems.originals.has(p))
125 .filter(p => !this._originals.has(p))
126 .filter(p => !this._globals.has(p)));
127
128 const removedOriginals = [...this._originals]
129 .filter(p => !this.constructor.isOverriden(this._filesystems.overrides, p))
130 .filter(p => !this.constructor.isOverriden(this._globals, p))
131 .filter(p => !this.constructor.isOverriden(added, p))
132 .filter(p => !paths.has(p))
133 .map(p => this.constructor.removeMode(p))
134 .map(p => this.constructor.negate(p));
135
136 const removedGlobals = [...this._globals]
137 .filter(p => !this.constructor.isOverriden(this._filesystems.originals, p))
138 .filter(p => !this.constructor.isOverriden(this._filesystems.overrides, p))
139 .filter(p => !this.constructor.isOverriden(this._originals, p))
140 .filter(p => !this.constructor.isOverriden(added, p))
141 .filter(p => !paths.has(p))
142 .map(p => this.constructor.removeMode(p))
143 .map(p => this.constructor.negate(p));
144
145 this._overrides = new Set([...added, ...removedOriginals, ...removedGlobals]);
146 }
147
148 updateStatusProperty(proxy) {
149 const values = proxy.filesystems_other
150 .split(';')
151 .filter(p => p.length !== 0)
152 .map(p => this._getStatusForPermission(p));
153
154 proxy.set_property('filesystems-other-status', values.join(';'));
155 }
156
157 updateProxyProperty(proxy) {
158 const originals = [...this._originals]
159 .filter(p => !this.constructor.isOverriden(this._filesystems.globals, p))
160 .filter(p => !this.constructor.isOverriden(this._filesystems.overrides, p))
161 .filter(p => !this.constructor.isOverriden(this._globals, p))
162 .filter(p => !this.constructor.isOverriden(this._overrides, p));
163
164 const globals = [...this._globals]
165 .filter(p => !this.constructor.isStrictlyOverriden(this._originals, p))
166 .filter(p => !this.constructor.isOverriden(this._filesystems.overrides, p))
167 .filter(p => !this.constructor.isOverriden(this._overrides, p))
168 .filter(p => !(this.constructor.isOverriden(this._filesystems.originals, p) &&
169 this.constructor.isNegated(p) &&
170 !this.constructor.isResetOverride(p)))
171 .filter(p => !(this.constructor.isOverriden(this._originals, p) &&
172 this.constructor.isNegated(p) &&
173 !this.constructor.isResetOverride(p)));
174
175 const overrides = [...this._overrides]
176 .filter(p => !this.constructor.isStrictlyOverriden(this._originals, p))
177 .filter(p => !this.constructor.isStrictlyOverriden(this._globals, p))
178 .filter(p => !(this.constructor.isOverriden(this._filesystems.originals, p) &&
179 this.constructor.isNegated(p) &&
180 !this.constructor.isResetOverride(p)))
181 .filter(p => !(this.constructor.isOverriden(this._originals, p) &&
182 this.constructor.isNegated(p) &&
183 !this.constructor.isResetOverride(p)))
184 .filter(p => !(this.constructor.isOverriden(this._globals, p) &&
185 this.constructor.isNegated(p) &&
186 !this.constructor.isResetOverride(p)));
187
188 const paths = new Set([...originals, ...globals, ...overrides]);
189 const value = this.constructor.serialize([...paths]);
190
191 proxy.set_property('filesystems-other', value);
192 }
193
194 loadFromKeyFile(group, key, value, overrides, global) {
195 if (value.length === 0)
196 return;
197
198 const set = this._findProperSet(overrides, global);
199 set.add(value);
200 }
201 });