| 1 |
/* exported FlatsealPathsViewer */ |
| 2 |
|
| 3 |
/* pathsViewer.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, Gtk} = imports.gi; |
| 22 |
const {FlatsealOverrideStatus} = imports.models.overrideStatus; |
| 23 |
|
| 24 |
const _propFlags = GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT; |
| 25 |
|
| 26 |
|
| 27 |
var FlatsealPathsViewer = GObject.registerClass({ |
| 28 |
GTypeName: 'FlatsealPathsViewer', |
| 29 |
Template: 'resource:///com/github/tchx84/Flatseal/widgets/pathsViewer.ui', |
| 30 |
Properties: { |
| 31 |
text: GObject.ParamSpec.string( |
| 32 |
'text', |
| 33 |
'text', |
| 34 |
'text', |
| 35 |
_propFlags, ''), |
| 36 |
status: GObject.ParamSpec.string( |
| 37 |
'status', |
| 38 |
'status', |
| 39 |
'status', |
| 40 |
_propFlags, |
| 41 |
FlatsealOverrideStatus.ORIGINAL), |
| 42 |
}, |
| 43 |
}, class FlatsealPathsViewer extends Gtk.Box { |
| 44 |
_init(serializeFunc, deserializeFunc, rowClass) { |
| 45 |
this._serializeFunc = serializeFunc; |
| 46 |
this._deserializeFunc = deserializeFunc; |
| 47 |
this._status = FlatsealOverrideStatus.ORIGINAL; |
| 48 |
super._init({}); |
| 49 |
this._rowClass = rowClass; |
| 50 |
} |
| 51 |
|
| 52 |
_changed() { |
| 53 |
this.notify('text'); |
| 54 |
} |
| 55 |
|
| 56 |
_remove(row) { |
| 57 |
this.remove(row); |
| 58 |
if (this.get_first_child() === null) |
| 59 |
this.visible = false; |
| 60 |
|
| 61 |
this._changed(); |
| 62 |
} |
| 63 |
|
| 64 |
_update(text) { |
| 65 |
for (const row of Array.from(this)) |
| 66 |
this.remove(row); |
| 67 |
|
| 68 |
this.visible = false; |
| 69 |
const paths = this._deserializeFunc(text); |
| 70 |
|
| 71 |
paths.forEach(path => { |
| 72 |
if (path.length === 0) |
| 73 |
return; |
| 74 |
|
| 75 |
this.add(path); |
| 76 |
}); |
| 77 |
|
| 78 |
this._updateStatus(); |
| 79 |
} |
| 80 |
|
| 81 |
_updateStatus() { |
| 82 |
const statuses = this._status |
| 83 |
.split(';') |
| 84 |
.filter(s => s.length !== 0); |
| 85 |
const rows = Array.from(this).reverse(); |
| 86 |
|
| 87 |
/* if it's still out of sync, bail out */ |
| 88 |
if (statuses.length !== rows.length) |
| 89 |
return; |
| 90 |
|
| 91 |
rows.forEach((row, index) => { |
| 92 |
row.status = statuses[index]; |
| 93 |
}); |
| 94 |
} |
| 95 |
|
| 96 |
set text(text) { |
| 97 |
if (text === '') { |
| 98 |
this._update(text); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
this._update(text); |
| 103 |
this.notify('text'); |
| 104 |
} |
| 105 |
|
| 106 |
get text() { |
| 107 |
return this._serializeFunc(Array.from(this) |
| 108 |
.map(row => row.text) |
| 109 |
.reverse(), |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
set status(status) { |
| 114 |
this._status = status; |
| 115 |
this._updateStatus(); |
| 116 |
} |
| 117 |
|
| 118 |
get status() { |
| 119 |
return this._status; |
| 120 |
} |
| 121 |
|
| 122 |
add(path) { |
| 123 |
const row = new this._rowClass(); |
| 124 |
row.text = path; |
| 125 |
row.connect('remove-requested', this._remove.bind(this, row)); |
| 126 |
row.connect('notify::text', this._changed.bind(this)); |
| 127 |
|
| 128 |
this.prepend(row); |
| 129 |
this.visible = true; |
| 130 |
} |
| 131 |
}); |