-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdconf
72 lines (61 loc) · 2.09 KB
/
dconf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# DConf
#
# Copyright (C) 2021 Rodrigo Silva (MestreLion) <[email protected]>
# License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
include gvariant
# Wrapper to dconf, automatically invoking setup_run on write and load,
# and dispatching to extra commands
dconf() {
cmd=$1
shift
case "$cmd" in
load ) ;&
write ) setup_run command dconf "$cmd" "$@";;
swrite) dconf_write_string "$@";;
sread ) dconf_read_string "$@";;
array ) dconf_array "$@";;
* ) command dconf "$cmd" "$@";;
esac
}
dconf_write_string() {
local key=$1
local value=$2
dconf write "$key" "$(gvariant_quote "$value")"
}
dconf_read_string() {
local key=$1
gvariant_unquote "$(dconf read "$key")"
}
dconf_array() {
local op=$1 # Operation to perform: include, remove, clear/new, set
local key=$2 # Full path of the DConf key
local type=$3 # List item type. See gvariant_array()
shift 3 # Remaining arguments are list items
# This is a LOT trickier than using gsettings, as dconf:
# - Does not set exit status on any error, missing keys, etc
# - Does not return the default value if key is not set, unless -d
# (and -d return the default value regardless if a value is set)
# - And even that default value is useless: the dconf default value is
# NOT the same as the gsettings default value from schema, which is the
# one that really matters, so dconf default is usually not set either.
# DConf prints '@as []' on empty lists, and nothing on keys not set
local list; list=$(dconf read "$key")
# If not set, try the dconf default value
if [[ -z "$list" ]]; then list=$(dconf read -d "$key"); fi
# If default is empty (or unset), try via gsettings
if [[ -z "$list" ]]; then
local path=${key%/*}
local leaf=${key##*/}
local schema; schema=$(
gsettings list-schemas --print-paths |
grep -iF "$path" | cut -f1 -d' ' || true
)
if [[ "$schema" ]]; then
list=$(gsettings get "$schema" "$leaf")
fi
fi
# If still empty, format it.
if [[ -z "$list" ]]; then list="@a${type} []"; fi
list=$(gvariant_array "$op" "$type" "$list" "$@")
dconf write "$key" "$list"
}