-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptionsUtils.js
143 lines (118 loc) · 4.43 KB
/
optionsUtils.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
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Gettext = imports.gettext;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Domain = Gettext.domain(Me.metadata.uuid);
const { gettext, ngettext } = Domain;
const _ = gettext;
// Driver path
let filesSysfsDir = null;
// All options that the extension can support, regardless of the current device running the extension.
const allOptions = [_("Conservation Mode"), _("Camera"), _("Fn Lock"), _("Touchpad"), _("USB Charging")];
// Files names for each option in the driver files.
const allOptionsFiles = ["conservation_mode", "camera_power", "fn_lock", "touchpad", "usb_charging"];
// Lists to store options supported by this device.
let options = null;
let translatedOptions = null;
let optionsFiles = null;
// Extensions settings
let extensionSettings = null;
async function writeStringToFile(string, filePath) {
const fd = Gio.File.new_for_path(filePath);
const contentBytes = new GLib.Bytes(string);
try {
await new Promise((resolve, reject) => {
// Try to write the file and throw an error if it's not writable
// We don't use the REPLACE_DESTINATION FileCreateFlag, as it would lead to permission errors.
fd.replace_contents_bytes_async(contentBytes, null, false, Gio.FileCreateFlags.NONE, null, (_, res) => {
try {
resolve(fd.replace_contents_finish(res));
} catch (e) {
reject(e);
}
});
});
} catch(e) {
imports.ui.main.notify(_("Ideapad Controls"),
_(`Can't write %s to %s. See journalctl logs for more details`).format(string, filePath));
console.log(`Something went wrong while writing ${string} to ${filePath}: ${e}`);
console.log(`Look at the readme for permission errors fixes.`);
}
}
// Check each option and determine if they are available in this device.
function prepareAvailableOptions() {
options = [];
translatedOptions = [];
optionsFiles = [];
if (!extensionSettings) {
extensionSettings = ExtensionUtils.getSettings();
}
filesSysfsDir = extensionSettings.get_string("sysfs-path")
for (let i = 0; i < allOptionsFiles.length; i++) {
if(GLib.file_test(filesSysfsDir + allOptionsFiles[i], GLib.FileTest.EXISTS)){
options.push(allOptions[i]);
translatedOptions.push(gettext(allOptions[i]));
optionsFiles.push(allOptionsFiles[i]);
}
}
}
// Returns available options in this device.
function getOptions() {
if (options == null) {
prepareAvailableOptions();
}
return options;
}
// Returns available options in this devices (translated)
function getTranslatedOptions() {
if (translatedOptions == null) {
prepareAvailableOptions();
}
return translatedOptions;
}
// Returns available options files in this device.
function getOptionsFiles() {
if (optionsFiles == null) {
prepareAvailableOptions();
}
return optionsFiles;
}
// Read option value from driver file.
function getOptionValue(optionIndex) {
const file = Gio.File.new_for_path(filesSysfsDir + getOptionsFiles()[optionIndex]);
const [, contents, etag] = file.load_contents(null);
const decoder = new TextDecoder("utf-8");
const contentsString = decoder.decode(contents);
return contentsString.trim();
}
// Write option value to driver file.
function setOptionValue(optionIndex, value) {
const optionFile = getOptionsFiles()[optionIndex];
const destinationFile = filesSysfsDir + optionFile;
const notificationBody = `${value == true ? _("Enabled") : _("Disabled")} ${getTranslatedOptions()[optionIndex]}`;
if (extensionSettings.get_boolean("use-pkexec")) {
if (extensionSettings.get_boolean("send-success-notifications")) {
GLib.spawn_command_line_async("bash -c \"pkexec bash -c 'echo " + value + " > " + destinationFile + `' && notify-send '${_("Ideapad Controls")}' '${notificationBody}' "`);
} else {
GLib.spawn_command_line_async("pkexec bash -c 'echo " + value + " > " + destinationFile + "'");
}
} else {
console.log("Writing string to file " + value + " " + destinationFile);
writeStringToFile(value.toString(), destinationFile);
if (extensionSettings.get_boolean("send-success-notifications")) {
imports.ui.main.notify(_("Ideapad Controls"), notificationBody);
}
}
}
function destroy() {
if(options != null) {
options = null;
}
if(optionsFiles != null) {
optionsFiles = null;
}
if(extensionSettings != null) {
extensionSettings = null;
}
}