-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsettings.js
174 lines (154 loc) · 5.5 KB
/
settings.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import Gio from 'gi://Gio';
export const PANEL_ICON_PATH = 'default-panel-icon';
export const VALID_PANEL_ICONS = 'valid-panel-icons';
export const HASS_URL = 'hass-url';
export const HASS_ENTITIES_CACHE = 'hass-entities-cache';
export const HASS_ENABLED_ENTITIES = 'hass-enabled-entities';
export const HASS_ENABLED_RUNNABLES = 'hass-enabled-runnables';
export const HASS_ENABLED_SENSOR_IDS = 'hass-enabled-sensor-ids';
export const SHOW_NOTIFICATIONS_KEY = 'show-notifications';
export const DO_REFRESH = 'sensors-refresh';
export const REFRESH_RATE = 'sensors-refresh-seconds';
export const DEBUG_MODE = 'debug-mode';
export var MscOptions = class MscOptions {
constructor(metadata, mainDir) {
this._metadata = metadata;
this._mainDir = mainDir;
this._gsettings = this._getSettings();
this._connectionIds = [];
}
/**
* A helper function to get the Gio.Settings object for the extension
* @param {String} schema_name
* @return {Gio.Settings} The settings corresponding to the input schema
*/
_getSettings(schema=null) {
schema = schema ? schema : this._metadata['settings-schema'];
const schemaDir = this._mainDir.get_child('schemas');
let schemaSource;
if (schemaDir.query_exists(null)) {
schemaSource = Gio.SettingsSchemaSource.new_from_directory(
schemaDir.get_path(),
Gio.SettingsSchemaSource.get_default(),
false
);
} else {
schemaSource = Gio.SettingsSchemaSource.get_default();
}
const schemaObj = schemaSource.lookup(schema, true);
if (!schemaObj) {
throw new Error(
'Schema' + schema + ' could not be found for extension ' +
this._metadata.uuid + '. Please check your installation.'
);
}
const args = { settings_schema: schemaObj };
// let path = schema.replace('.', '/');
// if (path) {
// args.path = path;
// }
return new Gio.Settings(args);
}
connect(name, callback) {
const id = this._gsettings.connect(name, callback);
this._connectionIds.push(id);
return id;
}
destroy() {
this._connectionIds.forEach(id => this._gsettings.disconnect(id));
this._gsettings = null;
}
// Panel Icons
get panelIcon() {
return this._gsettings.get_string(PANEL_ICON_PATH);
}
set panelIcon(icon_path) {
this._gsettings.set_string(PANEL_ICON_PATH, icon_path);
}
get validIcons() {
return this._gsettings.get_strv(VALID_PANEL_ICONS);
}
set validIcons(icon_paths) {
this._gsettings.set_strv(VALID_PANEL_ICONS, icon_paths);
}
// General Settings
get hassUrl() {
return this._gsettings.get_string(HASS_URL);
}
set hassUrl(bool_val) {
this._gsettings.set_string(HASS_URL, bool_val);
}
get doRefresh() {
return this._gsettings.get_boolean(DO_REFRESH);
}
set doRefresh(bool_val) {
this._gsettings.set_boolean(DO_REFRESH, bool_val);
}
get refreshRate() {
return this._gsettings.get_string(REFRESH_RATE);
}
set refreshRate(rate) {
this._gsettings.set_string(REFRESH_RATE, rate);
}
// Entities cache
get entitiesCache() {
return this._gsettings.get_strv(HASS_ENTITIES_CACHE).map(ent => JSON.parse(ent));
}
set entitiesCache(entities) {
this._gsettings.set_strv(HASS_ENTITIES_CACHE, entities.map(ent => JSON.stringify(ent)));
}
// Togglable entities of menu
get enabledEntities() {
return this._gsettings.get_strv(HASS_ENABLED_ENTITIES);
}
set enabledEntities(entities) {
this._gsettings.set_strv(HASS_ENABLED_ENTITIES, entities);
}
// Runnable entities of menu (script and scene domains)
get enabledRunnables() {
return this._gsettings.get_strv(HASS_ENABLED_RUNNABLES);
}
set enabledRunnables(entities) {
this._gsettings.set_strv(HASS_ENABLED_RUNNABLES, entities);
}
// Panel extra sensors
get enabledSensors() {
return this._gsettings.get_strv(HASS_ENABLED_SENSOR_IDS);
}
set enabledSensors(entities) {
this._gsettings.set_strv(HASS_ENABLED_SENSOR_IDS, entities);
}
// abstraction layer for togglables, runnables and sensors
getEnabledByType(type) {
if (type === "runnable") {
return this.enabledRunnables; // calls the getter
} else if (type === "togglable") {
return this.enabledEntities; // calls the getter
} else if (type === "sensor") {
return this.enabledSensors; // calls the getter
}
}
setEnabledByType(type, enabledEntities) {
if (type === "runnable") {
this.enabledRunnables = enabledEntities; // calls the setter
} else if (type === "togglable") {
this.enabledEntities = enabledEntities; // calls the setter
} else if (type === "sensor") {
this.enabledSensors = enabledEntities; // calls the setter
}
}
// Debug mode
get debugMode() {
return this._gsettings.get_boolean(DEBUG_MODE);
}
set debugMode(bool_val) {
this._gsettings.set_boolean(DEBUG_MODE, bool_val);
}
// Show notifications
get showNotifications() {
return this._gsettings.get_boolean(SHOW_NOTIFICATIONS_KEY);
}
set showNotifications(bool_val) {
this._gsettings.set_boolean(SHOW_NOTIFICATIONS_KEY, bool_val);
}
}